# squirrel.hpp

Go to the documentation of this file.

    00001 /* $Id: squirrel.hpp 15558 2009-02-23 19:35:19Z yexo $ */
    00002
    00005 #ifndef SQUIRREL_HPP
    00006 #define SQUIRREL_HPP
    00007
    00008 class Squirrel {
    00009 private:
    00010   typedef void (SQPrintFunc)(bool error_msg, const SQChar *message);
    00011
    00012   HSQUIRRELVM vm;
    00013   void *global_pointer;
    00014   SQPrintFunc *print_func;
    00015   bool crashed;
    00016
    00020   static SQInteger _RunError(HSQUIRRELVM vm);
    00021
    00025   HSQUIRRELVM GetVM() { return this->vm; }
    00026
    00027 protected:
    00031   static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column);
    00032
    00036   static void RunError(HSQUIRRELVM vm, const SQChar *error);
    00037
    00041   static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
    00042
    00046   static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
    00047
    00048 public:
    00049   friend class AIScanner;
    00050   friend class AIInstance;
    00051
    00052   Squirrel();
    00053   ~Squirrel();
    00054
    00060   bool LoadScript(const char *script);
    00061   static bool LoadScript(HSQUIRRELVM vm, const char *script, bool in_root = true);
    00062
    00066   static SQRESULT LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printerror);
    00067
    00072   void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam = 0, const char *params = NULL, void *userdata = NULL, int size = 0);
    00073
    00078   void AddConst(const char *var_name, int value);
    00079
    00084   void AddClassBegin(const char *class_name);
    00085
    00090   void AddClassBegin(const char *class_name, const char *parent_class);
    00091
    00096   void AddClassEnd();
    00097
    00101   bool Resume(int suspend = -1);
    00102
    00106   void CollectGarbage();
    00107
    00108   void InsertResult(bool result);
    00109   void InsertResult(int result);
    00110
    00115   bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend = -1);
    00116   bool CallMethod(HSQOBJECT instance, const char *method_name, int suspend = -1) { return this->CallMethod(instance, method_name, NULL, suspend); }
    00117   bool CallStringMethodStrdup(HSQOBJECT instance, const char *method_name, const char **res, int suspend = -1);
    00118   bool CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend = -1);
    00119
    00123   bool MethodExists(HSQOBJECT instance, const char *method_name);
    00124
    00133   static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook);
    00134
    00138   bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance);
    00139
    00145   static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); }
    00146
    00152   static bool GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr, int pos = 1) { sq_getclass(vm, pos); sq_getstackobj(vm, pos, ptr); sq_pop(vm, 1); return true; }
    00153
    00157   static const char *ObjectToString(HSQOBJECT *ptr) { return FS2OTTD(sq_objtostring(ptr)); }
    00158
    00162   static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); }
    00163
    00168   void SetGlobalPointer(void *ptr) { this->global_pointer = ptr; }
    00169
    00173   static void *GetGlobalPointer(HSQUIRRELVM vm) { return ((Squirrel *)sq_getforeignptr(vm))->global_pointer; }
    00174
    00178   void SetPrintFunction(SQPrintFunc *func) { this->print_func = func; }
    00179
    00183   void ThrowError(const char *error) { sq_throwerror(this->vm, OTTD2FS(error)); }
    00184
    00188   void ReleaseObject(HSQOBJECT *ptr) { sq_release(this->vm, ptr); }
    00189
    00193   static void DecreaseOps(HSQUIRRELVM vm, int amount);
    00194
    00199   bool IsSuspended();
    00200
    00204   bool HasScriptCrashed();
    00205
    00209   void ResetCrashed();
    00210 };
    00211
    00212 #endif /* SQUIRREL_HPP */