usertype_proxy.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_USERTYPE_PROXY_HPP
  19. #define SOL_USERTYPE_PROXY_HPP
  20. #include <sol/traits.hpp>
  21. #include <sol/function.hpp>
  22. #include <sol/protected_function.hpp>
  23. #include <sol/proxy_base.hpp>
  24. namespace sol {
  25. template <typename Table, typename Key>
  26. struct usertype_proxy : public proxy_base<usertype_proxy<Table, Key>> {
  27. private:
  28. using key_type = detail::proxy_key_t<Key>;
  29. template <typename T, std::size_t... I>
  30. decltype(auto) tuple_get(std::index_sequence<I...>) const& {
  31. return tbl.template traverse_get<T>(std::get<I>(key)...);
  32. }
  33. template <typename T, std::size_t... I>
  34. decltype(auto) tuple_get(std::index_sequence<I...>) && {
  35. return tbl.template traverse_get<T>(std::get<I>(std::move(key))...);
  36. }
  37. template <std::size_t... I, typename T>
  38. void tuple_set(std::index_sequence<I...>, T&& value) & {
  39. if constexpr (sizeof...(I) > 1) {
  40. tbl.traverse_set(std::get<I>(key)..., std::forward<T>(value));
  41. }
  42. else {
  43. tbl.set(std::get<I>(key)..., std::forward<T>(value));
  44. }
  45. }
  46. template <std::size_t... I, typename T>
  47. void tuple_set(std::index_sequence<I...>, T&& value) && {
  48. if constexpr (sizeof...(I) > 1) {
  49. tbl.traverse_set(std::get<I>(std::move(key))..., std::forward<T>(value));
  50. }
  51. else {
  52. tbl.set(std::get<I>(std::move(key))..., std::forward<T>(value));
  53. }
  54. }
  55. public:
  56. Table tbl;
  57. key_type key;
  58. template <typename T>
  59. usertype_proxy(Table table, T&& k) : tbl(table), key(std::forward<T>(k)) {
  60. }
  61. template <typename T>
  62. usertype_proxy& set(T&& item) & {
  63. using idx_seq = std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>;
  64. tuple_set(idx_seq(), std::forward<T>(item));
  65. return *this;
  66. }
  67. template <typename T>
  68. usertype_proxy&& set(T&& item) && {
  69. using idx_seq = std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>;
  70. std::move(*this).tuple_set(idx_seq(), std::forward<T>(item));
  71. return std::move(*this);
  72. }
  73. template <typename T>
  74. usertype_proxy& operator=(T&& other) & {
  75. return set(std::forward<T>(other));
  76. }
  77. template <typename T>
  78. usertype_proxy&& operator=(T&& other) && {
  79. return std::move(*this).set(std::forward<T>(other));
  80. }
  81. template <typename T>
  82. usertype_proxy& operator=(std::initializer_list<T> other) & {
  83. return set(std::move(other));
  84. }
  85. template <typename T>
  86. usertype_proxy&& operator=(std::initializer_list<T> other) && {
  87. return std::move(*this).set(std::move(other));
  88. }
  89. template <typename T>
  90. decltype(auto) get() const& {
  91. using idx_seq = std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>;
  92. return tuple_get<T>(idx_seq());
  93. }
  94. template <typename T>
  95. decltype(auto) get() && {
  96. using idx_seq = std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>;
  97. return std::move(*this).template tuple_get<T>(idx_seq());
  98. }
  99. template <typename K>
  100. decltype(auto) operator[](K&& k) const& {
  101. auto keys = meta::tuplefy(key, std::forward<K>(k));
  102. return usertype_proxy<Table, decltype(keys)>(tbl, std::move(keys));
  103. }
  104. template <typename K>
  105. decltype(auto) operator[](K&& k) & {
  106. auto keys = meta::tuplefy(key, std::forward<K>(k));
  107. return usertype_proxy<Table, decltype(keys)>(tbl, std::move(keys));
  108. }
  109. template <typename K>
  110. decltype(auto) operator[](K&& k) && {
  111. auto keys = meta::tuplefy(std::move(key), std::forward<K>(k));
  112. return usertype_proxy<Table, decltype(keys)>(tbl, std::move(keys));
  113. }
  114. template <typename... Ret, typename... Args>
  115. decltype(auto) call(Args&&... args) {
  116. #if !defined(__clang__) && defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 191200000
  117. // MSVC is ass sometimes
  118. return get<function>().call<Ret...>(std::forward<Args>(args)...);
  119. #else
  120. return get<function>().template call<Ret...>(std::forward<Args>(args)...);
  121. #endif
  122. }
  123. template <typename... Args>
  124. decltype(auto) operator()(Args&&... args) {
  125. return call<>(std::forward<Args>(args)...);
  126. }
  127. bool valid() const {
  128. auto pp = stack::push_pop(tbl);
  129. auto p = stack::probe_get_field<std::is_same<meta::unqualified_t<Table>, global_table>::value>(lua_state(), key, lua_gettop(lua_state()));
  130. lua_pop(lua_state(), p.levels);
  131. return p;
  132. }
  133. int push() const noexcept {
  134. return push(this->lua_state());
  135. }
  136. int push(lua_State* L) const noexcept {
  137. return get<reference>().push(L);
  138. }
  139. type get_type() const {
  140. type t = type::none;
  141. auto pp = stack::push_pop(tbl);
  142. auto p = stack::probe_get_field<std::is_same<meta::unqualified_t<Table>, global_table>::value>(lua_state(), key, lua_gettop(lua_state()));
  143. if (p) {
  144. t = type_of(lua_state(), -1);
  145. }
  146. lua_pop(lua_state(), p.levels);
  147. return t;
  148. }
  149. lua_State* lua_state() const {
  150. return tbl.lua_state();
  151. }
  152. };
  153. } // namespace sol
  154. #endif // SOL_USERTYPE_PROXY_HPP