table_iterator.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_ITERATOR_HPP
  19. #define SOL_TABLE_ITERATOR_HPP
  20. #include <sol/object.hpp>
  21. #include <iterator>
  22. namespace sol {
  23. template <typename reference_type>
  24. class basic_table_iterator {
  25. public:
  26. typedef object key_type;
  27. typedef object mapped_type;
  28. typedef std::pair<object, object> value_type;
  29. typedef std::input_iterator_tag iterator_category;
  30. typedef std::ptrdiff_t difference_type;
  31. typedef value_type* pointer;
  32. typedef value_type& reference;
  33. typedef const value_type& const_reference;
  34. private:
  35. std::pair<object, object> kvp;
  36. reference_type ref;
  37. int tableidx = 0;
  38. int keyidx = 0;
  39. std::ptrdiff_t idx = 0;
  40. public:
  41. basic_table_iterator() noexcept : keyidx(-1), idx(-1) {
  42. }
  43. basic_table_iterator(reference_type x) noexcept : ref(std::move(x)) {
  44. ref.push();
  45. tableidx = lua_gettop(ref.lua_state());
  46. stack::push(ref.lua_state(), lua_nil);
  47. this->operator++();
  48. if (idx == -1) {
  49. return;
  50. }
  51. --idx;
  52. }
  53. basic_table_iterator& operator++() noexcept {
  54. if (idx == -1)
  55. return *this;
  56. if (lua_next(ref.lua_state(), tableidx) == 0) {
  57. idx = -1;
  58. keyidx = -1;
  59. return *this;
  60. }
  61. ++idx;
  62. kvp.first = object(ref.lua_state(), -2);
  63. kvp.second = object(ref.lua_state(), -1);
  64. lua_pop(ref.lua_state(), 1);
  65. // leave key on the stack
  66. keyidx = lua_gettop(ref.lua_state());
  67. return *this;
  68. }
  69. basic_table_iterator operator++(int) noexcept {
  70. auto saved = *this;
  71. this->operator++();
  72. return saved;
  73. }
  74. reference operator*() const noexcept {
  75. return const_cast<reference>(kvp);
  76. }
  77. bool operator==(const basic_table_iterator& right) const noexcept {
  78. return idx == right.idx;
  79. }
  80. bool operator!=(const basic_table_iterator& right) const noexcept {
  81. return idx != right.idx;
  82. }
  83. ~basic_table_iterator() {
  84. if (keyidx != -1) {
  85. stack::remove(ref.lua_state(), keyidx, 1);
  86. }
  87. if (ref.lua_state() != nullptr && ref.valid()) {
  88. stack::remove(ref.lua_state(), tableidx, 1);
  89. }
  90. }
  91. };
  92. } // namespace sol
  93. #endif // SOL_TABLE_ITERATOR_HPP