json_direct_binder.hpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // json_direct_binder.hpp
  3. // json
  4. //
  5. // Created by Sam Jaffe on 4/23/16.
  6. //
  7. #pragma once
  8. namespace json { namespace binder {
  9. template <typename T, typename E, typename = void>
  10. class direct_binder : public binder_impl<T> {
  11. public:
  12. direct_binder(E T::*p, binder<E> const& i) :
  13. ptr(p), impl(i) {
  14. }
  15. virtual binder_impl<T>* clone() const override {
  16. return new direct_binder(*this);
  17. }
  18. virtual void parse(T& val, char const*& data, parser::options opts) const override {
  19. impl.parse(val.*ptr, data, opts);
  20. }
  21. virtual void write(T const& val, std::ostream & data) const override {
  22. impl.write(val.*ptr, data);
  23. }
  24. private:
  25. E T::*ptr;
  26. binder<E> impl;
  27. };
  28. template <typename T>
  29. class value_binder : public binder_impl<T> {
  30. private:
  31. struct detail {
  32. T value;
  33. };
  34. public:
  35. value_binder() : impl(&detail::value) {}
  36. virtual binder_impl<T>* clone() const override {
  37. return new value_binder(*this);
  38. }
  39. virtual void parse(T & val, char const *&data, parser::options opts) const override {
  40. detail tmp;
  41. impl.parse(tmp, data, opts);
  42. val = std::move(tmp.value);
  43. }
  44. virtual void write(T const& val, std::ostream & data) const override {
  45. impl.write({val}, data);
  46. }
  47. private:
  48. direct_binder<detail, T> impl;
  49. };
  50. } }