da61b26744b7e9438c70cb2e450eafb9243ed4be
[supertux.git] / src / scripting / squirrel_util.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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__
21
22 #include <squirrel.h>
23 #include <sstream>
24 #include <stdexcept>
25 #include <string>
26 #include "wrapper.hpp"
27 #include "squirrel_error.hpp"
28
29 namespace Scripting
30 {
31
32   extern HSQUIRRELVM global_vm;
33
34   void init_squirrel(bool enable_debugger);
35   void exit_squirrel();
36   void update_debugger();
37
38   std::string squirrel2string(HSQUIRRELVM vm, SQInteger i);
39   void print_squirrel_stack(HSQUIRRELVM vm);
40
41   HSQOBJECT create_thread(HSQUIRRELVM vm);
42   SQObject vm_to_object(HSQUIRRELVM vm);
43   HSQUIRRELVM object_to_vm(HSQOBJECT object);
44
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);
49
50   template<typename T>
51   void expose_object(HSQUIRRELVM v, SQInteger table_idx, T* object,
52                      const std::string& name, bool free = false)
53   {
54     sq_pushstring(v, name.c_str(), -1);
55     Scripting::create_squirrel_instance(v, object, free);
56
57     if(table_idx < 0)
58       table_idx -= 2;
59
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());
65     }
66   }
67
68   static inline void unexpose_object(HSQUIRRELVM v, SQInteger table_idx,
69                                      const std::string& name)
70   {
71     sq_pushstring(v, name.c_str(), name.length());
72
73     if(table_idx < 0)
74       table_idx -= 1;
75
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());
80     }
81   }
82
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);
88
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);
93
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);
98
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
104
105 }
106
107 #endif