traits.h
Go to the documentation of this file.
1 #ifndef TTG_SERIALIZATION_TRAITS_H
2 #define TTG_SERIALIZATION_TRAITS_H
3 
4 #include "ttg/util/meta.h"
5 
6 #include <type_traits>
7 
8 #include <iostream>
9 
10 namespace boost {
11  namespace archive {}
12  namespace serialization {
13  struct unsigned_int {
14  unsigned int v;
15  unsigned_int(unsigned int v) : v(v) {}
16  operator unsigned int() const { return v; }
17  };
19  } // namespace serialization
20 } // namespace boost
21 
22 namespace ttg::detail {
23 
27  template <typename T, typename Archive>
28  using has_member_serialize_t = decltype(std::declval<T&>().serialize(std::declval<Archive&>()));
29 
33  template <typename T, typename Archive>
34  using has_member_serialize_with_version_t = decltype(std::declval<T&>().serialize(std::declval<Archive&>(), 0u));
35 
39  template <typename T, typename Archive>
40  using has_member_load_t = decltype(std::declval<T&>().load(std::declval<Archive&>()));
41 
45  template <typename T, typename Archive>
46  using has_member_load_with_version_t = decltype(std::declval<T&>().load(std::declval<Archive&>(), 0u));
47 
51  template <typename T, typename Archive>
52  using has_member_save_t = decltype(std::declval<T&>().save(std::declval<Archive&>()));
53 
57  template <typename T, typename Archive>
58  using has_member_save_with_version_t = decltype(std::declval<T&>().save(std::declval<Archive&>(), 0u));
59 
62  template <typename T, typename Archive>
63  using has_freestanding_serialize_t = decltype(serialize(std::declval<Archive&>(), std::declval<T&>()));
64 
67  template <typename T, typename Archive>
69  decltype(serialize(std::declval<Archive&>(), std::declval<T&>(), 0u));
70 
73  template <typename T, typename Archive>
75  decltype(serialize(std::declval<Archive&>(), std::declval<T&>(), boost::serialization::version_as_adl_tag));
76 
82  template <typename T, typename Archive>
83  inline constexpr bool has_member_serialize_v = ttg::meta::is_detected_v<has_member_serialize_t, T, Archive>;
84 
90  template <typename T, typename Archive>
91  inline constexpr bool has_member_serialize_with_version_v =
92  ttg::meta::is_detected_v<has_member_serialize_with_version_t, T, Archive>;
93 
99  template <typename T, typename Archive>
100  inline constexpr bool has_member_load_with_version_v =
101  ttg::meta::is_detected_v<has_member_load_with_version_t, T, Archive>;
102 
108  template <typename T, typename Archive>
109  inline constexpr bool has_member_save_with_version_v =
110  ttg::meta::is_detected_v<has_member_save_with_version_t, T, Archive>;
111 
117  template <typename T, typename Archive>
118  inline constexpr bool has_freestanding_serialize_v =
119  ttg::meta::is_detected_v<has_freestanding_serialize_t, T, Archive>;
120 
126  template <typename T, typename Archive>
128  ttg::meta::is_detected_v<has_freestanding_serialize_with_version_t, T, Archive>;
129 
135  template <typename T, typename Archive>
137  ttg::meta::is_detected_v<has_freestanding_boost_serialize_with_version_t, T, Archive>;
138 
140 
141  template <typename T, typename Enabler = void>
142  inline constexpr bool is_archive_v = false;
143 
144  template <typename T, typename Enabler>
145  inline constexpr bool is_input_archive_v = false;
146 
147  template <typename T, typename Enabler = void>
148  inline constexpr bool is_output_archive_v = false;
149 
151 
152  template <class, class = void>
153  struct is_printable : std::false_type {};
154 
155  template <class T>
156  struct is_printable<T, std::void_t<decltype(std::declval<std::ostream&>() << std::declval<T>())>> : std::true_type {};
157 
158  template <typename T, typename Enabler = void>
159  struct printer_helper {
160  static void print(const void* object) { std::cout << "[unprintable object]" << std::endl; }
161  };
162 
163  template <typename T>
164  struct printer_helper<T, std::enable_if_t<is_printable<T>::value>> {
165  static void print(const void* object) { std::cout << *(static_cast<const T*>(object)) << std::endl; }
166  };
167 
168 } // namespace ttg::detail
169 
170 #include "ttg/serialization/backends.h"
171 
172 namespace ttg::detail {
174  template <typename T, typename Enabler = void>
175  struct is_user_buffer_serializable : std::false_type {};
176 
177  // template <typename T>
178  // struct is_user_buffer_serializable<T, std::enable_if_t<is_madness_user_buffer_serializable_v<T> ||
179  // is_boost_user_buffer_serializable_v<T>>> : std::true_type {};
180  template <typename T>
181  struct is_user_buffer_serializable<
182  T, std::enable_if_t<is_madness_user_buffer_serializable_v<T> || is_boost_user_buffer_serializable_v<T>>> : std::true_type {};
183 
184  template <typename T>
185  inline constexpr bool is_user_buffer_serializable_v = is_user_buffer_serializable<T>::value;
186 
188 
193  template <typename T>
194  inline constexpr bool is_memcpyable_override_v = std::is_trivially_copyable_v<T>;
195 
196  // std::pair of trivially-copyable types is trivially copyable
197  template <typename T1, typename T2>
198  inline constexpr bool is_memcpyable_override_v<std::pair<T1,T2>> = std::is_trivially_copyable_v<T1> && std::is_trivially_copyable_v<T2>;
199 
201 
203  template <typename T>
204  struct is_memcpyable : std::bool_constant<std::is_trivially_copyable_v<T> || is_memcpyable_override_v<T>> {};
205 
206  template <typename T>
207  inline constexpr bool is_memcpyable_v = is_memcpyable<T>::value;
208 
209 } // namespace ttg::detail
210 
211 #endif // TTG_SERIALIZATION_TRAITS_H
const unsigned_int version_as_adl_tag
Definition: traits.h:18
decltype(serialize(std::declval< Archive & >(), std::declval< T & >(), boost::serialization::version_as_adl_tag)) has_freestanding_boost_serialize_with_version_t
Definition: traits.h:75
decltype(serialize(std::declval< Archive & >(), std::declval< T & >())) has_freestanding_serialize_t
Definition: traits.h:63
decltype(std::declval< T & >().save(std::declval< Archive & >())) has_member_save_t
Definition: traits.h:52
constexpr bool has_member_serialize_with_version_v
Definition: traits.h:91
decltype(std::declval< T & >().serialize(std::declval< Archive & >())) has_member_serialize_t
Definition: traits.h:28
decltype(std::declval< T & >().load(std::declval< Archive & >())) has_member_load_t
Definition: traits.h:40
constexpr bool is_input_archive_v
Definition: traits.h:145
decltype(std::declval< T & >().save(std::declval< Archive & >(), 0u)) has_member_save_with_version_t
Definition: traits.h:58
constexpr bool is_output_archive_v
Definition: traits.h:148
constexpr bool has_member_load_with_version_v
Definition: traits.h:100
constexpr bool has_member_serialize_v
Definition: traits.h:83
constexpr bool has_freestanding_serialize_with_version_v
Definition: traits.h:127
decltype(std::declval< T & >().serialize(std::declval< Archive & >(), 0u)) has_member_serialize_with_version_t
Definition: traits.h:34
constexpr bool has_freestanding_boost_serialize_with_version_v
Definition: traits.h:136
constexpr bool is_archive_v
Definition: traits.h:142
constexpr bool has_member_save_with_version_v
Definition: traits.h:109
decltype(serialize(std::declval< Archive & >(), std::declval< T & >(), 0u)) has_freestanding_serialize_with_version_t
Definition: traits.h:69
decltype(std::declval< T & >().load(std::declval< Archive & >(), 0u)) has_member_load_with_version_t
Definition: traits.h:46
constexpr bool has_freestanding_serialize_v
Definition: traits.h:118
void void_t
Definition: meta.h:23
unsigned_int(unsigned int v)
Definition: traits.h:15