Turi Create  4.0
conditional_serialize.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_SERIALIZATION_CONDITIONAL_SERIALIZE_HPP
7 #define TURI_SERIALIZATION_CONDITIONAL_SERIALIZE_HPP
8 #include <core/storage/serialization/oarchive.hpp>
9 #include <core/storage/serialization/iarchive.hpp>
10 namespace turi {
11 
12 template <typename T>
13 struct conditional_serialize {
14  bool hasval;
15  T val;
16 
17  conditional_serialize(): hasval(false) { }
18  conditional_serialize(T& val): hasval(true), val(val) { }
19 
20  conditional_serialize(const conditional_serialize& cs): hasval(cs.hasval), val(cs.val) { }
21  conditional_serialize& operator=(const conditional_serialize& cs) {
22  hasval = cs.hasval;
23  val = cs.val;
24  return (*this);
25  }
26  void save(oarchive& oarc) const {
27  oarc << hasval;
28  if (hasval) oarc << val;
29  }
30 
31  void load(iarchive& iarc) {
32  iarc >> hasval;
33  if (hasval) iarc >> val;
34  }
35 };
36 
37 };
38 
39 #endif