OpenTTD Source 20250714-master-g67e56391c7
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
35 template <typename Func>
36 void DefSQMethod(Squirrel &engine, Func function_proc, std::string_view function_name, std::string_view params = {})
37 {
38 using namespace SQConvert;
39 engine.AddMethod(function_name, DefSQNonStaticCallback<CL, Func, ST>, params, &function_proc, sizeof(function_proc));
40 }
41
45 template <typename Func>
46 void DefSQAdvancedMethod(Squirrel &engine, Func function_proc, std::string_view function_name)
47 {
48 using namespace SQConvert;
49 engine.AddMethod(function_name, DefSQAdvancedNonStaticCallback<CL, Func, ST>, {}, &function_proc, sizeof(function_proc));
50 }
51
58 template <typename Func>
59 void DefSQStaticMethod(Squirrel &engine, Func function_proc, std::string_view function_name, std::string_view params = {})
60 {
61 using namespace SQConvert;
62 engine.AddMethod(function_name, DefSQStaticCallback<CL, Func>, params, &function_proc, sizeof(function_proc));
63 }
64
68 template <typename Func>
69 void DefSQAdvancedStaticMethod(Squirrel &engine, Func function_proc, std::string_view function_name)
70 {
71 using namespace SQConvert;
72 engine.AddMethod(function_name, DefSQAdvancedStaticCallback<CL, Func>, {}, &function_proc, sizeof(function_proc));
73 }
74
75 template <typename Var>
76 void DefSQConst(Squirrel &engine, Var value, std::string_view var_name)
77 {
78 engine.AddConst(var_name, value);
79 }
80
81 void PreRegister(Squirrel &engine)
82 {
83 engine.AddClassBegin(this->classname);
84 }
85
86 void PreRegister(Squirrel &engine, std::string_view parent_class)
87 {
88 engine.AddClassBegin(this->classname, parent_class);
89 }
90
91 template <typename Func>
92 void AddConstructor(Squirrel &engine, std::string_view params)
93 {
94 using namespace SQConvert;
95 engine.AddMethod("constructor", DefSQConstructorCallback<CL, Func>, params);
96 }
97
98 void AddSQAdvancedConstructor(Squirrel &engine)
99 {
100 using namespace SQConvert;
101 engine.AddMethod("constructor", DefSQAdvancedConstructorCallback<CL>);
102 }
103
104 void PostRegister(Squirrel &engine)
105 {
106 engine.AddClassEnd();
107 }
108};
109
110#endif /* SQUIRREL_CLASS_HPP */
The template to define classes in Squirrel.
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 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 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