1 /* $Id: squirrel_class.hpp 23651 2011-12-21 14:55:28Z yexo $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file squirrel_class.hpp Defines templates for converting C++ classes to Squirrel classes */
12 #ifndef SQUIRREL_CLASS_HPP
13 #define SQUIRREL_CLASS_HPP
15 #include "squirrel_helper.hpp"
18 * The template to define classes in Squirrel. It takes care of the creation
19 * and calling of such classes, to minimize the API layer.
21 template <class CL
, ScriptType ST
>
24 const char *classname
;
27 DefSQClass(const char *_classname
) :
32 * This defines a method inside a class for Squirrel.
34 template <typename Func
>
35 void DefSQMethod(Squirrel
*engine
, Func function_proc
, const char *function_name
)
37 using namespace SQConvert
;
38 engine
->AddMethod(function_name
, DefSQNonStaticCallback
<CL
, Func
, ST
>, 0, nullptr, &function_proc
, sizeof(function_proc
));
42 * This defines a method inside a class for Squirrel, which has access to the 'engine' (experts only!).
44 template <typename Func
>
45 void DefSQAdvancedMethod(Squirrel
*engine
, Func function_proc
, const char *function_name
)
47 using namespace SQConvert
;
48 engine
->AddMethod(function_name
, DefSQAdvancedNonStaticCallback
<CL
, Func
, ST
>, 0, nullptr, &function_proc
, sizeof(function_proc
));
52 * This defines a method inside a class for Squirrel with defined params.
53 * @note If you define nparam, make sure that he first param is always 'x',
54 * which is the 'this' inside the function. This is hidden from the rest
55 * of the code, but without it calling your function will fail!
57 template <typename Func
>
58 void DefSQMethod(Squirrel
*engine
, Func function_proc
, const char *function_name
, int nparam
, const char *params
)
60 using namespace SQConvert
;
61 engine
->AddMethod(function_name
, DefSQNonStaticCallback
<CL
, Func
, ST
>, nparam
, params
, &function_proc
, sizeof(function_proc
));
65 * This defines a static method inside a class for Squirrel.
67 template <typename Func
>
68 void DefSQStaticMethod(Squirrel
*engine
, Func function_proc
, const char *function_name
)
70 using namespace SQConvert
;
71 engine
->AddMethod(function_name
, DefSQStaticCallback
<CL
, Func
>, 0, nullptr, &function_proc
, sizeof(function_proc
));
75 * This defines a static method inside a class for Squirrel, which has access to the 'engine' (experts only!).
77 template <typename Func
>
78 void DefSQAdvancedStaticMethod(Squirrel
*engine
, Func function_proc
, const char *function_name
)
80 using namespace SQConvert
;
81 engine
->AddMethod(function_name
, DefSQAdvancedStaticCallback
<CL
, Func
>, 0, nullptr, &function_proc
, sizeof(function_proc
));
85 * This defines a static method inside a class for Squirrel with defined params.
86 * @note If you define nparam, make sure that he first param is always 'x',
87 * which is the 'this' inside the function. This is hidden from the rest
88 * of the code, but without it calling your function will fail!
90 template <typename Func
>
91 void DefSQStaticMethod(Squirrel
*engine
, Func function_proc
, const char *function_name
, int nparam
, const char *params
)
93 using namespace SQConvert
;
94 engine
->AddMethod(function_name
, DefSQStaticCallback
<CL
, Func
>, nparam
, params
, &function_proc
, sizeof(function_proc
));
97 template <typename Var
>
98 void DefSQConst(Squirrel
*engine
, Var value
, const char *var_name
)
100 engine
->AddConst(var_name
, value
);
103 void PreRegister(Squirrel
*engine
)
105 engine
->AddClassBegin(this->classname
);
108 void PreRegister(Squirrel
*engine
, const char *parent_class
)
110 engine
->AddClassBegin(this->classname
, parent_class
);
113 template <typename Func
, int Tnparam
>
114 void AddConstructor(Squirrel
*engine
, const char *params
)
116 using namespace SQConvert
;
117 engine
->AddMethod("constructor", DefSQConstructorCallback
<CL
, Func
, Tnparam
>, Tnparam
, params
);
120 void AddSQAdvancedConstructor(Squirrel
*engine
)
122 using namespace SQConvert
;
123 engine
->AddMethod("constructor", DefSQAdvancedConstructorCallback
<CL
>, 0, nullptr);
126 void PostRegister(Squirrel
*engine
)
128 engine
->AddClassEnd();
132 #endif /* SQUIRREL_CLASS_HPP */