# squirrel.hpp

Go to the documentation of this file.

    00001 /* $Id: squirrel.hpp 16835 2009-07-15 20:29:45Z rubidium $ */
    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   bool CallBoolMethod(HSQOBJECT instance, const char *method_name, bool *res, int suspend = -1);
    00120
    00124   bool MethodExists(HSQOBJECT instance, const char *method_name);
    00125
    00134   static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook);
    00135
    00139   bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance);
    00140
    00146   static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); }
    00147
    00153   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; }
    00154
    00158   static const char *ObjectToString(HSQOBJECT *ptr) { return FS2OTTD(sq_objtostring(ptr)); }
    00159
    00163   static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); }
    00164
    00168   static bool ObjectToBool(HSQOBJECT *ptr) { return sq_objtobool(ptr) == 1; }
    00169
    00174   void SetGlobalPointer(void *ptr) { this->global_pointer = ptr; }
    00175
    00179   static void *GetGlobalPointer(HSQUIRRELVM vm) { return ((Squirrel *)sq_getforeignptr(vm))->global_pointer; }
    00180
    00184   void SetPrintFunction(SQPrintFunc *func) { this->print_func = func; }
    00185
    00189   void ThrowError(const char *error) { sq_throwerror(this->vm, OTTD2FS(error)); }
    00190
    00194   void ReleaseObject(HSQOBJECT *ptr) { sq_release(this->vm, ptr); }
    00195
    00199   static void DecreaseOps(HSQUIRRELVM vm, int amount);
    00200
    00205   bool IsSuspended();
    00206
    00210   bool HasScriptCrashed();
    00211
    00215   void ResetCrashed();
    00216
    00220   void CrashOccurred();
    00221
    00225   bool CanSuspend();
    00226 };
    00227
    00228 #endif /* SQUIRREL_HPP */