json_pointer_binder.hpp 854 B

1234567891011121314151617181920212223242526272829303132
  1. //
  2. // json_pointer_binder.hpp
  3. // json
  4. //
  5. // Created by Sam Jaffe on 11/15/18.
  6. // Copyright © 2018 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. namespace json { namespace binder {
  10. template <typename P>
  11. class pointer_binder : public binder_impl<P> {
  12. using T = typename std::remove_reference<decltype(*std::declval<P>())>::type;
  13. public:
  14. pointer_binder(binder<T> impl) : m_impl(impl) {}
  15. virtual binder_impl<P>* clone() const { return new pointer_binder(*this); }
  16. virtual void parse(P & v, char const*& data, parser::options opts) const {
  17. if (!strncmp(data, "null", 4)) v = nullptr;
  18. else m_impl.parse(*(v = P(new T)), data, opts);
  19. }
  20. void write(P const & v, std::ostream & os) const {
  21. if (v) m_impl.write(*v, os);
  22. else os << "null";
  23. }
  24. private:
  25. binder<T> m_impl;
  26. };
  27. } }