json_direct_binder.hpp 1.6 KB

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