serialization.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef dap_serialization_h
  15. #define dap_serialization_h
  16. #include "typeof.h"
  17. #include "types.h"
  18. #include <cstddef> // ptrdiff_t
  19. #include <type_traits>
  20. namespace dap {
  21. // Field describes a single field of a struct.
  22. struct Field {
  23. std::string name; // name of the field
  24. ptrdiff_t offset; // offset of the field to the base of the struct
  25. const TypeInfo* type; // type of the field
  26. };
  27. ////////////////////////////////////////////////////////////////////////////////
  28. // Deserializer
  29. ////////////////////////////////////////////////////////////////////////////////
  30. // Deserializer is the interface used to decode data from structured storage.
  31. // Methods that return a bool use this to indicate success.
  32. class Deserializer {
  33. public:
  34. virtual ~Deserializer() = default;
  35. // deserialization methods for simple data types.
  36. // If the stored object is not of the correct type, then these function will
  37. // return false.
  38. virtual bool deserialize(boolean*) const = 0;
  39. virtual bool deserialize(integer*) const = 0;
  40. virtual bool deserialize(number*) const = 0;
  41. virtual bool deserialize(string*) const = 0;
  42. virtual bool deserialize(object*) const = 0;
  43. virtual bool deserialize(any*) const = 0;
  44. // count() returns the number of elements in the array object referenced by
  45. // this Deserializer.
  46. virtual size_t count() const = 0;
  47. // array() calls the provided std::function for deserializing each array
  48. // element in the array object referenced by this Deserializer.
  49. virtual bool array(const std::function<bool(Deserializer*)>&) const = 0;
  50. // field() calls the provided std::function for deserializing the field with
  51. // the given name from the struct object referenced by this Deserializer.
  52. virtual bool field(const std::string& name,
  53. const std::function<bool(Deserializer*)>&) const = 0;
  54. // deserialize() delegates to TypeOf<T>::type()->deserialize().
  55. template <typename T,
  56. typename = std::enable_if<TypeOf<T>::has_custom_serialization>>
  57. inline bool deserialize(T*) const;
  58. // deserialize() decodes an array.
  59. template <typename T>
  60. inline bool deserialize(dap::array<T>*) const;
  61. // deserialize() decodes an optional.
  62. template <typename T>
  63. inline bool deserialize(dap::optional<T>*) const;
  64. // deserialize() decodes an variant.
  65. template <typename T0, typename... Types>
  66. inline bool deserialize(dap::variant<T0, Types...>*) const;
  67. // deserialize() decodes the struct field f with the given name.
  68. template <typename T>
  69. inline bool field(const std::string& name, T* f) const;
  70. };
  71. template <typename T, typename>
  72. bool Deserializer::deserialize(T* ptr) const {
  73. return TypeOf<T>::type()->deserialize(this, ptr);
  74. }
  75. template <typename T>
  76. bool Deserializer::deserialize(dap::array<T>* vec) const {
  77. auto n = count();
  78. vec->resize(n);
  79. size_t i = 0;
  80. if (!array([&](Deserializer* d) { return d->deserialize(&(*vec)[i++]); })) {
  81. return false;
  82. }
  83. return true;
  84. }
  85. template <typename T>
  86. bool Deserializer::deserialize(dap::optional<T>* opt) const {
  87. T v;
  88. if (deserialize(&v)) {
  89. *opt = v;
  90. }
  91. return true;
  92. }
  93. template <typename T0, typename... Types>
  94. bool Deserializer::deserialize(dap::variant<T0, Types...>* var) const {
  95. return deserialize(&var->value);
  96. }
  97. template <typename T>
  98. bool Deserializer::field(const std::string& name, T* v) const {
  99. return this->field(name,
  100. [&](const Deserializer* d) { return d->deserialize(v); });
  101. }
  102. ////////////////////////////////////////////////////////////////////////////////
  103. // Serializer
  104. ////////////////////////////////////////////////////////////////////////////////
  105. class FieldSerializer;
  106. // Serializer is the interface used to encode data to structured storage.
  107. // A Serializer is associated with a single storage object, whos type and value
  108. // is assigned by a call to serialize().
  109. // If serialize() is called multiple times on the same Serializer instance,
  110. // the last type and value is stored.
  111. // Methods that return a bool use this to indicate success.
  112. class Serializer {
  113. public:
  114. virtual ~Serializer() = default;
  115. // serialization methods for simple data types.
  116. virtual bool serialize(boolean) = 0;
  117. virtual bool serialize(integer) = 0;
  118. virtual bool serialize(number) = 0;
  119. virtual bool serialize(const string&) = 0;
  120. virtual bool serialize(const dap::object&) = 0;
  121. virtual bool serialize(const any&) = 0;
  122. // array() encodes count array elements to the array object referenced by this
  123. // Serializer. The std::function will be called count times, each time with a
  124. // Serializer that should be used to encode the n'th array element's data.
  125. virtual bool array(size_t count, const std::function<bool(Serializer*)>&) = 0;
  126. // object() begins encoding the object referenced by this Serializer.
  127. // The std::function will be called with a FieldSerializer to serialize the
  128. // object's fields.
  129. virtual bool object(const std::function<bool(dap::FieldSerializer*)>&) = 0;
  130. // remove() deletes the object referenced by this Serializer.
  131. // remove() can be used to serialize optionals with no value assigned.
  132. virtual void remove() = 0;
  133. // serialize() delegates to TypeOf<T>::type()->serialize().
  134. template <typename T,
  135. typename = std::enable_if<TypeOf<T>::has_custom_serialization>>
  136. inline bool serialize(const T&);
  137. // serialize() encodes the given array.
  138. template <typename T>
  139. inline bool serialize(const dap::array<T>&);
  140. // serialize() encodes the given optional.
  141. template <typename T>
  142. inline bool serialize(const dap::optional<T>& v);
  143. // serialize() encodes the given variant.
  144. template <typename T0, typename... Types>
  145. inline bool serialize(const dap::variant<T0, Types...>&);
  146. // deserialize() encodes the given string.
  147. inline bool serialize(const char* v);
  148. protected:
  149. static inline const TypeInfo* get_any_type(const any&);
  150. static inline const void* get_any_val(const any&);
  151. };
  152. inline const TypeInfo* Serializer::get_any_type(const any& a){
  153. return a.type;
  154. }
  155. const void* Serializer::get_any_val(const any& a) {
  156. return a.value;
  157. }
  158. template <typename T, typename>
  159. bool Serializer::serialize(const T& object) {
  160. return TypeOf<T>::type()->serialize(this, &object);
  161. }
  162. template <typename T>
  163. bool Serializer::serialize(const dap::array<T>& vec) {
  164. auto it = vec.begin();
  165. return array(vec.size(), [&](Serializer* s) { return s->serialize(*it++); });
  166. }
  167. template <typename T>
  168. bool Serializer::serialize(const dap::optional<T>& opt) {
  169. if (!opt.has_value()) {
  170. remove();
  171. return true;
  172. }
  173. return serialize(opt.value());
  174. }
  175. template <typename T0, typename... Types>
  176. bool Serializer::serialize(const dap::variant<T0, Types...>& var) {
  177. return serialize(var.value);
  178. }
  179. bool Serializer::serialize(const char* v) {
  180. return serialize(std::string(v));
  181. }
  182. ////////////////////////////////////////////////////////////////////////////////
  183. // FieldSerializer
  184. ////////////////////////////////////////////////////////////////////////////////
  185. // FieldSerializer is the interface used to serialize fields of an object.
  186. class FieldSerializer {
  187. public:
  188. using SerializeFunc = std::function<bool(Serializer*)>;
  189. template <typename T>
  190. using IsSerializeFunc = std::is_convertible<T, SerializeFunc>;
  191. virtual ~FieldSerializer() = default;
  192. // field() encodes a field to the struct object referenced by this Serializer.
  193. // The SerializeFunc will be called with a Serializer used to encode the
  194. // field's data.
  195. virtual bool field(const std::string& name, const SerializeFunc&) = 0;
  196. // field() encodes the field with the given name and value.
  197. template <
  198. typename T,
  199. typename = typename std::enable_if<!IsSerializeFunc<T>::value>::type>
  200. inline bool field(const std::string& name, const T& v);
  201. };
  202. template <typename T, typename>
  203. bool FieldSerializer::field(const std::string& name, const T& v) {
  204. return this->field(name, [&](Serializer* s) { return s->serialize(v); });
  205. }
  206. } // namespace dap
  207. #endif // dap_serialization_h