# squirrel_class.hpp

Go to the documentation of this file.

    00001 /* $Id: squirrel_class.hpp 17248 2009-08-21 20:21:05Z rubidium $ */
    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
    00020 template <class CL>
    00021 class DefSQClass {
    00022 private:
    00023   const char *classname;
    00024
    00025 public:
    00026   DefSQClass(const char *_classname) :
    00027     classname(_classname)
    00028   {}
    00029
    00033   template <typename Func>
    00034   void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name)
    00035   {
    00036     using namespace SQConvert;
    00037     engine->AddMethod(function_name, DefSQNonStaticCallback<CL, Func>, 0, NULL, &function_proc, sizeof(function_proc));
    00038   }
    00039
    00043   template <typename Func>
    00044   void DefSQAdvancedMethod(Squirrel *engine, Func function_proc, const char *function_name)
    00045   {
    00046     using namespace SQConvert;
    00047     engine->AddMethod(function_name, DefSQAdvancedNonStaticCallback<CL, Func>, 0, NULL, &function_proc, sizeof(function_proc));
    00048   }
    00049
    00056   template <typename Func>
    00057   void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params)
    00058   {
    00059     using namespace SQConvert;
    00060     engine->AddMethod(function_name, DefSQNonStaticCallback<CL, Func>, nparam, params, &function_proc, sizeof(function_proc));
    00061   }
    00062
    00066   template <typename Func>
    00067   void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name)
    00068   {
    00069     using namespace SQConvert;
    00070     engine->AddMethod(function_name, DefSQStaticCallback<CL, Func>, 0, NULL, &function_proc, sizeof(function_proc));
    00071   }
    00072
    00079   template <typename Func>
    00080   void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params)
    00081   {
    00082     using namespace SQConvert;
    00083     engine->AddMethod(function_name, DefSQStaticCallback<CL, Func>, nparam, params, &function_proc, sizeof(function_proc));
    00084   }
    00085
    00086   template <typename Var>
    00087   void DefSQConst(Squirrel *engine, Var value, const char *var_name)
    00088   {
    00089     engine->AddConst(var_name, value);
    00090   }
    00091
    00092   void PreRegister(Squirrel *engine)
    00093   {
    00094     engine->AddClassBegin(this->classname);
    00095   }
    00096
    00097   void PreRegister(Squirrel *engine, const char *parent_class)
    00098   {
    00099     engine->AddClassBegin(this->classname, parent_class);
    00100   }
    00101
    00102   template <typename Func, int Tnparam>
    00103   void AddConstructor(Squirrel *engine, const char *params)
    00104   {
    00105     using namespace SQConvert;
    00106     engine->AddMethod("constructor", DefSQConstructorCallback<CL, Func, Tnparam>, Tnparam, params);
    00107   }
    00108
    00109   void PostRegister(Squirrel *engine)
    00110   {
    00111     engine->AddClassEnd();
    00112   }
    00113 };
    00114
    00115 #endif /* SQUIRREL_CLASS_HPP */