File: function_types_stateless.hpp

package info (click to toggle)
sol2 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,096 kB
  • sloc: cpp: 43,816; ansic: 1,018; python: 356; sh: 288; makefile: 202
file content (385 lines) | stat: -rw-r--r-- 11,082 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// sol2

// The MIT License (MIT)

// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#ifndef SOL_FUNCTION_TYPES_STATELESS_HPP
#define SOL_FUNCTION_TYPES_STATELESS_HPP

#include <sol/stack.hpp>
#include <sol/call.hpp>
#include <sol/bind_traits.hpp>

namespace sol { namespace function_detail {
	template <typename Function>
	struct upvalue_free_function {
		using function_type = std::remove_pointer_t<std::decay_t<Function>>;
		using traits_type = meta::bind_traits<function_type>;

		static int real_call(lua_State* L)
#if SOL_IS_ON(SOL_COMPILER_VCXX)
		// MSVC is broken, what a surprise...
#else
			noexcept(traits_type::is_noexcept)
#endif
		{
			auto udata = stack::stack_detail::get_as_upvalues<function_type*>(L);
			function_type* fx = udata.first;
			return call_detail::call_wrapped<void, true, false>(L, fx);
		}

		template <bool is_yielding, bool no_trampoline>
		static int call(lua_State* L) {
			int nr;
			if constexpr (no_trampoline) {
				nr = real_call(L);
			}
			else {
				nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
			}
			if (is_yielding) {
				return lua_yield(L, nr);
			}
			else {
				return nr;
			}
		}
	};

	template <typename T, typename Function>
	struct upvalue_member_function {
		typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
		typedef lua_bind_traits<function_type> traits_type;

		static int real_call(lua_State* L)
#if SOL_IS_ON(SOL_COMPILER_VCXX)
		// MSVC is broken, what a surprise...
#else
			noexcept(traits_type::is_noexcept)
#endif
		{
			// Layout:
			// idx 1...n: verbatim data of member function pointer
			// idx n + 1: is the object's void pointer
			// We don't need to store the size, because the other side is templated
			// with the same member function pointer type
			function_type& memfx = stack::get<user<function_type>>(L, upvalue_index(2));
			auto& item = *static_cast<T*>(stack::get<void*>(L, upvalue_index(3)));
			return call_detail::call_wrapped<T, true, false, -1>(L, memfx, item);
		}

		template <bool is_yielding, bool no_trampoline>
		static int call(lua_State* L)
#if SOL_IS_ON(SOL_COMPILER_VCXX)
		// MSVC is broken, what a surprise...
#else
			noexcept(traits_type::is_noexcept)
#endif
		{
			int nr;
			if constexpr (no_trampoline) {
				nr = real_call(L);
			}
			else {
				nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
			}
			if (is_yielding) {
				return lua_yield(L, nr);
			}
			else {
				return nr;
			}
		}

		int operator()(lua_State* L)
#if SOL_IS_ON(SOL_COMPILER_VCXX)
		// MSVC is broken, what a surprise...
#else
			noexcept(traits_type::is_noexcept)
#endif
		{
			return call(L);
		}
	};

	template <typename T, typename Function>
	struct upvalue_member_variable {
		typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
		typedef lua_bind_traits<function_type> traits_type;

		static int real_call(lua_State* L)
#if SOL_IS_ON(SOL_COMPILER_VCXX)
		// MSVC is broken, what a surprise...
#else
			noexcept(traits_type::is_noexcept)
#endif
		{
			// Layout:
			// idx 1...n: verbatim data of member variable pointer
			// idx n + 1: is the object's void pointer
			// We don't need to store the size, because the other side is templated
			// with the same member function pointer type
			auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
			auto objdata = stack::stack_detail::get_as_upvalues<T*>(L, memberdata.second);
			auto& mem = *objdata.first;
			function_type& var = memberdata.first;
			switch (lua_gettop(L)) {
			case 0:
				return call_detail::call_wrapped<T, true, false, -1>(L, var, mem);
			case 1:
				return call_detail::call_wrapped<T, false, false, -1>(L, var, mem);
			default:
				return luaL_error(L, "sol: incorrect number of arguments to member variable function");
			}
		}

		template <bool is_yielding, bool no_trampoline>
		static int call(lua_State* L)
#if SOL_IS_ON(SOL_COMPILER_VCXX)
		// MSVC is broken, what a surprise...
#else
			noexcept(traits_type::is_noexcept)
#endif
		{
			int nr;
			if constexpr (no_trampoline) {
				nr = real_call(L);
			}
			else {
				nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
			}
			if (is_yielding) {
				return lua_yield(L, nr);
			}
			else {
				return nr;
			}
		}

		int operator()(lua_State* L)
#if SOL_IS_ON(SOL_COMPILER_VCXX)
		// MSVC is broken, what a surprise...
#else
			noexcept(traits_type::is_noexcept)
#endif
		{
			return call(L);
		}
	};

	template <typename T, typename Function>
	struct upvalue_member_variable<T, readonly_wrapper<Function>> {
		typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
		typedef lua_bind_traits<function_type> traits_type;

		static int real_call(lua_State* L)
#if SOL_IS_ON(SOL_COMPILER_VCXX)
		// MSVC is broken, what a surprise...
#else
			noexcept(traits_type::is_noexcept)
#endif
		{
			// Layout:
			// idx 1...n: verbatim data of member variable pointer
			// idx n + 1: is the object's void pointer
			// We don't need to store the size, because the other side is templated
			// with the same member function pointer type
			auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
			auto objdata = stack::stack_detail::get_as_upvalues<T*>(L, memberdata.second);
			auto& mem = *objdata.first;
			function_type& var = memberdata.first;
			switch (lua_gettop(L)) {
			case 0:
				return call_detail::call_wrapped<T, true, false, -1>(L, var, mem);
			default:
				return luaL_error(L, "sol: incorrect number of arguments to member variable function");
			}
		}

		template <bool is_yielding, bool no_trampoline>
		static int call(lua_State* L)
#if SOL_IS_ON(SOL_COMPILER_VCXX)
		// MSVC is broken, what a surprise...
#else
			noexcept(traits_type::is_noexcept)
#endif
		{
			int nr;
			if constexpr (no_trampoline) {
				nr = real_call(L);
			}
			else {
				nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
			}
			if (is_yielding) {
				return lua_yield(L, nr);
			}
			else {
				return nr;
			}
		}

		int operator()(lua_State* L)
#if SOL_IS_ON(SOL_COMPILER_VCXX)
		// MSVC is broken, what a surprise...
#else
			noexcept(traits_type::is_noexcept)
#endif
		{
			return call(L);
		}
	};

	template <typename T, typename Function>
	struct upvalue_this_member_function {
		typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
		typedef lua_bind_traits<function_type> traits_type;

		static int real_call(lua_State* L)
#if SOL_IS_ON(SOL_COMPILER_VCXX)
		// MSVC is broken, what a surprise...
#else
			noexcept(traits_type::is_noexcept)
#endif
		{
			// Layout:
			// idx 1...n: verbatim data of member variable pointer
			function_type& memfx = stack::get<user<function_type>>(L, upvalue_index(2));
			return call_detail::call_wrapped<T, false, false>(L, memfx);
		}

		template <bool is_yielding, bool no_trampoline>
		static int call(lua_State* L)
#if SOL_IS_ON(SOL_COMPILER_VCXX)
		// MSVC is broken, what a surprise...
#else
			noexcept(traits_type::is_noexcept)
#endif
		{
			int nr;
			if constexpr (no_trampoline) {
				nr = real_call(L);
			}
			else {
				nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
			}
			if (is_yielding) {
				return lua_yield(L, nr);
			}
			else {
				return nr;
			}
		}

		int operator()(lua_State* L)
#if SOL_IS_ON(SOL_COMPILER_VCXX)
		// MSVC is broken, what a surprise...
#else
			noexcept(traits_type::is_noexcept)
#endif
		{
			return call(L);
		}
	};

	template <typename T, typename Function>
	struct upvalue_this_member_variable {
		typedef std::remove_pointer_t<std::decay_t<Function>> function_type;

		static int real_call(lua_State* L) noexcept(std::is_nothrow_copy_assignable_v<T>) {
			// Layout:
			// idx 1...n: verbatim data of member variable pointer
			auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
			function_type& var = memberdata.first;
			switch (lua_gettop(L)) {
			case 1:
				return call_detail::call_wrapped<T, true, false>(L, var);
			case 2:
				return call_detail::call_wrapped<T, false, false>(L, var);
			default:
				return luaL_error(L, "sol: incorrect number of arguments to member variable function");
			}
		}

		template <bool is_yielding, bool no_trampoline>
		static int call(lua_State* L) noexcept(std::is_nothrow_copy_assignable_v<T>) {
			int nr;
			if constexpr (no_trampoline) {
				nr = real_call(L);
			}
			else {
				nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
			}
			if (is_yielding) {
				return lua_yield(L, nr);
			}
			else {
				return nr;
			}
		}

		int operator()(lua_State* L) noexcept(std::is_nothrow_copy_assignable_v<T>) {
			return call(L);
		}
	};

	template <typename T, typename Function>
	struct upvalue_this_member_variable<T, readonly_wrapper<Function>> {
		typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
		typedef lua_bind_traits<function_type> traits_type;

		static int real_call(lua_State* L) noexcept(std::is_nothrow_copy_assignable_v<T>) {
			// Layout:
			// idx 1...n: verbatim data of member variable pointer
			auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
			function_type& var = memberdata.first;
			switch (lua_gettop(L)) {
			case 1:
				return call_detail::call_wrapped<T, true, false>(L, var);
			default:
				return luaL_error(L, "sol: incorrect number of arguments to member variable function");
			}
		}

		template <bool is_yielding, bool no_trampoline>
		static int call(lua_State* L) noexcept(std::is_nothrow_copy_assignable_v<T>) {
			int nr;
			if constexpr (no_trampoline) {
				nr = real_call(L);
			}
			else {
				nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
			}
			if (is_yielding) {
				return lua_yield(L, nr);
			}
			else {
				return nr;
			}
		}

		int operator()(lua_State* L) noexcept(std::is_nothrow_copy_assignable_v<T>) {
			return call(L);
		}
	};
}} // namespace sol::function_detail

#endif // SOL_FUNCTION_TYPES_STATELESS_HPP