json_binder.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // json_binder.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 1/31/16.
  6. // Copyright © 2016 Sam Jaffe. All rights reserved.
  7. //
  8. #ifndef json_binder_h
  9. #define json_binder_h
  10. #pragma once
  11. #include "json_common.hpp"
  12. #include <map>
  13. #include <string>
  14. #include <sstream>
  15. #include <vector>
  16. namespace json {
  17. namespace binder {
  18. template <typename T>
  19. class binder_impl {
  20. public:
  21. virtual binder_impl<T>* clone() const = 0;
  22. virtual ~binder_impl() {}
  23. virtual void parse(T&, char const*&) const = 0;
  24. virtual void write(T const&, std::ostream &) const = 0;
  25. };
  26. template <typename T>
  27. class binder {
  28. public:
  29. binder() :
  30. impl(nullptr) {
  31. }
  32. binder(binder const& other) :
  33. impl(other.impl->clone()) {
  34. }
  35. explicit binder(binder_impl<T> const* p) :
  36. impl(p) {
  37. }
  38. explicit binder(binder_impl<T> const& r) :
  39. impl(r.clone()) {
  40. }
  41. ~binder() {
  42. if (impl) {
  43. delete impl;
  44. impl = nullptr;
  45. }
  46. }
  47. void parse(T& object, char const*& data) const {
  48. if (!impl) return;
  49. impl->parse(object, data);
  50. }
  51. void write(T const& object, std::ostream & data) const {
  52. if (!impl) return;
  53. impl->write(object, data);
  54. }
  55. private:
  56. binder_impl<T> const* impl;
  57. };
  58. template <typename T, typename S = T>
  59. class visitor {
  60. public:
  61. visitor(S& o, binder_impl<T>& b) : obj(o), b(b) {}
  62. void parse(char const* data) {
  63. b.parse(obj, data);
  64. }
  65. void write(std::ostream & data) const {
  66. b.write(obj, data);
  67. }
  68. private:
  69. S& obj;
  70. binder_impl<T>& b;
  71. };
  72. template <typename T>
  73. visitor<T> bind(T& object, binder_impl<T>& b) {
  74. return visitor<T>{object, b};
  75. }
  76. }
  77. namespace parser {
  78. template <typename T>
  79. void parse(binder::visitor<T>& visitor, char const* data) {
  80. visitor.parse(data);
  81. }
  82. template <typename T, typename S>
  83. void write(binder::visitor<T, S> const & visitor, std::ostream & out) {
  84. visitor.write(out);
  85. }
  86. template <typename T, typename S>
  87. void write(binder::visitor<T, S> const & visitor, std::string & data) {
  88. std::stringstream ss;
  89. visitor.write(ss);
  90. data = ss.str();
  91. }
  92. }
  93. }
  94. #include "json/json_binder_parser.hpp"
  95. #include "json/json_direct_binder.hpp"
  96. #include "json/json_tuple_binder.hpp"
  97. #include "json/json_object_binder.hpp"
  98. #include "json/json_direct_map_binder.hpp"
  99. #include "json/json_direct_scalar_binder.hpp"
  100. #include "json/json_direct_vector_binder.hpp"
  101. #endif /* json_binder_h */