提交 5e89cb0a 编写于 作者: N Niels

implemented key()/value() for reverse iterators

上级 47ea1c10
...@@ -96,6 +96,8 @@ class basic_json ...@@ -96,6 +96,8 @@ class basic_json
// forward declarations // forward declarations
class iterator; class iterator;
class const_iterator; class const_iterator;
class reverse_iterator;
class const_reverse_iterator;
/*! /*!
@brief the type of elements in a basic_json container @brief the type of elements in a basic_json container
...@@ -151,13 +153,13 @@ class basic_json ...@@ -151,13 +153,13 @@ class basic_json
@brief a reverse iterator for a basic_json container @brief a reverse iterator for a basic_json container
@ingroup reversiblecontainer @ingroup reversiblecontainer
*/ */
using reverse_iterator = std::reverse_iterator<iterator>; using reverse_iterator = basic_json::reverse_iterator;
/*! /*!
@brief a const reverse iterator for a basic_json container @brief a const reverse iterator for a basic_json container
@ingroup reversiblecontainer @ingroup reversiblecontainer
*/ */
using const_reverse_iterator = std::reverse_iterator<const_iterator>; using const_reverse_iterator = basic_json::const_reverse_iterator;
/// returns the allocator associated with the container /// returns the allocator associated with the container
...@@ -2561,6 +2563,7 @@ class basic_json ...@@ -2561,6 +2563,7 @@ class basic_json
} }
} }
/// return the key of an object iterator
inline typename object_t::key_type key() const inline typename object_t::key_type key() const
{ {
switch (m_object->m_type) switch (m_object->m_type)
...@@ -2577,6 +2580,7 @@ class basic_json ...@@ -2577,6 +2580,7 @@ class basic_json
} }
} }
/// return the key of an iterator
inline reference value() inline reference value()
{ {
return operator*(); return operator*();
...@@ -3096,6 +3100,7 @@ class basic_json ...@@ -3096,6 +3100,7 @@ class basic_json
} }
} }
/// return the key of an object iterator
inline typename object_t::key_type key() const inline typename object_t::key_type key() const
{ {
switch (m_object->m_type) switch (m_object->m_type)
...@@ -3112,6 +3117,7 @@ class basic_json ...@@ -3112,6 +3117,7 @@ class basic_json
} }
} }
/// return the value of an iterator
inline reference value() const inline reference value() const
{ {
return operator*(); return operator*();
...@@ -3124,6 +3130,48 @@ class basic_json ...@@ -3124,6 +3130,48 @@ class basic_json
internal_iterator<typename array_t::const_iterator, typename object_t::const_iterator> m_it; internal_iterator<typename array_t::const_iterator, typename object_t::const_iterator> m_it;
}; };
/// a reverse random access iterator for the basic_json class
class reverse_iterator : private std::reverse_iterator<basic_json::iterator>
{
public:
inline reverse_iterator(const typename std::reverse_iterator<basic_json::iterator>::iterator_type&
it)
: std::reverse_iterator<basic_json::iterator>(it) {}
/// return the key of an object iterator
inline typename object_t::key_type key() const
{
return this->base().key();
}
/// return the value of an iterator
inline reference value() const
{
return this->base().operator * ();
}
};
/// a const reverse random access iterator for the basic_json class
class const_reverse_iterator : private std::reverse_iterator<basic_json::const_iterator>
{
public:
inline const_reverse_iterator(const typename
std::reverse_iterator<basic_json::const_iterator>::iterator_type& it)
: std::reverse_iterator<basic_json::const_iterator>(it) {}
/// return the key of an object iterator
inline typename object_t::key_type key() const
{
return this->base().key();
}
/// return the value of an iterator
inline reference value() const
{
return this->base().operator * ();
}
};
private: private:
////////////////////// //////////////////////
......
...@@ -96,6 +96,8 @@ class basic_json ...@@ -96,6 +96,8 @@ class basic_json
// forward declarations // forward declarations
class iterator; class iterator;
class const_iterator; class const_iterator;
class reverse_iterator;
class const_reverse_iterator;
/*! /*!
@brief the type of elements in a basic_json container @brief the type of elements in a basic_json container
...@@ -151,13 +153,13 @@ class basic_json ...@@ -151,13 +153,13 @@ class basic_json
@brief a reverse iterator for a basic_json container @brief a reverse iterator for a basic_json container
@ingroup reversiblecontainer @ingroup reversiblecontainer
*/ */
using reverse_iterator = std::reverse_iterator<iterator>; using reverse_iterator = basic_json::reverse_iterator;
/*! /*!
@brief a const reverse iterator for a basic_json container @brief a const reverse iterator for a basic_json container
@ingroup reversiblecontainer @ingroup reversiblecontainer
*/ */
using const_reverse_iterator = std::reverse_iterator<const_iterator>; using const_reverse_iterator = basic_json::const_reverse_iterator;
/// returns the allocator associated with the container /// returns the allocator associated with the container
...@@ -2561,6 +2563,7 @@ class basic_json ...@@ -2561,6 +2563,7 @@ class basic_json
} }
} }
/// return the key of an object iterator
inline typename object_t::key_type key() const inline typename object_t::key_type key() const
{ {
switch (m_object->m_type) switch (m_object->m_type)
...@@ -2577,6 +2580,7 @@ class basic_json ...@@ -2577,6 +2580,7 @@ class basic_json
} }
} }
/// return the key of an iterator
inline reference value() inline reference value()
{ {
return operator*(); return operator*();
...@@ -3096,6 +3100,7 @@ class basic_json ...@@ -3096,6 +3100,7 @@ class basic_json
} }
} }
/// return the key of an object iterator
inline typename object_t::key_type key() const inline typename object_t::key_type key() const
{ {
switch (m_object->m_type) switch (m_object->m_type)
...@@ -3112,6 +3117,7 @@ class basic_json ...@@ -3112,6 +3117,7 @@ class basic_json
} }
} }
/// return the value of an iterator
inline reference value() const inline reference value() const
{ {
return operator*(); return operator*();
...@@ -3124,6 +3130,48 @@ class basic_json ...@@ -3124,6 +3130,48 @@ class basic_json
internal_iterator<typename array_t::const_iterator, typename object_t::const_iterator> m_it; internal_iterator<typename array_t::const_iterator, typename object_t::const_iterator> m_it;
}; };
/// a reverse random access iterator for the basic_json class
class reverse_iterator : private std::reverse_iterator<basic_json::iterator>
{
public:
inline reverse_iterator(const typename std::reverse_iterator<basic_json::iterator>::iterator_type&
it)
: std::reverse_iterator<basic_json::iterator>(it) {}
/// return the key of an object iterator
inline typename object_t::key_type key() const
{
return this->base().key();
}
/// return the value of an iterator
inline reference value() const
{
return this->base().operator * ();
}
};
/// a const reverse random access iterator for the basic_json class
class const_reverse_iterator : private std::reverse_iterator<basic_json::const_iterator>
{
public:
inline const_reverse_iterator(const typename
std::reverse_iterator<basic_json::const_iterator>::iterator_type& it)
: std::reverse_iterator<basic_json::const_iterator>(it) {}
/// return the key of an object iterator
inline typename object_t::key_type key() const
{
return this->base().key();
}
/// return the value of an iterator
inline reference value() const
{
return this->base().operator * ();
}
};
private: private:
////////////////////// //////////////////////
......
...@@ -2810,6 +2810,13 @@ TEST_CASE("iterators") ...@@ -2810,6 +2810,13 @@ TEST_CASE("iterators")
CHECK(it.value() == json(true)); CHECK(it.value() == json(true));
CHECK_THROWS_AS(cit.key(), std::domain_error); CHECK_THROWS_AS(cit.key(), std::domain_error);
CHECK(cit.value() == json(true)); CHECK(cit.value() == json(true));
auto rit = j.rend();
auto crit = j.rend();
CHECK_THROWS_AS(rit.key(), std::domain_error);
CHECK(rit.value() == json(true));
CHECK_THROWS_AS(crit.key(), std::domain_error);
CHECK(crit.value() == json(true));
} }
} }
...@@ -3001,6 +3008,13 @@ TEST_CASE("iterators") ...@@ -3001,6 +3008,13 @@ TEST_CASE("iterators")
CHECK(it.value() == json("hello world")); CHECK(it.value() == json("hello world"));
CHECK_THROWS_AS(cit.key(), std::domain_error); CHECK_THROWS_AS(cit.key(), std::domain_error);
CHECK(cit.value() == json("hello world")); CHECK(cit.value() == json("hello world"));
auto rit = j.rend();
auto crit = j.rend();
CHECK_THROWS_AS(rit.key(), std::domain_error);
CHECK(rit.value() == json("hello world"));
CHECK_THROWS_AS(crit.key(), std::domain_error);
CHECK(crit.value() == json("hello world"));
} }
} }
...@@ -3185,6 +3199,13 @@ TEST_CASE("iterators") ...@@ -3185,6 +3199,13 @@ TEST_CASE("iterators")
CHECK(it.value() == json(1)); CHECK(it.value() == json(1));
CHECK_THROWS_AS(cit.key(), std::domain_error); CHECK_THROWS_AS(cit.key(), std::domain_error);
CHECK(cit.value() == json(1)); CHECK(cit.value() == json(1));
auto rit = j.rend();
auto crit = j.rend();
CHECK_THROWS_AS(rit.key(), std::domain_error);
CHECK(rit.value() == json(1));
CHECK_THROWS_AS(crit.key(), std::domain_error);
CHECK(crit.value() == json(1));
} }
} }
...@@ -3369,6 +3390,13 @@ TEST_CASE("iterators") ...@@ -3369,6 +3390,13 @@ TEST_CASE("iterators")
CHECK(it.value() == json(1)); CHECK(it.value() == json(1));
CHECK(cit.key() == "A"); CHECK(cit.key() == "A");
CHECK(cit.value() == json(1)); CHECK(cit.value() == json(1));
auto rit = j.rend();
auto crit = j.rend();
CHECK(rit.key() == "A");
CHECK(rit.value() == json(1));
CHECK(crit.key() == "A");
CHECK(crit.value() == json(1));
} }
} }
...@@ -3560,6 +3588,13 @@ TEST_CASE("iterators") ...@@ -3560,6 +3588,13 @@ TEST_CASE("iterators")
CHECK(it.value() == json(23)); CHECK(it.value() == json(23));
CHECK_THROWS_AS(cit.key(), std::domain_error); CHECK_THROWS_AS(cit.key(), std::domain_error);
CHECK(cit.value() == json(23)); CHECK(cit.value() == json(23));
auto rit = j.rend();
auto crit = j.rend();
CHECK_THROWS_AS(rit.key(), std::domain_error);
CHECK(rit.value() == json(23));
CHECK_THROWS_AS(crit.key(), std::domain_error);
CHECK(crit.value() == json(23));
} }
} }
...@@ -3751,6 +3786,13 @@ TEST_CASE("iterators") ...@@ -3751,6 +3786,13 @@ TEST_CASE("iterators")
CHECK(it.value() == json(23.42)); CHECK(it.value() == json(23.42));
CHECK_THROWS_AS(cit.key(), std::domain_error); CHECK_THROWS_AS(cit.key(), std::domain_error);
CHECK(cit.value() == json(23.42)); CHECK(cit.value() == json(23.42));
auto rit = j.rend();
auto crit = j.rend();
CHECK_THROWS_AS(rit.key(), std::domain_error);
CHECK(rit.value() == json(23.42));
CHECK_THROWS_AS(crit.key(), std::domain_error);
CHECK(crit.value() == json(23.42));
} }
} }
...@@ -3812,6 +3854,13 @@ TEST_CASE("iterators") ...@@ -3812,6 +3854,13 @@ TEST_CASE("iterators")
CHECK_THROWS_AS(it.value(), std::out_of_range); CHECK_THROWS_AS(it.value(), std::out_of_range);
CHECK_THROWS_AS(cit.key(), std::domain_error); CHECK_THROWS_AS(cit.key(), std::domain_error);
CHECK_THROWS_AS(cit.value(), std::out_of_range); CHECK_THROWS_AS(cit.value(), std::out_of_range);
auto rit = j.rend();
auto crit = j.rend();
CHECK_THROWS_AS(rit.key(), std::domain_error);
CHECK_THROWS_AS(rit.value(), std::out_of_range);
CHECK_THROWS_AS(crit.key(), std::domain_error);
CHECK_THROWS_AS(crit.value(), std::out_of_range);
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册