namespace Script { /* Script::DataType is used when getting or setting properties to ensure correct data type, as well as casting from one Variant to another. */ enum DataType { DATA_TYPE_UNKNOWN = 0, DATA_TYPE_BOOLEAN = 1, DATA_TYPE_CHAR = 2, DATA_TYPE_UCHAR = 3, DATA_TYPE_SHORT = 4, DATA_TYPE_USHORT = 5, DATA_TYPE_INT = 6, DATA_TYPE_UINT = 7, DATA_TYPE_SINGLE = 8, DATA_TYPE_DOUBLE = 9, DATA_TYPE_STRING = 10, DATA_TYPE_COLOR = 11, DATA_TYPE_SIZE = 12, DATA_TYPE_POINT4 = 13, }; /* Script::ScriptError is thrown when getting or setting invalid properties or invalid property types */ enum ScriptError { SCRIPT_ERROR_NONE = 0, SCRIPT_ERROR_UNKNOWN_ERROR = 1, SCRIPT_ERROR_UNKNOWN_PROPERTY = 2, SCRIPT_ERROR_INCORRECT_TYPE = 3, SCRIPT_ERROR_INCORRECT_VALUE = 4, SCRIPT_ERROR_READ_ONLY = 5, }; /* Script::Variant is the type passed back and forth between property get'ter and set'ter methods */ struct Variant { static char m_chrBuffer[64]; const DataType m_enmType; union { bool m_blnValue; char m_chrValue; unsigned char m_uchrValue; short m_shtValue; unsigned short m_ushtValue; int m_intValue; unsigned int m_uintValue; float m_fltValue; double m_dblValue; const char * m_ptrString; unsigned char m_uchrColor[sizeof(ASW::Colorb)]; unsigned char m_uchrSize[sizeof(ASW::Size)]; unsigned char m_uchrPoint4f[sizeof(ASW::Point4f)]; }; Variant() : m_enmType(DATA_TYPE_UNKNOWN) {} explicit Variant(const char * Value) : m_enmType(DATA_TYPE_STRING), m_ptrString(Value) {} explicit Variant(const int Value) : m_enmType(DATA_TYPE_INT), m_intValue(Value) {} explicit Variant(const unsigned int Value) : m_enmType(DATA_TYPE_UINT), m_uintValue(Value) {} explicit Variant(const unsigned char Value) : m_enmType(DATA_TYPE_UCHAR), m_uchrValue(Value) {} explicit Variant(const double Value) : m_enmType(DATA_TYPE_DOUBLE), m_dblValue(Value) {} explicit Variant(const bool Value) : m_enmType(DATA_TYPE_BOOLEAN), m_blnValue(Value) {} explicit Variant(const ASW::Size Value) : m_enmType(DATA_TYPE_SIZE) { memcpy(&m_uchrSize, &Value, sizeof(ASW::Size)); } explicit Variant(const ASW::Colorb Value) : m_enmType(DATA_TYPE_COLOR) { memcpy(&m_uchrColor, &Value, sizeof(ASW::Colorb)); } explicit Variant(const ASW::Point4f Value) : m_enmType(DATA_TYPE_POINT4) { memcpy(&m_uchrPoint4f, &Value, sizeof(ASW::Point4f)); } Variant operator=(const Variant Value) {memcpy(this, &Value, sizeof(Variant)); return Value;} bool operator==(const Variant & Value) const; bool operator!=(const Variant & Value) const {return !(*this == Value);} operator bool () const; operator char () const; operator unsigned char () const; operator short () const; operator unsigned short () const; operator int () const; operator unsigned int () const; operator float () const; operator double () const; operator const char * () const; operator ASW::Colorb () const; operator ASW::Size () const; operator ASW::Point4f () const; }; /* Script::Unit is the abstract base type that other types must inherit from in order to be visible from scripts */ struct Unit { virtual Unit * Child(const char * Name) const = 0; virtual const char * Name(void) const = 0; virtual Variant PropertyGet(const char * Name) const = 0; virtual void PropertySet(const char * Name, const Variant & Value) = 0; }; /* Script::Manager contains root Script::Units, lua objects, and lua callbacks */ class Manager : public Unit { private: struct lua_State * m_ptrState; UTIL::Log * m_ptrLogDebug; UTIL::Log * m_ptrLogError; UTIL::Array m_clsRootUnits; struct UserData { Unit * m_ptrRootUnit; Unit * m_ptrCurrentUnit; }; void StackDump(void); static void * Alloc(void * ud, void * ptr, size_t osize, size_t nsize); static int FunctionNew(struct lua_State * State); static int FunctionIndex(struct lua_State * State); public: Manager() : m_ptrState(NULL), m_ptrLogDebug(NULL), m_ptrLogError(NULL) {} ~Manager() {Finish();} void AddRoot(Unit * UnitRef); void Finish(const bool ClearUnits = true); bool Init(void); // // Script::Manager is also a Script::Unit // virtual Unit * Child(const char * Name) const; virtual const char * Name(void) const; virtual Variant PropertyGet(const char * Name) const; virtual void PropertySet(const char * Name, const Variant & Value); }; }