squirrel_class.hpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD 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, version 2.
00006  * OpenTTD 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.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #ifndef SQUIRREL_CLASS_HPP
00013 #define SQUIRREL_CLASS_HPP
00014 
00015 #include "squirrel_helper.hpp"
00016 
00021 template <class CL, ScriptType ST>
00022 class DefSQClass {
00023 private:
00024   const char *classname;
00025 
00026 public:
00027   DefSQClass(const char *_classname) :
00028     classname(_classname)
00029   {}
00030 
00034   template <typename Func>
00035   void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name)
00036   {
00037     using namespace SQConvert;
00038     engine->AddMethod(function_name, DefSQNonStaticCallback<CL, Func, ST>, 0, NULL, &function_proc, sizeof(function_proc));
00039   }
00040 
00044   template <typename Func>
00045   void DefSQAdvancedMethod(Squirrel *engine, Func function_proc, const char *function_name)
00046   {
00047     using namespace SQConvert;
00048     engine->AddMethod(function_name, DefSQAdvancedNonStaticCallback<CL, Func, ST>, 0, NULL, &function_proc, sizeof(function_proc));
00049   }
00050 
00057   template <typename Func>
00058   void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params)
00059   {
00060     using namespace SQConvert;
00061     engine->AddMethod(function_name, DefSQNonStaticCallback<CL, Func, ST>, nparam, params, &function_proc, sizeof(function_proc));
00062   }
00063 
00067   template <typename Func>
00068   void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name)
00069   {
00070     using namespace SQConvert;
00071     engine->AddMethod(function_name, DefSQStaticCallback<CL, Func>, 0, NULL, &function_proc, sizeof(function_proc));
00072   }
00073 
00080   template <typename Func>
00081   void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params)
00082   {
00083     using namespace SQConvert;
00084     engine->AddMethod(function_name, DefSQStaticCallback<CL, Func>, nparam, params, &function_proc, sizeof(function_proc));
00085   }
00086 
00087   template <typename Var>
00088   void DefSQConst(Squirrel *engine, Var value, const char *var_name)
00089   {
00090     engine->AddConst(var_name, value);
00091   }
00092 
00093   void PreRegister(Squirrel *engine)
00094   {
00095     engine->AddClassBegin(this->classname);
00096   }
00097 
00098   void PreRegister(Squirrel *engine, const char *parent_class)
00099   {
00100     engine->AddClassBegin(this->classname, parent_class);
00101   }
00102 
00103   template <typename Func, int Tnparam>
00104   void AddConstructor(Squirrel *engine, const char *params)
00105   {
00106     using namespace SQConvert;
00107     engine->AddMethod("constructor", DefSQConstructorCallback<CL, Func, Tnparam>, Tnparam, params);
00108   }
00109 
00110   void PostRegister(Squirrel *engine)
00111   {
00112     engine->AddClassEnd();
00113   }
00114 };
00115 
00116 #endif /* SQUIRREL_CLASS_HPP */