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 #pragma once
12 
13 // Defined only if boost serialization is enabled
14 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
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  * PR https://github.com/boostorg/serialization/pull/163 was merged
60  * on September 3rd 2023,
61  * and so the below code is now a part of Boost 1.84.
62  * We include it for posterity, hence the check for BOOST_VERSION being less
63  * than 1.84.
64  */
65 #if BOOST_VERSION < 108400
66 // function specializations must be defined in the appropriate
67 // namespace - boost::serialization
68 namespace boost {
69 namespace serialization {
70 
71 template <class Archive, class T>
72 void save(Archive& ar, const std::optional<T>& t, const unsigned int /*version*/
73 ) {
74  // It is an inherent limitation to the serialization of optional.hpp
75  // that the underlying type must be either a pointer or must have a
76  // default constructor.
78  const bool tflag = t.has_value();
79  ar << boost::serialization::make_nvp("initialized", tflag);
80  if (tflag) {
81  ar << boost::serialization::make_nvp("value", *t);
82  }
83 }
84 
85 template <class Archive, class T>
86 void load(Archive& ar, std::optional<T>& t, const unsigned int /*version*/) {
87  bool tflag;
88  ar >> boost::serialization::make_nvp("initialized", tflag);
89  if (!tflag) {
90  t.reset();
91  return;
92  }
93 
94  if (!t.has_value()) {
95  // Need to be default constructible
96  t.emplace();
97  }
98  ar >> boost::serialization::make_nvp("value", *t);
99 }
100 
101 template <class Archive, class T>
102 void serialize(Archive& ar, std::optional<T>& t, const unsigned int version) {
103  boost::serialization::split_free(ar, t, version);
104 }
105 
106 } // namespace serialization
107 } // namespace boost
108 #endif // BOOST_VERSION < 108400
109 #endif // GTSAM_ENABLE_BOOST_SERIALIZATION
libsize.save
save
Definition: libsize.py:15
boost
Definition: boostmultiprec.cpp:109
conf.version
version
Definition: gtsam/3rdparty/GeographicLib/python/doc/conf.py:67
std
Definition: BFloat16.h:88
U
@ U
Definition: testDecisionTree.cpp:349
align_3::t
Point2 t(10, 10)
test_callbacks.value
value
Definition: test_callbacks.py:160


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:05:00