SUNphi  1.0
Serializable.hpp
Go to the documentation of this file.
1 #ifndef _SERIALIZE_SERIALIZABLE_HPP
2 #define _SERIALIZE_SERIALIZABLE_HPP
3 
4 /// \file serialize/Serializable.hpp
5 ///
6 /// \brief Define a serializable quantity
7 
8 #ifdef HAVE_CONFIG_H
9  #include <config.hpp>
10 #endif
11 
12 #include <yaml-cpp/yaml.h>
13 
14 #include <metaprogramming/TypeTraits.hpp>
15 #include <metaprogramming/UniversalReferences.hpp>
16 #include <serialize/Base.hpp>
17 #include <serialize/Binarize.hpp>
18 #include <serialize/Default.hpp>
19 #include <utility/Macros.hpp>
20 
21 namespace SUNphi
22 {
23  /// Provides a class with serializability features via CRTP
24  ///
25  /// The type can be only trivial, or a serializable class (map). A
26  /// dedicated wrapper is in place for a sequence
27  template <typename T>
28  class Serializable
30  , public Binarizable<Serializable<T>>
31  {
32  /// Detect if the value is a map
33  static constexpr bool isMap=
35 
37 
38  /// Stored variable
39  T value;
40 
41  public:
42 
43  /// Mapped type
44  using Type=
45  T;
46 
47  /// Access the value
48  const T& operator()()
49  const
50  {
51  return
52  value;
53  }
54 
56 
57  /// Name used to refer
58  const char* name;
59 
60  /// Creates a serializable with default value
61  template <typename...P>
62  explicit Serializable(const char* name, ///< Name to be used
63  P&&...def) ///< Default, if passed
64  :
66  value(forw<P>(def)...),
67  name(name)
68  {
69  // if constexpr(not isMap)
70  // this->putToDefault();
71  }
72 
73  /// Check if the value is default or not
74  bool isDefault()
75  const
76  {
77  // If the value can be checked, check it
79  return
80  value.isDefault();
81  else
82  // If reference is not a serializable class, check default
83  return
84  value==this->def;
85  }
86 
87  /// Returns a YAML node after serializing to it
89  const bool& onlyNonDefault=false)
90  const
91  {
92  if(not (onlyNonDefault and isDefault()))
93  {
95  node[name]=
97  else
98  node[name]=
99  value;
100  }
101 
102  return
103  node;
104  }
105 
106  /// Returns a YAML node
107  YAML::Node serialize(const bool& onlyNonDefault=false)
108  const
109  {
110  /// Returned value
111  YAML::Node node;
112 
113  return
115  }
116 
117  /// Convert from a YAML node
118  bool deSerialize(const YAML::Node& node) ///< Node from which to convert
119  {
120  if(not node[name])
121  this->putToDefault();
122  else
123  {
124  value=
125  node[name].template as<T>();
126  }
127 
128  return
129  true;
130  }
131 
132  /// Provide a simple friend binary operator
133 #define PROVIDE_SIMPLE_FRIEND_BINARY_OPERATOR(OP)
134  /*! Assignment operator */
135  template <typename O>
136  DECLAUTO friend operator OP (O& first,
137  const Serializable& second)
138  {
139  return
140  first OP second.value;
141  }
142 
143  /// Provide a simple binary operator
144 #define PROVIDE_SIMPLE_BINARY_OPERATOR(OP)
145  /*! Assignment operator */
146  template <typename O,
147  SFINAE_ON_TEMPLATE_ARG(not isSame<O,Serializable>)>
148  DECLAUTO operator OP (const O& oth)
149  {
150  return
151  value OP oth;
152  }
153 
156 
157  /// Assignment operator
158  ///
159  /// Need to stay on his own
160  template <typename O>
162  {
163  return
164  value=oth;
165  }
166 
167 #undef PROVIDE_SIMPLE_BINARY_OPERATOR
168 
169  /// Copy assignment operator
171  {
172  value=
173  oth.value;
174 
175  return
176  *this;
177  }
178 
179  /// Cast to base value
181  {
182  return
183  value;
184  }
185 
186  /// Cast to base value, const version
187  operator const T&()
188  const
189  {
190  return
191  value;
192  }
193  };
194 
196 }
197 
198 #endif
#define PROVIDE_ALL_BINARY_OPERATORS(PROVIDER)
Provides all binary operators through a simple macro.
Definition: Macros.hpp:49
#define PROVIDE_CRTP_CAST_OPERATOR(CLASS)
Definition: CRTP.hpp:16
#define DEFINE_IS_THE_TEMPLATED_CLASS(CLASS)
Provides a check returning whether the class is of a given kind.
Definition: TypeTraits.hpp:716
#define SFINAE_ON_TEMPLATE_ARG(...)
Definition: SFINAE.hpp:24
decltype(auto) operator+(T1 &&smet1, T2 &&smet2)
Implement smet1+smet2.
Definition: Add.hpp:87
#define PROVIDE_ALSO_NON_CONST_METHOD(NAME)
Definition: TypeTraits.hpp:376
#define DECLAUTO
Short name for decltype(auto)