1 // The MIT License (MIT)
2 
3 // Copyright (c) 2013-2016 Rapptz, ThePhD and contributors
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal in
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 // the Software, and to permit persons to whom the Software is furnished to do so,
10 // subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #ifndef SOL_TYPES_HPP
23 #define SOL_TYPES_HPP
24 
25 #include "optional.hpp"
26 #include "compatibility.hpp"
27 #include "traits.hpp"
28 #include "string_shim.hpp"
29 #include <array>
30 #include <string>
31 
32 namespace sol {
33 	namespace detail {
34 #ifdef SOL_NO_EXCEPTIONS
35 		template <lua_CFunction f>
static_trampoline(lua_State * L)36 		int static_trampoline(lua_State* L) {
37 			return f(L);
38 		}
39 
40 		template <typename Fx, typename... Args>
trampoline(lua_State * L,Fx && f,Args &&...args)41 		int trampoline(lua_State* L, Fx&& f, Args&&... args) {
42 			return f(L, std::forward<Args>(args)...);
43 		}
44 
c_trampoline(lua_State * L,lua_CFunction f)45 		inline int c_trampoline(lua_State* L, lua_CFunction f) {
46 			return trampoline(L, f);
47 		}
48 #else
49 		template <lua_CFunction f>
50 		int static_trampoline(lua_State* L) {
51 			try {
52 				return f(L);
53 			}
54 			catch (const char *s) {
55 				lua_pushstring(L, s);
56 			}
57 			catch (const std::exception& e) {
58 				lua_pushstring(L, e.what());
59 			}
60 #if !defined(SOL_EXCEPTIONS_SAFE_PROPAGATION)
61 			catch (...) {
62 				lua_pushstring(L, "caught (...) exception");
63 			}
64 #endif
65 			return lua_error(L);
66 		}
67 
68 		template <typename Fx, typename... Args>
69 		int trampoline(lua_State* L, Fx&& f, Args&&... args) {
70 			try {
71 				return f(L, std::forward<Args>(args)...);
72 			}
73 			catch (const char *s) {
74 				lua_pushstring(L, s);
75 			}
76 			catch (const std::exception& e) {
77 				lua_pushstring(L, e.what());
78 			}
79 #if !defined(SOL_EXCEPTIONS_SAFE_PROPAGATION)
80 			catch (...) {
81 				lua_pushstring(L, "caught (...) exception");
82 			}
83 #endif
84 			return lua_error(L);
85 		}
86 
87 		inline int c_trampoline(lua_State* L, lua_CFunction f) {
88 			return trampoline(L, f);
89 		}
90 #endif // Exceptions vs. No Exceptions
91 
92 		template <typename T>
93 		struct unique_usertype {};
94 
95 		template <typename T>
96 		struct implicit_wrapper {
97 			T& item;
implicit_wrappersol::detail::implicit_wrapper98 			implicit_wrapper(T* item) : item(*item) {}
implicit_wrappersol::detail::implicit_wrapper99 			implicit_wrapper(T& item) : item(item) {}
operator T&sol::detail::implicit_wrapper100 			operator T& () {
101 				return item;
102 			}
operator T*sol::detail::implicit_wrapper103 			operator T* () {
104 				return std::addressof(item);
105 			}
106 		};
107 	} // detail
108 
109 	struct lua_nil_t {};
110 	const lua_nil_t lua_nil{};
operator ==(lua_nil_t,lua_nil_t)111 	inline bool operator==(lua_nil_t, lua_nil_t) { return true; }
operator !=(lua_nil_t,lua_nil_t)112 	inline bool operator!=(lua_nil_t, lua_nil_t) { return false; }
113 #ifndef __OBJC__
114 	typedef lua_nil_t nil_t;
115 	const nil_t nil{};
116 #endif
117 
118 	struct metatable_key_t {};
119 	const metatable_key_t metatable_key = {};
120 
121 	struct no_metatable_t {};
122 	const no_metatable_t no_metatable = {};
123 
124 	typedef std::remove_pointer_t<lua_CFunction> lua_r_CFunction;
125 
126 	template <typename T>
127 	struct unique_usertype_traits {
128 		typedef T type;
129 		typedef T actual_type;
130 		static const bool value = false;
131 
132 		template <typename U>
is_nullsol::unique_usertype_traits133 		static bool is_null(U&&) {
134 			return false;
135 		}
136 
137 		template <typename U>
getsol::unique_usertype_traits138 		static auto get(U&& value) {
139 			return std::addressof(detail::deref(value));
140 		}
141 	};
142 
143 	template <typename T>
144 	struct unique_usertype_traits<std::shared_ptr<T>> {
145 		typedef T type;
146 		typedef std::shared_ptr<T> actual_type;
147 		static const bool value = true;
148 
is_nullsol::unique_usertype_traits149 		static bool is_null(const actual_type& p) {
150 			return p == nullptr;
151 		}
152 
getsol::unique_usertype_traits153 		static type* get(const actual_type& p) {
154 			return p.get();
155 		}
156 	};
157 
158 	template <typename T, typename D>
159 	struct unique_usertype_traits<std::unique_ptr<T, D>> {
160 		typedef T type;
161 		typedef std::unique_ptr<T, D> actual_type;
162 		static const bool value = true;
163 
is_nullsol::unique_usertype_traits164 		static bool is_null(const actual_type& p) {
165 			return p == nullptr;
166 		}
167 
getsol::unique_usertype_traits168 		static type* get(const actual_type& p) {
169 			return p.get();
170 		}
171 	};
172 
173 	template <typename T>
174 	struct non_null {};
175 
176 	template <typename... Args>
177 	struct function_sig {};
178 
179 	struct upvalue_index {
180 		int index;
upvalue_indexsol::upvalue_index181 		upvalue_index(int idx) : index(lua_upvalueindex(idx)) {}
operator intsol::upvalue_index182 		operator int() const { return index; }
183 	};
184 
185 	struct raw_index {
186 		int index;
raw_indexsol::raw_index187 		raw_index(int i) : index(i) {}
operator intsol::raw_index188 		operator int() const { return index; }
189 	};
190 
191 	struct absolute_index {
192 		int index;
absolute_indexsol::absolute_index193 		absolute_index(lua_State* L, int idx) : index(lua_absindex(L, idx)) {}
operator intsol::absolute_index194 		operator int() const { return index; }
195 	};
196 
197 	struct ref_index {
198 		int index;
ref_indexsol::ref_index199 		ref_index(int idx) : index(idx) {}
operator intsol::ref_index200 		operator int() const { return index; }
201 	};
202 
203 	struct lightuserdata_value {
204 		void* value;
lightuserdata_valuesol::lightuserdata_value205 		lightuserdata_value(void* data) : value(data) {}
operator void*sol::lightuserdata_value206 		operator void*() const { return value; }
207 	};
208 
209 	struct userdata_value {
210 		void* value;
userdata_valuesol::userdata_value211 		userdata_value(void* data) : value(data) {}
operator void*sol::userdata_value212 		operator void*() const { return value; }
213 	};
214 
215 	template <typename L>
216 	struct light {
217 		L* value;
218 
lightsol::light219 		light(L& x) : value(std::addressof(x)) {}
lightsol::light220 		light(L* x) : value(x) {}
lightsol::light221 		light(void* x) : value(static_cast<L*>(x)) {}
operator L*sol::light222 		operator L* () const { return value; }
operator L&sol::light223 		operator L& () const { return *value; }
224 	};
225 
226 	template <typename T>
make_light(T & l)227 	auto make_light(T& l) {
228 		typedef meta::unwrapped_t<std::remove_pointer_t<std::remove_pointer_t<T>>> L;
229 		return light<L>(l);
230 	}
231 
232 	template <typename U>
233 	struct user {
234 		U value;
235 
usersol::user236 		user(U x) : value(std::move(x)) {}
operator U*sol::user237 		operator U* () { return std::addressof(value); }
operator U&sol::user238 		operator U& () { return value; }
operator const U&sol::user239 		operator const U& () const { return value; }
240 	};
241 
242 	template <typename T>
make_user(T && u)243 	auto make_user(T&& u) {
244 		typedef meta::unwrapped_t<meta::unqualified_t<T>> U;
245 		return user<U>(std::forward<T>(u));
246 	}
247 
248 	template <typename T>
249 	struct metatable_registry_key {
250 		T key;
251 
metatable_registry_keysol::metatable_registry_key252 		metatable_registry_key(T key) : key(std::forward<T>(key)) {}
253 	};
254 
255 	template <typename T>
meta_registry_key(T && key)256 	auto meta_registry_key(T&& key) {
257 		typedef meta::unqualified_t<T> K;
258 		return metatable_registry_key<K>(std::forward<T>(key));
259 	}
260 
261 	template <typename... Upvalues>
262 	struct closure {
263 		lua_CFunction c_function;
264 		std::tuple<Upvalues...> upvalues;
closuresol::closure265 		closure(lua_CFunction f, Upvalues... targetupvalues) : c_function(f), upvalues(std::forward<Upvalues>(targetupvalues)...) {}
266 	};
267 
268 	template <>
269 	struct closure<> {
270 		lua_CFunction c_function;
271 		int upvalues;
closuresol::closure272 		closure(lua_CFunction f, int upvalue_count = 0) : c_function(f), upvalues(upvalue_count) {}
273 	};
274 
275 	typedef closure<> c_closure;
276 
277 	template <typename... Args>
make_closure(lua_CFunction f,Args &&...args)278 	closure<Args...> make_closure(lua_CFunction f, Args&&... args) {
279 		return closure<Args...>(f, std::forward<Args>(args)...);
280 	}
281 
282 	template <typename Sig, typename... Ps>
283 	struct function_arguments {
284 		std::tuple<Ps...> arguments;
285 		template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, function_arguments>> = meta::enabler>
function_argumentssol::function_arguments286 		function_arguments(Arg&& arg, Args&&... args) : arguments(std::forward<Arg>(arg), std::forward<Args>(args)...) {}
287 	};
288 
289 	template <typename Sig = function_sig<>, typename... Args>
as_function(Args &&...args)290 	auto as_function(Args&&... args) {
291 		return function_arguments<Sig, std::decay_t<Args>...>(std::forward<Args>(args)...);
292 	}
293 
294 	template <typename Sig = function_sig<>, typename... Args>
as_function_reference(Args &&...args)295 	auto as_function_reference(Args&&... args) {
296 		return function_arguments<Sig, Args...>(std::forward<Args>(args)...);
297 	}
298 
299 	template <typename T>
300 	struct as_table_t {
301 		T source;
302 		template <typename... Args>
as_table_tsol::as_table_t303 		as_table_t(Args&&... args) : source(std::forward<Args>(args)...) {}
304 
operator std::add_lvalue_reference_t<T>sol::as_table_t305 		operator std::add_lvalue_reference_t<T> () {
306 			return source;
307 		}
308 	};
309 
310 	template <typename T>
as_table(T && container)311 	as_table_t<T> as_table(T&& container) {
312 		return as_table_t<T>(std::forward<T>(container));
313 	}
314 
315 	struct this_state {
316 		lua_State* L;
operator lua_State*sol::this_state317 		operator lua_State* () const {
318 			return L;
319 		}
operator ->sol::this_state320 		lua_State* operator-> () const {
321 			return L;
322 		}
323 	};
324 
325 	enum class call_syntax {
326 		dot = 0,
327 		colon = 1
328 	};
329 
330 	enum class call_status : int {
331 		ok = LUA_OK,
332 		yielded = LUA_YIELD,
333 		runtime = LUA_ERRRUN,
334 		memory = LUA_ERRMEM,
335 		handler = LUA_ERRERR,
336 		gc = LUA_ERRGCMM
337 	};
338 
339 	enum class thread_status : int {
340 		ok = LUA_OK,
341 		yielded = LUA_YIELD,
342 		runtime = LUA_ERRRUN,
343 		memory = LUA_ERRMEM,
344 		gc = LUA_ERRGCMM,
345 		handler = LUA_ERRERR,
346 		dead = -1,
347 	};
348 
349 	enum class load_status : int {
350 		ok = LUA_OK,
351 		syntax = LUA_ERRSYNTAX,
352 		memory = LUA_ERRMEM,
353 		gc = LUA_ERRGCMM,
354 		file = LUA_ERRFILE,
355 	};
356 
357 	enum class type : int {
358 		none = LUA_TNONE,
359 		lua_nil = LUA_TNIL,
360 #ifndef __OBJC__
361 		nil = lua_nil,
362 #endif // Objective C++ Keyword
363 		string = LUA_TSTRING,
364 		number = LUA_TNUMBER,
365 		thread = LUA_TTHREAD,
366 		boolean = LUA_TBOOLEAN,
367 		function = LUA_TFUNCTION,
368 		userdata = LUA_TUSERDATA,
369 		lightuserdata = LUA_TLIGHTUSERDATA,
370 		table = LUA_TTABLE,
371 		poly = none | lua_nil | string | number | thread |
372 		table | boolean | function | userdata | lightuserdata
373 	};
374 
375 	enum class meta_function {
376 		construct,
377 		index,
378 		new_index,
379 		mode,
380 		call,
381 		call_function = call,
382 		metatable,
383 		to_string,
384 		length,
385 		unary_minus,
386 		addition,
387 		subtraction,
388 		multiplication,
389 		division,
390 		modulus,
391 		power_of,
392 		involution = power_of,
393 		concatenation,
394 		equal_to,
395 		less_than,
396 		less_than_or_equal_to,
397 		garbage_collect,
398 		floor_division,
399 		bitwise_left_shift,
400 		bitwise_right_shift,
401 		bitwise_not,
402 		bitwise_and,
403 		bitwise_or,
404 		bitwise_xor,
405 		pairs,
406 		next
407 	};
408 
409 	typedef meta_function meta_method;
410 
411 	const std::array<std::string, 2> meta_variable_names = { {
412 		"__index",
413 		"__newindex",
414 	} };
415 
416 	const std::array<std::string, 29> meta_function_names = { {
417 		"new",
418 		"__index",
419 		"__newindex",
420 		"__mode",
421 		"__call",
422 		"__mt",
423 		"__tostring",
424 		"__len",
425 		"__unm",
426 		"__add",
427 		"__sub",
428 		"__mul",
429 		"__div",
430 		"__mod",
431 		"__pow",
432 		"__concat",
433 		"__eq",
434 		"__lt",
435 		"__le",
436 		"__gc",
437 
438 		"__idiv",
439 		"__shl",
440 		"__shr",
441 		"__bnot",
442 		"__band",
443 		"__bor",
444 		"__bxor",
445 
446 		"__pairs",
447 		"__next"
448 	} };
449 
name_of(meta_function mf)450 	inline const std::string& name_of(meta_function mf) {
451 		return meta_function_names[static_cast<int>(mf)];
452 	}
453 
type_of(lua_State * L,int index)454 	inline type type_of(lua_State* L, int index) {
455 		return static_cast<type>(lua_type(L, index));
456 	}
457 
type_panic(lua_State * L,int index,type expected,type actual)458 	inline int type_panic(lua_State* L, int index, type expected, type actual) {
459 		return luaL_error(L, "stack index %d, expected %s, received %s", index,
460 			expected == type::poly ? "anything" : lua_typename(L, static_cast<int>(expected)),
461 			expected == type::poly ? "anything" : lua_typename(L, static_cast<int>(actual))
462 		);
463 	}
464 
465 	// Specify this function as the handler for lua::check if you know there's nothing wrong
no_panic(lua_State *,int,type,type)466 	inline int no_panic(lua_State*, int, type, type) noexcept {
467 		return 0;
468 	}
469 
type_error(lua_State * L,int expected,int actual)470 	inline void type_error(lua_State* L, int expected, int actual) {
471 		luaL_error(L, "expected %s, received %s", lua_typename(L, expected), lua_typename(L, actual));
472 	}
473 
type_error(lua_State * L,type expected,type actual)474 	inline void type_error(lua_State* L, type expected, type actual) {
475 		type_error(L, static_cast<int>(expected), static_cast<int>(actual));
476 	}
477 
type_assert(lua_State * L,int index,type expected,type actual)478 	inline void type_assert(lua_State* L, int index, type expected, type actual) {
479 		if (expected != type::poly && expected != actual) {
480 			type_panic(L, index, expected, actual);
481 		}
482 	}
483 
type_assert(lua_State * L,int index,type expected)484 	inline void type_assert(lua_State* L, int index, type expected) {
485 		type actual = type_of(L, index);
486 		type_assert(L, index, expected, actual);
487 	}
488 
type_name(lua_State * L,type t)489 	inline std::string type_name(lua_State* L, type t) {
490 		return lua_typename(L, static_cast<int>(t));
491 	}
492 
493 	class reference;
494 	class stack_reference;
495 	template <typename Table, typename Key>
496 	struct proxy;
497 	template<typename T>
498 	class usertype;
499 	template <bool, typename T>
500 	class basic_table_core;
501 	template <bool b>
502 	using table_core = basic_table_core<b, reference>;
503 	template <bool b>
504 	using stack_table_core = basic_table_core<b, stack_reference>;
505 	typedef table_core<false> table;
506 	typedef table_core<true> global_table;
507 	typedef stack_table_core<false> stack_table;
508 	typedef stack_table_core<true> stack_global_table;
509 	template <typename T>
510 	class basic_function;
511 	template <typename T>
512 	class basic_protected_function;
513 	using function = basic_function<reference>;
514 	using protected_function = basic_protected_function<reference>;
515 	using stack_function = basic_function<stack_reference>;
516 	using stack_protected_function = basic_protected_function<stack_reference>;
517 	template <typename base_t>
518 	class basic_object;
519 	template <typename base_t>
520 	class basic_userdata;
521 	template <typename base_t>
522 	class basic_lightuserdata;
523 	struct variadic_args;
524 	using object = basic_object<reference>;
525 	using stack_object = basic_object<stack_reference>;
526 	using userdata = basic_userdata<reference>;
527 	using stack_userdata = basic_userdata<stack_reference>;
528 	using lightuserdata = basic_lightuserdata<reference>;
529 	using stack_lightuserdata = basic_lightuserdata<stack_reference>;
530 	class coroutine;
531 	class thread;
532 	struct variadic_args;
533 	struct this_state;
534 
535 	namespace detail {
536 		template <typename T, typename = void>
537 		struct lua_type_of : std::integral_constant<type, type::userdata> {};
538 
539 		template <>
540 		struct lua_type_of<std::string> : std::integral_constant<type, type::string> {};
541 
542 		template <>
543 		struct lua_type_of<std::wstring> : std::integral_constant<type, type::string> {};
544 
545 		template <>
546 		struct lua_type_of<std::u16string> : std::integral_constant<type, type::string> {};
547 
548 		template <>
549 		struct lua_type_of<std::u32string> : std::integral_constant<type, type::string> {};
550 
551 		template <std::size_t N>
552 		struct lua_type_of<char[N]> : std::integral_constant<type, type::string> {};
553 
554 		template <std::size_t N>
555 		struct lua_type_of<wchar_t[N]> : std::integral_constant<type, type::string> {};
556 
557 		template <std::size_t N>
558 		struct lua_type_of<char16_t[N]> : std::integral_constant<type, type::string> {};
559 
560 		template <std::size_t N>
561 		struct lua_type_of<char32_t[N]> : std::integral_constant<type, type::string> {};
562 
563 		template <>
564 		struct lua_type_of<char> : std::integral_constant<type, type::string> {};
565 
566 		template <>
567 		struct lua_type_of<wchar_t> : std::integral_constant<type, type::string> {};
568 
569 		template <>
570 		struct lua_type_of<char16_t> : std::integral_constant<type, type::string> {};
571 
572 		template <>
573 		struct lua_type_of<char32_t> : std::integral_constant<type, type::string> {};
574 
575 		template <>
576 		struct lua_type_of<const char*> : std::integral_constant<type, type::string> {};
577 
578 		template <>
579 		struct lua_type_of<const char16_t*> : std::integral_constant<type, type::string> {};
580 
581 		template <>
582 		struct lua_type_of<const char32_t*> : std::integral_constant<type, type::string> {};
583 
584 		template <>
585 		struct lua_type_of<string_detail::string_shim> : std::integral_constant<type, type::string> {};
586 
587 		template <>
588 		struct lua_type_of<bool> : std::integral_constant<type, type::boolean> {};
589 
590 		template <>
591 		struct lua_type_of<lua_nil_t> : std::integral_constant<type, type::lua_nil> { };
592 
593 		template <>
594 		struct lua_type_of<nullopt_t> : std::integral_constant<type, type::lua_nil> { };
595 
596 		template <>
597 		struct lua_type_of<std::nullptr_t> : std::integral_constant<type, type::lua_nil> { };
598 
599 		template <>
600 		struct lua_type_of<sol::error> : std::integral_constant<type, type::string> { };
601 
602 		template <bool b, typename Base>
603 		struct lua_type_of<basic_table_core<b, Base>> : std::integral_constant<type, type::table> { };
604 
605 		template <>
606 		struct lua_type_of<reference> : std::integral_constant<type, type::poly> {};
607 
608 		template <>
609 		struct lua_type_of<stack_reference> : std::integral_constant<type, type::poly> {};
610 
611 		template <typename Base>
612 		struct lua_type_of<basic_object<Base>> : std::integral_constant<type, type::poly> {};
613 
614 		template <typename... Args>
615 		struct lua_type_of<std::tuple<Args...>> : std::integral_constant<type, type::poly> {};
616 
617 		template <typename A, typename B>
618 		struct lua_type_of<std::pair<A, B>> : std::integral_constant<type, type::poly> {};
619 
620 		template <>
621 		struct lua_type_of<void*> : std::integral_constant<type, type::lightuserdata> {};
622 
623 		template <>
624 		struct lua_type_of<lightuserdata_value> : std::integral_constant<type, type::lightuserdata> {};
625 
626 		template <>
627 		struct lua_type_of<userdata_value> : std::integral_constant<type, type::userdata> {};
628 
629 		template <typename T>
630 		struct lua_type_of<light<T>> : std::integral_constant<type, type::lightuserdata> {};
631 
632 		template <typename T>
633 		struct lua_type_of<user<T>> : std::integral_constant<type, type::userdata> {};
634 
635 		template <typename Base>
636 		struct lua_type_of<basic_lightuserdata<Base>> : std::integral_constant<type, type::lightuserdata> {};
637 
638 		template <typename Base>
639 		struct lua_type_of<basic_userdata<Base>> : std::integral_constant<type, type::userdata> {};
640 
641 		template <>
642 		struct lua_type_of<lua_CFunction> : std::integral_constant<type, type::function> {};
643 
644 		template <>
645 		struct lua_type_of<std::remove_pointer_t<lua_CFunction>> : std::integral_constant<type, type::function> {};
646 
647 		template <typename Base>
648 		struct lua_type_of<basic_function<Base>> : std::integral_constant<type, type::function> {};
649 
650 		template <typename Base>
651 		struct lua_type_of<basic_protected_function<Base>> : std::integral_constant<type, type::function> {};
652 
653 		template <>
654 		struct lua_type_of<coroutine> : std::integral_constant<type, type::function> {};
655 
656 		template <>
657 		struct lua_type_of<thread> : std::integral_constant<type, type::thread> {};
658 
659 		template <typename Signature>
660 		struct lua_type_of<std::function<Signature>> : std::integral_constant<type, type::function> {};
661 
662 		template <typename T>
663 		struct lua_type_of<optional<T>> : std::integral_constant<type, type::poly> {};
664 
665 		template <>
666 		struct lua_type_of<variadic_args> : std::integral_constant<type, type::poly> {};
667 
668 		template <>
669 		struct lua_type_of<this_state> : std::integral_constant<type, type::poly> {};
670 
671 		template <>
672 		struct lua_type_of<type> : std::integral_constant<type, type::poly> {};
673 
674 		template <typename T>
675 		struct lua_type_of<T*> : std::integral_constant<type, type::userdata> {};
676 
677 		template <typename T>
678 		struct lua_type_of<T, std::enable_if_t<std::is_arithmetic<T>::value>> : std::integral_constant<type, type::number> {};
679 
680 		template <typename T>
681 		struct lua_type_of<T, std::enable_if_t<std::is_enum<T>::value>> : std::integral_constant<type, type::number> {};
682 
683 		template <typename T, typename C = void>
684 		struct is_container : std::false_type {};
685 
686 		template <typename T>
687 		struct is_container<T, std::enable_if_t<meta::has_begin_end<meta::unqualified_t<T>>::value>> : std::true_type {};
688 
689 		template <>
690 		struct lua_type_of<meta_function> : std::integral_constant<type, type::string> {};
691 
692 		template <typename C, C v, template <typename...> class V, typename... Args>
693 		struct accumulate : std::integral_constant<C, v> {};
694 
695 		template <typename C, C v, template <typename...> class V, typename T, typename... Args>
696 		struct accumulate<C, v, V, T, Args...> : accumulate<C, v + V<T>::value, V, Args...> {};
697 	} // detail
698 
699 	template <typename T>
700 	struct is_unique_usertype : std::integral_constant<bool, unique_usertype_traits<T>::value> {};
701 
702 	template <typename T>
703 	struct lua_type_of : detail::lua_type_of<T> {};
704 
705 	template <typename T>
706 	struct lua_size : std::integral_constant<int, 1> { };
707 
708 	template <typename A, typename B>
709 	struct lua_size<std::pair<A, B>> : std::integral_constant<int, lua_size<A>::value + lua_size<B>::value> { };
710 
711 	template <typename... Args>
712 	struct lua_size<std::tuple<Args...>> : std::integral_constant<int, detail::accumulate<int, 0, lua_size, Args...>::value> { };
713 
714 	template <typename T>
715 	struct is_lua_primitive : std::integral_constant<bool,
716 		type::userdata != lua_type_of<meta::unqualified_t<T>>::value
717 		|| (lua_size<T>::value > 1)
718 		|| std::is_base_of<reference, meta::unqualified_t<T>>::value
719 		|| std::is_base_of<stack_reference, meta::unqualified_t<T>>::value
720 		|| meta::is_specialization_of<std::tuple, meta::unqualified_t<T>>::value
721 		|| meta::is_specialization_of<std::pair, meta::unqualified_t<T>>::value
722 	> { };
723 
724 	template <typename T>
725 	struct is_lua_reference : std::integral_constant<bool,
726 		std::is_base_of<reference, meta::unqualified_t<T>>::value
727 		|| std::is_base_of<stack_reference, meta::unqualified_t<T>>::value
728 		|| meta::is_specialization_of<proxy, meta::unqualified_t<T>>::value
729 	> { };
730 
731 	template <typename T>
732 	struct is_lua_primitive<T*> : std::true_type {};
733 	template <typename T>
734 	struct is_lua_primitive<std::reference_wrapper<T>> : std::true_type { };
735 	template <typename T>
736 	struct is_lua_primitive<user<T>> : std::true_type { };
737 	template <typename T>
738 	struct is_lua_primitive<light<T>> : is_lua_primitive<T*> { };
739 	template <typename T>
740 	struct is_lua_primitive<optional<T>> : std::true_type {};
741 	template <>
742 	struct is_lua_primitive<userdata_value> : std::true_type {};
743 	template <>
744 	struct is_lua_primitive<lightuserdata_value> : std::true_type {};
745 	template <typename T>
746 	struct is_lua_primitive<non_null<T>> : is_lua_primitive<T*> {};
747 
748 	template <typename T>
749 	struct is_proxy_primitive : is_lua_primitive<T> { };
750 
751 	template <typename T>
752 	struct is_transparent_argument : std::false_type {};
753 
754 	template <>
755 	struct is_transparent_argument<this_state> : std::true_type {};
756 
757 	template <>
758 	struct is_transparent_argument<variadic_args> : std::true_type {};
759 
760 	template <typename T>
761 	struct is_variadic_arguments : std::is_same<T, variadic_args> {};
762 
763 	template <typename Signature>
764 	struct lua_bind_traits : meta::bind_traits<Signature> {
765 	private:
766 		typedef meta::bind_traits<Signature> base_t;
767 	public:
768 		typedef std::integral_constant<bool, meta::count_for<is_transparent_argument, typename base_t::args_list>::value != 0> runtime_variadics_t;
769 		static const std::size_t true_arity = base_t::arity;
770 		static const std::size_t arity = base_t::arity - meta::count_for<is_transparent_argument, typename base_t::args_list>::value;
771 		static const std::size_t true_free_arity = base_t::free_arity;
772 		static const std::size_t free_arity = base_t::free_arity - meta::count_for<is_transparent_argument, typename base_t::args_list>::value;
773 	};
774 
775 	template <typename T>
776 	struct is_table : std::false_type {};
777 	template <bool x, typename T>
778 	struct is_table<basic_table_core<x, T>> : std::true_type {};
779 
780 	template <typename T>
781 	struct is_function : std::false_type {};
782 	template <typename T>
783 	struct is_function<basic_function<T>> : std::true_type {};
784 	template <typename T>
785 	struct is_function<basic_protected_function<T>> : std::true_type {};
786 
787 	template <typename T>
788 	struct is_lightuserdata : std::false_type {};
789 	template <typename T>
790 	struct is_lightuserdata<basic_lightuserdata<T>> : std::true_type {};
791 
792 	template <typename T>
793 	struct is_userdata : std::false_type {};
794 	template <typename T>
795 	struct is_userdata<basic_userdata<T>> : std::true_type {};
796 
797 	template <typename T>
798 	struct is_container : detail::is_container<T>{};
799 
800 	template<typename T>
type_of()801 	inline type type_of() {
802 		return lua_type_of<meta::unqualified_t<T>>::value;
803 	}
804 } // sol
805 
806 #endif // SOL_TYPES_HPP
807