OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

squirrel_class.hpp
Go to the documentation of this file.
1 /* $Id: squirrel_class.hpp 23651 2011-12-21 14:55:28Z yexo $ */
2
3 /*
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/>.
8 */
9
12 #ifndef SQUIRREL_CLASS_HPP
13 #define SQUIRREL_CLASS_HPP
14
15 #include "squirrel_helper.hpp"
16
21 template <class CL, ScriptType ST>
22 class DefSQClass {
23 private:
24 const char *classname;
25
26 public:
27 DefSQClass(const char *_classname) :
28 classname(_classname)
29 {}
30
34 template <typename Func>
35 void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name)
36 {
37 using namespace SQConvert;
38 engine->AddMethod(function_name, DefSQNonStaticCallback<CL, Func, ST>, 0, NULL, &function_proc, sizeof(function_proc));
39 }
40
44 template <typename Func>
45 void DefSQAdvancedMethod(Squirrel *engine, Func function_proc, const char *function_name)
46 {
47 using namespace SQConvert;
48 engine->AddMethod(function_name, DefSQAdvancedNonStaticCallback<CL, Func, ST>, 0, NULL, &function_proc, sizeof(function_proc));
49 }
50
57 template <typename Func>
58 void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params)
59 {
60 using namespace SQConvert;
61 engine->AddMethod(function_name, DefSQNonStaticCallback<CL, Func, ST>, nparam, params, &function_proc, sizeof(function_proc));
62 }
63
67 template <typename Func>
68 void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name)
69 {
70 using namespace SQConvert;
71 engine->AddMethod(function_name, DefSQStaticCallback<CL, Func>, 0, NULL, &function_proc, sizeof(function_proc));
72 }
73
77 template <typename Func>
78 void DefSQAdvancedStaticMethod(Squirrel *engine, Func function_proc, const char *function_name)
79 {
80 using namespace SQConvert;
81 engine->AddMethod(function_name, DefSQAdvancedStaticCallback<CL, Func>, 0, NULL, &function_proc, sizeof(function_proc));
82 }
83
90 template <typename Func>
91 void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params)
92 {
93 using namespace SQConvert;
94 engine->AddMethod(function_name, DefSQStaticCallback<CL, Func>, nparam, params, &function_proc, sizeof(function_proc));
95 }
96
97 template <typename Var>
98 void DefSQConst(Squirrel *engine, Var value, const char *var_name)
99 {
100 engine->AddConst(var_name, value);
101 }
102
103 void PreRegister(Squirrel *engine)
104 {
105 engine->AddClassBegin(this->classname);
106 }
107
108 void PreRegister(Squirrel *engine, const char *parent_class)
109 {
110 engine->AddClassBegin(this->classname, parent_class);
111 }
112
113 template <typename Func, int Tnparam>
114 void AddConstructor(Squirrel *engine, const char *params)
115 {
116 using namespace SQConvert;
117 engine->AddMethod("constructor", DefSQConstructorCallback<CL, Func, Tnparam>, Tnparam, params);
118 }
119
120 void AddSQAdvancedConstructor(Squirrel *engine)
121 {
122 using namespace SQConvert;
123 engine->AddMethod("constructor", DefSQAdvancedConstructorCallback<CL>, 0, NULL);
124 }
125
126 void PostRegister(Squirrel *engine)
127 {
128 engine->AddClassEnd();
129 }
130 };
131
132 #endif /* SQUIRREL_CLASS_HPP */