2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef HEADER_SUPERTUX_SCRIPTING_SQUIRREL_UTIL_HPP
18 #define HEADER_SUPERTUX_SCRIPTING_SQUIRREL_UTIL_HPP
22 #include "scripting/squirrel_error.hpp"
23 #include "scripting/wrapper.hpp"
27 std::string squirrel2string(HSQUIRRELVM vm, SQInteger i);
28 void print_squirrel_stack(HSQUIRRELVM vm);
30 SQInteger squirrel_read_char(SQUserPointer file);
32 HSQOBJECT create_thread(HSQUIRRELVM vm);
33 SQObject vm_to_object(HSQUIRRELVM vm);
34 HSQUIRRELVM object_to_vm(HSQOBJECT object);
36 void compile_script(HSQUIRRELVM vm, std::istream& in,
37 const std::string& sourcename);
38 void compile_and_run(HSQUIRRELVM vm, std::istream& in,
39 const std::string& sourcename);
42 void expose_object(HSQUIRRELVM v, SQInteger table_idx, T* object,
43 const std::string& name, bool free = false)
45 sq_pushstring(v, name.c_str(), -1);
46 scripting::create_squirrel_instance(v, object, free);
51 // register instance in root table
52 if(SQ_FAILED(sq_createslot(v, table_idx))) {
53 std::ostringstream msg;
54 msg << "Couldn't register object '" << name << "' in squirrel table";
55 throw scripting::SquirrelError(v, msg.str());
59 static inline void unexpose_object(HSQUIRRELVM v, SQInteger table_idx,
60 const std::string& name)
62 sq_pushstring(v, name.c_str(), name.length());
67 if(SQ_FAILED(sq_deleteslot(v, table_idx, SQFalse))) {
68 std::ostringstream msg;
69 msg << "Couldn't unregister object '" << name << "' in squirrel root table";
70 throw scripting::SquirrelError(v, msg.str());
74 // begin serialization functions
75 void store_float(HSQUIRRELVM vm, const char* name, float val);
76 void store_int(HSQUIRRELVM vm, const char* name, int val);
77 void store_string(HSQUIRRELVM vm, const char* name, const std::string& val);
78 void store_bool(HSQUIRRELVM vm, const char* name, bool val);
80 bool has_float(HSQUIRRELVM vm, const char* name);
81 bool has_int(HSQUIRRELVM vm, const char* name);
82 bool has_string(HSQUIRRELVM vm, const char* name);
83 bool has_bool(HSQUIRRELVM vm, const char* name);
85 bool get_float(HSQUIRRELVM vm, const char* name, float& val);
86 bool get_int(HSQUIRRELVM vm, const char* name, int& val);
87 bool get_string(HSQUIRRELVM vm, const char* name, std::string& val);
88 bool get_bool(HSQUIRRELVM vm, const char* name, bool& val);
90 float read_float(HSQUIRRELVM vm, const char* name);
91 int read_int(HSQUIRRELVM vm, const char* name);
92 std::string read_string(HSQUIRRELVM vm, const char* name);
93 bool read_bool(HSQUIRRELVM vm, const char* name);
94 // end serialization functions