2020-02-16 23:52:39 +08:00
|
|
|
// sol3
|
2017-12-20 17:58:32 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
// The MIT License (MIT)
|
2016-03-12 00:36:13 +08:00
|
|
|
|
2020-08-13 22:42:37 +08:00
|
|
|
// Copyright (c) 2013-2020 Rapptz, ThePhD and contributors
|
2016-03-12 00:36:13 +08:00
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef SOL_TABLE_ITERATOR_HPP
|
|
|
|
#define SOL_TABLE_ITERATOR_HPP
|
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
#include <sol/object.hpp>
|
2016-03-12 00:36:13 +08:00
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
namespace sol {
|
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
template <typename reference_type>
|
2020-02-16 23:52:39 +08:00
|
|
|
class basic_table_iterator {
|
2016-06-20 05:59:40 +08:00
|
|
|
public:
|
|
|
|
typedef object key_type;
|
|
|
|
typedef object mapped_type;
|
2017-12-07 21:24:50 +08:00
|
|
|
typedef std::pair<object, object> value_type;
|
|
|
|
typedef std::input_iterator_tag iterator_category;
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
|
|
|
typedef value_type* pointer;
|
|
|
|
typedef value_type& reference;
|
2016-06-20 05:59:40 +08:00
|
|
|
typedef const value_type& const_reference;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::pair<object, object> kvp;
|
|
|
|
reference_type ref;
|
|
|
|
int tableidx = 0;
|
|
|
|
int keyidx = 0;
|
|
|
|
std::ptrdiff_t idx = 0;
|
|
|
|
|
|
|
|
public:
|
2021-01-21 02:55:44 +08:00
|
|
|
basic_table_iterator() noexcept : keyidx(-1), idx(-1) {
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2021-01-21 02:55:44 +08:00
|
|
|
basic_table_iterator(reference_type x) noexcept : ref(std::move(x)) {
|
2016-06-20 05:59:40 +08:00
|
|
|
ref.push();
|
|
|
|
tableidx = lua_gettop(ref.lua_state());
|
2016-12-03 20:33:18 +08:00
|
|
|
stack::push(ref.lua_state(), lua_nil);
|
2016-06-20 05:59:40 +08:00
|
|
|
this->operator++();
|
|
|
|
if (idx == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
--idx;
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:55:44 +08:00
|
|
|
basic_table_iterator& operator++() noexcept {
|
2016-06-20 05:59:40 +08:00
|
|
|
if (idx == -1)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
if (lua_next(ref.lua_state(), tableidx) == 0) {
|
|
|
|
idx = -1;
|
|
|
|
keyidx = -1;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
kvp.first = object(ref.lua_state(), -2);
|
|
|
|
kvp.second = object(ref.lua_state(), -1);
|
|
|
|
lua_pop(ref.lua_state(), 1);
|
|
|
|
// leave key on the stack
|
|
|
|
keyidx = lua_gettop(ref.lua_state());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:55:44 +08:00
|
|
|
basic_table_iterator operator++(int) noexcept {
|
2016-06-20 05:59:40 +08:00
|
|
|
auto saved = *this;
|
|
|
|
this->operator++();
|
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:55:44 +08:00
|
|
|
reference operator*() noexcept {
|
2016-06-20 05:59:40 +08:00
|
|
|
return kvp;
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:55:44 +08:00
|
|
|
const_reference operator*() const noexcept {
|
2016-06-20 05:59:40 +08:00
|
|
|
return kvp;
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:55:44 +08:00
|
|
|
bool operator==(const basic_table_iterator& right) const noexcept {
|
2016-06-20 05:59:40 +08:00
|
|
|
return idx == right.idx;
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:55:44 +08:00
|
|
|
bool operator!=(const basic_table_iterator& right) const noexcept {
|
2016-06-20 05:59:40 +08:00
|
|
|
return idx != right.idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
~basic_table_iterator() {
|
|
|
|
if (keyidx != -1) {
|
|
|
|
stack::remove(ref.lua_state(), keyidx, 1);
|
|
|
|
}
|
2018-11-28 11:19:18 +08:00
|
|
|
if (ref.lua_state() != nullptr && ref.valid()) {
|
2016-06-20 05:59:40 +08:00
|
|
|
stack::remove(ref.lua_state(), tableidx, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-03-12 00:36:13 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace sol
|
2016-03-12 00:36:13 +08:00
|
|
|
|
|
|
|
#endif // SOL_TABLE_ITERATOR_HPP
|