Turi Create  4.0
variant_deep_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_VARIANT_DEEP_SERIALIZE_H_
7 #define TURI_VARIANT_DEEP_SERIALIZE_H_
8 
9 #include <model_server/lib/variant.hpp>
10 
11 namespace turi {
12 
13 /**
14  * Serialize the variant type, deep copying the pointer types.
15  */
16 void variant_deep_save(const variant_type& v, oarchive& oarc);
17 
18 /**
19  * Overload of above for types castable to and from variant_type
20  */
21 template <typename T>
22 void variant_deep_save(const T& v, oarchive& oarc) {
23 
24  static uint64_t type_check_hash = hash64(0);
25 
26  oarc << type_check_hash;
27 
28  variant_deep_save(to_variant(v), oarc);
29 }
30 
31 /**
32  * Deserialize the variant type, allocate new resources for the pointer types.
33  */
35 
36 /**
37  * Overload of above for types castable to and from variant_type
38  */
39 template <typename T>
40 void variant_deep_load(T& v, iarchive& iarc) {
41 
42  static uint64_t type_check_hash = hash64(0);
43 
44  uint64_t type_check_hash_load;
45  iarc >> type_check_hash_load;
46 
47  ASSERT_MSG(type_check_hash == type_check_hash_load,
48  (std::string("Deserialization of type ") + typeid(T).name()
49  + " failed, likely due to corruption earlier in the stream. ").c_str());
50 
51  variant_type vv;
52  variant_deep_load(vv, iarc);
53  v = variant_get_value<T>(vv);
54 }
55 
56 } // namespace turi
57 
58 #endif /* _VARIANT_DEEP_SERIALIZE_H_ */
The serialization input archive object which, provided with a reference to an istream, will read from the istream, providing deserialization capabilities.
Definition: iarchive.hpp:60
void variant_deep_load(variant_type &v, iarchive &iarc)
void variant_deep_save(const variant_type &v, oarchive &oarc)
boost::make_recursive_variant< flexible_type, std::shared_ptr< unity_sgraph_base >, dataframe_t, std::shared_ptr< model_base >, std::shared_ptr< unity_sframe_base >, std::shared_ptr< unity_sarray_base >, std::map< std::string, boost::recursive_variant_ >, std::vector< boost::recursive_variant_ >, boost::recursive_wrapper< function_closure_info > >::type variant_type
Definition: variant.hpp:24
static uint64_t hash64(const char *s, size_t len)
variant_type to_variant(const T &f)
Definition: variant.hpp:308
The serialization output archive object which, provided with a reference to an ostream, will write to the ostream, providing serialization capabilities.
Definition: oarchive.hpp:80