stack_iterator.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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_ITERATOR_HPP
  19. #define SOL_STACK_ITERATOR_HPP
  20. #include <sol/stack.hpp>
  21. #include <limits>
  22. #include <iterator>
  23. namespace sol {
  24. template <typename proxy_t, bool is_const>
  25. struct stack_iterator {
  26. typedef meta::conditional_t<is_const, const proxy_t, proxy_t> reference;
  27. typedef meta::conditional_t<is_const, const proxy_t*, proxy_t*> pointer;
  28. typedef proxy_t value_type;
  29. typedef std::ptrdiff_t difference_type;
  30. typedef std::random_access_iterator_tag iterator_category;
  31. lua_State* L;
  32. int index;
  33. int stacktop;
  34. proxy_t sp;
  35. stack_iterator() : L(nullptr), index((std::numeric_limits<int>::max)()), stacktop((std::numeric_limits<int>::max)()), sp() {
  36. }
  37. stack_iterator(const stack_iterator<proxy_t, true>& r) : L(r.L), index(r.index), stacktop(r.stacktop), sp(r.sp) {
  38. }
  39. stack_iterator(lua_State* luastate, int idx, int topidx) : L(luastate), index(idx), stacktop(topidx), sp(luastate, idx) {
  40. }
  41. reference operator*() {
  42. return proxy_t(L, index);
  43. }
  44. reference operator*() const {
  45. return proxy_t(L, index);
  46. }
  47. pointer operator->() {
  48. sp = proxy_t(L, index);
  49. return &sp;
  50. }
  51. pointer operator->() const {
  52. const_cast<proxy_t&>(sp) = proxy_t(L, index);
  53. return &sp;
  54. }
  55. stack_iterator& operator++() {
  56. ++index;
  57. return *this;
  58. }
  59. stack_iterator operator++(int) {
  60. auto r = *this;
  61. this->operator++();
  62. return r;
  63. }
  64. stack_iterator& operator--() {
  65. --index;
  66. return *this;
  67. }
  68. stack_iterator operator--(int) {
  69. auto r = *this;
  70. this->operator--();
  71. return r;
  72. }
  73. stack_iterator& operator+=(difference_type idx) {
  74. index += static_cast<int>(idx);
  75. return *this;
  76. }
  77. stack_iterator& operator-=(difference_type idx) {
  78. index -= static_cast<int>(idx);
  79. return *this;
  80. }
  81. difference_type operator-(const stack_iterator& r) const {
  82. return index - r.index;
  83. }
  84. stack_iterator operator+(difference_type idx) const {
  85. stack_iterator r = *this;
  86. r += idx;
  87. return r;
  88. }
  89. reference operator[](difference_type idx) const {
  90. return proxy_t(L, index + static_cast<int>(idx));
  91. }
  92. bool operator==(const stack_iterator& r) const {
  93. if (stacktop == (std::numeric_limits<int>::max)()) {
  94. return r.index == r.stacktop;
  95. }
  96. else if (r.stacktop == (std::numeric_limits<int>::max)()) {
  97. return index == stacktop;
  98. }
  99. return index == r.index;
  100. }
  101. bool operator!=(const stack_iterator& r) const {
  102. return !(this->operator==(r));
  103. }
  104. bool operator<(const stack_iterator& r) const {
  105. return index < r.index;
  106. }
  107. bool operator>(const stack_iterator& r) const {
  108. return index > r.index;
  109. }
  110. bool operator<=(const stack_iterator& r) const {
  111. return index <= r.index;
  112. }
  113. bool operator>=(const stack_iterator& r) const {
  114. return index >= r.index;
  115. }
  116. };
  117. template <typename proxy_t, bool is_const>
  118. inline stack_iterator<proxy_t, is_const> operator+(
  119. typename stack_iterator<proxy_t, is_const>::difference_type n, const stack_iterator<proxy_t, is_const>& r) {
  120. return r + n;
  121. }
  122. } // namespace sol
  123. #endif // SOL_STACK_ITERATOR_HPP