OpenTTD Source  20240919-master-gdf0233f4c2
squirrel_class.hpp
Go to the documentation of this file.
1 /*
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/>.
6  */
7 
10 #ifndef SQUIRREL_CLASS_HPP
11 #define SQUIRREL_CLASS_HPP
12 
13 #include "squirrel_helper.hpp"
14 
19 template <class CL, ScriptType ST>
20 class DefSQClass {
21 private:
22  const char *classname;
23 
24 public:
25  DefSQClass(const char *_classname) :
26  classname(_classname)
27  {}
28 
32  template <typename Func>
33  void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name)
34  {
35  using namespace SQConvert;
36  engine->AddMethod(function_name, DefSQNonStaticCallback<CL, Func, ST>, 0, nullptr, &function_proc, sizeof(function_proc));
37  }
38 
42  template <typename Func>
43  void DefSQAdvancedMethod(Squirrel *engine, Func function_proc, const char *function_name)
44  {
45  using namespace SQConvert;
46  engine->AddMethod(function_name, DefSQAdvancedNonStaticCallback<CL, Func, ST>, 0, nullptr, &function_proc, sizeof(function_proc));
47  }
48 
55  template <typename Func>
56  void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params)
57  {
58  using namespace SQConvert;
59  engine->AddMethod(function_name, DefSQNonStaticCallback<CL, Func, ST>, nparam, params, &function_proc, sizeof(function_proc));
60  }
61 
65  template <typename Func>
66  void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name)
67  {
68  using namespace SQConvert;
69  engine->AddMethod(function_name, DefSQStaticCallback<CL, Func>, 0, nullptr, &function_proc, sizeof(function_proc));
70  }
71 
75  template <typename Func>
76  void DefSQAdvancedStaticMethod(Squirrel *engine, Func function_proc, const char *function_name)
77  {
78  using namespace SQConvert;
79  engine->AddMethod(function_name, DefSQAdvancedStaticCallback<CL, Func>, 0, nullptr, &function_proc, sizeof(function_proc));
80  }
81 
88  template <typename Func>
89  void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params)
90  {
91  using namespace SQConvert;
92  engine->AddMethod(function_name, DefSQStaticCallback<CL, Func>, nparam, params, &function_proc, sizeof(function_proc));
93  }
94 
95  template <typename Var>
96  void DefSQConst(Squirrel *engine, Var value, const char *var_name)
97  {
98  engine->AddConst(var_name, value);
99  }
100 
101  void PreRegister(Squirrel *engine)
102  {
103  engine->AddClassBegin(this->classname);
104  }
105 
106  void PreRegister(Squirrel *engine, const char *parent_class)
107  {
108  engine->AddClassBegin(this->classname, parent_class);
109  }
110 
111  template <typename Func, int Tnparam>
112  void AddConstructor(Squirrel *engine, const char *params)
113  {
114  using namespace SQConvert;
115  engine->AddMethod("constructor", DefSQConstructorCallback<CL, Func, Tnparam>, Tnparam, params);
116  }
117 
118  void AddSQAdvancedConstructor(Squirrel *engine)
119  {
120  using namespace SQConvert;
121  engine->AddMethod("constructor", DefSQAdvancedConstructorCallback<CL>, 0, nullptr);
122  }
123 
124  void PostRegister(Squirrel *engine)
125  {
126  engine->AddClassEnd();
127  }
128 };
129 
130 #endif /* SQUIRREL_CLASS_HPP */
DefSQClass::DefSQStaticMethod
void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name)
This defines a static method inside a class for Squirrel.
Definition: squirrel_class.hpp:66
Squirrel::AddClassBegin
void AddClassBegin(const char *class_name)
Adds a class to the global scope.
Definition: squirrel.cpp:313
DefSQClass::DefSQMethod
void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name)
This defines a method inside a class for Squirrel.
Definition: squirrel_class.hpp:33
squirrel_helper.hpp
DefSQClass::DefSQStaticMethod
void DefSQStaticMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params)
This defines a static method inside a class for Squirrel with defined params.
Definition: squirrel_class.hpp:89
Squirrel
Definition: squirrel.hpp:23
Squirrel::AddClassEnd
void AddClassEnd()
Finishes adding a class to the global scope.
Definition: squirrel.cpp:337
DefSQClass::DefSQAdvancedMethod
void DefSQAdvancedMethod(Squirrel *engine, Func function_proc, const char *function_name)
This defines a method inside a class for Squirrel, which has access to the 'engine' (experts only!...
Definition: squirrel_class.hpp:43
DefSQClass::DefSQAdvancedStaticMethod
void DefSQAdvancedStaticMethod(Squirrel *engine, Func function_proc, const char *function_name)
This defines a static method inside a class for Squirrel, which has access to the 'engine' (experts o...
Definition: squirrel_class.hpp:76
Squirrel::AddConst
void AddConst(const char *var_name, int value)
Adds a const to the stack.
Definition: squirrel.cpp:295
SQConvert
The Squirrel convert routines.
Definition: squirrel_helper.hpp:25
Squirrel::AddMethod
void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam=0, const char *params=nullptr, void *userdata=nullptr, int size=0)
Adds a function to the stack.
Definition: squirrel.cpp:278
DefSQClass
The template to define classes in Squirrel.
Definition: squirrel_class.hpp:20
DefSQClass::DefSQMethod
void DefSQMethod(Squirrel *engine, Func function_proc, const char *function_name, int nparam, const char *params)
This defines a method inside a class for Squirrel with defined params.
Definition: squirrel_class.hpp:56