src/scripting/squirrel_util.hpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #ifndef HEADER_SUPERTUX_SCRIPTING_SQUIRREL_UTIL_HPP
00018 #define HEADER_SUPERTUX_SCRIPTING_SQUIRREL_UTIL_HPP
00019 
00020 #include <sstream>
00021 
00022 #include "scripting/squirrel_error.hpp"
00023 #include "scripting/wrapper.hpp"
00024 
00025 namespace scripting {
00026 
00027 extern HSQUIRRELVM global_vm;
00028 
00029 void init_squirrel(bool enable_debugger);
00030 void exit_squirrel();
00031 void update_debugger();
00032 
00033 std::string squirrel2string(HSQUIRRELVM vm, SQInteger i);
00034 void print_squirrel_stack(HSQUIRRELVM vm);
00035 
00036 SQInteger squirrel_read_char(SQUserPointer file);
00037 
00038 HSQOBJECT create_thread(HSQUIRRELVM vm);
00039 SQObject vm_to_object(HSQUIRRELVM vm);
00040 HSQUIRRELVM object_to_vm(HSQOBJECT object);
00041 
00042 void compile_script(HSQUIRRELVM vm, std::istream& in,
00043                     const std::string& sourcename);
00044 void compile_and_run(HSQUIRRELVM vm, std::istream& in,
00045                      const std::string& sourcename);
00046 
00047 template<typename T>
00048 void expose_object(HSQUIRRELVM v, SQInteger table_idx, T* object,
00049                    const std::string& name, bool free = false)
00050 {
00051   sq_pushstring(v, name.c_str(), -1);
00052   scripting::create_squirrel_instance(v, object, free);
00053 
00054   if(table_idx < 0)
00055     table_idx -= 2;
00056 
00057   // register instance in root table
00058   if(SQ_FAILED(sq_createslot(v, table_idx))) {
00059     std::ostringstream msg;
00060     msg << "Couldn't register object '" << name << "' in squirrel table";
00061     throw scripting::SquirrelError(v, msg.str());
00062   }
00063 }
00064 
00065 static inline void unexpose_object(HSQUIRRELVM v, SQInteger table_idx,
00066                                    const std::string& name)
00067 {
00068   sq_pushstring(v, name.c_str(), name.length());
00069 
00070   if(table_idx < 0)
00071     table_idx -= 1;
00072 
00073   if(SQ_FAILED(sq_deleteslot(v, table_idx, SQFalse))) {
00074     std::ostringstream msg;
00075     msg << "Couldn't unregister object '" << name << "' in squirrel root table";
00076     throw scripting::SquirrelError(v, msg.str());
00077   }
00078 }
00079 
00080 // begin serialization functions
00081 void store_float(HSQUIRRELVM vm, const char* name, float val);
00082 void store_int(HSQUIRRELVM vm, const char* name, int val);
00083 void store_string(HSQUIRRELVM vm, const char* name, const std::string& val);
00084 void store_bool(HSQUIRRELVM vm, const char* name, bool val);
00085 
00086 bool has_float(HSQUIRRELVM vm, const char* name);
00087 bool has_int(HSQUIRRELVM vm, const char* name);
00088 bool has_string(HSQUIRRELVM vm, const char* name);
00089 bool has_bool(HSQUIRRELVM vm, const char* name);
00090 
00091 bool get_float(HSQUIRRELVM vm, const char* name, float& val);
00092 bool get_int(HSQUIRRELVM vm, const char* name, int& val);
00093 bool get_string(HSQUIRRELVM vm, const char* name, std::string& val);
00094 bool get_bool(HSQUIRRELVM vm, const char* name, bool& val);
00095 
00096 float read_float(HSQUIRRELVM vm, const char* name);
00097 int read_int(HSQUIRRELVM vm, const char* name);
00098 std::string read_string(HSQUIRRELVM vm, const char* name);
00099 bool read_bool(HSQUIRRELVM vm, const char* name);
00100 // end serialization functions
00101 
00102 }
00103 
00104 #endif
00105 
00106 /* EOF */

Generated on Mon Jun 9 03:38:21 2014 for SuperTux by  doxygen 1.5.1