diff -r c132ce47f560 -r 33efcc5f1b09 src/squirrel.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/squirrel.hpp Thu Mar 15 19:33:07 2007 +0000 @@ -0,0 +1,127 @@ +/* $Id$ */ + +/** @file squirrel.hpp defines the Squirrel class */ + +#ifndef SQUIRREL_HPP +#define SQUIRREL_HPP + +class Squirrel { +private: + HSQUIRRELVM vm; ///< The VirtualMachine instnace for squirrel + + /** + * The internal RunError handler. It looks up the real error and calls RunError with it. + */ + static SQInteger _RunError(HSQUIRRELVM vm); + +protected: + /** + * The CompileError handler. + */ + static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column); + + /** + * The RunError handler. + */ + static void RunError(HSQUIRRELVM vm, const char *error); + + /** + * If a user runs 'print' inside a script, this function gets the params. + */ + static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...); + +public: + Squirrel(); + ~Squirrel(); + + /** + * Load a script. + * @param script The full script-name to load. + * @return False if loading failed. + */ + bool LoadScript(const char *script); + + /** + * Adds a function to the stack. Depending on the current state this means + * either a method or a global function. + */ + void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam, const char *params); + void AddMethod(const char *method_name, SQFUNCTION proc); + void AddMethod(const char *method_name, SQFUNCTION proc, void *userdata, int size); + + /** + * Adds a class to the global scope. Make sure to call AddClassEnd when you + * are done adding methods. + */ + void AddClassBegin(const char *class_name); + + /** + * Finishes adding a class to the global scope. If this isn't called, no + * class is really created. + */ + void AddClassEnd(); + + /** + * Call a method of an instance, in various flavors. + */ + void CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret); + void CallMethod(HSQOBJECT instance, const char *method_name) { this->CallMethod(instance, method_name, NULL); } + const char *CallStringMethod(HSQOBJECT instance, const char *method_name) { HSQOBJECT ret; this->CallMethod(instance, method_name, &ret); return ObjectToString(&ret); } + int CallIntegerMethod(HSQOBJECT instance, const char *method_name) { HSQOBJECT ret; this->CallMethod(instance, method_name, &ret); return ObjectToInteger(&ret); } + + /** + * Check if a method exists in an instance. + */ + bool MethodExists(HSQOBJECT instance, const char *method_name); + + /** + * Creates a class instance. + * @param class_name The name of the class of which we create an instance. + * @param real_instance The instance to the real class, if it represents a real class. + * @param instance Returning value with the pointer to the instance. + * @return False if creating failed. + */ + bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance); + + /** + * Get the real-instance pointer. + * @note This will only work just after a function-call from within Squirrel + * to your C++ function. + */ + static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); } + + /** + * Get the Squirrel-instance pointer. + * @note This will only work just after a function-call from within Squirrel + * to your C++ function. + */ + static bool GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr) { sq_getclass(vm, 1); sq_getstackobj(vm, 1, ptr); return true; } + + /** + * Convert a Squirrel-object to a string. + */ + static const char *ObjectToString(HSQOBJECT *ptr) { return sq_objtostring(ptr); } + + /** + * Convert a Squirrel-object to an integer. + */ + static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); } + + /** + * Sets a pointer in the VM that is reasable from where ever you are, what + * ever your state is. Useful to keep track of the main instance. + */ + void SetGlobalPointer(void *ptr) { sq_setforeignptr(this->vm, ptr); } + + /** + * Get the pointer as set by SetGlobalPointer. + */ + static void *GetGlobalPointer(HSQUIRRELVM vm) { return sq_getforeignptr(vm); } + + /** + * Throw a Squirrel error that will be nicely displayed to the user. + */ + void ThrowError(const char *error) { sq_throwerror(this->vm, error); } +}; + +#endif /* SQUIRREL_HPP */