Commit 48392cfa authored by Niels's avatar Niels

some bug fixing

parent fe64ed5f
This diff is collapsed.
......@@ -1604,26 +1604,11 @@ class basic_json
json_value m_value = {};
public:
private:
///////////////
// iterators //
///////////////
/// a bidirectional iterator for the basic_json class
class iterator : public std::iterator<std::bidirectional_iterator_tag, basic_json>
{
public:
/// the type of the values when the iterator is dereferenced
using value_type = basic_json::value_type;
/// a type to represent differences between iterators
using difference_type = basic_json::difference_type;
/// defines a pointer to the type iterated over (value_type)
using pointer = basic_json::pointer;
/// defines a reference to the type iterated over (value_type)
using reference = basic_json::reference;
/// the category of the iterator
using iterator_category = std::bidirectional_iterator_tag;
/// values of a generic iterator type of non-container JSON values
enum class generic_iterator_value
{
......@@ -1657,6 +1642,42 @@ class basic_json
internal_iterator(generic_iterator_value v) : generic_iterator(v) {}
};
/// a const iterator value
union internal_const_iterator
{
/// iterator for JSON objects
typename object_t::const_iterator object_iterator;
/// iterator for JSON arrays
typename array_t::const_iterator array_iterator;
/// generic iteraotr for all other value types
generic_iterator_value generic_iterator;
/// default constructor
internal_const_iterator() : generic_iterator(generic_iterator_value::uninitialized) {}
/// constructor for object iterators
internal_const_iterator(typename object_t::iterator v) : object_iterator(v) {}
/// constructor for array iterators
internal_const_iterator(typename array_t::iterator v) : array_iterator(v) {}
/// constructor for generic iterators
internal_const_iterator(generic_iterator_value v) : generic_iterator(v) {}
};
public:
/// a bidirectional iterator for the basic_json class
class iterator : public std::iterator<std::bidirectional_iterator_tag, basic_json>
{
public:
/// the type of the values when the iterator is dereferenced
using value_type = basic_json::value_type;
/// a type to represent differences between iterators
using difference_type = basic_json::difference_type;
/// defines a pointer to the type iterated over (value_type)
using pointer = basic_json::pointer;
/// defines a reference to the type iterated over (value_type)
using reference = basic_json::reference;
/// the category of the iterator
using iterator_category = std::bidirectional_iterator_tag;
/// constructor for a given JSON instance
inline iterator(pointer object) : m_object(object)
{
......@@ -2015,39 +2036,6 @@ class basic_json
/// the category of the iterator
using iterator_category = std::bidirectional_iterator_tag;
/// values of a generic iterator type of non-container JSON values
enum class generic_iterator_value
{
/// the iterator was not initialized
uninitialized,
/// the iterator points to the only value
begin,
/// the iterator points past the only value
end,
/// the iterator points to an invalid value
invalid
};
/// an iterator value
union internal_const_iterator
{
/// iterator for JSON objects
typename object_t::const_iterator object_iterator;
/// iterator for JSON arrays
typename array_t::const_iterator array_iterator;
/// generic iteraotr for all other value types
generic_iterator_value generic_iterator;
/// default constructor
internal_const_iterator() : generic_iterator(generic_iterator_value::uninitialized) {}
/// constructor for object iterators
internal_const_iterator(typename object_t::iterator v) : object_iterator(v) {}
/// constructor for array iterators
internal_const_iterator(typename array_t::iterator v) : array_iterator(v) {}
/// constructor for generic iterators
internal_const_iterator(generic_iterator_value v) : generic_iterator(v) {}
};
/// constructor for a given JSON instance
inline const_iterator(pointer object) : m_object(object)
{
......@@ -2072,8 +2060,29 @@ class basic_json
}
/// copy constructor given a nonconst iterator
inline const_iterator(const iterator& other) : m_object(other.m_object), m_it(other.m_it)
{}
inline const_iterator(const iterator& other) : m_object(other.m_object)
{
switch (m_object->m_type)
{
case (basic_json::value_t::object):
{
m_it.object_iterator = other.m_it.object_iterator;
break;
}
case (basic_json::value_t::array):
{
m_it.array_iterator = other.m_it.array_iterator;
break;
}
default:
{
m_it.generic_iterator = other.m_it.generic_iterator;
break;
}
}
}
/// copy assignment
inline const_iterator operator=(const const_iterator& other) noexcept
......
......@@ -2285,7 +2285,7 @@ TEST_CASE("iterators")
SECTION("json + begin/end")
{
auto it = j.begin();
json::iterator it = j.begin();
CHECK(it != j.end());
CHECK(*it == j);
......@@ -2310,7 +2310,7 @@ TEST_CASE("iterators")
SECTION("const json + begin/end")
{
auto it = j_const.begin();
json::const_iterator it = j_const.begin();
CHECK(it != j_const.end());
CHECK(*it == j_const);
......@@ -2335,7 +2335,7 @@ TEST_CASE("iterators")
SECTION("json + cbegin/cend")
{
auto it = j.cbegin();
json::const_iterator it = j.cbegin();
CHECK(it != j.cend());
CHECK(*it == j);
......@@ -2360,7 +2360,7 @@ TEST_CASE("iterators")
SECTION("const json + cbegin/cend")
{
auto it = j_const.cbegin();
json::const_iterator it = j_const.cbegin();
CHECK(it != j_const.cend());
CHECK(*it == j_const);
......@@ -2391,7 +2391,7 @@ TEST_CASE("iterators")
SECTION("json + begin/end")
{
auto it = j.begin();
json::iterator it = j.begin();
CHECK(it != j.end());
CHECK(*it == j);
......@@ -2416,7 +2416,7 @@ TEST_CASE("iterators")
SECTION("const json + begin/end")
{
auto it = j_const.begin();
json::const_iterator it = j_const.begin();
CHECK(it != j_const.end());
CHECK(*it == j_const);
......@@ -2441,7 +2441,7 @@ TEST_CASE("iterators")
SECTION("json + cbegin/cend")
{
auto it = j.cbegin();
json::const_iterator it = j.cbegin();
CHECK(it != j.cend());
CHECK(*it == j);
......@@ -2466,7 +2466,7 @@ TEST_CASE("iterators")
SECTION("const json + cbegin/cend")
{
auto it = j_const.cbegin();
json::const_iterator it = j_const.cbegin();
CHECK(it != j_const.cend());
CHECK(*it == j_const);
......@@ -2509,7 +2509,7 @@ TEST_CASE("iterators")
SECTION("json + begin/end")
{
auto it = j.begin();
json::iterator it = j.begin();
CHECK(it != j.end());
CHECK(*it == j);
......@@ -2534,7 +2534,7 @@ TEST_CASE("iterators")
SECTION("const json + begin/end")
{
auto it = j_const.begin();
json::const_iterator it = j_const.begin();
CHECK(it != j_const.end());
CHECK(*it == j_const);
......@@ -2559,7 +2559,7 @@ TEST_CASE("iterators")
SECTION("json + cbegin/cend")
{
auto it = j.cbegin();
json::const_iterator it = j.cbegin();
CHECK(it != j.cend());
CHECK(*it == j);
......@@ -2584,7 +2584,7 @@ TEST_CASE("iterators")
SECTION("const json + cbegin/cend")
{
auto it = j_const.cbegin();
json::const_iterator it = j_const.cbegin();
CHECK(it != j_const.cend());
CHECK(*it == j_const);
......@@ -2615,7 +2615,7 @@ TEST_CASE("iterators")
SECTION("json + begin/end")
{
auto it = j.begin();
json::iterator it = j.begin();
CHECK(it != j.end());
CHECK(*it == j);
......@@ -2640,7 +2640,7 @@ TEST_CASE("iterators")
SECTION("const json + begin/end")
{
auto it = j_const.begin();
json::const_iterator it = j_const.begin();
CHECK(it != j_const.end());
CHECK(*it == j_const);
......@@ -2665,7 +2665,7 @@ TEST_CASE("iterators")
SECTION("json + cbegin/cend")
{
auto it = j.cbegin();
json::const_iterator it = j.cbegin();
CHECK(it != j.cend());
CHECK(*it == j);
......@@ -2690,7 +2690,7 @@ TEST_CASE("iterators")
SECTION("const json + cbegin/cend")
{
auto it = j_const.cbegin();
json::const_iterator it = j_const.cbegin();
CHECK(it != j_const.cend());
CHECK(*it == j_const);
......@@ -2721,25 +2721,25 @@ TEST_CASE("iterators")
SECTION("json + begin/end")
{
auto it = j.begin();
json::iterator it = j.begin();
CHECK(it == j.end());
}
SECTION("const json + begin/end")
{
auto it = j_const.begin();
json::const_iterator it = j_const.begin();
CHECK(it == j_const.end());
}
SECTION("json + cbegin/cend")
{
auto it = j.cbegin();
json::const_iterator it = j.cbegin();
CHECK(it == j.cend());
}
SECTION("const json + cbegin/cend")
{
auto it = j_const.cbegin();
json::const_iterator it = j_const.cbegin();
CHECK(it == j_const.cend());
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment