4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #ifndef __SQUIRREL_UTIL_HPP__
20 #define __SQUIRREL_UTIL_HPP__
26 #include "wrapper.hpp"
27 #include "squirrel_error.hpp"
32 extern HSQUIRRELVM global_vm;
34 void init_squirrel(bool enable_debugger);
36 void update_debugger();
38 std::string squirrel2string(HSQUIRRELVM vm, SQInteger i);
39 void print_squirrel_stack(HSQUIRRELVM vm);
41 HSQOBJECT create_thread(HSQUIRRELVM vm);
42 SQObject vm_to_object(HSQUIRRELVM vm);
43 HSQUIRRELVM object_to_vm(HSQOBJECT object);
45 void compile_script(HSQUIRRELVM vm, std::istream& in,
46 const std::string& sourcename);
47 void compile_and_run(HSQUIRRELVM vm, std::istream& in,
48 const std::string& sourcename);
51 void expose_object(HSQUIRRELVM v, SQInteger table_idx, T* object,
52 const std::string& name, bool free = false)
54 sq_pushstring(v, name.c_str(), -1);
55 Scripting::create_squirrel_instance(v, object, free);
60 // register instance in root table
61 if(SQ_FAILED(sq_createslot(v, table_idx))) {
62 std::ostringstream msg;
63 msg << "Couldn't register object '" << name << "' in squirrel table";
64 throw Scripting::SquirrelError(v, msg.str());
68 static inline void unexpose_object(HSQUIRRELVM v, SQInteger table_idx,
69 const std::string& name)
71 sq_pushstring(v, name.c_str(), name.length());
76 if(SQ_FAILED(sq_deleteslot(v, table_idx, SQFalse))) {
77 std::ostringstream msg;
78 msg << "Couldn't unregister object '" << name << "' in squirrel root table";
79 throw Scripting::SquirrelError(v, msg.str());
83 // begin serialization functions
84 void store_float(HSQUIRRELVM vm, const char* name, float val);
85 void store_int(HSQUIRRELVM vm, const char* name, int val);
86 void store_string(HSQUIRRELVM vm, const char* name, const std::string& val);
87 void store_bool(HSQUIRRELVM vm, const char* name, bool val);
89 bool has_float(HSQUIRRELVM vm, const char* name);
90 bool has_int(HSQUIRRELVM vm, const char* name);
91 bool has_string(HSQUIRRELVM vm, const char* name);
92 bool has_bool(HSQUIRRELVM vm, const char* name);
94 bool get_float(HSQUIRRELVM vm, const char* name, float& val);
95 bool get_int(HSQUIRRELVM vm, const char* name, int& val);
96 bool get_string(HSQUIRRELVM vm, const char* name, std::string& val);
97 bool get_bool(HSQUIRRELVM vm, const char* name, bool& val);
99 float read_float(HSQUIRRELVM vm, const char* name);
100 int read_int(HSQUIRRELVM vm, const char* name);
101 std::string read_string(HSQUIRRELVM vm, const char* name);
102 bool read_bool(HSQUIRRELVM vm, const char* name);
103 // end serialization functions