mirror of
https://github.com/nlohmann/json
synced 2024-11-26 22:00:07 +00:00
add detail/iterators/iteration_proxy.hpp
This commit is contained in:
parent
bf06cf6c22
commit
5fc9ef2b90
3
Makefile
3
Makefile
@ -15,7 +15,8 @@ SRCS = ${SRCDIR}/json.hpp \
|
|||||||
${SRCDIR}/detail/parsing/parser.hpp \
|
${SRCDIR}/detail/parsing/parser.hpp \
|
||||||
${SRCDIR}/detail/iterators/primitive_iterator.hpp \
|
${SRCDIR}/detail/iterators/primitive_iterator.hpp \
|
||||||
${SRCDIR}/detail/iterators/internal_iterator.hpp \
|
${SRCDIR}/detail/iterators/internal_iterator.hpp \
|
||||||
${SRCDIR}/detail/iterators/iter_impl.hpp
|
${SRCDIR}/detail/iterators/iter_impl.hpp \
|
||||||
|
${SRCDIR}/detail/iterators/iteration_proxy.hpp
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
99
src/detail/iterators/iteration_proxy.hpp
Normal file
99
src/detail/iterators/iteration_proxy.hpp
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
#ifndef NLOHMANN_JSON_DETAIL_ITERATORS_ITERATION_PROXY_HPP
|
||||||
|
#define NLOHMANN_JSON_DETAIL_ITERATORS_ITERATION_PROXY_HPP
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace nlohmann
|
||||||
|
{
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
/// proxy class for the iterator_wrapper functions
|
||||||
|
template<typename IteratorType> class iteration_proxy
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
/// helper class for iteration
|
||||||
|
class iteration_proxy_internal
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
/// the iterator
|
||||||
|
IteratorType anchor;
|
||||||
|
/// an index for arrays (used to create key names)
|
||||||
|
std::size_t array_index = 0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit iteration_proxy_internal(IteratorType it) noexcept : anchor(it) {}
|
||||||
|
|
||||||
|
/// dereference operator (needed for range-based for)
|
||||||
|
iteration_proxy_internal& operator*()
|
||||||
|
{
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// increment operator (needed for range-based for)
|
||||||
|
iteration_proxy_internal& operator++()
|
||||||
|
{
|
||||||
|
++anchor;
|
||||||
|
++array_index;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// inequality operator (needed for range-based for)
|
||||||
|
bool operator!=(const iteration_proxy_internal& o) const noexcept
|
||||||
|
{
|
||||||
|
return anchor != o.anchor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// return key of the iterator
|
||||||
|
std::string key() const
|
||||||
|
{
|
||||||
|
assert(anchor.m_object != nullptr);
|
||||||
|
|
||||||
|
switch (anchor.m_object->type())
|
||||||
|
{
|
||||||
|
// use integer array index as key
|
||||||
|
case value_t::array:
|
||||||
|
return std::to_string(array_index);
|
||||||
|
|
||||||
|
// use key from the object
|
||||||
|
case value_t::object:
|
||||||
|
return anchor.key();
|
||||||
|
|
||||||
|
// use an empty key for all primitive types
|
||||||
|
default:
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// return value of the iterator
|
||||||
|
typename IteratorType::reference value() const
|
||||||
|
{
|
||||||
|
return anchor.value();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/// the container to iterate
|
||||||
|
typename IteratorType::reference container;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// construct iteration proxy from a container
|
||||||
|
explicit iteration_proxy(typename IteratorType::reference cont)
|
||||||
|
: container(cont) {}
|
||||||
|
|
||||||
|
/// return iterator begin (needed for range-based for)
|
||||||
|
iteration_proxy_internal begin() noexcept
|
||||||
|
{
|
||||||
|
return iteration_proxy_internal(container.begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// return iterator end (needed for range-based for)
|
||||||
|
iteration_proxy_internal end() noexcept
|
||||||
|
{
|
||||||
|
return iteration_proxy_internal(container.end());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
87
src/json.hpp
87
src/json.hpp
@ -63,6 +63,7 @@ SOFTWARE.
|
|||||||
#include "detail/iterators/primitive_iterator.hpp"
|
#include "detail/iterators/primitive_iterator.hpp"
|
||||||
#include "detail/iterators/internal_iterator.hpp"
|
#include "detail/iterators/internal_iterator.hpp"
|
||||||
#include "detail/iterators/iter_impl.hpp"
|
#include "detail/iterators/iter_impl.hpp"
|
||||||
|
#include "detail/iterators/iteration_proxy.hpp"
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief namespace for Niels Lohmann
|
@brief namespace for Niels Lohmann
|
||||||
@ -77,92 +78,6 @@ namespace detail
|
|||||||
// iterators //
|
// iterators //
|
||||||
///////////////
|
///////////////
|
||||||
|
|
||||||
/// proxy class for the iterator_wrapper functions
|
|
||||||
template<typename IteratorType> class iteration_proxy
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
/// helper class for iteration
|
|
||||||
class iteration_proxy_internal
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
/// the iterator
|
|
||||||
IteratorType anchor;
|
|
||||||
/// an index for arrays (used to create key names)
|
|
||||||
std::size_t array_index = 0;
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit iteration_proxy_internal(IteratorType it) noexcept : anchor(it) {}
|
|
||||||
|
|
||||||
/// dereference operator (needed for range-based for)
|
|
||||||
iteration_proxy_internal& operator*()
|
|
||||||
{
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// increment operator (needed for range-based for)
|
|
||||||
iteration_proxy_internal& operator++()
|
|
||||||
{
|
|
||||||
++anchor;
|
|
||||||
++array_index;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// inequality operator (needed for range-based for)
|
|
||||||
bool operator!=(const iteration_proxy_internal& o) const noexcept
|
|
||||||
{
|
|
||||||
return anchor != o.anchor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// return key of the iterator
|
|
||||||
std::string key() const
|
|
||||||
{
|
|
||||||
assert(anchor.m_object != nullptr);
|
|
||||||
|
|
||||||
switch (anchor.m_object->type())
|
|
||||||
{
|
|
||||||
// use integer array index as key
|
|
||||||
case value_t::array:
|
|
||||||
return std::to_string(array_index);
|
|
||||||
|
|
||||||
// use key from the object
|
|
||||||
case value_t::object:
|
|
||||||
return anchor.key();
|
|
||||||
|
|
||||||
// use an empty key for all primitive types
|
|
||||||
default:
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// return value of the iterator
|
|
||||||
typename IteratorType::reference value() const
|
|
||||||
{
|
|
||||||
return anchor.value();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/// the container to iterate
|
|
||||||
typename IteratorType::reference container;
|
|
||||||
|
|
||||||
public:
|
|
||||||
/// construct iteration proxy from a container
|
|
||||||
explicit iteration_proxy(typename IteratorType::reference cont)
|
|
||||||
: container(cont) {}
|
|
||||||
|
|
||||||
/// return iterator begin (needed for range-based for)
|
|
||||||
iteration_proxy_internal begin() noexcept
|
|
||||||
{
|
|
||||||
return iteration_proxy_internal(container.begin());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// return iterator end (needed for range-based for)
|
|
||||||
iteration_proxy_internal end() noexcept
|
|
||||||
{
|
|
||||||
return iteration_proxy_internal(container.end());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief a template for a reverse iterator class
|
@brief a template for a reverse iterator class
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user