Skip to content

Commit

Permalink
* Added Constants and Enumerations (See "Using Scrat" for details)
Browse files Browse the repository at this point in the history
* Moved all files up one directory, from "<scrat root>/include/scrat" to "<scrat root>/include"
* Fixed TypeWrapper issue with Object.Cast

git-svn-id: https://scrat.svn.sourceforge.net/svnroot/scrat/trunk@6 17df20b9-c460-4d53-a56e-632d1712e828
  • Loading branch information
toji committed May 14, 2009
1 parent c5e9cf9 commit bca132b
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 8 deletions.
7 changes: 6 additions & 1 deletion docs/History.txt
@@ -1,9 +1,14 @@
Scrat - Squirrel Binding Utility
(c) 2009 Brandon Jones
Version 0.0.4 (preview)
Version 0.0.5 (preview)

=== Version History ===

0.0.5 (???)
* Added Constants and Enumerations (See "Using Scrat" for details)
* Moved all files up one directory, from "<scrat root>/include/scrat" to "<scrat root>/include"
* Fixed TypeWrapper issue with Object.Cast

0.0.4 (05/12/2009)
* Corrected issue with some types being statically defined as unicode versions (ie: wstring)
* Added a Scrat::string typedef for ease of use/development in unicode and ASCII (typedef-ed as std::basic_string< SQChar >)
Expand Down
56 changes: 51 additions & 5 deletions docs/Using Scrat.txt
Expand Up @@ -33,16 +33,18 @@ versions may work fine, but the author makes no promises!
* Tables
* Bind Global functions and variables
* Can be used to support Namespaces
* Objecta
* Constants and Enumerations
* True support for compile time constant and enumeration values
* Objects
* HSQOBJECT Smart-pointer
* Provides simple lookup and casting capabilities
* Functions
* Exposes Squirrel functions for calling from C++

=== Including In Your Project ===

Ensure that "<Scrat root>/include" is in your include path and include
"scrat/scrat.h" Yup, that's all there is to it!
Ensure that "<scrat root>/include" is in your include path and include
"scrat.h" Yup, that's all there is to it!

=== Basic Usage ===

Expand Down Expand Up @@ -226,6 +228,52 @@ bound with Var:
And static variables are bound with StaticVar:

myClass.StaticVar("MyStaticInt", &MyClass::MyStaticInt);

=== Constants and Enumerations ===

Constants and enumerations are both handled similarly by Squirrel in
that both are evaluated at compile time (like C++ defines). This can
improve the performance of scripts by skipping the table lookups
normally associated with variable use.

Constant binding happens on the ConstTable object, which functions in a
very similar manner to the RootTable object. It is limited to two
operations, however: Const and Enum. Const will set one of the valid
constant values (int, float, or string) to the given name:

ConstTable(vm)
.Const("ConstInt", 12)
.Const("ConstFloat", 3.14)
.Const("ConstString", "Hello World!")
;

These are accessed from the script as global variables:

::print(ConstString); // prints "Hello World!"
local x = ConstInt - 4; // x = 8;

Enumerations are simply named groups of constant values. Creation of an
enumeration is done with the Enumeration object and bound to the
ConstTable with Enum.

Enumeration color(vm);
color.Const("Red", 1);
color.Const("Green", 2);
color.Const("Blue", 3);

ConstTable(vm).Enum("Color", color);

Or, of course:

ConstTable(vm).Enum("Color", Enumeration(vm)
.Const("Red", 1)
.Const("Green", 2)
.Const("Blue", 3)
);

Enumerations are accessed from script like static class members:

::print(Color.Red); // prints "1"

=== Function ===

Expand All @@ -238,8 +286,6 @@ Scrat:

* Function/Constructor Overloading
* Class Attributes
* Constants
* Enumerations

=== Contact ===

Expand Down
1 change: 1 addition & 0 deletions include/Scrat/Scrat.h → include/Scrat.h
Expand Up @@ -33,6 +33,7 @@
#include "ScratTable.h"
#include "ScratClass.h"
#include "ScratFunction.h"
#include "ScratConst.h"
#include "ScratUtil.h"

#endif
File renamed without changes.
File renamed without changes.
File renamed without changes.
119 changes: 119 additions & 0 deletions include/ScratConst.h
@@ -0,0 +1,119 @@
//
// ScratConst: Constant and Enumeration Binding
//

//
// Copyright (c) 2009 Brandon Jones
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//

#if !defined(_SCRAT_CONST_H_)
#define _SCRAT_CONST_H_

#include <squirrel.h>
#include <string.h>

#include "ScratObject.h"

namespace Scrat {

//
// Enumerations
//

class Enumeration : public Object {
public:
Enumeration(HSQUIRRELVM v, bool createTable = true) : Object(v, false) {
if(createTable) {
sq_newtable(vm);
sq_getstackobj(vm,-1,&obj);
sq_pop(vm,-1);
}
}

//
// Bind Constants
//

virtual Enumeration& Const(const SQChar* name, const int val) {
BindValue<int>(name, val, false);
return *this;
}

virtual Enumeration& Const(const SQChar* name, const float val) {
BindValue<float>(name, val, false);
return *this;
}

virtual Enumeration& Const(const SQChar* name, const SQChar* val) {
BindValue<const SQChar*>(name, val, false);
return *this;
}

};

//
// Constants
//

class ConstTable : public Enumeration {
public:
ConstTable(HSQUIRRELVM v) : Enumeration(v, false) {
sq_pushconsttable(vm);
sq_getstackobj(vm,-1, &obj);
sq_pop(v,-1);
}

//
// Bind Constants
//

virtual ConstTable& Const(const SQChar* name, const int val) {
Enumeration::Const(name, val);
return *this;
}

virtual ConstTable& Const(const SQChar* name, const float val) {
Enumeration::Const(name, val);
return *this;
}

virtual ConstTable& Const(const SQChar* name, const SQChar* val) {
Enumeration::Const(name, val);
return *this;
}

//
// Bind Enumerations
//

ConstTable& Enum(const SQChar* name, Enumeration& en) {
sq_pushobject(vm, GetObject());
sq_pushstring(vm, name, -1);
sq_pushobject(vm, en.GetObject());
sq_newslot(vm, -3, false);
sq_pop(vm,-1); // pop table
return *this;
}
};
}

#endif
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions include/Scrat/ScratTable.h → include/ScratTable.h
Expand Up @@ -40,7 +40,7 @@ namespace Scrat {
public:
Table(HSQUIRRELVM v) : Object(v, false) {
sq_newtable(vm);
sq_getstackobj(vm,-1,&GetObject());
sq_getstackobj(vm,-1,&obj);
sq_pop(vm,-1);
}

Expand Down Expand Up @@ -99,7 +99,7 @@ namespace Scrat {
public:
RootTable(HSQUIRRELVM v) : Table(v) {
sq_pushroottable(vm);
sq_getstackobj(vm,-1,&GetObject());
sq_getstackobj(vm,-1,&obj);
sq_pop(v,-1); // pop root table
}
};
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit bca132b

Please sign in to comment.