diff --git a/docs/History.txt b/docs/History.txt index 5e4cb78..b0c53f5 100644 --- a/docs/History.txt +++ b/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 "/include/scrat" to "/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 >) diff --git a/docs/Using Scrat.txt b/docs/Using Scrat.txt index 6b6f977..58c5595 100644 --- a/docs/Using Scrat.txt +++ b/docs/Using Scrat.txt @@ -33,7 +33,9 @@ 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 @@ -41,8 +43,8 @@ versions may work fine, but the author makes no promises! === Including In Your Project === -Ensure that "/include" is in your include path and include -"scrat/scrat.h" Yup, that's all there is to it! +Ensure that "/include" is in your include path and include +"scrat.h" Yup, that's all there is to it! === Basic Usage === @@ -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 === @@ -238,8 +286,6 @@ Scrat: * Function/Constructor Overloading * Class Attributes - * Constants - * Enumerations === Contact === diff --git a/include/Scrat/Scrat.h b/include/Scrat.h similarity index 94% rename from include/Scrat/Scrat.h rename to include/Scrat.h index e664a85..c7e82fd 100644 --- a/include/Scrat/Scrat.h +++ b/include/Scrat.h @@ -33,6 +33,7 @@ #include "ScratTable.h" #include "ScratClass.h" #include "ScratFunction.h" +#include "ScratConst.h" #include "ScratUtil.h" #endif diff --git a/include/Scrat/ScratAllocator.h b/include/ScratAllocator.h similarity index 100% rename from include/Scrat/ScratAllocator.h rename to include/ScratAllocator.h diff --git a/include/Scrat/ScratClass.h b/include/ScratClass.h similarity index 100% rename from include/Scrat/ScratClass.h rename to include/ScratClass.h diff --git a/include/Scrat/ScratClassType.h b/include/ScratClassType.h similarity index 100% rename from include/Scrat/ScratClassType.h rename to include/ScratClassType.h diff --git a/include/ScratConst.h b/include/ScratConst.h new file mode 100644 index 0000000..2e33358 --- /dev/null +++ b/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 +#include + +#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(name, val, false); + return *this; + } + + virtual Enumeration& Const(const SQChar* name, const float val) { + BindValue(name, val, false); + return *this; + } + + virtual Enumeration& Const(const SQChar* name, const SQChar* val) { + BindValue(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 diff --git a/include/Scrat/ScratFunction.h b/include/ScratFunction.h similarity index 100% rename from include/Scrat/ScratFunction.h rename to include/ScratFunction.h diff --git a/include/Scrat/ScratGlobalMethods.h b/include/ScratGlobalMethods.h similarity index 100% rename from include/Scrat/ScratGlobalMethods.h rename to include/ScratGlobalMethods.h diff --git a/include/Scrat/ScratMemberMethods.h b/include/ScratMemberMethods.h similarity index 100% rename from include/Scrat/ScratMemberMethods.h rename to include/ScratMemberMethods.h diff --git a/include/Scrat/ScratObject.h b/include/ScratObject.h similarity index 100% rename from include/Scrat/ScratObject.h rename to include/ScratObject.h diff --git a/include/Scrat/ScratTable.h b/include/ScratTable.h similarity index 93% rename from include/Scrat/ScratTable.h rename to include/ScratTable.h index 61f5a0a..c90f578 100644 --- a/include/Scrat/ScratTable.h +++ b/include/ScratTable.h @@ -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); } @@ -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 } }; diff --git a/include/Scrat/ScratTypes.h b/include/ScratTypes.h similarity index 100% rename from include/Scrat/ScratTypes.h rename to include/ScratTypes.h diff --git a/include/Scrat/ScratUtil.h b/include/ScratUtil.h similarity index 100% rename from include/Scrat/ScratUtil.h rename to include/ScratUtil.h