json_binder.hpp 1015 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #pragma once
  2. #include "json/json_common.hpp"
  3. #include <iostream>
  4. namespace json { namespace binder {
  5. template <typename T>
  6. class binder_impl {
  7. public:
  8. virtual binder_impl<T>* clone() const = 0;
  9. virtual ~binder_impl() {}
  10. virtual void parse(T&, char const*&, parser::options) const = 0;
  11. virtual void write(T const&, std::ostream &) const = 0;
  12. };
  13. template <typename T>
  14. class binder {
  15. public:
  16. binder() : impl(nullptr) {}
  17. binder(binder const& other) : impl(other.impl->clone()) {}
  18. binder(binder_impl<T> const* p) : impl(p) {}
  19. binder(binder_impl<T> const& r) : impl(r.clone()) {}
  20. ~binder() { delete impl; }
  21. void parse(T& object, char const*& data, parser::options opts) const {
  22. if (!impl) return; // error?
  23. impl->parse(object, data, opts);
  24. }
  25. void write(T const& object, std::ostream & data) const {
  26. if (!impl) return; // error?
  27. impl->write(object, data);
  28. }
  29. private:
  30. binder_impl<T> const* impl;
  31. };
  32. } }