Remove bogus assert
[supertux.git] / src / scripting / squirrel_util.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #ifndef HEADER_SUPERTUX_SCRIPTING_SQUIRREL_UTIL_HPP
18 #define HEADER_SUPERTUX_SCRIPTING_SQUIRREL_UTIL_HPP
19
20 #include <sstream>
21
22 #include "scripting/squirrel_error.hpp"
23 #include "scripting/wrapper.hpp"
24
25 namespace scripting {
26
27 std::string squirrel2string(HSQUIRRELVM vm, SQInteger i);
28 void print_squirrel_stack(HSQUIRRELVM vm);
29
30 SQInteger squirrel_read_char(SQUserPointer file);
31
32 HSQOBJECT create_thread(HSQUIRRELVM vm);
33 SQObject vm_to_object(HSQUIRRELVM vm);
34 HSQUIRRELVM object_to_vm(HSQOBJECT object);
35
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);
40
41 template<typename T>
42 void expose_object(HSQUIRRELVM v, SQInteger table_idx, T* object,
43                    const std::string& name, bool free = false)
44 {
45   sq_pushstring(v, name.c_str(), -1);
46   scripting::create_squirrel_instance(v, object, free);
47
48   if(table_idx < 0)
49     table_idx -= 2;
50
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());
56   }
57 }
58
59 static inline void unexpose_object(HSQUIRRELVM v, SQInteger table_idx,
60                                    const std::string& name)
61 {
62   sq_pushstring(v, name.c_str(), name.length());
63
64   if(table_idx < 0)
65     table_idx -= 1;
66
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());
71   }
72 }
73
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);
79
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);
84
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);
89
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
95
96 }
97
98 #endif
99
100 /* EOF */