Commit bbc45eb4 authored by Niels's avatar Niels

overworked iterators

parent 76be1ae1
This diff is collapsed.
This diff is collapsed.
...@@ -42,11 +42,12 @@ due to alignment. ...@@ -42,11 +42,12 @@ due to alignment.
*/ */
class json class json
{ {
public: private:
// forward declaration to friend this class // forward declaration to friend this class
class iterator; class iterator;
class const_iterator; class const_iterator;
public:
// container types // container types
using value_type = json; using value_type = json;
using reference = json&; using reference = json&;
...@@ -371,15 +372,15 @@ class json ...@@ -371,15 +372,15 @@ class json
/// the payload /// the payload
value value_ {}; value value_ {};
public: private:
/// an iterator /// an iterator
class iterator : public std::iterator<std::forward_iterator_tag, json> class iterator : private std::iterator<std::forward_iterator_tag, json>
{ {
friend class json; friend class json;
friend class json::const_iterator; friend class json::const_iterator;
public: public:
iterator() = default; iterator() = default;
iterator(json*); iterator(json*, bool);
iterator(const iterator&); iterator(const iterator&);
~iterator(); ~iterator();
...@@ -402,16 +403,18 @@ class json ...@@ -402,16 +403,18 @@ class json
array_t::iterator* vi_ = nullptr; array_t::iterator* vi_ = nullptr;
/// an iterator for JSON objects /// an iterator for JSON objects
object_t::iterator* oi_ = nullptr; object_t::iterator* oi_ = nullptr;
/// whether iterator points to a valid object
bool invalid = true;
}; };
/// a const iterator /// a const iterator
class const_iterator : public std::iterator<std::forward_iterator_tag, const json> class const_iterator : private std::iterator<std::forward_iterator_tag, const json>
{ {
friend class json; friend class json;
public: public:
const_iterator() = default; const_iterator() = default;
const_iterator(const json*); const_iterator(const json*, bool);
const_iterator(const const_iterator&); const_iterator(const const_iterator&);
const_iterator(const json::iterator&); const_iterator(const json::iterator&);
~const_iterator(); ~const_iterator();
...@@ -435,6 +438,8 @@ class json ...@@ -435,6 +438,8 @@ class json
array_t::const_iterator* vi_ = nullptr; array_t::const_iterator* vi_ = nullptr;
/// an iterator for JSON objects /// an iterator for JSON objects
object_t::const_iterator* oi_ = nullptr; object_t::const_iterator* oi_ = nullptr;
/// whether iterator reached past the end
bool invalid = true;
}; };
private: private:
......
...@@ -1371,35 +1371,35 @@ TEST_CASE("Iterators") ...@@ -1371,35 +1371,35 @@ TEST_CASE("Iterators")
CHECK(* j7.begin() == json("hello")); CHECK(* j7.begin() == json("hello"));
CHECK(* j7_const.begin() == json("hello")); CHECK(* j7_const.begin() == json("hello"));
CHECK_THROWS_AS(* j1.end(), std::runtime_error); CHECK_THROWS_AS(* j1.end(), std::out_of_range);
CHECK_THROWS_AS(* j1.cend(), std::runtime_error); CHECK_THROWS_AS(* j1.cend(), std::out_of_range);
CHECK_THROWS_AS(* j2.end(), std::runtime_error); CHECK_THROWS_AS(* j2.end(), std::out_of_range);
CHECK_THROWS_AS(* j2.cend(), std::runtime_error); CHECK_THROWS_AS(* j2.cend(), std::out_of_range);
CHECK_THROWS_AS(* j3.end(), std::runtime_error); CHECK_THROWS_AS(* j3.end(), std::out_of_range);
CHECK_THROWS_AS(* j3.cend(), std::runtime_error); CHECK_THROWS_AS(* j3.cend(), std::out_of_range);
CHECK_THROWS_AS(* j4.end(), std::runtime_error); CHECK_THROWS_AS(* j4.end(), std::out_of_range);
CHECK_THROWS_AS(* j4.cend(), std::runtime_error); CHECK_THROWS_AS(* j4.cend(), std::out_of_range);
CHECK_THROWS_AS(* j5.end(), std::runtime_error); CHECK_THROWS_AS(* j5.end(), std::out_of_range);
CHECK_THROWS_AS(* j5.cend(), std::runtime_error); CHECK_THROWS_AS(* j5.cend(), std::out_of_range);
CHECK_THROWS_AS(* j6.end(), std::runtime_error); CHECK_THROWS_AS(* j6.end(), std::out_of_range);
CHECK_THROWS_AS(* j6.cend(), std::runtime_error); CHECK_THROWS_AS(* j6.cend(), std::out_of_range);
CHECK_THROWS_AS(* j7.end(), std::runtime_error); CHECK_THROWS_AS(* j7.end(), std::out_of_range);
CHECK_THROWS_AS(* j7.cend(), std::runtime_error); CHECK_THROWS_AS(* j7.cend(), std::out_of_range);
CHECK_THROWS_AS(* j1_const.end(), std::runtime_error); CHECK_THROWS_AS(* j1_const.end(), std::out_of_range);
CHECK_THROWS_AS(* j1_const.cend(), std::runtime_error); CHECK_THROWS_AS(* j1_const.cend(), std::out_of_range);
CHECK_THROWS_AS(* j2_const.end(), std::runtime_error); CHECK_THROWS_AS(* j2_const.end(), std::out_of_range);
CHECK_THROWS_AS(* j2_const.cend(), std::runtime_error); CHECK_THROWS_AS(* j2_const.cend(), std::out_of_range);
CHECK_THROWS_AS(* j3_const.end(), std::runtime_error); CHECK_THROWS_AS(* j3_const.end(), std::out_of_range);
CHECK_THROWS_AS(* j3_const.cend(), std::runtime_error); CHECK_THROWS_AS(* j3_const.cend(), std::out_of_range);
CHECK_THROWS_AS(* j4_const.end(), std::runtime_error); CHECK_THROWS_AS(* j4_const.end(), std::out_of_range);
CHECK_THROWS_AS(* j4_const.cend(), std::runtime_error); CHECK_THROWS_AS(* j4_const.cend(), std::out_of_range);
CHECK_THROWS_AS(* j5_const.end(), std::runtime_error); CHECK_THROWS_AS(* j5_const.end(), std::out_of_range);
CHECK_THROWS_AS(* j5_const.cend(), std::runtime_error); CHECK_THROWS_AS(* j5_const.cend(), std::out_of_range);
CHECK_THROWS_AS(* j6_const.end(), std::runtime_error); CHECK_THROWS_AS(* j6_const.end(), std::out_of_range);
CHECK_THROWS_AS(* j6_const.cend(), std::runtime_error); CHECK_THROWS_AS(* j6_const.cend(), std::out_of_range);
CHECK_THROWS_AS(* j7_const.end(), std::runtime_error); CHECK_THROWS_AS(* j7_const.end(), std::out_of_range);
CHECK_THROWS_AS(* j7_const.cend(), std::runtime_error); CHECK_THROWS_AS(* j7_const.cend(), std::out_of_range);
// operator -> // operator ->
CHECK(j1.begin()->type() == json::value_t::number); CHECK(j1.begin()->type() == json::value_t::number);
...@@ -1432,35 +1432,35 @@ TEST_CASE("Iterators") ...@@ -1432,35 +1432,35 @@ TEST_CASE("Iterators")
CHECK(j7_const.begin()->type() == json::value_t::string); CHECK(j7_const.begin()->type() == json::value_t::string);
CHECK(j7_const.cbegin()->type() == json::value_t::string); CHECK(j7_const.cbegin()->type() == json::value_t::string);
CHECK_THROWS_AS(j1.end()->type(), std::runtime_error); CHECK_THROWS_AS(j1.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j1.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j1.cend()->type(), std::out_of_range);
CHECK_THROWS_AS(j2.end()->type(), std::runtime_error); CHECK_THROWS_AS(j2.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j2.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j2.cend()->type(), std::out_of_range);
CHECK_THROWS_AS(j3.end()->type(), std::runtime_error); CHECK_THROWS_AS(j3.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j3.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j3.cend()->type(), std::out_of_range);
CHECK_THROWS_AS(j4.end()->type(), std::runtime_error); CHECK_THROWS_AS(j4.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j4.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j4.cend()->type(), std::out_of_range);
CHECK_THROWS_AS(j5.end()->type(), std::runtime_error); CHECK_THROWS_AS(j5.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j5.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j5.cend()->type(), std::out_of_range);
CHECK_THROWS_AS(j6.end()->type(), std::runtime_error); CHECK_THROWS_AS(j6.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j6.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j6.cend()->type(), std::out_of_range);
CHECK_THROWS_AS(j7.end()->type(), std::runtime_error); CHECK_THROWS_AS(j7.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j7.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j7.cend()->type(), std::out_of_range);
CHECK_THROWS_AS(j1_const.end()->type(), std::runtime_error); CHECK_THROWS_AS(j1_const.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j1_const.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j1_const.cend()->type(), std::out_of_range);
CHECK_THROWS_AS(j2_const.end()->type(), std::runtime_error); CHECK_THROWS_AS(j2_const.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j2_const.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j2_const.cend()->type(), std::out_of_range);
CHECK_THROWS_AS(j3_const.end()->type(), std::runtime_error); CHECK_THROWS_AS(j3_const.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j3_const.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j3_const.cend()->type(), std::out_of_range);
CHECK_THROWS_AS(j4_const.end()->type(), std::runtime_error); CHECK_THROWS_AS(j4_const.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j4_const.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j4_const.cend()->type(), std::out_of_range);
CHECK_THROWS_AS(j5_const.end()->type(), std::runtime_error); CHECK_THROWS_AS(j5_const.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j5_const.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j5_const.cend()->type(), std::out_of_range);
CHECK_THROWS_AS(j6_const.end()->type(), std::runtime_error); CHECK_THROWS_AS(j6_const.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j6_const.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j6_const.cend()->type(), std::out_of_range);
CHECK_THROWS_AS(j7_const.end()->type(), std::runtime_error); CHECK_THROWS_AS(j7_const.end()->type(), std::out_of_range);
CHECK_THROWS_AS(j7_const.cend()->type(), std::runtime_error); CHECK_THROWS_AS(j7_const.cend()->type(), std::out_of_range);
// value // value
CHECK(j1.begin().value().type() == json::value_t::number); CHECK(j1.begin().value().type() == json::value_t::number);
......
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