std_optional_serialization.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 * Use, modification and distribution is subject to the Boost Software
3 * License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
5 
6 * See http://www.boost.org for updates, documentation, and revision history.
7 
8 * Functionality to serialize std::optional<T> to boost::archive
9 * Inspired from this PR: https://github.com/boostorg/serialization/pull/163
10 * ---------------------------------------------------------------------------- */
11 
12 // Defined only if boost serialization is enabled
13 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
14 #pragma once
15 #include <optional>
16 #include <boost/config.hpp>
17 
18 #include <boost/archive/detail/basic_iarchive.hpp>
19 #include <boost/move/utility_core.hpp>
20 
21 #include <boost/serialization/item_version_type.hpp>
22 #include <boost/serialization/split_free.hpp>
23 #include <boost/serialization/level.hpp>
24 #include <boost/serialization/nvp.hpp>
25 #include <boost/serialization/version.hpp>
26 #include <boost/type_traits/is_pointer.hpp>
27 #include <boost/serialization/detail/stack_constructor.hpp>
28 #include <boost/serialization/detail/is_default_constructible.hpp>
29 
49 #ifdef __GNUC__
50 #if __GNUC__ >= 7 && __cplusplus >= 201703L
51 namespace boost { namespace serialization { struct U; } }
52 namespace std { template<> struct is_trivially_default_constructible<boost::serialization::U> : std::false_type {}; }
53 namespace std { template<> struct is_trivially_copy_constructible<boost::serialization::U> : std::false_type {}; }
54 namespace std { template<> struct is_trivially_move_constructible<boost::serialization::U> : std::false_type {}; }
55 #endif
56 #endif
57 
58 
59 // function specializations must be defined in the appropriate
60 // namespace - boost::serialization
61 namespace boost {
62 namespace serialization {
63 
64 template <class Archive, class T>
65 void save(Archive& ar, const std::optional<T>& t, const unsigned int /*version*/
66 ) {
67  // It is an inherent limitation to the serialization of optional.hpp
68  // that the underlying type must be either a pointer or must have a
69  // default constructor.
71  const bool tflag = t.has_value();
72  ar << boost::serialization::make_nvp("initialized", tflag);
73  if (tflag) {
74  ar << boost::serialization::make_nvp("value", *t);
75  }
76 }
77 
78 template <class Archive, class T>
79 void load(Archive& ar, std::optional<T>& t, const unsigned int /*version*/) {
80  bool tflag;
81  ar >> boost::serialization::make_nvp("initialized", tflag);
82  if (!tflag) {
83  t.reset();
84  return;
85  }
86 
87  if (!t.has_value()) {
88  // Need to be default constructible
89  t.emplace();
90  }
91  ar >> boost::serialization::make_nvp("value", *t);
92 }
93 
94 template <class Archive, class T>
95 void serialize(Archive& ar, std::optional<T>& t, const unsigned int version) {
96  boost::serialization::split_free(ar, t, version);
97 }
98 
99 } // namespace serialization
100 } // namespace boost
101 #endif
std::string serialize(const T &input)
serializes to a string
Definition: BFloat16.h:88
Point2 t(10, 10)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:36:20