stack_field.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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_STACK_FIELD_HPP
  19. #define SOL_STACK_FIELD_HPP
  20. #include <sol/stack_core.hpp>
  21. #include <sol/stack_push.hpp>
  22. #include <sol/stack_get.hpp>
  23. #include <sol/stack_check_get.hpp>
  24. namespace sol { namespace stack {
  25. namespace stack_detail {
  26. template <typename T, bool global, bool raw>
  27. inline constexpr bool is_get_direct_tableless_v = (global && !raw && meta::is_c_str_or_string_v<T>);
  28. template <typename T, bool global, bool raw>
  29. inline constexpr bool is_get_direct_v = (is_get_direct_tableless_v<T, global, raw>) // cf-hack
  30. || (!global && !raw && (meta::is_c_str_or_string_v<T> || meta::is_string_of_v<T, char>)) // cf-hack
  31. || (!global && raw && (std::is_integral_v<T> && !std::is_same_v<T, bool>))
  32. #if SOL_LUA_VERSION_I_ >= 503
  33. || (!global && !raw && (std::is_integral_v<T> && !std::is_same_v<T, bool>))
  34. #endif // integer keys 5.3 or better
  35. #if SOL_LUA_VERSION_I_ >= 502
  36. || (!global && raw && std::is_pointer_v<T> && std::is_void_v<std::remove_pointer_t<T>>)
  37. #endif // void pointer keys 5.2 or better
  38. ;
  39. template <typename T, bool global, bool raw>
  40. inline constexpr bool is_set_direct_tableless_v = (global && !raw && meta::is_c_str_or_string_v<T>);
  41. template <typename T, bool global, bool raw>
  42. inline constexpr bool is_set_direct_v = (is_set_direct_tableless_v<T, global, raw>) // cf-hack
  43. || (!global && !raw && (meta::is_c_str_or_string_v<T> || meta::is_string_of_v<T, char>)) // cf-hack
  44. || (!global && raw && (std::is_integral_v<T> && !std::is_same_v<T, bool>)) // cf-hack
  45. #if SOL_LUA_VERSION_I_ >= 503
  46. || (!global && !raw && (std::is_integral_v<T> && !std::is_same_v<T, bool>))
  47. #endif // integer keys 5.3 or better
  48. #if SOL_LUA_VERSION_I_ >= 502
  49. || (!global && raw && (std::is_pointer_v<T> && std::is_void_v<std::remove_pointer_t<T>>))
  50. #endif // void pointer keys 5.2 or better
  51. ;
  52. } // namespace stack_detail
  53. template <typename T, bool global, bool raw, typename>
  54. struct field_getter {
  55. static inline constexpr int default_table_index
  56. = meta::conditional_t<stack_detail::is_get_direct_v<T, global, raw>, std::integral_constant<int, -1>, std::integral_constant<int, -2>>::value;
  57. template <typename Key>
  58. void get(lua_State* L, Key&& key, int tableindex = default_table_index) {
  59. if constexpr (std::is_same_v<T, update_if_empty_t> || std::is_same_v<T, override_value_t> || std::is_same_v<T, create_if_nil_t>) {
  60. (void)L;
  61. (void)key;
  62. (void)tableindex;
  63. }
  64. else if constexpr (std::is_same_v<T, env_key_t>) {
  65. (void)key;
  66. #if SOL_LUA_VERSION_I_ < 502
  67. // Use lua_setfenv
  68. lua_getfenv(L, tableindex);
  69. #else
  70. // Use upvalues as explained in Lua 5.2 and beyond's manual
  71. if (lua_getupvalue(L, tableindex, 1) == nullptr) {
  72. push(L, lua_nil);
  73. }
  74. #endif
  75. }
  76. else if constexpr (std::is_same_v<T, metatable_key_t>) {
  77. (void)key;
  78. if (lua_getmetatable(L, tableindex) == 0)
  79. push(L, lua_nil);
  80. }
  81. else if constexpr (raw) {
  82. if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
  83. lua_rawgeti(L, tableindex, static_cast<lua_Integer>(key));
  84. }
  85. #if SOL_LUA_VERSION_I_ >= 502
  86. else if constexpr (std::is_pointer_v<T> && std::is_void_v<std::remove_pointer_t<T>>) {
  87. lua_rawgetp(L, tableindex, key);
  88. }
  89. #endif // Lua 5.2.x+
  90. else {
  91. push(L, std::forward<Key>(key));
  92. lua_rawget(L, tableindex);
  93. }
  94. }
  95. else {
  96. if constexpr (meta::is_c_str_or_string_v<T>) {
  97. if constexpr (global) {
  98. (void)tableindex;
  99. lua_getglobal(L, &key[0]);
  100. }
  101. else {
  102. lua_getfield(L, tableindex, &key[0]);
  103. }
  104. }
  105. else if constexpr (std::is_same_v<T, meta_function>) {
  106. const auto& real_key = to_string(key);
  107. lua_getfield(L, tableindex, &real_key[0]);
  108. }
  109. #if SOL_LUA_VERSION_I_ >= 503
  110. else if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
  111. lua_geti(L, tableindex, static_cast<lua_Integer>(key));
  112. }
  113. #endif // Lua 5.3.x+
  114. else {
  115. push(L, std::forward<Key>(key));
  116. lua_gettable(L, tableindex);
  117. }
  118. }
  119. }
  120. };
  121. template <typename... Args, bool b, bool raw, typename C>
  122. struct field_getter<std::tuple<Args...>, b, raw, C> {
  123. template <std::size_t... I, typename Keys>
  124. void apply(std::index_sequence<0, I...>, lua_State* L, Keys&& keys, int tableindex) {
  125. get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)), tableindex);
  126. void(detail::swallow { (get_field<false, raw>(L, std::get<I>(std::forward<Keys>(keys))), 0)... });
  127. reference saved(L, -1);
  128. lua_pop(L, static_cast<int>(sizeof...(I)));
  129. saved.push();
  130. }
  131. template <typename Keys>
  132. void get(lua_State* L, Keys&& keys) {
  133. apply(std::make_index_sequence<sizeof...(Args)>(), L, std::forward<Keys>(keys), lua_absindex(L, -1));
  134. }
  135. template <typename Keys>
  136. void get(lua_State* L, Keys&& keys, int tableindex) {
  137. apply(std::make_index_sequence<sizeof...(Args)>(), L, std::forward<Keys>(keys), tableindex);
  138. }
  139. };
  140. template <typename A, typename B, bool b, bool raw, typename C>
  141. struct field_getter<std::pair<A, B>, b, raw, C> {
  142. template <typename Keys>
  143. void get(lua_State* L, Keys&& keys, int tableindex) {
  144. get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)), tableindex);
  145. get_field<false, raw>(L, std::get<1>(std::forward<Keys>(keys)));
  146. reference saved(L, -1);
  147. lua_pop(L, static_cast<int>(2));
  148. saved.push();
  149. }
  150. template <typename Keys>
  151. void get(lua_State* L, Keys&& keys) {
  152. get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)));
  153. get_field<false, raw>(L, std::get<1>(std::forward<Keys>(keys)));
  154. reference saved(L, -1);
  155. lua_pop(L, static_cast<int>(2));
  156. saved.push();
  157. }
  158. };
  159. template <typename T, bool global, bool raw, typename>
  160. struct field_setter {
  161. static constexpr int default_table_index
  162. = meta::conditional_t<stack_detail::is_set_direct_v<T, global, raw>, std::integral_constant<int, -2>, std::integral_constant<int, -3>>::value;
  163. template <typename Key, typename Value>
  164. void set(lua_State* L, Key&& key, Value&& value, int tableindex = default_table_index) {
  165. if constexpr (std::is_same_v<T, update_if_empty_t> || std::is_same_v<T, override_value_t>) {
  166. (void)L;
  167. (void)key;
  168. (void)value;
  169. (void)tableindex;
  170. }
  171. else if constexpr (std::is_same_v<T, metatable_key_t>) {
  172. (void)key;
  173. push(L, std::forward<Value>(value));
  174. lua_setmetatable(L, tableindex);
  175. }
  176. else if constexpr (raw) {
  177. if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
  178. push(L, std::forward<Value>(value));
  179. lua_rawseti(L, tableindex, static_cast<lua_Integer>(key));
  180. }
  181. #if SOL_LUA_VERSION_I_ >= 502
  182. else if constexpr (std::is_pointer_v<T> && std::is_void_v<std::remove_pointer_t<T>>) {
  183. push(L, std::forward<Value>(value));
  184. lua_rawsetp(L, tableindex, std::forward<Key>(key));
  185. }
  186. #endif // Lua 5.2.x
  187. else {
  188. push(L, std::forward<Key>(key));
  189. push(L, std::forward<Value>(value));
  190. lua_rawset(L, tableindex);
  191. }
  192. }
  193. else {
  194. if constexpr (meta::is_c_str_or_string_v<T>) {
  195. if constexpr (global) {
  196. push(L, std::forward<Value>(value));
  197. lua_setglobal(L, &key[0]);
  198. (void)tableindex;
  199. }
  200. else {
  201. push(L, std::forward<Value>(value));
  202. lua_setfield(L, tableindex, &key[0]);
  203. }
  204. }
  205. #if SOL_LUA_VERSION_I_ >= 503
  206. else if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
  207. push(L, std::forward<Value>(value));
  208. lua_seti(L, tableindex, static_cast<lua_Integer>(key));
  209. }
  210. #endif // Lua 5.3.x
  211. else {
  212. push(L, std::forward<Key>(key));
  213. push(L, std::forward<Value>(value));
  214. lua_settable(L, tableindex);
  215. }
  216. }
  217. }
  218. };
  219. template <typename... Args, bool b, bool raw, typename C>
  220. struct field_setter<std::tuple<Args...>, b, raw, C> {
  221. template <bool g, std::size_t I, typename Keys, typename Value>
  222. void apply(std::index_sequence<I>, lua_State* L, Keys&& keys, Value&& value, int tableindex) {
  223. I < 1 ? set_field<g, raw>(L, std::get<I>(std::forward<Keys>(keys)), std::forward<Value>(value), tableindex)
  224. : set_field<g, raw>(L, std::get<I>(std::forward<Keys>(keys)), std::forward<Value>(value));
  225. }
  226. template <bool g, std::size_t I0, std::size_t I1, std::size_t... I, typename Keys, typename Value>
  227. void apply(std::index_sequence<I0, I1, I...>, lua_State* L, Keys&& keys, Value&& value, int tableindex) {
  228. I0 < 1 ? get_field<g, raw>(L, std::get<I0>(std::forward<Keys>(keys)), tableindex)
  229. : get_field<g, raw>(L, std::get<I0>(std::forward<Keys>(keys)), -1);
  230. apply<false>(std::index_sequence<I1, I...>(), L, std::forward<Keys>(keys), std::forward<Value>(value), -1);
  231. }
  232. template <bool g, std::size_t I0, std::size_t... I, typename Keys, typename Value>
  233. void top_apply(std::index_sequence<I0, I...>, lua_State* L, Keys&& keys, Value&& value, int tableindex) {
  234. apply<g>(std::index_sequence<I0, I...>(), L, std::forward<Keys>(keys), std::forward<Value>(value), tableindex);
  235. lua_pop(L, static_cast<int>(sizeof...(I)));
  236. }
  237. template <typename Keys, typename Value>
  238. void set(lua_State* L, Keys&& keys, Value&& value, int tableindex = -3) {
  239. top_apply<b>(std::make_index_sequence<sizeof...(Args)>(), L, std::forward<Keys>(keys), std::forward<Value>(value), tableindex);
  240. }
  241. };
  242. template <typename A, typename B, bool b, bool raw, typename C>
  243. struct field_setter<std::pair<A, B>, b, raw, C> {
  244. template <typename Keys, typename Value>
  245. void set(lua_State* L, Keys&& keys, Value&& value, int tableindex = -1) {
  246. get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)), tableindex);
  247. set_field<false, raw>(L, std::get<1>(std::forward<Keys>(keys)), std::forward<Value>(value), lua_gettop(L));
  248. lua_pop(L, 1);
  249. }
  250. };
  251. }} // namespace sol::stack
  252. #endif // SOL_STACK_FIELD_HPP