tables.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Luwra
  2. * Minimal-overhead Lua wrapper for C++
  3. *
  4. * Copyright (C) 2016, Ole Krüger <ole@vprsm.de>
  5. */
  6. #ifndef LUWRA_TABLES_H_
  7. #define LUWRA_TABLES_H_
  8. #include "common.hpp"
  9. #include "types.hpp"
  10. #include "auxiliary.hpp"
  11. LUWRA_NS_BEGIN
  12. namespace internal {
  13. template <typename K>
  14. struct TableAccessor;
  15. }
  16. struct Table {
  17. Reference ref;
  18. Table(const Reference& ref):
  19. ref(ref)
  20. {}
  21. Table(State* state, int index):
  22. ref(state, index, true)
  23. {
  24. luaL_checktype(state, index, LUA_TTABLE);
  25. }
  26. template <typename K> inline
  27. internal::TableAccessor<K> access(K&& key);
  28. template <typename K> inline
  29. internal::TableAccessor<K> operator [](K&& key) {
  30. return access<K>(std::forward<K>(key));
  31. }
  32. inline
  33. void update(const FieldVector& fields) {
  34. State* state = ref.impl->state;
  35. push(state, ref);
  36. setFields(state, -1, fields);
  37. lua_pop(state, 1);
  38. }
  39. template <typename K> inline
  40. bool has(K&& key) {
  41. State* state = ref.impl->state;
  42. push(state, ref);
  43. size_t pushedKeys = push(state, key);
  44. if (pushedKeys > 1) lua_pop(state, pushedKeys - 1);
  45. lua_rawget(state, -2);
  46. bool isNil = lua_isnil(state, -1);
  47. lua_pop(state, 2);
  48. return !isNil;
  49. }
  50. template <typename V, typename K> inline
  51. void set(K&& key, V&& value) {
  52. State* state = ref.impl->state;
  53. push(state, ref);
  54. size_t pushedKeys = push(state, key);
  55. if (pushedKeys > 1) lua_pop(state, static_cast<int>(pushedKeys - 1));
  56. size_t pushedValues = push(state, value);
  57. if (pushedValues > 1) lua_pop(state, static_cast<int>(pushedValues - 1));
  58. lua_rawset(state, -3);
  59. lua_pop(state, 1);
  60. }
  61. template <typename V, typename K> inline
  62. V get(K&& key) {
  63. State* state = ref.impl->state;
  64. push(state, ref);
  65. size_t pushedKeys = push(state, key);
  66. if (pushedKeys > 1) lua_pop(state, static_cast<int>(pushedKeys - 1));
  67. lua_rawget(state, -2);
  68. V ret = read<V>(state, -1);
  69. lua_pop(state, 2);
  70. return ret;
  71. }
  72. };
  73. namespace internal {
  74. template <typename K>
  75. struct TableAccessor {
  76. Table parent;
  77. K key;
  78. template <typename V> inline
  79. V read() {
  80. return parent.get<V>(key);
  81. }
  82. template <typename V> inline
  83. operator V() {
  84. return parent.get<V>(key);
  85. }
  86. template <typename V> inline
  87. TableAccessor<K>& write(V&& value) {
  88. parent.set(key, std::forward<V>(value));
  89. return *this;
  90. }
  91. template <typename V> inline
  92. TableAccessor<K>& operator =(V&& value) {
  93. parent.set(key, std::forward<V>(value));
  94. return *this;
  95. }
  96. template <typename T> inline
  97. TableAccessor<T> access(T&& subkey) {
  98. return {read<Table>(), subkey};
  99. }
  100. template <typename T> inline
  101. TableAccessor<T> operator [](T&& subkey) {
  102. return {read<Table>(), subkey};
  103. }
  104. };
  105. }
  106. /**
  107. * Table
  108. */
  109. template <typename K> inline
  110. internal::TableAccessor<K> Table::access(K&& key) {
  111. return {*this, key};
  112. }
  113. /**
  114. * See [Table](@ref Table).
  115. */
  116. template <>
  117. struct Value<Table> {
  118. static inline
  119. Table read(State* state, int index) {
  120. return {state, index};
  121. }
  122. static inline
  123. size_t push(State* state, const Table& value) {
  124. return value.ref.impl->push(state);
  125. }
  126. };
  127. /**
  128. * Retrieve the table containing all global values.
  129. * \param state Lua state
  130. * \returns Reference to the globals table.
  131. */
  132. static inline
  133. Table getGlobalsTable(State* state) {
  134. #if LUA_VERSION_NUM <= 501
  135. return {{state, internal::referenceValue(state, LUA_GLOBALSINDEX), false}};
  136. #else
  137. return {{state, LUA_RIDX_GLOBALS, false}};
  138. #endif
  139. }
  140. LUWRA_NS_END
  141. #endif