json_direct_binder.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. template <typename... Args>
  36. value_binder(Args &&... args) : impl(&detail::value, args...) {}
  37. virtual binder_impl<T>* clone() const override {
  38. return new value_binder(*this);
  39. }
  40. virtual void parse(T & val, char const *&data, parser::options opts) const override {
  41. detail tmp;
  42. impl.parse(tmp, data, opts);
  43. val = std::move(tmp.value);
  44. }
  45. virtual void write(T const& val, std::ostream & data) const override {
  46. impl.write({val}, data);
  47. }
  48. private:
  49. direct_binder<detail, T> impl;
  50. };
  51. } }