mirror of
https://github.com/nlohmann/json.git
synced 2025-07-13 20:21:48 +03:00
fixed iterator_wrapper
This commit is contained in:
@ -5712,10 +5712,41 @@ class basic_json
|
||||
json_iterator anchor;
|
||||
/// an index for arrays
|
||||
size_t array_index = 0;
|
||||
size_t container_size = 0;
|
||||
|
||||
/// calculate a key for the iterator
|
||||
std::string calculate_key()
|
||||
public:
|
||||
/// construct wrapper given an iterator
|
||||
iterator_wrapper_internal(json_iterator i) : anchor(i)
|
||||
{}
|
||||
|
||||
/// dereference operator (needed for range-based for)
|
||||
iterator_wrapper_internal& operator*()
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// increment operator (needed for range-based for)
|
||||
iterator_wrapper_internal& operator++()
|
||||
{
|
||||
++anchor;
|
||||
++array_index;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// inequality operator (needed for range-based for)
|
||||
bool operator!= (const iterator_wrapper_internal& o)
|
||||
{
|
||||
return anchor != o.anchor;
|
||||
}
|
||||
|
||||
/// stream operator
|
||||
friend std::ostream& operator<<(std::ostream& o, const iterator_wrapper_internal& w)
|
||||
{
|
||||
return o << w.value();
|
||||
}
|
||||
|
||||
/// return key of the iterator
|
||||
typename basic_json::string_t key() const
|
||||
{
|
||||
switch (anchor.m_object->type())
|
||||
{
|
||||
@ -5739,61 +5770,11 @@ class basic_json
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
/// construct wrapper given an iterator
|
||||
iterator_wrapper_internal(json_iterator i, size_t s)
|
||||
: anchor(i), container_size(s), first(calculate_key()), second(*i)
|
||||
{}
|
||||
|
||||
/// dereference operator (needed for range-based for)
|
||||
iterator_wrapper_internal operator*()
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// increment operator (needed for range-based for)
|
||||
iterator_wrapper_internal& operator++()
|
||||
{
|
||||
++anchor;
|
||||
++array_index;
|
||||
|
||||
if (array_index < container_size)
|
||||
{
|
||||
first = calculate_key();
|
||||
second = *anchor;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// inequality operator (needed for range-based for)
|
||||
bool operator!= (const iterator_wrapper_internal& o)
|
||||
{
|
||||
return anchor != o.anchor;
|
||||
}
|
||||
|
||||
/// stream operator
|
||||
friend std::ostream& operator<<(std::ostream& o, const iterator_wrapper_internal& w)
|
||||
{
|
||||
return o << w.value();
|
||||
}
|
||||
|
||||
/// return key of the iterator
|
||||
typename basic_json::string_t key() const
|
||||
{
|
||||
return first;
|
||||
}
|
||||
|
||||
/// return value of the iterator
|
||||
basic_json value() const
|
||||
typename json_iterator::reference value() const
|
||||
{
|
||||
return second;
|
||||
return anchor.value();
|
||||
}
|
||||
|
||||
/// public member to simulate std::map::iterator access
|
||||
typename basic_json::string_t first;
|
||||
/// public member to simulate std::map::iterator access
|
||||
basic_json& second;
|
||||
};
|
||||
|
||||
public:
|
||||
@ -5805,13 +5786,13 @@ class basic_json
|
||||
/// return iterator begin (needed for range-based for)
|
||||
iterator_wrapper_internal begin()
|
||||
{
|
||||
return iterator_wrapper_internal(container.begin(), container.size());
|
||||
return iterator_wrapper_internal(container.begin());
|
||||
}
|
||||
|
||||
/// return iterator end (needed for range-based for)
|
||||
iterator_wrapper_internal end()
|
||||
{
|
||||
return iterator_wrapper_internal(container.end(), container.size());
|
||||
return iterator_wrapper_internal(container.end());
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user