fade out console
[supertux.git] / src / scripting / wrapper_util.hpp
index 54ca2e9..f183086 100644 (file)
@@ -1,22 +1,72 @@
+//  $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"
 
-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 print_squirrel_stack(HSQUIRRELVM v);
+template<typename T>
+void expose_object(HSQUIRRELVM v, int table_idx, T* object,
+                   const std::string& name, bool free = false)
+{
+  sq_pushstring(v, name.c_str(), -1);
+  Scripting::create_squirrel_instance(v, object, free);
+
+  if(table_idx < 0)
+    table_idx -= 2;
+
+  // 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());
+  }
+}
+
+static inline void unexpose_object(HSQUIRRELVM v, int table_idx, const std::string& name)
+{
+  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());
+  }
+}
+
+}
 
 #endif