# squirrel_class.hpp

Go to the documentation of this file.

    00001 /* $Id: squirrel_class.hpp 23651 2011-12-21 14:55:28Z yexo $ */
    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
    00077   template <typename Func>
    00078   void DefSQAdvancedStaticMethod(Squirrel *engine, Func function_proc, const char *function_name)
    00079   {
    00080     using namespace SQConvert;
    00081     engine->AddMethod(function_name, DefSQAdvancedStaticCallback<CL, Func>, 0, NULL, &function_proc, sizeof(function_proc));
    00082   }
    00083
    00090   template <typename Func>
    00091   void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params)
    00092   {
    00093     using namespace SQConvert;
    00094     engine->AddMethod(function_name, DefSQStaticCallback<CL, Func>, nparam, params, &function_proc, sizeof(function_proc));
    00095   }
    00096
    00097   template <typename Var>
    00098   void DefSQConst(Squirrel *engine, Var value, const char *var_name)
    00099   {
    00100     engine->AddConst(var_name, value);
    00101   }
    00102
    00103   void PreRegister(Squirrel *engine)
    00104   {
    00105     engine->AddClassBegin(this->classname);
    00106   }
    00107
    00108   void PreRegister(Squirrel *engine, const char *parent_class)
    00109   {
    00110     engine->AddClassBegin(this->classname, parent_class);
    00111   }
    00112
    00113   template <typename Func, int Tnparam>
    00114   void AddConstructor(Squirrel *engine, const char *params)
    00115   {
    00116     using namespace SQConvert;
    00117     engine->AddMethod("constructor", DefSQConstructorCallback<CL, Func, Tnparam>, Tnparam, params);
    00118   }
    00119
    00120   void AddSQAdvancedConstructor(Squirrel *engine)
    00121   {
    00122     using namespace SQConvert;
    00123     engine->AddMethod("constructor", DefSQAdvancedConstructorCallback<CL>, 0, NULL);
    00124   }
    00125
    00126   void PostRegister(Squirrel *engine)
    00127   {
    00128     engine->AddClassEnd();
    00129   }
    00130 };
    00131
    00132 #endif /* SQUIRREL_CLASS_HPP */