2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
8 /** @file squirrel_class.hpp Defines templates for converting C++ classes to Squirrel classes */
10 #ifndef SQUIRREL_CLASS_HPP
11 #define SQUIRREL_CLASS_HPP
13 #include "squirrel_helper.hpp"
16 * The template to define classes in Squirrel. It takes care of the creation
17 * and calling of such classes, to minimize the API layer.
19 template <class CL
, ScriptType ST
>
22 const char *classname
;
25 DefSQClass(const char *_classname
) :
30 * This defines a method inside a class for Squirrel.
32 template <typename Func
>
33 void DefSQMethod(Squirrel
*engine
, Func function_proc
, const char *function_name
)
35 using namespace SQConvert
;
36 engine
->AddMethod(function_name
, DefSQNonStaticCallback
<CL
, Func
, ST
>, 0, nullptr, &function_proc
, sizeof(function_proc
));
40 * This defines a method inside a class for Squirrel, which has access to the 'engine' (experts only!).
42 template <typename Func
>
43 void DefSQAdvancedMethod(Squirrel
*engine
, Func function_proc
, const char *function_name
)
45 using namespace SQConvert
;
46 engine
->AddMethod(function_name
, DefSQAdvancedNonStaticCallback
<CL
, Func
, ST
>, 0, nullptr, &function_proc
, sizeof(function_proc
));
50 * This defines a method inside a class for Squirrel with defined params.
51 * @note If you define nparam, make sure that the first param is always 'x',
52 * which is the 'this' inside the function. This is hidden from the rest
53 * of the code, but without it calling your function will fail!
55 template <typename Func
>
56 void DefSQMethod(Squirrel
*engine
, Func function_proc
, const char *function_name
, int nparam
, const char *params
)
58 using namespace SQConvert
;
59 engine
->AddMethod(function_name
, DefSQNonStaticCallback
<CL
, Func
, ST
>, nparam
, params
, &function_proc
, sizeof(function_proc
));
63 * This defines a static method inside a class for Squirrel.
65 template <typename Func
>
66 void DefSQStaticMethod(Squirrel
*engine
, Func function_proc
, const char *function_name
)
68 using namespace SQConvert
;
69 engine
->AddMethod(function_name
, DefSQStaticCallback
<CL
, Func
>, 0, nullptr, &function_proc
, sizeof(function_proc
));
73 * This defines a static method inside a class for Squirrel, which has access to the 'engine' (experts only!).
75 template <typename Func
>
76 void DefSQAdvancedStaticMethod(Squirrel
*engine
, Func function_proc
, const char *function_name
)
78 using namespace SQConvert
;
79 engine
->AddMethod(function_name
, DefSQAdvancedStaticCallback
<CL
, Func
>, 0, nullptr, &function_proc
, sizeof(function_proc
));
83 * This defines a static method inside a class for Squirrel with defined params.
84 * @note If you define nparam, make sure that the first param is always 'x',
85 * which is the 'this' inside the function. This is hidden from the rest
86 * of the code, but without it calling your function will fail!
88 template <typename Func
>
89 void DefSQStaticMethod(Squirrel
*engine
, Func function_proc
, const char *function_name
, int nparam
, const char *params
)
91 using namespace SQConvert
;
92 engine
->AddMethod(function_name
, DefSQStaticCallback
<CL
, Func
>, nparam
, params
, &function_proc
, sizeof(function_proc
));
95 template <typename Var
>
96 void DefSQConst(Squirrel
*engine
, Var value
, const char *var_name
)
98 engine
->AddConst(var_name
, value
);
101 void PreRegister(Squirrel
*engine
)
103 engine
->AddClassBegin(this->classname
);
106 void PreRegister(Squirrel
*engine
, const char *parent_class
)
108 engine
->AddClassBegin(this->classname
, parent_class
);
111 template <typename Func
, int Tnparam
>
112 void AddConstructor(Squirrel
*engine
, const char *params
)
114 using namespace SQConvert
;
115 engine
->AddMethod("constructor", DefSQConstructorCallback
<CL
, Func
, Tnparam
>, Tnparam
, params
);
118 void AddSQAdvancedConstructor(Squirrel
*engine
)
120 using namespace SQConvert
;
121 engine
->AddMethod("constructor", DefSQAdvancedConstructorCallback
<CL
>, 0, nullptr);
124 void PostRegister(Squirrel
*engine
)
126 engine
->AddClassEnd();
130 #endif /* SQUIRREL_CLASS_HPP */