types.hpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. // sol2
  2. // The MIT License (MIT)
  3. // Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. // this software and associated documentation files (the "Software"), to deal in
  6. // the Software without restriction, including without limitation the rights to
  7. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. // the Software, and to permit persons to whom the Software is furnished to do so,
  9. // subject to the following conditions:
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #ifndef SOL_TYPES_HPP
  19. #define SOL_TYPES_HPP
  20. #include <sol/error.hpp>
  21. #include <sol/optional.hpp>
  22. #include <sol/compatibility.hpp>
  23. #include <sol/forward.hpp>
  24. #include <sol/forward_detail.hpp>
  25. #include <sol/traits.hpp>
  26. #include <sol/string_view.hpp>
  27. #include <sol/raii.hpp>
  28. #include <sol/policies.hpp>
  29. #include <sol/ebco.hpp>
  30. #include <array>
  31. #include <initializer_list>
  32. #include <string>
  33. #include <string_view>
  34. #include <limits>
  35. #include <optional>
  36. #include <memory>
  37. #if SOL_IS_ON(SOL_STD_VARIANT)
  38. #include <variant>
  39. #endif // variant shenanigans (thanks, Mac OSX)
  40. namespace sol {
  41. namespace d {
  42. // shortest possible hidden detail namespace
  43. // when types are transcribed, this saves
  44. // quite a bit of space, actually.
  45. // it's a little unfortunate, but here we are?
  46. template <typename T>
  47. struct u { };
  48. } // namespace d
  49. namespace detail {
  50. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE)
  51. typedef int (*lua_CFunction_noexcept)(lua_State* L) noexcept;
  52. #else
  53. typedef int (*lua_CFunction_noexcept)(lua_State* L);
  54. #endif // noexcept function type for lua_CFunction
  55. template <typename T>
  56. struct implicit_wrapper {
  57. T& value;
  58. implicit_wrapper(T* value_) : value(*value_) {
  59. }
  60. implicit_wrapper(T& value_) : value(value_) {
  61. }
  62. operator T&() {
  63. return value;
  64. }
  65. operator T*() {
  66. return std::addressof(value);
  67. }
  68. };
  69. struct yield_tag_t { };
  70. inline constexpr yield_tag_t yield_tag {};
  71. } // namespace detail
  72. struct lua_nil_t { };
  73. inline constexpr lua_nil_t lua_nil {};
  74. inline bool operator==(lua_nil_t, lua_nil_t) {
  75. return true;
  76. }
  77. inline bool operator!=(lua_nil_t, lua_nil_t) {
  78. return false;
  79. }
  80. #if SOL_IS_ON(SOL_NIL)
  81. using nil_t = lua_nil_t;
  82. inline constexpr const nil_t& nil = lua_nil;
  83. #endif
  84. namespace detail {
  85. struct non_lua_nil_t { };
  86. } // namespace detail
  87. struct metatable_key_t { };
  88. inline constexpr metatable_key_t metatable_key {};
  89. struct global_tag_t {
  90. } inline constexpr global_tag {};
  91. struct env_key_t { };
  92. inline constexpr env_key_t env_key {};
  93. struct no_metatable_t { };
  94. inline constexpr no_metatable_t no_metatable {};
  95. template <typename T>
  96. struct yielding_t {
  97. T func;
  98. yielding_t() = default;
  99. yielding_t(const yielding_t&) = default;
  100. yielding_t(yielding_t&&) = default;
  101. yielding_t& operator=(const yielding_t&) = default;
  102. yielding_t& operator=(yielding_t&&) = default;
  103. template <typename Arg,
  104. meta::enable<meta::neg<std::is_same<meta::unqualified_t<Arg>, yielding_t>>,
  105. meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<Arg>>>> = meta::enabler>
  106. yielding_t(Arg&& arg) : func(std::forward<Arg>(arg)) {
  107. }
  108. template <typename Arg0, typename Arg1, typename... Args>
  109. yielding_t(Arg0&& arg0, Arg1&& arg1, Args&&... args) : func(std::forward<Arg0>(arg0), std::forward<Arg1>(arg1), std::forward<Args>(args)...) {
  110. }
  111. };
  112. template <typename F>
  113. inline yielding_t<std::decay_t<F>> yielding(F&& f) {
  114. return yielding_t<std::decay_t<F>>(std::forward<F>(f));
  115. }
  116. typedef std::remove_pointer_t<lua_CFunction> lua_CFunction_ref;
  117. template <typename T>
  118. struct non_null { };
  119. template <typename... Args>
  120. struct function_sig { };
  121. struct upvalue_index {
  122. int index;
  123. upvalue_index(int idx) : index(lua_upvalueindex(idx)) {
  124. }
  125. operator int() const {
  126. return index;
  127. }
  128. };
  129. struct raw_index {
  130. int index;
  131. raw_index(int i) : index(i) {
  132. }
  133. operator int() const {
  134. return index;
  135. }
  136. };
  137. struct absolute_index {
  138. int index;
  139. absolute_index(lua_State* L, int idx) : index(lua_absindex(L, idx)) {
  140. }
  141. operator int() const {
  142. return index;
  143. }
  144. };
  145. struct ref_index {
  146. int index;
  147. ref_index(int idx) : index(idx) {
  148. }
  149. operator int() const {
  150. return index;
  151. }
  152. };
  153. struct stack_count {
  154. int count;
  155. stack_count(int cnt) : count(cnt) {
  156. }
  157. };
  158. struct lightuserdata_value {
  159. void* value;
  160. lightuserdata_value(void* data) : value(data) {
  161. }
  162. operator void*() const {
  163. return value;
  164. }
  165. };
  166. struct userdata_value {
  167. private:
  168. void* m_value;
  169. public:
  170. userdata_value(void* data) : m_value(data) {
  171. }
  172. void* value() const {
  173. return m_value;
  174. }
  175. operator void*() const {
  176. return value();
  177. }
  178. };
  179. template <typename T>
  180. struct light {
  181. private:
  182. static_assert(!std::is_void_v<T>, "the type for light will never be void");
  183. T* m_value;
  184. public:
  185. light(T& x) : m_value(std::addressof(x)) {
  186. }
  187. light(T* x) : m_value(x) {
  188. }
  189. explicit light(void* x) : m_value(static_cast<T*>(x)) {
  190. }
  191. T* value() const {
  192. return m_value;
  193. }
  194. operator T*() const {
  195. return m_value;
  196. }
  197. operator T&() const {
  198. return *m_value;
  199. }
  200. void* void_value() const {
  201. return m_value;
  202. }
  203. };
  204. template <typename T>
  205. auto make_light(T& l) {
  206. typedef meta::unwrapped_t<std::remove_pointer_t<std::remove_pointer_t<T>>> L;
  207. return light<L>(l);
  208. }
  209. template <typename T>
  210. struct user : private detail::ebco<T> {
  211. private:
  212. using base_t = detail::ebco<T>;
  213. public:
  214. using base_t::base_t;
  215. using base_t::value;
  216. operator std::add_pointer_t<std::remove_reference_t<T>>() {
  217. return std::addressof(this->base_t::value());
  218. }
  219. operator std::add_pointer_t<std::add_const_t<std::remove_reference_t<T>>>() const {
  220. return std::addressof(this->base_t::value());
  221. }
  222. operator std::add_lvalue_reference_t<T>() {
  223. return this->base_t::value();
  224. }
  225. operator std::add_const_t<std::add_lvalue_reference_t<T>>&() const {
  226. return this->base_t::value();
  227. }
  228. };
  229. template <typename T>
  230. auto make_user(T&& u) {
  231. typedef meta::unwrapped_t<meta::unqualified_t<T>> U;
  232. return user<U>(std::forward<T>(u));
  233. }
  234. template <typename T>
  235. struct metatable_registry_key : private detail::ebco<T> {
  236. private:
  237. using base_t = detail::ebco<T>;
  238. public:
  239. using base_t::base_t;
  240. using base_t::value;
  241. };
  242. template <typename T>
  243. auto meta_registry_key(T&& key) {
  244. typedef meta::unqualified_t<T> K;
  245. return metatable_registry_key<K>(std::forward<T>(key));
  246. }
  247. template <typename... Upvalues>
  248. struct closure {
  249. lua_CFunction c_function;
  250. std::tuple<Upvalues...> upvalues;
  251. closure(lua_CFunction f, Upvalues... targetupvalues) : c_function(f), upvalues(std::forward<Upvalues>(targetupvalues)...) {
  252. }
  253. };
  254. template <>
  255. struct closure<> {
  256. lua_CFunction c_function;
  257. int upvalues;
  258. closure(lua_CFunction f, int upvalue_count = 0) : c_function(f), upvalues(upvalue_count) {
  259. }
  260. };
  261. typedef closure<> c_closure;
  262. template <typename... Args>
  263. closure<Args...> make_closure(lua_CFunction f, Args&&... args) {
  264. return closure<Args...>(f, std::forward<Args>(args)...);
  265. }
  266. template <typename Sig, typename... Ps>
  267. struct function_arguments {
  268. std::tuple<Ps...> arguments;
  269. template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, function_arguments>> = meta::enabler>
  270. function_arguments(Arg&& arg, Args&&... args) : arguments(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  271. }
  272. };
  273. template <typename Sig = function_sig<>, typename... Args>
  274. auto as_function(Args&&... args) {
  275. return function_arguments<Sig, std::decay_t<Args>...>(std::forward<Args>(args)...);
  276. }
  277. template <typename Sig = function_sig<>, typename... Args>
  278. auto as_function_reference(Args&&... args) {
  279. return function_arguments<Sig, Args...>(std::forward<Args>(args)...);
  280. }
  281. template <typename T>
  282. struct as_table_t : private detail::ebco<T> {
  283. private:
  284. using base_t = detail::ebco<T>;
  285. public:
  286. as_table_t() = default;
  287. as_table_t(const as_table_t&) = default;
  288. as_table_t(as_table_t&&) = default;
  289. as_table_t& operator=(const as_table_t&) = default;
  290. as_table_t& operator=(as_table_t&&) = default;
  291. as_table_t(const meta::unqualified_t<T>& obj) noexcept(std::is_nothrow_constructible_v<base_t, const meta::unqualified_t<T>&>) : base_t(obj) {
  292. }
  293. as_table_t(meta::unqualified_t<T>&& obj) noexcept(std::is_nothrow_constructible_v<base_t, meta::unqualified_t<T>&&>) : base_t(std::move(obj)) {
  294. }
  295. template <typename Arg, typename... Args,
  296. std::enable_if_t<
  297. !std::is_same_v<as_table_t, meta::unqualified_t<Arg>> && !std::is_same_v<meta::unqualified_t<T>, meta::unqualified_t<Arg>>>* = nullptr>
  298. as_table_t(Arg&& arg, Args&&... args) noexcept(std::is_nothrow_constructible_v<base_t, Arg, Args...>)
  299. : base_t(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  300. }
  301. using base_t::value;
  302. operator std::add_lvalue_reference_t<T>() {
  303. return this->base_t::value();
  304. }
  305. operator std::add_const_t<std::add_lvalue_reference_t<T>>() const {
  306. return this->base_t::value();
  307. }
  308. };
  309. template <typename T>
  310. struct nested : private detail::ebco<T> {
  311. private:
  312. using base_t = detail::ebco<T>;
  313. public:
  314. using nested_type = T;
  315. nested() = default;
  316. nested(const nested&) = default;
  317. nested(nested&&) = default;
  318. nested& operator=(const nested&) = default;
  319. nested& operator=(nested&&) = default;
  320. nested(const meta::unqualified_t<T>& obj) noexcept(std::is_nothrow_constructible_v<base_t, const meta::unqualified_t<T>&>) : base_t(obj) {
  321. }
  322. nested(meta::unqualified_t<T>&& obj) noexcept(std::is_nothrow_constructible_v<base_t, meta::unqualified_t<T>&&>) : base_t(std::move(obj)) {
  323. }
  324. template <typename Arg, typename... Args,
  325. std::enable_if_t<
  326. !std::is_same_v<nested, meta::unqualified_t<Arg>> && !std::is_same_v<meta::unqualified_t<T>, meta::unqualified_t<Arg>>>* = nullptr>
  327. nested(Arg&& arg, Args&&... args) noexcept(std::is_nothrow_constructible_v<base_t, Arg, Args...>)
  328. : base_t(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  329. }
  330. using base_t::value;
  331. operator std::add_lvalue_reference_t<T>() {
  332. return this->base_t::value();
  333. }
  334. operator std::add_const_t<std::add_lvalue_reference_t<T>>() const {
  335. return this->base_t::value();
  336. }
  337. };
  338. struct nested_tag_t { };
  339. constexpr inline nested_tag_t nested_tag {};
  340. template <typename T>
  341. as_table_t<T> as_table_ref(T&& container) {
  342. return as_table_t<T>(std::forward<T>(container));
  343. }
  344. template <typename T>
  345. as_table_t<meta::unqualified_t<T>> as_table(T&& container) {
  346. return as_table_t<meta::unqualified_t<T>>(std::forward<T>(container));
  347. }
  348. template <typename T>
  349. nested<T> as_nested_ref(T&& container) {
  350. return nested<T>(std::forward<T>(container));
  351. }
  352. template <typename T>
  353. nested<meta::unqualified_t<T>> as_nested(T&& container) {
  354. return nested<meta::unqualified_t<T>>(std::forward<T>(container));
  355. }
  356. template <typename T>
  357. struct as_container_t : private detail::ebco<T> {
  358. private:
  359. using base_t = detail::ebco<T>;
  360. public:
  361. using type = T;
  362. as_container_t() = default;
  363. as_container_t(const as_container_t&) = default;
  364. as_container_t(as_container_t&&) = default;
  365. as_container_t& operator=(const as_container_t&) = default;
  366. as_container_t& operator=(as_container_t&&) = default;
  367. using base_t::base_t;
  368. using base_t::value;
  369. operator std::add_lvalue_reference_t<T>() {
  370. return value();
  371. }
  372. };
  373. template <typename T>
  374. auto as_container(T&& value) {
  375. return as_container_t<T>(std::forward<T>(value));
  376. }
  377. template <typename T, std::size_t Limit = 15>
  378. struct exhaustive_until : private detail::ebco<T> {
  379. private:
  380. using base_t = detail::ebco<T>;
  381. public:
  382. using base_t::base_t;
  383. using base_t::value;
  384. operator std::add_pointer_t<std::remove_reference_t<T>>() {
  385. return std::addressof(this->base_t::value());
  386. }
  387. operator std::add_pointer_t<std::add_const_t<std::remove_reference_t<T>>>() const {
  388. return std::addressof(this->base_t::value());
  389. }
  390. operator std::add_lvalue_reference_t<T>() {
  391. return this->base_t::value();
  392. }
  393. operator std::add_const_t<std::add_lvalue_reference_t<T>>&() const {
  394. return this->base_t::value();
  395. }
  396. };
  397. template <typename T>
  398. using exhaustive = exhaustive_until<T, (std::numeric_limits<size_t>::max)()>;
  399. template <typename T>
  400. struct non_exhaustive : private detail::ebco<T> {
  401. private:
  402. using base_t = detail::ebco<T>;
  403. public:
  404. using base_t::base_t;
  405. using base_t::value;
  406. operator std::add_pointer_t<std::remove_reference_t<T>>() {
  407. return std::addressof(this->base_t::value());
  408. }
  409. operator std::add_pointer_t<std::add_const_t<std::remove_reference_t<T>>>() const {
  410. return std::addressof(this->base_t::value());
  411. }
  412. operator std::add_lvalue_reference_t<T>() {
  413. return this->base_t::value();
  414. }
  415. operator std::add_const_t<std::add_lvalue_reference_t<T>>&() const {
  416. return this->base_t::value();
  417. }
  418. };
  419. template <typename T>
  420. struct push_invoke_t : private detail::ebco<T> {
  421. private:
  422. using base_t = detail::ebco<T>;
  423. public:
  424. push_invoke_t() = default;
  425. push_invoke_t(const push_invoke_t&) = default;
  426. push_invoke_t(push_invoke_t&&) = default;
  427. push_invoke_t& operator=(const push_invoke_t&) = default;
  428. push_invoke_t& operator=(push_invoke_t&&) = default;
  429. using base_t::base_t;
  430. using base_t::value;
  431. };
  432. template <typename Fx>
  433. auto push_invoke(Fx&& fx) {
  434. return push_invoke_t<Fx>(std::forward<Fx>(fx));
  435. }
  436. template <typename T>
  437. struct forward_as_value_t : private detail::ebco<T> {
  438. private:
  439. using base_t = detail::ebco<T>;
  440. public:
  441. forward_as_value_t() = default;
  442. forward_as_value_t(const forward_as_value_t&) = default;
  443. forward_as_value_t(forward_as_value_t&&) = default;
  444. forward_as_value_t& operator=(const forward_as_value_t&) = default;
  445. forward_as_value_t& operator=(forward_as_value_t&&) = default;
  446. using base_t::base_t;
  447. using base_t::value;
  448. };
  449. template <typename T>
  450. auto pass_as_value(T& value_ref_) {
  451. return forward_as_value_t<T>(value_ref_);
  452. }
  453. struct override_value_t { };
  454. constexpr inline override_value_t override_value = override_value_t();
  455. struct update_if_empty_t { };
  456. constexpr inline update_if_empty_t update_if_empty = update_if_empty_t();
  457. struct create_if_nil_t { };
  458. constexpr inline create_if_nil_t create_if_nil = create_if_nil_t();
  459. namespace detail {
  460. enum insert_mode { none = 0x0, update_if_empty = 0x01, override_value = 0x02, create_if_nil = 0x04 };
  461. template <typename T, typename...>
  462. using is_insert_mode = std::integral_constant<bool,
  463. std::is_same_v<T, override_value_t> || std::is_same_v<T, update_if_empty_t> || std::is_same_v<T, create_if_nil_t>>;
  464. template <typename T, typename...>
  465. using is_not_insert_mode = meta::neg<is_insert_mode<T>>;
  466. } // namespace detail
  467. struct this_state {
  468. lua_State* L;
  469. this_state(lua_State* Ls) : L(Ls) {
  470. }
  471. operator lua_State*() const noexcept {
  472. return lua_state();
  473. }
  474. lua_State* operator->() const noexcept {
  475. return lua_state();
  476. }
  477. lua_State* lua_state() const noexcept {
  478. return L;
  479. }
  480. };
  481. struct this_main_state {
  482. lua_State* L;
  483. this_main_state(lua_State* Ls) : L(Ls) {
  484. }
  485. operator lua_State*() const noexcept {
  486. return lua_state();
  487. }
  488. lua_State* operator->() const noexcept {
  489. return lua_state();
  490. }
  491. lua_State* lua_state() const noexcept {
  492. return L;
  493. }
  494. };
  495. struct new_table {
  496. int sequence_hint = 0;
  497. int map_hint = 0;
  498. new_table() = default;
  499. new_table(const new_table&) = default;
  500. new_table(new_table&&) = default;
  501. new_table& operator=(const new_table&) = default;
  502. new_table& operator=(new_table&&) = default;
  503. new_table(int sequence_hint_, int map_hint_ = 0) noexcept : sequence_hint(sequence_hint_), map_hint(map_hint_) {
  504. }
  505. };
  506. const new_table create = {};
  507. enum class lib : unsigned char {
  508. // print, assert, and other base functions
  509. base,
  510. // require and other package functions
  511. package,
  512. // coroutine functions and utilities
  513. coroutine,
  514. // string library
  515. string,
  516. // functionality from the OS
  517. os,
  518. // all things math
  519. math,
  520. // the table manipulator and observer functions
  521. table,
  522. // the debug library
  523. debug,
  524. // the bit library: different based on which you're using
  525. bit32,
  526. // input/output library
  527. io,
  528. // LuaJIT only
  529. ffi,
  530. // LuaJIT only
  531. jit,
  532. // library for handling utf8: new to Lua
  533. utf8,
  534. // do not use
  535. count
  536. };
  537. enum class call_syntax { dot = 0, colon = 1 };
  538. enum class load_mode {
  539. any = 0,
  540. text = 1,
  541. binary = 2,
  542. };
  543. enum class call_status : int {
  544. ok = LUA_OK,
  545. yielded = LUA_YIELD,
  546. runtime = LUA_ERRRUN,
  547. memory = LUA_ERRMEM,
  548. handler = LUA_ERRERR,
  549. gc = LUA_ERRGCMM,
  550. syntax = LUA_ERRSYNTAX,
  551. file = LUA_ERRFILE,
  552. };
  553. enum class thread_status : int {
  554. ok = LUA_OK,
  555. yielded = LUA_YIELD,
  556. runtime = LUA_ERRRUN,
  557. memory = LUA_ERRMEM,
  558. gc = LUA_ERRGCMM,
  559. handler = LUA_ERRERR,
  560. dead = -1,
  561. };
  562. enum class load_status : int {
  563. ok = LUA_OK,
  564. syntax = LUA_ERRSYNTAX,
  565. memory = LUA_ERRMEM,
  566. gc = LUA_ERRGCMM,
  567. file = LUA_ERRFILE,
  568. };
  569. enum class gc_mode : int {
  570. incremental = 0,
  571. generational = 1,
  572. default_value = incremental,
  573. };
  574. enum class type : int {
  575. none = LUA_TNONE,
  576. lua_nil = LUA_TNIL,
  577. #if SOL_IS_ON(SOL_NIL)
  578. nil = lua_nil,
  579. #endif // Objective C/C++ Keyword that's found in OSX SDK and OBJC -- check for all forms to protect
  580. string = LUA_TSTRING,
  581. number = LUA_TNUMBER,
  582. thread = LUA_TTHREAD,
  583. boolean = LUA_TBOOLEAN,
  584. function = LUA_TFUNCTION,
  585. userdata = LUA_TUSERDATA,
  586. lightuserdata = LUA_TLIGHTUSERDATA,
  587. table = LUA_TTABLE,
  588. poly = -0xFFFF
  589. };
  590. inline const std::string& to_string(call_status c) {
  591. static const std::array<std::string, 10> names { { "ok",
  592. "yielded",
  593. "runtime",
  594. "memory",
  595. "handler",
  596. "gc",
  597. "syntax",
  598. "file",
  599. "CRITICAL_EXCEPTION_FAILURE",
  600. "CRITICAL_INDETERMINATE_STATE_FAILURE" } };
  601. switch (c) {
  602. case call_status::ok:
  603. return names[0];
  604. case call_status::yielded:
  605. return names[1];
  606. case call_status::runtime:
  607. return names[2];
  608. case call_status::memory:
  609. return names[3];
  610. case call_status::handler:
  611. return names[4];
  612. case call_status::gc:
  613. return names[5];
  614. case call_status::syntax:
  615. return names[6];
  616. case call_status::file:
  617. return names[7];
  618. }
  619. if (static_cast<std::ptrdiff_t>(c) == -1) {
  620. // One of the many cases where a critical exception error has occurred
  621. return names[8];
  622. }
  623. return names[9];
  624. }
  625. inline bool is_indeterminate_call_failure(call_status c) {
  626. switch (c) {
  627. case call_status::ok:
  628. case call_status::yielded:
  629. case call_status::runtime:
  630. case call_status::memory:
  631. case call_status::handler:
  632. case call_status::gc:
  633. case call_status::syntax:
  634. case call_status::file:
  635. return false;
  636. }
  637. return true;
  638. }
  639. inline const std::string& to_string(load_status c) {
  640. static const std::array<std::string, 7> names {
  641. { "ok", "memory", "gc", "syntax", "file", "CRITICAL_EXCEPTION_FAILURE", "CRITICAL_INDETERMINATE_STATE_FAILURE" }
  642. };
  643. switch (c) {
  644. case load_status::ok:
  645. return names[0];
  646. case load_status::memory:
  647. return names[1];
  648. case load_status::gc:
  649. return names[2];
  650. case load_status::syntax:
  651. return names[3];
  652. case load_status::file:
  653. return names[4];
  654. }
  655. if (static_cast<int>(c) == -1) {
  656. // One of the many cases where a critical exception error has occurred
  657. return names[5];
  658. }
  659. return names[6];
  660. }
  661. inline const std::string& to_string(load_mode c) {
  662. static const std::array<std::string, 3> names { {
  663. "bt",
  664. "t",
  665. "b",
  666. } };
  667. return names[static_cast<std::size_t>(c)];
  668. }
  669. enum class meta_function : unsigned {
  670. construct,
  671. index,
  672. new_index,
  673. mode,
  674. call,
  675. call_function = call,
  676. metatable,
  677. to_string,
  678. length,
  679. unary_minus,
  680. addition,
  681. subtraction,
  682. multiplication,
  683. division,
  684. modulus,
  685. power_of,
  686. involution = power_of,
  687. concatenation,
  688. equal_to,
  689. less_than,
  690. less_than_or_equal_to,
  691. garbage_collect,
  692. floor_division,
  693. bitwise_left_shift,
  694. bitwise_right_shift,
  695. bitwise_not,
  696. bitwise_and,
  697. bitwise_or,
  698. bitwise_xor,
  699. pairs,
  700. ipairs,
  701. next,
  702. type,
  703. type_info,
  704. call_construct,
  705. storage,
  706. gc_names,
  707. static_index,
  708. static_new_index,
  709. };
  710. typedef meta_function meta_method;
  711. inline const std::array<std::string, 37>& meta_function_names() {
  712. static const std::array<std::string, 37> names = { { "new",
  713. "__index",
  714. "__newindex",
  715. "__mode",
  716. "__call",
  717. "__metatable",
  718. "__tostring",
  719. "__len",
  720. "__unm",
  721. "__add",
  722. "__sub",
  723. "__mul",
  724. "__div",
  725. "__mod",
  726. "__pow",
  727. "__concat",
  728. "__eq",
  729. "__lt",
  730. "__le",
  731. "__gc",
  732. "__idiv",
  733. "__shl",
  734. "__shr",
  735. "__bnot",
  736. "__band",
  737. "__bor",
  738. "__bxor",
  739. "__pairs",
  740. "__ipairs",
  741. "next",
  742. "__type",
  743. "__typeinfo",
  744. "__sol.call_new",
  745. "__sol.storage",
  746. "__sol.gc_names",
  747. "__sol.static_index",
  748. "__sol.static_new_index" } };
  749. return names;
  750. }
  751. inline const std::string& to_string(meta_function mf) {
  752. return meta_function_names()[static_cast<std::size_t>(mf)];
  753. }
  754. inline type type_of(lua_State* L, int index) {
  755. return static_cast<type>(lua_type(L, index));
  756. }
  757. inline std::string type_name(lua_State* L, type t) {
  758. return lua_typename(L, static_cast<int>(t));
  759. }
  760. template <typename T>
  761. struct is_stateless_lua_reference
  762. : std::integral_constant<bool,
  763. (std::is_base_of_v<stateless_stack_reference, T> || std::is_base_of_v<stateless_reference, T>)&&(
  764. !std::is_base_of_v<stack_reference, T> && !std::is_base_of_v<reference, T> && !std::is_base_of_v<main_reference, T>)> { };
  765. template <typename T>
  766. inline constexpr bool is_stateless_lua_reference_v = is_stateless_lua_reference<T>::value;
  767. template <typename T>
  768. struct is_lua_reference
  769. : std::integral_constant<bool,
  770. std::is_base_of_v<reference,
  771. T> || std::is_base_of_v<main_reference, T> || std::is_base_of_v<stack_reference, T> || std::is_base_of_v<stateless_stack_reference, T> || std::is_base_of_v<stateless_reference, T>> {
  772. };
  773. template <typename T>
  774. inline constexpr bool is_lua_reference_v = is_lua_reference<T>::value;
  775. template <typename T>
  776. struct is_lua_reference_or_proxy : std::integral_constant<bool, is_lua_reference_v<T> || meta::is_specialization_of_v<T, table_proxy>> { };
  777. template <typename T>
  778. inline constexpr bool is_lua_reference_or_proxy_v = is_lua_reference_or_proxy<T>::value;
  779. template <typename T>
  780. struct is_transparent_argument
  781. : std::integral_constant<bool,
  782. std::is_same_v<meta::unqualified_t<T>,
  783. this_state> || std::is_same_v<meta::unqualified_t<T>, this_main_state> || std::is_same_v<meta::unqualified_t<T>, this_environment> || std::is_same_v<meta::unqualified_t<T>, variadic_args>> {
  784. };
  785. template <typename T>
  786. constexpr inline bool is_transparent_argument_v = is_transparent_argument<T>::value;
  787. template <typename T>
  788. struct is_variadic_arguments : meta::any<std::is_same<T, variadic_args>, meta::is_optional<T>> { };
  789. template <typename T>
  790. struct is_container
  791. : std::integral_constant<bool,
  792. !std::is_same_v<state_view,
  793. T> && !std::is_same_v<state, T> && !meta::is_initializer_list_v<T> && !meta::is_string_like_v<T> && !meta::is_string_literal_array_v<T> && !is_transparent_argument_v<T> && !is_lua_reference_v<T> && (meta::has_begin_end_v<T> || std::is_array_v<T>)> {
  794. };
  795. template <typename T>
  796. constexpr inline bool is_container_v = is_container<T>::value;
  797. template <typename T>
  798. struct is_to_stringable : meta::any<meta::supports_to_string_member<meta::unqualified_t<T>>, meta::supports_adl_to_string<meta::unqualified_t<T>>,
  799. meta::supports_op_left_shift<std::ostream, meta::unqualified_t<T>>> { };
  800. template <typename T>
  801. inline constexpr bool is_to_stringable_v = is_to_stringable<T>::value;
  802. template <typename T>
  803. struct is_callable : std::true_type { };
  804. template <typename T>
  805. inline constexpr bool is_callable_v = is_callable<T>::value;
  806. namespace detail {
  807. template <typename T, typename = void>
  808. struct lua_type_of : std::integral_constant<type, type::userdata> { };
  809. template <typename C, typename T, typename A>
  810. struct lua_type_of<std::basic_string<C, T, A>> : std::integral_constant<type, type::string> { };
  811. template <typename C, typename T>
  812. struct lua_type_of<basic_string_view<C, T>> : std::integral_constant<type, type::string> { };
  813. template <std::size_t N>
  814. struct lua_type_of<char[N]> : std::integral_constant<type, type::string> { };
  815. template <std::size_t N>
  816. struct lua_type_of<wchar_t[N]> : std::integral_constant<type, type::string> { };
  817. #if SOL_IS_ON(SOL_CHAR8_T)
  818. template <std::size_t N>
  819. struct lua_type_of<char8_t[N]> : std::integral_constant<type, type::string> { };
  820. #endif
  821. template <std::size_t N>
  822. struct lua_type_of<char16_t[N]> : std::integral_constant<type, type::string> { };
  823. template <std::size_t N>
  824. struct lua_type_of<char32_t[N]> : std::integral_constant<type, type::string> { };
  825. template <>
  826. struct lua_type_of<char> : std::integral_constant<type, type::string> { };
  827. template <>
  828. struct lua_type_of<wchar_t> : std::integral_constant<type, type::string> { };
  829. #if SOL_IS_ON(SOL_CHAR8_T)
  830. template <>
  831. struct lua_type_of<char8_t> : std::integral_constant<type, type::string> { };
  832. #endif
  833. template <>
  834. struct lua_type_of<char16_t> : std::integral_constant<type, type::string> { };
  835. template <>
  836. struct lua_type_of<char32_t> : std::integral_constant<type, type::string> { };
  837. template <>
  838. struct lua_type_of<const char*> : std::integral_constant<type, type::string> { };
  839. template <>
  840. struct lua_type_of<const wchar_t*> : std::integral_constant<type, type::string> { };
  841. #if SOL_IS_ON(SOL_CHAR8_T)
  842. template <>
  843. struct lua_type_of<const char8_t*> : std::integral_constant<type, type::string> { };
  844. #endif
  845. template <>
  846. struct lua_type_of<const char16_t*> : std::integral_constant<type, type::string> { };
  847. template <>
  848. struct lua_type_of<const char32_t*> : std::integral_constant<type, type::string> { };
  849. template <>
  850. struct lua_type_of<bool> : std::integral_constant<type, type::boolean> { };
  851. template <>
  852. struct lua_type_of<lua_nil_t> : std::integral_constant<type, type::lua_nil> { };
  853. template <>
  854. struct lua_type_of<nullopt_t> : std::integral_constant<type, type::lua_nil> { };
  855. template <>
  856. struct lua_type_of<lua_value> : std::integral_constant<type, type::poly> { };
  857. template <>
  858. struct lua_type_of<detail::non_lua_nil_t> : std::integral_constant<type, type::poly> { };
  859. template <>
  860. struct lua_type_of<std::nullptr_t> : std::integral_constant<type, type::lua_nil> { };
  861. template <>
  862. struct lua_type_of<error> : std::integral_constant<type, type::string> { };
  863. template <bool b, typename Base>
  864. struct lua_type_of<basic_table_core<b, Base>> : std::integral_constant<type, type::table> { };
  865. template <typename Base>
  866. struct lua_type_of<basic_lua_table<Base>> : std::integral_constant<type, type::table> { };
  867. template <typename Base>
  868. struct lua_type_of<basic_metatable<Base>> : std::integral_constant<type, type::table> { };
  869. template <typename T, typename Base>
  870. struct lua_type_of<basic_usertype<T, Base>> : std::integral_constant<type, type::table> { };
  871. template <>
  872. struct lua_type_of<metatable_key_t> : std::integral_constant<type, type::table> { };
  873. template <typename B>
  874. struct lua_type_of<basic_environment<B>> : std::integral_constant<type, type::poly> { };
  875. template <>
  876. struct lua_type_of<env_key_t> : std::integral_constant<type, type::poly> { };
  877. template <>
  878. struct lua_type_of<new_table> : std::integral_constant<type, type::table> { };
  879. template <typename T>
  880. struct lua_type_of<as_table_t<T>> : std::integral_constant<type, type::table> { };
  881. template <typename T>
  882. struct lua_type_of<std::initializer_list<T>> : std::integral_constant<type, type::table> { };
  883. template <bool b>
  884. struct lua_type_of<basic_reference<b>> : std::integral_constant<type, type::poly> { };
  885. template <>
  886. struct lua_type_of<stack_reference> : std::integral_constant<type, type::poly> { };
  887. template <typename Base>
  888. struct lua_type_of<basic_object<Base>> : std::integral_constant<type, type::poly> { };
  889. template <typename... Args>
  890. struct lua_type_of<std::tuple<Args...>> : std::integral_constant<type, type::poly> { };
  891. template <typename A, typename B>
  892. struct lua_type_of<std::pair<A, B>> : std::integral_constant<type, type::poly> { };
  893. template <>
  894. struct lua_type_of<void*> : std::integral_constant<type, type::lightuserdata> { };
  895. template <>
  896. struct lua_type_of<const void*> : std::integral_constant<type, type::lightuserdata> { };
  897. template <>
  898. struct lua_type_of<lightuserdata_value> : std::integral_constant<type, type::lightuserdata> { };
  899. template <>
  900. struct lua_type_of<userdata_value> : std::integral_constant<type, type::userdata> { };
  901. template <typename T>
  902. struct lua_type_of<light<T>> : std::integral_constant<type, type::lightuserdata> { };
  903. template <typename T>
  904. struct lua_type_of<user<T>> : std::integral_constant<type, type::userdata> { };
  905. template <typename Base>
  906. struct lua_type_of<basic_lightuserdata<Base>> : std::integral_constant<type, type::lightuserdata> { };
  907. template <typename Base>
  908. struct lua_type_of<basic_userdata<Base>> : std::integral_constant<type, type::userdata> { };
  909. template <>
  910. struct lua_type_of<lua_CFunction> : std::integral_constant<type, type::function> { };
  911. template <>
  912. struct lua_type_of<std::remove_pointer_t<lua_CFunction>> : std::integral_constant<type, type::function> { };
  913. template <typename Base, bool aligned>
  914. struct lua_type_of<basic_function<Base, aligned>> : std::integral_constant<type, type::function> { };
  915. template <typename Base, bool aligned, typename Handler>
  916. struct lua_type_of<basic_protected_function<Base, aligned, Handler>> : std::integral_constant<type, type::function> { };
  917. template <typename Base>
  918. struct lua_type_of<basic_coroutine<Base>> : std::integral_constant<type, type::function> { };
  919. template <typename Base>
  920. struct lua_type_of<basic_thread<Base>> : std::integral_constant<type, type::thread> { };
  921. template <typename Signature>
  922. struct lua_type_of<std::function<Signature>> : std::integral_constant<type, type::function> { };
  923. template <typename T>
  924. struct lua_type_of<optional<T>> : std::integral_constant<type, type::poly> { };
  925. template <typename T>
  926. struct lua_type_of<std::optional<T>> : std::integral_constant<type, type::poly> { };
  927. template <>
  928. struct lua_type_of<variadic_args> : std::integral_constant<type, type::poly> { };
  929. template <>
  930. struct lua_type_of<variadic_results> : std::integral_constant<type, type::poly> { };
  931. template <>
  932. struct lua_type_of<stack_count> : std::integral_constant<type, type::poly> { };
  933. template <>
  934. struct lua_type_of<this_state> : std::integral_constant<type, type::poly> { };
  935. template <>
  936. struct lua_type_of<this_main_state> : std::integral_constant<type, type::poly> { };
  937. template <>
  938. struct lua_type_of<this_environment> : std::integral_constant<type, type::poly> { };
  939. template <>
  940. struct lua_type_of<type> : std::integral_constant<type, type::poly> { };
  941. #if SOL_IS_ON(SOL_GET_FUNCTION_POINTER_UNSAFE)
  942. template <typename T>
  943. struct lua_type_of<T*> : std::integral_constant<type, std::is_function_v<T> ? type::function : type::userdata> { };
  944. #else
  945. template <typename T>
  946. struct lua_type_of<T*> : std::integral_constant<type, type::userdata> { };
  947. #endif
  948. template <typename T>
  949. struct lua_type_of<T, std::enable_if_t<std::is_arithmetic_v<T> || std::is_same_v<T, lua_Number> || std::is_same_v<T, lua_Integer>>>
  950. : std::integral_constant<type, type::number> { };
  951. template <typename T>
  952. struct lua_type_of<T, std::enable_if_t<std::is_function_v<T>>> : std::integral_constant<type, type::function> { };
  953. template <typename T>
  954. struct lua_type_of<T, std::enable_if_t<std::is_enum_v<T>>> : std::integral_constant<type, type::number> { };
  955. template <>
  956. struct lua_type_of<meta_function> : std::integral_constant<type, type::string> { };
  957. #if SOL_IS_ON(SOL_STD_VARIANT)
  958. template <typename... Tn>
  959. struct lua_type_of<std::variant<Tn...>> : std::integral_constant<type, type::poly> { };
  960. #endif // std::variant deployment sucks on Clang
  961. template <typename T>
  962. struct lua_type_of<nested<T>> : meta::conditional_t<::sol::is_container_v<T>, std::integral_constant<type, type::table>, lua_type_of<T>> { };
  963. template <typename C, C v, template <typename...> class V, typename... Args>
  964. struct accumulate : std::integral_constant<C, v> { };
  965. template <typename C, C v, template <typename...> class V, typename T, typename... Args>
  966. struct accumulate<C, v, V, T, Args...> : accumulate<C, v + V<T>::value, V, Args...> { };
  967. template <typename C, C v, template <typename...> class V, typename List>
  968. struct accumulate_list;
  969. template <typename C, C v, template <typename...> class V, typename... Args>
  970. struct accumulate_list<C, v, V, types<Args...>> : accumulate<C, v, V, Args...> { };
  971. } // namespace detail
  972. template <typename T>
  973. struct lua_type_of : detail::lua_type_of<T> {
  974. typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_;
  975. };
  976. template <typename T>
  977. inline constexpr type lua_type_of_v = lua_type_of<T>::value;
  978. template <typename T>
  979. struct lua_size : std::integral_constant<int, 1> {
  980. typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_;
  981. };
  982. template <typename A, typename B>
  983. struct lua_size<std::pair<A, B>> : std::integral_constant<int, lua_size<A>::value + lua_size<B>::value> { };
  984. template <typename... Args>
  985. struct lua_size<std::tuple<Args...>> : std::integral_constant<int, detail::accumulate<int, 0, lua_size, Args...>::value> { };
  986. template <typename T>
  987. inline constexpr int lua_size_v = lua_size<T>::value;
  988. namespace detail {
  989. // MSVC's decltype detection is broken, which breaks other
  990. // parts of the code. So we add more workarounds. The moment it's fixed,
  991. // we take it away and break everyone that doesn't upgrade.
  992. template <typename T>
  993. using is_msvc_callable_rigged = meta::any<meta::is_specialization_of<T, push_invoke_t>, meta::is_specialization_of<T, as_table_t>,
  994. meta::is_specialization_of<T, forward_as_value_t>, meta::is_specialization_of<T, as_container_t>, meta::is_specialization_of<T, nested>,
  995. meta::is_specialization_of<T, yielding_t>>;
  996. template <typename T>
  997. inline constexpr bool is_msvc_callable_rigged_v = is_msvc_callable_rigged<T>::value;
  998. } // namespace detail
  999. template <typename T>
  1000. struct is_lua_primitive : std::integral_constant<bool,
  1001. type::userdata != lua_type_of_v<T> // cf
  1002. || ((type::userdata == lua_type_of_v<T>) // cf
  1003. &&meta::meta_detail::has_internal_marker_v<lua_type_of<T>> // cf
  1004. && !meta::meta_detail::has_internal_marker_v<lua_size<T>>) // cf
  1005. || is_lua_reference_or_proxy_v<T> // cf
  1006. || meta::is_specialization_of_v<T, std::tuple> // cf
  1007. || meta::is_specialization_of_v<T, std::pair>> { };
  1008. template <typename T>
  1009. constexpr inline bool is_lua_primitive_v = is_lua_primitive<T>::value;
  1010. template <typename T>
  1011. struct is_value_semantic_for_function
  1012. #if SOL_IS_ON(SOL_FUNCTION_CALL_VALUE_SEMANTICS)
  1013. : std::true_type {
  1014. };
  1015. #else
  1016. : std::false_type {
  1017. };
  1018. #endif
  1019. template <typename T>
  1020. constexpr inline bool is_value_semantic_for_function_v = is_value_semantic_for_function<T>::value;
  1021. template <typename T>
  1022. struct is_main_threaded : std::is_base_of<main_reference, T> { };
  1023. template <typename T>
  1024. inline constexpr bool is_main_threaded_v = is_main_threaded<T>::value;
  1025. template <typename T>
  1026. struct is_stack_based : std::is_base_of<stack_reference, T> { };
  1027. template <>
  1028. struct is_stack_based<variadic_args> : std::true_type { };
  1029. template <>
  1030. struct is_stack_based<unsafe_function_result> : std::true_type { };
  1031. template <>
  1032. struct is_stack_based<protected_function_result> : std::true_type { };
  1033. template <>
  1034. struct is_stack_based<stack_proxy> : std::true_type { };
  1035. template <>
  1036. struct is_stack_based<stack_proxy_base> : std::true_type { };
  1037. template <>
  1038. struct is_stack_based<stack_count> : std::true_type { };
  1039. template <typename T>
  1040. constexpr inline bool is_stack_based_v = is_stack_based<T>::value;
  1041. template <typename T>
  1042. struct is_lua_primitive<T*> : std::true_type { };
  1043. template <>
  1044. struct is_lua_primitive<unsafe_function_result> : std::true_type { };
  1045. template <>
  1046. struct is_lua_primitive<protected_function_result> : std::true_type { };
  1047. template <typename T>
  1048. struct is_lua_primitive<std::reference_wrapper<T>> : std::true_type { };
  1049. template <typename T>
  1050. struct is_lua_primitive<user<T>> : std::true_type { };
  1051. template <typename T>
  1052. struct is_lua_primitive<light<T>> : is_lua_primitive<T*> { };
  1053. template <typename T>
  1054. struct is_lua_primitive<optional<T>> : std::true_type { };
  1055. template <typename T>
  1056. struct is_lua_primitive<std::optional<T>> : std::true_type { };
  1057. template <typename T>
  1058. struct is_lua_primitive<as_table_t<T>> : std::true_type { };
  1059. template <typename T>
  1060. struct is_lua_primitive<nested<T>> : std::true_type { };
  1061. template <>
  1062. struct is_lua_primitive<userdata_value> : std::true_type { };
  1063. template <>
  1064. struct is_lua_primitive<lightuserdata_value> : std::true_type { };
  1065. template <>
  1066. struct is_lua_primitive<stack_proxy> : std::true_type { };
  1067. template <>
  1068. struct is_lua_primitive<stack_proxy_base> : std::true_type { };
  1069. template <typename T>
  1070. struct is_lua_primitive<non_null<T>> : is_lua_primitive<T*> { };
  1071. template <typename T>
  1072. struct is_lua_index : std::is_integral<T> { };
  1073. template <>
  1074. struct is_lua_index<raw_index> : std::true_type { };
  1075. template <>
  1076. struct is_lua_index<absolute_index> : std::true_type { };
  1077. template <>
  1078. struct is_lua_index<ref_index> : std::true_type { };
  1079. template <>
  1080. struct is_lua_index<upvalue_index> : std::true_type { };
  1081. template <typename Signature>
  1082. struct lua_bind_traits : meta::bind_traits<Signature> {
  1083. private:
  1084. typedef meta::bind_traits<Signature> base_t;
  1085. public:
  1086. typedef std::integral_constant<bool, meta::count_for<is_variadic_arguments, typename base_t::args_list>::value != 0> runtime_variadics_t;
  1087. static const std::size_t true_arity = base_t::arity;
  1088. static const std::size_t arity = detail::accumulate_list<std::size_t, 0, lua_size, typename base_t::args_list>::value
  1089. - meta::count_for<is_transparent_argument, typename base_t::args_list>::value;
  1090. static const std::size_t true_free_arity = base_t::free_arity;
  1091. static const std::size_t free_arity = detail::accumulate_list<std::size_t, 0, lua_size, typename base_t::free_args_list>::value
  1092. - meta::count_for<is_transparent_argument, typename base_t::args_list>::value;
  1093. };
  1094. template <typename T>
  1095. struct is_table : std::false_type { };
  1096. template <bool x, typename T>
  1097. struct is_table<basic_table_core<x, T>> : std::true_type { };
  1098. template <typename T>
  1099. struct is_table<basic_lua_table<T>> : std::true_type { };
  1100. template <typename T>
  1101. inline constexpr bool is_table_v = is_table<T>::value;
  1102. template <typename T>
  1103. struct is_global_table : std::false_type { };
  1104. template <typename T>
  1105. struct is_global_table<basic_table_core<true, T>> : std::true_type { };
  1106. template <typename T>
  1107. inline constexpr bool is_global_table_v = is_global_table<T>::value;
  1108. template <typename T>
  1109. struct is_stack_table : std::false_type { };
  1110. template <bool x, typename T>
  1111. struct is_stack_table<basic_table_core<x, T>> : std::integral_constant<bool, std::is_base_of_v<stack_reference, T>> { };
  1112. template <typename T>
  1113. struct is_stack_table<basic_lua_table<T>> : std::integral_constant<bool, std::is_base_of_v<stack_reference, T>> { };
  1114. template <typename T>
  1115. inline constexpr bool is_stack_table_v = is_stack_table<T>::value;
  1116. template <typename T>
  1117. struct is_function : std::false_type { };
  1118. template <typename T, bool aligned>
  1119. struct is_function<basic_function<T, aligned>> : std::true_type { };
  1120. template <typename T, bool aligned, typename Handler>
  1121. struct is_function<basic_protected_function<T, aligned, Handler>> : std::true_type { };
  1122. template <typename T>
  1123. using is_lightuserdata = meta::is_specialization_of<T, basic_lightuserdata>;
  1124. template <typename T>
  1125. inline constexpr bool is_lightuserdata_v = is_lightuserdata<T>::value;
  1126. template <typename T>
  1127. using is_userdata = meta::is_specialization_of<T, basic_userdata>;
  1128. template <typename T>
  1129. inline constexpr bool is_userdata_v = is_userdata<T>::value;
  1130. template <typename T>
  1131. using is_environment = std::integral_constant<bool, is_userdata_v<T> || is_table_v<T> || meta::is_specialization_of_v<T, basic_environment>>;
  1132. template <typename T>
  1133. inline constexpr bool is_environment_v = is_environment<T>::value;
  1134. template <typename T>
  1135. using is_table_like = std::integral_constant<bool, is_table_v<T> || is_environment_v<T> || is_userdata_v<T>>;
  1136. template <typename T>
  1137. inline constexpr bool is_table_like_v = is_table_like<T>::value;
  1138. template <typename T>
  1139. struct is_automagical
  1140. : std::integral_constant<bool,
  1141. (SOL_IS_ON(SOL_DEFAULT_AUTOMAGICAL_USERTYPES))
  1142. || (std::is_array_v<
  1143. meta::unqualified_t<T>> || (!std::is_same_v<meta::unqualified_t<T>, state> && !std::is_same_v<meta::unqualified_t<T>, state_view>))> {
  1144. };
  1145. template <typename T>
  1146. inline type type_of() {
  1147. return lua_type_of<meta::unqualified_t<T>>::value;
  1148. }
  1149. namespace detail {
  1150. template <typename T>
  1151. struct is_non_factory_constructor : std::false_type { };
  1152. template <typename... Args>
  1153. struct is_non_factory_constructor<constructors<Args...>> : std::true_type { };
  1154. template <typename... Args>
  1155. struct is_non_factory_constructor<constructor_wrapper<Args...>> : std::true_type { };
  1156. template <>
  1157. struct is_non_factory_constructor<no_construction> : std::true_type { };
  1158. template <typename T>
  1159. inline constexpr bool is_non_factory_constructor_v = is_non_factory_constructor<T>::value;
  1160. template <typename T>
  1161. struct is_constructor : is_non_factory_constructor<T> { };
  1162. template <typename... Args>
  1163. struct is_constructor<factory_wrapper<Args...>> : std::true_type { };
  1164. template <typename T>
  1165. struct is_constructor<protect_t<T>> : is_constructor<meta::unqualified_t<T>> { };
  1166. template <typename F, typename... Policies>
  1167. struct is_constructor<policy_wrapper<F, Policies...>> : is_constructor<meta::unqualified_t<F>> { };
  1168. template <typename T>
  1169. inline constexpr bool is_constructor_v = is_constructor<T>::value;
  1170. template <typename... Args>
  1171. using any_is_constructor = meta::any<is_constructor<meta::unqualified_t<Args>>...>;
  1172. template <typename... Args>
  1173. inline constexpr bool any_is_constructor_v = any_is_constructor<Args...>::value;
  1174. template <typename T>
  1175. struct is_destructor : std::false_type { };
  1176. template <typename Fx>
  1177. struct is_destructor<destructor_wrapper<Fx>> : std::true_type { };
  1178. template <typename... Args>
  1179. using any_is_destructor = meta::any<is_destructor<meta::unqualified_t<Args>>...>;
  1180. template <typename... Args>
  1181. inline constexpr bool any_is_destructor_v = any_is_destructor<Args...>::value;
  1182. } // namespace detail
  1183. template <typename T>
  1184. using is_lua_c_function = meta::any<std::is_same<lua_CFunction, T>, std::is_same<detail::lua_CFunction_noexcept, T>, std::is_same<lua_CFunction_ref, T>>;
  1185. template <typename T>
  1186. inline constexpr bool is_lua_c_function_v = is_lua_c_function<T>::value;
  1187. enum class automagic_flags : unsigned {
  1188. none = 0x000u,
  1189. default_constructor = 0x001,
  1190. destructor = 0x002u,
  1191. pairs_operator = 0x004u,
  1192. to_string_operator = 0x008u,
  1193. call_operator = 0x010u,
  1194. less_than_operator = 0x020u,
  1195. less_than_or_equal_to_operator = 0x040u,
  1196. length_operator = 0x080u,
  1197. equal_to_operator = 0x100u,
  1198. all = default_constructor | destructor | pairs_operator | to_string_operator | call_operator | less_than_operator | less_than_or_equal_to_operator
  1199. | length_operator | equal_to_operator
  1200. };
  1201. inline constexpr automagic_flags operator|(automagic_flags left, automagic_flags right) noexcept {
  1202. return static_cast<automagic_flags>(
  1203. static_cast<std::underlying_type_t<automagic_flags>>(left) | static_cast<std::underlying_type_t<automagic_flags>>(right));
  1204. }
  1205. inline constexpr automagic_flags operator&(automagic_flags left, automagic_flags right) noexcept {
  1206. return static_cast<automagic_flags>(
  1207. static_cast<std::underlying_type_t<automagic_flags>>(left) & static_cast<std::underlying_type_t<automagic_flags>>(right));
  1208. }
  1209. inline constexpr automagic_flags& operator|=(automagic_flags& left, automagic_flags right) noexcept {
  1210. left = left | right;
  1211. return left;
  1212. }
  1213. inline constexpr automagic_flags& operator&=(automagic_flags& left, automagic_flags right) noexcept {
  1214. left = left & right;
  1215. return left;
  1216. }
  1217. template <typename Left, typename Right>
  1218. constexpr bool has_flag(Left left, Right right) noexcept {
  1219. return (left & right) == right;
  1220. }
  1221. template <typename Left, typename Right>
  1222. constexpr bool has_any_flag(Left left, Right right) noexcept {
  1223. return (left & right) != static_cast<Left>(static_cast<std::underlying_type_t<Left>>(0));
  1224. }
  1225. template <typename Left, typename Right>
  1226. constexpr auto clear_flags(Left left, Right right) noexcept {
  1227. return static_cast<Left>(static_cast<std::underlying_type_t<Left>>(left) & ~static_cast<std::underlying_type_t<Right>>(right));
  1228. }
  1229. struct automagic_enrollments {
  1230. bool default_constructor = true;
  1231. bool destructor = true;
  1232. bool pairs_operator = true;
  1233. bool to_string_operator = true;
  1234. bool call_operator = true;
  1235. bool less_than_operator = true;
  1236. bool less_than_or_equal_to_operator = true;
  1237. bool length_operator = true;
  1238. bool equal_to_operator = true;
  1239. };
  1240. template <automagic_flags compile_time_defaults = automagic_flags::all>
  1241. struct constant_automagic_enrollments : public automagic_enrollments { };
  1242. } // namespace sol
  1243. #endif // SOL_TYPES_HPP