File: unique_usertype_traits.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 (240 lines) | stat: -rw-r--r-- 9,104 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
// 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_UNIQUE_USERTYPE_TRAITS_HPP
#define SOL_UNIQUE_USERTYPE_TRAITS_HPP

#include <sol/base_traits.hpp>
#include <sol/pointer_like.hpp>

#include <sol/forward.hpp>

#include <memory>

namespace sol {

	namespace detail {
		template <typename T>
		struct unique_fallback {
			using SOL_INTERNAL_UNSPECIALIZED_MARKER_ = int;
		};

		template <typename T>
		struct unique_fallback<std::shared_ptr<T>> {
		private:
			using pointer = typename std::pointer_traits<std::shared_ptr<T>>::element_type*;

		public:
			// rebind is non-void
			// if and only if unique usertype
			// is cast-capable
			template <typename X>
			using rebind_actual_type = std::shared_ptr<X>;

			static bool is_null(lua_State*, const std::shared_ptr<T>& p) noexcept {
				return p == nullptr;
			}

			static pointer get(lua_State*, const std::shared_ptr<T>& p) noexcept {
				return p.get();
			}
		};

		template <typename T, typename D>
		struct unique_fallback<std::unique_ptr<T, D>> {
		private:
			using pointer = typename std::unique_ptr<T, D>::pointer;

		public:
			static bool is_null(lua_State*, const std::unique_ptr<T, D>& p) noexcept {
				return p == nullptr;
			}

			static pointer get(lua_State*, const std::unique_ptr<T, D>& p) noexcept {
				return p.get();
			}
		};
	} // namespace detail

	namespace meta { namespace meta_detail {
		template <typename T, typename = void>
		struct unique_actual_type;

		template <typename T>
		struct unique_actual_type<T, meta::void_t<typename T::actual_type>> {
			using type = typename T::actual_type;
		};

		template <typename T, typename... Rest, template <typename...> class Templ>
		struct unique_actual_type<Templ<T, Rest...>> {
			using type = T;
		};

	}} // namespace meta::meta_detail

	template <typename T>
	using unique_usertype_actual_t = typename meta::meta_detail::unique_actual_type<unique_usertype_traits<T>>::type;

	namespace meta { namespace meta_detail {
		template <typename T>
		using value_test_t = decltype(T::value);

		template <typename T>
		using type_test_t = typename T::type;

		template <typename T>
		using type_element_type_t = typename T::element_type;

		template <typename T, typename = void>
		struct unique_element_type {
			using type = typename std::pointer_traits<typename unique_actual_type<T>::type>::element_type;
		};

		template <typename T>
		struct unique_element_type<T, std::enable_if_t<meta::is_detected_v<type_element_type_t, T>>> {
			using type = typename T::element_type;
		};

		template <typename T>
		struct unique_element_type<T, std::enable_if_t<meta::is_detected_v<type_test_t, T>>> {
			using type = typename T::type;
		};

		template <typename T, typename = void>
		struct unique_valid : std::integral_constant<bool, !has_internal_marker_v<T>> { };

		template <typename T>
		struct unique_valid<T, meta::void_t<decltype(T::value)>> : std::integral_constant<bool, T::value> { };
	}} // namespace meta::meta_detail

	template <typename T>
	using unique_usertype_element_t = typename meta::meta_detail::unique_element_type<unique_usertype_traits<T>>::type;

	template <typename T, typename Element = void>
	using unique_usertype_rebind_actual_t = typename unique_usertype_traits<T>::template rebind_actual_type<Element>;

	template <typename T>
	struct unique_usertype_traits : public detail::unique_fallback<T> { };

	template <typename T>
	struct is_unique_usertype : std::integral_constant<bool, meta::meta_detail::unique_valid<unique_usertype_traits<T>>::value> { };

	template <typename T>
	inline constexpr bool is_unique_usertype_v = is_unique_usertype<T>::value;

	namespace meta { namespace meta_detail {
		template <typename T>
		using adl_sol_lua_check_access_test_t
			= decltype(sol_lua_check_access(types<T>(), static_cast<lua_State*>(nullptr), -1, std::declval<stack::record&>()));

		template <typename T>
		inline constexpr bool is_adl_sol_lua_check_access_v = meta::is_detected_v<adl_sol_lua_check_access_test_t, T>;

		template <typename T>
		using unique_usertype_get_with_state_test_t
			= decltype(unique_usertype_traits<T>::get(static_cast<lua_State*>(nullptr), std::declval<unique_usertype_actual_t<T>>()));

		template <typename T>
		inline constexpr bool unique_usertype_get_with_state_v = meta::is_detected_v<unique_usertype_get_with_state_test_t, T>;

		template <typename T>
		using unique_usertype_is_null_with_state_test_t
			= decltype(unique_usertype_traits<T>::is_null(static_cast<lua_State*>(nullptr), std::declval<unique_usertype_actual_t<T>>()));

		template <typename T>
		inline constexpr bool unique_usertype_is_null_with_state_v = meta::is_detected_v<unique_usertype_is_null_with_state_test_t, T>;
	}} // namespace meta::meta_detail

	namespace detail {
		template <typename T>
		constexpr bool unique_is_null_noexcept() noexcept {
			if constexpr (meta::meta_detail::unique_usertype_is_null_with_state_v<std::remove_cv_t<T>>) {
				return noexcept(
				     unique_usertype_traits<T>::is_null(static_cast<lua_State*>(nullptr), std::declval<unique_usertype_actual_t<std::remove_cv_t<T>>>()));
			}
			else {
				return noexcept(unique_usertype_traits<T>::is_null(std::declval<unique_usertype_actual_t<std::remove_cv_t<T>>>()));
			}
		}

		template <typename T>
		bool unique_is_null(lua_State* L_, T& value_) noexcept(unique_is_null_noexcept<std::remove_cv_t<T>>()) {
			using Tu = std::remove_cv_t<T>;
			if constexpr (meta::meta_detail::unique_usertype_is_null_with_state_v<Tu>) {
				return unique_usertype_traits<Tu>::is_null(L_, value_);
			}
			else {
				return unique_usertype_traits<Tu>::is_null(value_);
			}
		}

		template <typename T>
		constexpr bool unique_get_noexcept() noexcept {
			if constexpr (meta::meta_detail::unique_usertype_get_with_state_v<std::remove_cv_t<T>>) {
				return noexcept(
				     unique_usertype_traits<T>::get(static_cast<lua_State*>(nullptr), std::declval<unique_usertype_actual_t<std::remove_cv_t<T>>>()));
			}
			else {
				return noexcept(unique_usertype_traits<T>::get(std::declval<unique_usertype_actual_t<std::remove_cv_t<T>>>()));
			}
		}

		template <typename T>
		auto unique_get(lua_State* L_, T& value_) noexcept(unique_get_noexcept<std::remove_cv_t<T>>()) {
			using Tu = std::remove_cv_t<T>;
			if constexpr (meta::meta_detail::unique_usertype_get_with_state_v<Tu>) {
				return unique_usertype_traits<Tu>::get(L_, value_);
			}
			else {
				return unique_usertype_traits<Tu>::get(value_);
			}
		}
	} // namespace detail

	namespace meta { namespace meta_detail {
		template <typename T, typename Element = void>
		using is_rebind_actual_type_test_t = typename T::template rebind_actual_type<Element>;

		template <typename T, typename Element = void>
		using is_rebind_actual_type = meta::is_detected<is_rebind_actual_type_test_t, T, Element>;

		template <typename T, typename Element = void>
		inline constexpr bool is_rebind_actual_type_v = is_rebind_actual_type<T, Element>::value;

		template <typename T, typename Element, bool = is_rebind_actual_type_v<T, Element>>
		struct is_actual_type_rebindable_for_test : std::false_type { };

		template <typename T, typename Element>
		struct is_actual_type_rebindable_for_test<T, Element, true>
		: std::integral_constant<bool, !std::is_void_v<typename T::template rebind_actual_type<Element>>> { };
	}} // namespace meta::meta_detail

	template <typename T, typename Element = void>
	using is_actual_type_rebindable_for = typename meta::meta_detail::is_actual_type_rebindable_for_test<unique_usertype_traits<T>, Element>::type;

	template <typename T, typename Element = void>
	inline constexpr bool is_actual_type_rebindable_for_v = is_actual_type_rebindable_for<T, Element>::value;

} // namespace sol

#endif // SOL_UNIQUE_USERTYPE_TRAITS_HPP