some bug fixing

This commit is contained in:
Niels 2015-02-09 18:02:31 +01:00
parent fe64ed5f01
commit 48392cfa79
3 changed files with 482 additions and 809 deletions

File diff suppressed because it is too large Load diff

View file

@ -1604,11 +1604,65 @@ class basic_json
json_value m_value = {}; json_value m_value = {};
public: private:
/////////////// ///////////////
// iterators // // iterators //
/////////////// ///////////////
/// 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_iterator
{
/// iterator for JSON objects
typename object_t::iterator object_iterator;
/// iterator for JSON arrays
typename array_t::iterator array_iterator;
/// generic iteraotr for all other value types
generic_iterator_value generic_iterator;
/// default constructor
internal_iterator() : generic_iterator(generic_iterator_value::uninitialized) {}
/// constructor for object iterators
internal_iterator(typename object_t::iterator v) : object_iterator(v) {}
/// constructor for array iterators
internal_iterator(typename array_t::iterator v) : array_iterator(v) {}
/// constructor for generic iterators
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 /// a bidirectional iterator for the basic_json class
class iterator : public std::iterator<std::bidirectional_iterator_tag, basic_json> class iterator : public std::iterator<std::bidirectional_iterator_tag, basic_json>
{ {
@ -1624,39 +1678,6 @@ class basic_json
/// the category of the iterator /// the category of the iterator
using iterator_category = std::bidirectional_iterator_tag; 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_iterator
{
/// iterator for JSON objects
typename object_t::iterator object_iterator;
/// iterator for JSON arrays
typename array_t::iterator array_iterator;
/// generic iteraotr for all other value types
generic_iterator_value generic_iterator;
/// default constructor
internal_iterator() : generic_iterator(generic_iterator_value::uninitialized) {}
/// constructor for object iterators
internal_iterator(typename object_t::iterator v) : object_iterator(v) {}
/// constructor for array iterators
internal_iterator(typename array_t::iterator v) : array_iterator(v) {}
/// constructor for generic iterators
internal_iterator(generic_iterator_value v) : generic_iterator(v) {}
};
/// constructor for a given JSON instance /// constructor for a given JSON instance
inline iterator(pointer object) : m_object(object) inline iterator(pointer object) : m_object(object)
{ {
@ -2015,39 +2036,6 @@ class basic_json
/// the category of the iterator /// the category of the iterator
using iterator_category = std::bidirectional_iterator_tag; 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 /// constructor for a given JSON instance
inline const_iterator(pointer object) : m_object(object) inline const_iterator(pointer object) : m_object(object)
{ {
@ -2072,8 +2060,29 @@ class basic_json
} }
/// copy constructor given a nonconst iterator /// 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 /// copy assignment
inline const_iterator operator=(const const_iterator& other) noexcept inline const_iterator operator=(const const_iterator& other) noexcept

View file

@ -2285,7 +2285,7 @@ TEST_CASE("iterators")
SECTION("json + begin/end") SECTION("json + begin/end")
{ {
auto it = j.begin(); json::iterator it = j.begin();
CHECK(it != j.end()); CHECK(it != j.end());
CHECK(*it == j); CHECK(*it == j);
@ -2310,7 +2310,7 @@ TEST_CASE("iterators")
SECTION("const json + begin/end") 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.end());
CHECK(*it == j_const); CHECK(*it == j_const);
@ -2335,7 +2335,7 @@ TEST_CASE("iterators")
SECTION("json + cbegin/cend") SECTION("json + cbegin/cend")
{ {
auto it = j.cbegin(); json::const_iterator it = j.cbegin();
CHECK(it != j.cend()); CHECK(it != j.cend());
CHECK(*it == j); CHECK(*it == j);
@ -2360,7 +2360,7 @@ TEST_CASE("iterators")
SECTION("const json + cbegin/cend") 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.cend());
CHECK(*it == j_const); CHECK(*it == j_const);
@ -2391,7 +2391,7 @@ TEST_CASE("iterators")
SECTION("json + begin/end") SECTION("json + begin/end")
{ {
auto it = j.begin(); json::iterator it = j.begin();
CHECK(it != j.end()); CHECK(it != j.end());
CHECK(*it == j); CHECK(*it == j);
@ -2416,7 +2416,7 @@ TEST_CASE("iterators")
SECTION("const json + begin/end") 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.end());
CHECK(*it == j_const); CHECK(*it == j_const);
@ -2441,7 +2441,7 @@ TEST_CASE("iterators")
SECTION("json + cbegin/cend") SECTION("json + cbegin/cend")
{ {
auto it = j.cbegin(); json::const_iterator it = j.cbegin();
CHECK(it != j.cend()); CHECK(it != j.cend());
CHECK(*it == j); CHECK(*it == j);
@ -2466,7 +2466,7 @@ TEST_CASE("iterators")
SECTION("const json + cbegin/cend") 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.cend());
CHECK(*it == j_const); CHECK(*it == j_const);
@ -2509,7 +2509,7 @@ TEST_CASE("iterators")
SECTION("json + begin/end") SECTION("json + begin/end")
{ {
auto it = j.begin(); json::iterator it = j.begin();
CHECK(it != j.end()); CHECK(it != j.end());
CHECK(*it == j); CHECK(*it == j);
@ -2534,7 +2534,7 @@ TEST_CASE("iterators")
SECTION("const json + begin/end") 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.end());
CHECK(*it == j_const); CHECK(*it == j_const);
@ -2559,7 +2559,7 @@ TEST_CASE("iterators")
SECTION("json + cbegin/cend") SECTION("json + cbegin/cend")
{ {
auto it = j.cbegin(); json::const_iterator it = j.cbegin();
CHECK(it != j.cend()); CHECK(it != j.cend());
CHECK(*it == j); CHECK(*it == j);
@ -2584,7 +2584,7 @@ TEST_CASE("iterators")
SECTION("const json + cbegin/cend") 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.cend());
CHECK(*it == j_const); CHECK(*it == j_const);
@ -2615,7 +2615,7 @@ TEST_CASE("iterators")
SECTION("json + begin/end") SECTION("json + begin/end")
{ {
auto it = j.begin(); json::iterator it = j.begin();
CHECK(it != j.end()); CHECK(it != j.end());
CHECK(*it == j); CHECK(*it == j);
@ -2640,7 +2640,7 @@ TEST_CASE("iterators")
SECTION("const json + begin/end") 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.end());
CHECK(*it == j_const); CHECK(*it == j_const);
@ -2665,7 +2665,7 @@ TEST_CASE("iterators")
SECTION("json + cbegin/cend") SECTION("json + cbegin/cend")
{ {
auto it = j.cbegin(); json::const_iterator it = j.cbegin();
CHECK(it != j.cend()); CHECK(it != j.cend());
CHECK(*it == j); CHECK(*it == j);
@ -2690,7 +2690,7 @@ TEST_CASE("iterators")
SECTION("const json + cbegin/cend") 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.cend());
CHECK(*it == j_const); CHECK(*it == j_const);
@ -2721,25 +2721,25 @@ TEST_CASE("iterators")
SECTION("json + begin/end") SECTION("json + begin/end")
{ {
auto it = j.begin(); json::iterator it = j.begin();
CHECK(it == j.end()); CHECK(it == j.end());
} }
SECTION("const json + begin/end") 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.end());
} }
SECTION("json + cbegin/cend") SECTION("json + cbegin/cend")
{ {
auto it = j.cbegin(); json::const_iterator it = j.cbegin();
CHECK(it == j.cend()); CHECK(it == j.cend());
} }
SECTION("const json + cbegin/cend") 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.cend());
} }
} }