json_direct_binder.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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;
  11. template <typename T, typename E, typename = void>
  12. class forward_binder : public binder_impl<T> {
  13. public:
  14. forward_binder(E T::*p, binder<E> const& i) :
  15. ptr(p), impl(i) {
  16. }
  17. virtual binder_impl<T>* clone() const override {
  18. return new forward_binder(*this);
  19. }
  20. virtual void parse(T& val, char const*& data, parser::options opts) const override {
  21. impl.parse(val.*ptr, data, opts);
  22. }
  23. virtual void write(T const& val, std::ostream & data) const override {
  24. impl.write(val.*ptr, data);
  25. }
  26. private:
  27. E T::*ptr;
  28. binder<E> impl;
  29. };
  30. template <typename T>
  31. class value_binder : public binder_impl<T> {
  32. private:
  33. struct detail {
  34. T value;
  35. };
  36. public:
  37. template <typename... Args>
  38. value_binder(Args &&... args) : impl(&detail::value, args...) {}
  39. virtual binder_impl<T>* clone() const override {
  40. return new value_binder(*this);
  41. }
  42. virtual void parse(T & val, char const *&data, parser::options opts) const override {
  43. detail tmp;
  44. impl.parse(tmp, data, opts);
  45. val = std::move(tmp.value);
  46. }
  47. virtual void write(T const& val, std::ostream & data) const override {
  48. impl.write({val}, data);
  49. }
  50. private:
  51. direct_binder<detail, T> impl;
  52. };
  53. } }