+// $Id: main.cpp 3275 2006-04-09 00:32:34Z sommer $
+//
+// SuperTux
+// Copyright (C) 2005 Matthias Braun <matze@braunis.de>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+// 02111-1307, USA.
#ifndef __WRAPPERUTIL_HPP__
#define __WRAPPERUTIL_HPP__
#include <squirrel.h>
-#include <exception>
#include <sstream>
+#include <stdexcept>
#include <string>
+#include "wrapper.hpp"
+#include "squirrel_error.hpp"
-struct WrappedFunction {
- const char* name;
- SQFUNCTION f;
-};
-
-template<typename T>
-struct WrappedConstant {
- const char* name;
- T value;
-};
-
-struct WrappedClass {
- const char* name;
- WrappedFunction* functions;
- WrappedConstant<int>* int_consts;
- WrappedConstant<float>* float_consts;
- WrappedConstant<const char*>* string_consts;
-};
-
-class SquirrelError : public std::exception
+namespace Scripting
{
-public:
- SquirrelError(HSQUIRRELVM v, const std::string& message) throw();
- virtual ~SquirrelError() throw();
- const char* what() const throw();
-private:
- std::string message;
-};
+std::string squirrel2string(HSQUIRRELVM vm, int i);
+void print_squirrel_stack(HSQUIRRELVM vm);
+void compile_script(HSQUIRRELVM vm, std::istream& in, const std::string& sourcename);
+void compile_and_run(HSQUIRRELVM vm, std::istream& in, const std::string& sourcename);
-void register_functions(HSQUIRRELVM v, WrappedFunction* functions);
-void register_classes(HSQUIRRELVM v, WrappedClass* classes);
-
-static inline void push_value(HSQUIRRELVM v, int val)
+template<typename T>
+void expose_object(HSQUIRRELVM v, int table_idx, T* object,
+ const std::string& name, bool free = false)
{
- sq_pushinteger(v, val);
-}
+ sq_pushstring(v, name.c_str(), -1);
+ Scripting::create_squirrel_instance(v, object, free);
-static inline void push_value(HSQUIRRELVM v, float val)
-{
- sq_pushfloat(v, val);
-}
+ if(table_idx < 0)
+ table_idx -= 2;
-static inline void push_value(HSQUIRRELVM v, const char* str)
-{
- sq_pushstring(v, str, -1);
+ // register instance in root table
+ if(SQ_FAILED(sq_createslot(v, table_idx))) {
+ std::ostringstream msg;
+ msg << "Couldn't register object '" << name << "' in squirrel table";
+ throw Scripting::SquirrelError(v, msg.str());
+ }
}
-template<typename T>
-void _register_constants(HSQUIRRELVM v, WrappedConstant<T>* constants)
+static inline void unexpose_object(HSQUIRRELVM v, int table_idx, const std::string& name)
{
- for(WrappedConstant<T>* c = constants; c->name != 0; ++c) {
- sq_pushstring(v, c->name, -1);
- push_value(v, c->value);
- if(sq_createslot(v, -3) < 0) {
- std::stringstream msg;
- msg << "Couldn't register int constant '" << c->name << "'";
- throw SquirrelError(v, msg.str());
- }
- }
+ sq_pushstring(v, name.c_str(), name.length());
+
+ if(table_idx < 0)
+ table_idx -= 1;
+
+ if(SQ_FAILED(sq_deleteslot(v, table_idx, SQFalse))) {
+ std::ostringstream msg;
+ msg << "Couldn't unregister object '" << name << "' in squirrel root table";
+ throw Scripting::SquirrelError(v, msg.str());
+ }
}
-template<typename T>
-void register_constants(HSQUIRRELVM v, WrappedConstant<T>* constants)
-{
- sq_pushroottable(v);
- _register_constants(v, constants);
- sq_pop(v, 1);
}
-void print_squirrel_stack(HSQUIRRELVM v);
-
#endif