mlpack
has_serialize.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_UTIL_HAS_SERIALIZE_HPP
14 #define MLPACK_CORE_UTIL_HAS_SERIALIZE_HPP
15 
17 #include <cereal/archives/xml.hpp>
18 #include <cereal/cereal.hpp>
19 
20 #include <type_traits>
21 
22 namespace mlpack {
23 namespace data {
24 
25 // This gives us a HasSerializeCheck<T, U> type (where U is a function pointer)
26 // we can use with SFINAE to catch when a type has a Serialize() function.
27 HAS_EXACT_METHOD_FORM(serialize, HasSerializeCheck);
28 
29 // Don't call this with a non-class. HasSerializeFunction::value is true if the
30 // type T has a static or non-static Serialize() function.
31 template<typename T>
33 {
34  template<typename C>
35  using NonStaticSerialize = void(C::*)(cereal::XMLOutputArchive&,
36  const uint32_t version);
37 
38  template<typename /* C */>
39  using StaticSerialize = void(*)(cereal::XMLOutputArchive&,
40  const uint32_t version);
41 
42  static const bool value = HasSerializeCheck<T, NonStaticSerialize>::value ||
43  HasSerializeCheck<T, StaticSerialize>::value;
44 };
45 
46 template<typename T>
48 {
49  // We have to handle the case where T isn't a class...
50  typedef char yes[1];
51  typedef char no [2];
52  template<typename U, typename V, typename W> struct check;
53  template<typename U> static yes& chk( // This matches classes.
54  check<U,
55  typename std::enable_if_t<std::is_class<U>::value>*,
56  typename std::enable_if_t<HasSerializeFunction<U>::value>*>*);
57  template<typename > static no& chk(...); // This matches non-classes.
58 
59  static const bool value = (sizeof(chk<T>(0)) == sizeof(yes));
60 };
61 
62 } // namespace data
63 } // namespace mlpack
64 
65 #endif
Definition: has_serialize.hpp:47
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
Definition: sfinae_test.cpp:40
#define HAS_EXACT_METHOD_FORM(METHOD, NAME)
HAS_EXACT_METHOD_FORM generates a template that allows a compile time check whether a given class has...
Definition: sfinae_utility.hpp:285
Definition: has_serialize.hpp:32
Definition: has_serialize.hpp:52