json_direct_binder.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // json_direct_binder.hpp
  3. // json
  4. //
  5. // Created by Sam Jaffe on 4/23/16.
  6. //
  7. #pragma once
  8. #include "json_binder.hpp"
  9. namespace json { namespace binder {
  10. template <typename T, typename E, typename = void> 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) : ptr(p), impl(i) {}
  15. virtual binder_impl<T> * clone() const override {
  16. return new forward_binder(*this);
  17. }
  18. virtual void parse(T & val, char const *& data,
  19. parser::options opts) const override {
  20. impl.parse(val.*ptr, data, opts);
  21. }
  22. virtual void write(T const & val, std::ostream & data) const override {
  23. impl.write(val.*ptr, data);
  24. }
  25. private:
  26. E T::*ptr;
  27. binder<E> impl;
  28. };
  29. template <typename T> 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,
  41. parser::options opts) const override {
  42. detail tmp;
  43. impl.parse(tmp, data, opts);
  44. val = std::move(tmp.value);
  45. }
  46. virtual void write(T const & val, std::ostream & data) const override {
  47. impl.write({val}, data);
  48. }
  49. private:
  50. direct_binder<detail, T> impl;
  51. };
  52. }}