Turi Create  4.0
string_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_STRING_HPP
7 #define TURI_SERIALIZE_GL_STRING_HPP
8 
9 #include <core/storage/serialization/iarchive.hpp>
10 #include <core/storage/serialization/oarchive.hpp>
11 #include <core/storage/serialization/iterator.hpp>
12 #include <core/generics/gl_string.hpp>
13 
14 namespace turi {
15 
16  namespace archive_detail {
17 
18  /// Serialization of gl_string
19  template <typename OutArcType>
20  struct serialize_impl<OutArcType, gl_string, false> {
21  static void exec(OutArcType& oarc, const gl_string& s) {
22  size_t length = s.length();
23  oarc << length;
24  if(length > 0) {
25  oarc.write(reinterpret_cast<const char*>(s.data()), (std::streamsize)length);
26  }
27  DASSERT_FALSE(oarc.fail());
28  }
29  };
30 
31 
32  /// Deserialization of gl_string
33  template <typename InArcType>
34  struct deserialize_impl<InArcType, gl_string, false> {
35  static void exec(InArcType& iarc, gl_string& s) {
36  //read the length
37  size_t length;
38  iarc >> length;
39  //resize the string and read the characters
40  s.resize(length);
41  if(length > 0) {
42  iarc.read(&(s[0]), (std::streamsize)length);
43  }
44  DASSERT_FALSE(iarc.fail());
45  }
46  };
47  }
48 
49 } // namespace turi
50 
51 #endif
#define DASSERT_FALSE(cond)
Definition: assertions.hpp:365