Turi Create  4.0
vector.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_VECTOR_HPP
7 #define TURI_SERIALIZE_VECTOR_HPP
8 #include <vector>
9 #include <core/storage/serialization/iarchive.hpp>
10 #include <core/storage/serialization/oarchive.hpp>
11 #include <core/storage/serialization/iterator.hpp>
12 
13 
14 namespace turi {
15  namespace archive_detail {
16  /**
17  * We re-dispatch vectors because based on the contained type,
18  * it is actually possible to serialize them like a POD
19  */
20  template <typename OutArcType, typename ValueType, bool IsPOD>
21  struct vector_serialize_impl {
22  static void exec(OutArcType& oarc, const ValueType& vec) {
23  // really this is an assert false. But the static assert
24  // must depend on a template parameter
25  BOOST_STATIC_ASSERT(sizeof(OutArcType) == 0);
26  assert(false);
27  };
28  };
29  /**
30  * We re-dispatch vectors because based on the contained type,
31  * it is actually possible to deserialize them like iarc POD
32  */
33  template <typename InArcType, typename ValueType, bool IsPOD>
34  struct vector_deserialize_impl {
35  static void exec(InArcType& iarc, ValueType& vec) {
36  // really this is an assert false. But the static assert
37  // must depend on a template parameter
38  BOOST_STATIC_ASSERT(sizeof(InArcType) == 0);
39  assert(false);
40  };
41  };
42 
43  /// If contained type is not a POD use the standard serializer
44  template <typename OutArcType, typename ValueType>
45  struct vector_serialize_impl<OutArcType, ValueType, false > {
46  static void exec(OutArcType& oarc, const std::vector<ValueType>& vec) {
47  oarc << size_t(vec.size());
48  for (size_t i = 0;i < vec.size(); ++i) {
49  oarc << vec[i];
50  }
51  }
52  };
53 
54  /// Fast vector serialization if contained type is a POD
55  template <typename OutArcType, typename ValueType>
56  struct vector_serialize_impl<OutArcType, ValueType, true > {
57  static void exec(OutArcType& oarc, const std::vector<ValueType>& vec) {
58  oarc << size_t(vec.size());
59  serialize(oarc, vec.data(),sizeof(ValueType)*vec.size());
60  }
61  };
62 
63  /// If contained type is not a POD use the standard deserializer
64  template <typename InArcType, typename ValueType>
65  struct vector_deserialize_impl<InArcType, ValueType, false > {
66  static void exec(InArcType& iarc, std::vector<ValueType>& vec){
67  size_t len;
68  iarc >> len;
69  vec.clear(); vec.resize(len);
70  for (size_t i = 0;i < len; ++i) {
71  iarc >> vec[i];
72  }
73  }
74  };
75 
76  /// Fast vector deserialization if contained type is a POD
77  template <typename InArcType, typename ValueType>
78  struct vector_deserialize_impl<InArcType, ValueType, true > {
79  static void exec(InArcType& iarc, std::vector<ValueType>& vec){
80  size_t len;
81  iarc >> len;
82  vec.clear(); vec.resize(len);
83  deserialize(iarc, vec.data(), sizeof(ValueType)*vec.size());
84  }
85  };
86 
87 
88 
89  /**
90  Serializes a vector */
91  template <typename OutArcType, typename ValueType>
92  struct serialize_impl<OutArcType, std::vector<ValueType>, false > {
93  static void exec(OutArcType& oarc, const std::vector<ValueType>& vec) {
94  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, std::vector<ValueType>, false > {
102  static void exec(InArcType& iarc, std::vector<ValueType>& vec){
103  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
STL namespace.