Turi Create  4.0
vector_serialization.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_SERIALIZE_GL_VECTOR_HPP
7 #define TURI_SERIALIZE_GL_VECTOR_HPP
8 
9 #include <core/storage/serialization/iarchive.hpp>
10 #include <core/storage/serialization/oarchive.hpp>
11 #include <core/storage/serialization/iterator.hpp>
12 
13 namespace turi {
14 
15 template <typename T> class gl_vector;
16 
17  namespace archive_detail {
18  /**
19  * We re-dispatch vectors because based on the contained type,
20  * it is actually possible to serialize them like a POD
21  */
22  template <typename OutArcType, typename ValueType, bool IsPOD>
23  struct gl_vector_serialize_impl {
24  static void exec(OutArcType& oarc, const ValueType& vec) {
25  // really this is an assert false. But the static assert
26  // must depend on a template parameter
27  BOOST_STATIC_ASSERT(sizeof(OutArcType) == 0);
28  assert(false);
29  };
30  };
31  /**
32  * We re-dispatch vectors because based on the contained type,
33  * it is actually possible to deserialize them like iarc POD
34  */
35  template <typename InArcType, typename ValueType, bool IsPOD>
36  struct gl_vector_deserialize_impl {
37  static void exec(InArcType& iarc, ValueType& vec) {
38  // really this is an assert false. But the static assert
39  // must depend on a template parameter
40  BOOST_STATIC_ASSERT(sizeof(InArcType) == 0);
41  assert(false);
42  };
43  };
44 
45  /// If contained type is not a POD use the standard serializer
46  template <typename OutArcType, typename ValueType>
47  struct gl_vector_serialize_impl<OutArcType, ValueType, false > {
48  static void exec(OutArcType& oarc, const gl_vector<ValueType>& vec) {
49  oarc << size_t(vec.size());
50  for (size_t i = 0;i < vec.size(); ++i) {
51  oarc << vec[i];
52  }
53  }
54  };
55 
56  /// Fast vector serialization if contained type is a POD
57  template <typename OutArcType, typename ValueType>
58  struct gl_vector_serialize_impl<OutArcType, ValueType, true > {
59  static void exec(OutArcType& oarc, const gl_vector<ValueType>& vec) {
60  oarc << size_t(vec.size());
61  serialize(oarc, vec.data(),sizeof(ValueType)*vec.size());
62  }
63  };
64 
65  /// If contained type is not a POD use the standard deserializer
66  template <typename InArcType, typename ValueType>
67  struct gl_vector_deserialize_impl<InArcType, ValueType, false > {
68  static void exec(InArcType& iarc, gl_vector<ValueType>& vec){
69  size_t len;
70  iarc >> len;
71  vec.clear(); vec.resize(len);
72  for (size_t i = 0;i < len; ++i) {
73  iarc >> vec[i];
74  }
75  }
76  };
77 
78  /// Fast vector deserialization if contained type is a POD
79  template <typename InArcType, typename ValueType>
80  struct gl_vector_deserialize_impl<InArcType, ValueType, true > {
81  static void exec(InArcType& iarc, gl_vector<ValueType>& vec){
82  size_t len;
83  iarc >> len;
84  vec.clear(); vec.resize(len);
85  deserialize(iarc, vec.data(), sizeof(ValueType)*vec.size());
86  }
87  };
88 
89  /**
90  Serializes a vector */
91  template <typename OutArcType, typename ValueType>
92  struct serialize_impl<OutArcType, gl_vector<ValueType>, false > {
93  static void exec(OutArcType& oarc, const gl_vector<ValueType>& vec) {
94  gl_vector_serialize_impl<OutArcType, ValueType,
95  gl_is_pod_or_scaler<ValueType>::value >::exec(oarc, vec);
96  }
97  };
98  /**
99  deserializes a vector */
100  template <typename InArcType, typename ValueType>
101  struct deserialize_impl<InArcType, gl_vector<ValueType>, false > {
102  static void exec(InArcType& iarc, gl_vector<ValueType>& vec){
103  gl_vector_deserialize_impl<InArcType, ValueType,
104  gl_is_pod_or_scaler<ValueType>::value >::exec(iarc, vec);
105  }
106  };
107  } // archive_detail
108 } // namespace turi
109 
110 #endif