| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #pragma once
- #include <unordered_map>
- #include <vector>
- #include <jvalidate/adapter.h>
- #include <jvalidate/detail/array_iterator.h>
- #include <jvalidate/detail/object_iterator.h>
- #include <jvalidate/forward.h>
- namespace jvalidate::adapter::detail {
- template <typename JSON, typename Adapter = AdapterFor<JSON>> class SimpleObjectAdapter {
- public:
- using underlying_iterator_t = decltype(std::declval<JSON>().begin());
- using const_iterator = JsonObjectIterator<underlying_iterator_t, Adapter>;
- SimpleObjectAdapter(JSON * value) : value_(value) {}
- size_t size() const { return value_ ? value_->size() : 0; }
- const_iterator find(std::string const & key) const {
- return std::find_if(begin(), end(), [key](auto const & kv) { return kv.first == key; });
- }
- bool contains(std::string const & key) const { return find(key) != end(); }
- Adapter operator[](std::string const & key) const {
- auto it = find(key);
- return it != end() ? it->second : Adapter();
- }
- const_iterator begin() const {
- return value_ ? const_iterator(value_->begin()) : const_iterator();
- }
- const_iterator end() const { return value_ ? const_iterator(value_->end()) : const_iterator(); }
- std::map<std::string_view, Adapter> operator*() const {
- using C = std::map<std::string_view, Adapter>;
- return value_ ? C(begin(), end()) : C();
- }
- protected:
- JSON * value() const { return value_; }
- JSON const & const_value() const { return value_ ? *value_ : AdapterTraits<JSON>::const_empty(); }
- private:
- JSON * value_;
- };
- template <typename JSON, typename Adapter = AdapterFor<JSON>> class SimpleArrayAdapter {
- public:
- using underlying_iterator_t = decltype(std::declval<JSON>().begin());
- using const_iterator = JsonArrayIterator<underlying_iterator_t, Adapter>;
- SimpleArrayAdapter(JSON * value) : value_(value) {}
- size_t size() const { return value_ ? value_->size() : 0; }
- Adapter operator[](size_t index) const {
- if (index > size()) {
- return Adapter();
- }
- auto it = begin();
- std::advance(it, index);
- return *it;
- }
- const_iterator begin() const {
- return value_ ? const_iterator(value_->begin()) : const_iterator();
- }
- const_iterator end() const { return value_ ? const_iterator(value_->end()) : const_iterator(); }
- std::vector<Adapter> operator*() const {
- using C = std::vector<Adapter>;
- return value_ ? C(begin(), end()) : C();
- }
- protected:
- JSON * value() const { return value_; }
- JSON const & const_value() const { return value_ ? *value_ : AdapterTraits<JSON>::const_empty(); }
- private:
- JSON * value_;
- };
- template <typename JSON, typename CRTP = AdapterFor<JSON>> class SimpleAdapter : public Adapter {
- public:
- using value_type = std::remove_const_t<JSON>;
- public:
- SimpleAdapter(JSON * value) : value_(value) {}
- SimpleAdapter(JSON & value) : value_(&value) {}
- size_t array_size() const { return self().as_array().size(); }
- CRTP operator[](size_t index) const { return self().as_array()[index]; }
- detail::SimpleArrayAdapter<JSON> as_array() const { return value_; }
- bool apply_array(AdapterCallback const & cb) const final {
- bool result = true;
- for (auto const & child : self().as_array()) {
- result = cb(child) && result;
- }
- return result;
- }
- size_t object_size() const { return self().as_object().size(); }
- bool contains(std::string const & key) const { return self().as_object().contains(key); }
- CRTP operator[](std::string const & key) const { return self().as_object()[key]; }
- detail::SimpleObjectAdapter<JSON, CRTP> as_object() const { return value_; }
- bool apply_object(ObjectAdapterCallback const & cb) const final {
- bool result = true;
- for (auto const & [key, child] : self().as_object()) {
- result = cb(key, child) && result;
- }
- return result;
- }
- std::unique_ptr<Const const> freeze() const final {
- return std::make_unique<GenericConst<value_type>>(const_value());
- }
- protected:
- JSON * value() const { return value_; }
- JSON const & const_value() const { return value_ ? *value_ : AdapterTraits<JSON>::const_empty(); }
- private:
- CRTP const & self() const { return static_cast<CRTP const &>(*this); }
- private:
- JSON * value_;
- };
- }
|