table.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_TABLE_HPP
  19. #define SOL_TABLE_HPP
  20. #include <sol/table_core.hpp>
  21. #include <sol/lua_table.hpp>
  22. #include <sol/usertype.hpp>
  23. namespace sol {
  24. typedef table_core<false> table;
  25. template <bool is_global, typename base_type>
  26. template <typename Class, typename Key>
  27. usertype<Class> basic_table_core<is_global, base_type>::new_usertype(Key&& key) {
  28. constant_automagic_enrollments<> enrollments {};
  29. return this->new_usertype<Class>(std::forward<Key>(key), std::move(enrollments));
  30. }
  31. template <bool is_global, typename base_type>
  32. template <typename Class, typename Key, automagic_flags enrollment_flags>
  33. usertype<Class> basic_table_core<is_global, base_type>::new_usertype(Key&& key, constant_automagic_enrollments<enrollment_flags> enrollments) {
  34. int mt_index = u_detail::register_usertype<Class, enrollment_flags>(this->lua_state(), std::move(enrollments));
  35. usertype<Class> mt(this->lua_state(), -mt_index);
  36. lua_pop(this->lua_state(), 1);
  37. set(std::forward<Key>(key), mt);
  38. return mt;
  39. }
  40. template <bool is_global, typename base_type>
  41. template <typename Class, typename Key>
  42. usertype<Class> basic_table_core<is_global, base_type>::new_usertype(Key&& key, automagic_enrollments enrollments) {
  43. int mt_index = u_detail::register_usertype<Class, automagic_flags::all>(this->lua_state(), std::move(enrollments));
  44. usertype<Class> mt(this->lua_state(), -mt_index);
  45. lua_pop(this->lua_state(), 1);
  46. set(std::forward<Key>(key), mt);
  47. return mt;
  48. }
  49. template <bool is_global, typename base_type>
  50. template <typename Class, typename Key, typename Arg, typename... Args, typename>
  51. usertype<Class> basic_table_core<is_global, base_type>::new_usertype(Key&& key, Arg&& arg, Args&&... args) {
  52. constexpr automagic_flags enrollment_flags = meta::any_same_v<no_construction, meta::unqualified_t<Arg>, meta::unqualified_t<Args>...>
  53. ? clear_flags(automagic_flags::all, automagic_flags::default_constructor)
  54. : automagic_flags::all;
  55. constant_automagic_enrollments<enrollment_flags> enrollments;
  56. enrollments.default_constructor = !detail::any_is_constructor_v<Arg, Args...>;
  57. enrollments.destructor = !detail::any_is_destructor_v<Arg, Args...>;
  58. usertype<Class> ut = this->new_usertype<Class>(std::forward<Key>(key), std::move(enrollments));
  59. static_assert(sizeof...(Args) % 2 == static_cast<std::size_t>(!detail::any_is_constructor_v<Arg>),
  60. "you must pass an even number of arguments to new_usertype after first passing a constructor");
  61. if constexpr (detail::any_is_constructor_v<Arg>) {
  62. ut.set(meta_function::construct, std::forward<Arg>(arg));
  63. ut.tuple_set(std::make_index_sequence<(sizeof...(Args)) / 2>(), std::forward_as_tuple(std::forward<Args>(args)...));
  64. }
  65. else {
  66. ut.tuple_set(std::make_index_sequence<(sizeof...(Args) + 1) / 2>(), std::forward_as_tuple(std::forward<Arg>(arg), std::forward<Args>(args)...));
  67. }
  68. return ut;
  69. }
  70. template <typename base_type>
  71. template <typename Key, typename Value>
  72. basic_metatable<base_type>& basic_metatable<base_type>::set(Key&& key, Value&& value) {
  73. this->push();
  74. lua_State* L = this->lua_state();
  75. int target = lua_gettop(L);
  76. optional<u_detail::usertype_storage_base&> maybe_uts = nullopt;
  77. maybe_uts = u_detail::maybe_get_usertype_storage_base(L, target);
  78. if (maybe_uts) {
  79. u_detail::usertype_storage_base& uts = *maybe_uts;
  80. uts.set(L, std::forward<Key>(key), std::forward<Value>(value));
  81. return *this;
  82. }
  83. else {
  84. base_t::set(std::forward<Key>(key), std::forward<Value>(value));
  85. }
  86. this->pop();
  87. return *this;
  88. }
  89. namespace stack {
  90. template <>
  91. struct unqualified_getter<metatable_key_t> {
  92. static metatable get(lua_State* L, int index = -1) {
  93. if (lua_getmetatable(L, index) == 0) {
  94. return metatable(L, ref_index(LUA_REFNIL));
  95. }
  96. return metatable(L, -1);
  97. }
  98. };
  99. } // namespace stack
  100. } // namespace sol
  101. #endif // SOL_TABLE_HPP