SUNphi  1.0
Base.hpp
Go to the documentation of this file.
1 #ifndef _SERIALIZE_BASE_HPP
2 #define _SERIALIZE_BASE_HPP
3 
4 /// \file serialize/Base.hpp
5 ///
6 /// \brief Defines the basic serializable feature
7 ///
8 /// The value mapped can be of three types:
9 ///
10 /// - a scalar, of a trivially copyable kind
11 /// - a map, namely a class with a "serializableMembers" member
12 /// - a sequence, namely a tuple-like or vector-like container
13 
14 
15 #include <yaml-cpp/yaml.h>
16 
17 #include <metaprogramming/CRTP.hpp>
18 #include <metaprogramming/SFINAE.hpp>
19 #include <metaprogramming/TypeTraits.hpp>
20 
21 namespace SUNphi
22 {
23  /// Forward definition of serializable
24  template <typename T>
25  class Serializable;
26 
28 
29  /// Check if the class is a serializable map
30  template <typename T>
31  [[ maybe_unused ]]
32  constexpr bool isSerializableMap=
34 
36 
37  /// Stream to an output a YAML node
38  template <typename S,
40  const YAML::Node&>)>
41  S& operator<<(S&& stream,
42  const YAML::Node& node)
43  {
44  /// Converter to string
46  emitter<<node;
47 
48  stream<<
49  emitter.c_str();
50 
51  return
52  stream;
53  }
54  /// Stream to an output anything with a \c serialize member
55  template <typename S,
56  typename T,
57  SFINAE_ON_TEMPLATE_ARG(not canPrint<S,const T&>),
59  S& operator<<(S&& stream,
60  const T& ser)
61  {
62  stream<<
63  ser.serialize();
64 
65  return
66  stream;
67  }
68 }
69 
70 namespace YAML
71 {
72  using namespace SUNphi;
73 
74  /// Converter for any type providing member serialize
75  template<typename T>
77  {
78  /// Encode into a node
79  static Node encode(const T& rhs)
80  {
81  return
82  rhs.serialize();
83  }
84 
85  /// Decode from a node
86  static bool decode(const Node& node,
87  T& rhs)
88  {
90 
91  return
92  true;
93  }
94  };
95 }
96 
97 #endif
#define DEFINE_HAS_MEMBER(TAG)
Definition: TypeTraits.hpp:581
constexpr bool isSerializableMap
Check if the class is a serializable map.
Definition: Base.hpp:32
#define SFINAE_ON_TEMPLATE_ARG(...)
Definition: SFINAE.hpp:24
decltype(auto) operator+(T1 &&smet1, T2 &&smet2)
Implement smet1+smet2.
Definition: Add.hpp:87
static bool decode(const Node &node, T &rhs)
Decode from a node.
Definition: Base.hpp:86