boost.h
Go to the documentation of this file.
1 //
2 // Created by Eduard Valeyev on 5/3/21.
3 //
4 
5 #ifndef TTG_SERIALIZATION_BOOST_H
6 #define TTG_SERIALIZATION_BOOST_H
7 
8 #include <type_traits>
9 
10 #if __has_include(<boost/type_traits/is_array.hpp>)
11 # define TTG_HAS_BOOST_HEADERS 1
12 # include <boost/type_traits/is_array.hpp>
13 # include <boost/type_traits/remove_extent.hpp>
14 #endif
15 
16 #ifdef TTG_SERIALIZATION_SUPPORTS_BOOST
17 #include <boost/archive/binary_iarchive.hpp>
18 #include <boost/archive/binary_oarchive.hpp>
19 #include <boost/serialization/level.hpp>
20 #endif // TTG_SERIALIZATION_SUPPORTS_BOOST
21 
22 namespace ttg::detail {
23 
24  /*----- is_boost_{input,output,}_archive_v -----*/
25 
26 #ifdef TTG_SERIALIZATION_SUPPORTS_BOOST
27  template <typename T>
28  inline constexpr bool is_boost_archive_v = std::is_base_of_v<boost::archive::detail::basic_iarchive, T> ||
29  std::is_base_of_v<boost::archive::detail::basic_oarchive, T>;
30  template <typename T>
31  inline constexpr bool is_boost_input_archive_v = std::is_base_of_v<boost::archive::detail::basic_iarchive, T>;
32 
33  template <typename T>
34  inline constexpr bool is_boost_output_archive_v = std::is_base_of_v<boost::archive::detail::basic_oarchive, T>;
35 
36  /*----- is_archive_v for boost archives -----*/
37  template <typename T>
38  inline constexpr bool is_archive_v<T, std::enable_if_t<is_boost_archive_v<T>>> = true;
39  template <typename T>
40  inline constexpr bool is_input_archive_v<T, std::enable_if_t<is_boost_input_archive_v<T>>> = true;
41  template <typename T>
42  inline constexpr bool is_output_archive_v<T, std::enable_if_t<is_boost_output_archive_v<T>>> = true;
43 
44 #else // TTG_SERIALIZATION_SUPPORTS_BOOST
45  template <typename T>
46  inline constexpr bool is_boost_archive_v = false;
47  template <typename T>
48  inline constexpr bool is_boost_input_archive_v = false;
49  template <typename T>
50  inline constexpr bool is_boost_output_archive_v = false;
51 #endif // TTG_SERIALIZATION_SUPPORTS_BOOST
52 
53  /*----- is_boost_serializable_v -----*/
54 
55  template <typename Archive, typename T, typename Enabler = void>
56  inline static constexpr bool is_boost_serializable_v = false;
57 
58  template <typename Archive, typename T, typename Enabler = void>
60 
61 #ifdef TTG_HAS_BOOST_HEADERS
62  template <typename Archive, typename T>
63  struct is_boost_array_serializable<Archive, T, std::enable_if_t<!boost::is_array<T>::value>> : std::false_type {};
64 
65  template <typename Archive, typename T>
66  struct is_boost_array_serializable<Archive, T, std::enable_if_t<boost::is_array<T>::value>>
67  : std::bool_constant<is_boost_serializable_v<Archive, boost::remove_extent_t<T>>> {};
68 #else
69  template <typename Archive, typename T>
70  struct is_boost_array_serializable<Archive, T> : std::false_type {};
71 #endif
72 
73  template <typename Archive, typename T>
74  inline static constexpr bool is_boost_array_serializable_v = is_boost_array_serializable<Archive, T>::value;
75 
76  template <typename Archive, typename T>
77  inline static constexpr bool is_stlcontainer_boost_serializable_v = false;
78 
79 #ifdef TTG_SERIALIZATION_SUPPORTS_BOOST
84  template <typename Archive, typename T>
85  inline static constexpr bool is_boost_serializable_v<
86  Archive, T,
87  std::enable_if_t<
88  // Archive is a boost archive
89  is_boost_archive_v<Archive>
90  // T is not not_serializable
91  && !std::is_same_v<typename boost::serialization::implementation_level<T>::type,
92  boost::mpl::int_<boost::serialization::level_type::not_serializable>>
93  // T is primitive or T is an array of serializables or else T has serialize methods
94  && (std::is_same_v<typename boost::serialization::implementation_level<T>::type,
95  boost::mpl::int_<boost::serialization::level_type::primitive_type>> ||
96  is_boost_array_serializable_v<Archive, T> ||
97  (!std::is_same_v<typename boost::serialization::implementation_level<T>::type,
98  boost::mpl::int_<boost::serialization::level_type::primitive_type>> &&
99  (ttg::detail::has_freestanding_serialize_with_version_v<ttg::meta::remove_cvr_t<T>, Archive> ||
100  (ttg::detail::is_stlcontainer_boost_serializable_v<Archive, T> &&
101  ttg::detail::has_freestanding_boost_serialize_with_version_v<ttg::meta::remove_cvr_t<T>, Archive>) ||
103  (ttg::detail::has_member_load_with_version_v<T, Archive> &&
104  ttg::detail::has_member_save_with_version_v<T, Archive>))))>> = true;
105 #endif // TTG_SERIALIZATION_SUPPORTS_BOOST
106 
107  template <typename Archive, typename T>
108  struct is_boost_serializable : std::bool_constant<is_boost_serializable_v<Archive, T>> {};
109 
110  template <typename Archive, typename T, class = void>
111  struct is_boost_default_serializable : std::false_type {};
112 
113 #ifdef TTG_SERIALIZATION_SUPPORTS_BOOST
115  template <typename Archive, typename T>
117  Archive, T,
118  std::enable_if_t<
119  // Archive is a boost archive
120  is_boost_archive_v<Archive>
121  // T is not not_serializable
122  && !std::is_same_v<typename boost::serialization::implementation_level<T>::type,
123  boost::mpl::int_<boost::serialization::level_type::not_serializable>>
124  // T is primitive or T is an array of serializables or else T has serialize methods
125  && (std::is_same_v<typename boost::serialization::implementation_level<T>::type,
126  boost::mpl::int_<boost::serialization::level_type::primitive_type>> ||
127  is_boost_array_serializable_v<Archive, T> ||
128  (!std::is_same_v<typename boost::serialization::implementation_level<T>::type,
129  boost::mpl::int_<boost::serialization::level_type::primitive_type>> &&
130  (ttg::detail::is_stlcontainer_boost_serializable_v<Archive, T> &&
131  ttg::detail::has_freestanding_boost_serialize_with_version_v<ttg::meta::remove_cvr_t<T>, Archive>)))>>
132  : std::true_type {};
133 #endif // TTG_SERIALIZATION_SUPPORTS_BOOST
134 
135  template <typename Archive, typename T>
136  inline static constexpr bool is_boost_default_serializable_v = is_boost_default_serializable<Archive, T>::value;
137 
138  template <typename T, class = void>
139  struct is_boost_buffer_serializable : std::false_type {};
140 
141 #ifdef TTG_SERIALIZATION_SUPPORTS_BOOST
142  template <typename T>
143  struct is_boost_buffer_serializable<T, std::enable_if_t<is_boost_serializable_v<boost::archive::binary_iarchive, T> &&
144  is_boost_serializable_v<boost::archive::binary_oarchive, T>>>
145  : std::true_type {};
146 #endif // TTG_SERIALIZATION_SUPPORTS_BOOST
147 
149  template <typename T>
151 
152  template <typename T, class = void>
153  struct is_boost_default_buffer_serializable : std::false_type {};
154 
155 #ifdef TTG_SERIALIZATION_SUPPORTS_BOOST
156  template <typename T>
158  T, std::enable_if_t<is_boost_default_serializable_v<boost::archive::binary_iarchive, T> &&
159  is_boost_default_serializable_v<boost::archive::binary_oarchive, T>>> : std::true_type {};
160 #endif // TTG_SERIALIZATION_SUPPORTS_BOOST
161 
163  template <typename T>
165 
167  template <typename T>
168  inline constexpr bool is_boost_user_buffer_serializable_v =
169  is_boost_buffer_serializable<T>::value && !is_boost_default_buffer_serializable_v<T>;
170 
171 } // namespace ttg::detail
172 
173 #endif // TTG_SERIALIZATION_BOOST_H
constexpr bool is_boost_output_archive_v
Definition: boost.h:50
constexpr bool has_member_serialize_with_version_v
Definition: traits.h:91
constexpr bool is_boost_archive_v
Definition: boost.h:46
constexpr bool is_boost_default_buffer_serializable_v
evaluates to true if can serialize T to/from buffer using default Boost serialization
Definition: boost.h:164
constexpr bool is_boost_user_buffer_serializable_v
evaluates to true if can serialize T to/from buffer using user-provided Boost serialization
Definition: boost.h:168
constexpr bool is_boost_input_archive_v
Definition: boost.h:48
constexpr bool is_boost_buffer_serializable_v
evaluates to true if can serialize T to/from buffer using Boost serialization
Definition: boost.h:150
std::remove_cv_t< std::remove_reference_t< T > > remove_cvr_t
Definition: meta.h:27