OpenTTD Source 20260311-master-g511d3794ce
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef SQUIRREL_CLASS_HPP
11#define SQUIRREL_CLASS_HPP
12
13#include "squirrel_helper.hpp"
14
19template <class CL, ScriptType ST>
20class DefSQClass {
21private:
22 std::string_view classname;
23
24public:
25 DefSQClass(std::string_view _classname) :
26 classname(_classname)
27 {}
28
40 template <typename Func>
41 void DefSQMethod(Squirrel &engine, Func function_proc, std::string_view function_name, std::string_view params = {}, bool suspendable = false)
42 {
43 using namespace SQConvert;
44 engine.AddMethod(function_name, DefSQNonStaticCallback<CL, Func, ST>, params, &function_proc, sizeof(function_proc), suspendable);
45 }
46
54 template <typename Func>
55 void DefSQAdvancedMethod(Squirrel &engine, Func function_proc, std::string_view function_name, bool suspendable = false)
56 {
57 using namespace SQConvert;
58 engine.AddMethod(function_name, DefSQAdvancedNonStaticCallback<CL, Func, ST>, {}, &function_proc, sizeof(function_proc), suspendable);
59 }
60
72 template <typename Func>
73 void DefSQStaticMethod(Squirrel &engine, Func function_proc, std::string_view function_name, std::string_view params = {}, bool suspendable = false)
74 {
75 using namespace SQConvert;
76 engine.AddMethod(function_name, DefSQStaticCallback<CL, Func>, params, &function_proc, sizeof(function_proc), suspendable);
77 }
78
86 template <typename Func>
87 void DefSQAdvancedStaticMethod(Squirrel &engine, Func function_proc, std::string_view function_name, bool suspendable = false)
88 {
89 using namespace SQConvert;
90 engine.AddMethod(function_name, DefSQAdvancedStaticCallback<CL, Func>, {}, &function_proc, sizeof(function_proc), suspendable);
91 }
92
93 template <typename Var>
94 void DefSQConst(Squirrel &engine, Var value, std::string_view var_name)
95 {
96 engine.AddConst(var_name, value);
97 }
98
99 void PreRegister(Squirrel &engine)
100 {
101 engine.AddClassBegin(this->classname);
102 }
103
104 void PreRegister(Squirrel &engine, std::string_view parent_class)
105 {
106 engine.AddClassBegin(this->classname, parent_class);
107 }
108
109 template <typename Func>
110 void AddConstructor(Squirrel &engine, std::string_view params)
111 {
112 using namespace SQConvert;
113 engine.AddMethod("constructor", DefSQConstructorCallback<CL, Func>, params);
114 }
115
116 void AddSQAdvancedConstructor(Squirrel &engine)
117 {
118 using namespace SQConvert;
119 engine.AddMethod("constructor", DefSQAdvancedConstructorCallback<CL>);
120 }
121
122 void PostRegister(Squirrel &engine)
123 {
124 engine.AddClassEnd();
125 }
126};
127
128#endif /* SQUIRREL_CLASS_HPP */
void DefSQMethod(Squirrel &engine, Func function_proc, std::string_view function_name, std::string_view params={}, bool suspendable=false)
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={}, bool suspendable=false)
This defines a static method inside a class for Squirrel with defined params.
void DefSQAdvancedMethod(Squirrel &engine, Func function_proc, std::string_view function_name, bool suspendable=false)
This defines a method inside a class for Squirrel, which has access to the 'engine' (experts only!...
void DefSQAdvancedStaticMethod(Squirrel &engine, Func function_proc, std::string_view function_name, bool suspendable=false)
This defines a static method inside a class for Squirrel, which has access to the 'engine' (experts o...
void AddClassEnd()
Finishes adding a class to the global scope.
Definition squirrel.cpp:336
void AddConst(std::string_view var_name, SQInteger value)
Adds a const to the stack.
Definition squirrel.cpp:294
void AddMethod(std::string_view method_name, SQFUNCTION proc, std::string_view params={}, void *userdata=nullptr, int size=0, bool suspendable=false)
Adds a function to the stack.
Definition squirrel.cpp:259
void AddClassBegin(std::string_view class_name)
Adds a class to the global scope.
Definition squirrel.cpp:312
The Squirrel convert routines.
SQInteger DefSQAdvancedStaticCallback(HSQUIRRELVM vm)
A general template for all static advanced method callbacks from Squirrel.
SQInteger DefSQAdvancedNonStaticCallback(HSQUIRRELVM vm)
A general template for all non-static advanced method callbacks from Squirrel.
SQInteger DefSQNonStaticCallback(HSQUIRRELVM vm)
A general template for all non-static method callbacks from Squirrel.
SQInteger DefSQStaticCallback(HSQUIRRELVM vm)
A general template for all function/static method callbacks from Squirrel.
Declarations and parts of the implementation of the class for convert code.