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