SUNphi  1.0
Map.hpp
Go to the documentation of this file.
1 #ifndef _SERIALIZE_MAP_HPP
2 #define _SERIALIZE_MAP_HPP
3 
4 /// \file serialize/Map.hpp
5 ///
6 /// \brief Creates a list of a set of serializable scalars
7 ///
8 /// The list is a tuple, which can be added as a member to a class, to
9 /// make the class itself serializable
10 
11 #include <yaml-cpp/yaml.h>
12 
13 #include <metaprogramming/CRTP.hpp>
14 #include <serialize/Base.hpp>
15 #include <serialize/Serializable.hpp>
16 #include <tuple/TupleClass.hpp>
17 
18 #include <ios/Logger.hpp>
19 
20 namespace SUNphi
21 {
22  /// Helper function to create the serializable members
23  template <typename...Ts>
24  auto _serializableMembers(Ts&&...ts)
25  {
26  /// Check that all arguments are SerializableScalar
27  static_assert((isSerializable<Ts> && ...),"All arguments need to be Serializable");
28 
29  return
30  std::forward_as_tuple(ts...);
31  }
32 
33  /// Provides the class embedding with a serializableMemebers method
34 #define CONST_OR_NOT_SERIALIZABLE_MEMBERS(CONST,
35  ...)
36  /*! Gets the reference to the serializable members */
37  auto serializableMembers()
38  CONST
39  {
40  return
41  std::forward_as_tuple(__VA_ARGS__);
42  }
43 
44  /// Provides serialization features to a class
45  template <typename T>
47  {
48  public:
49 
51 
52  /// Check if the class is default
53  bool isDefault()
54  const
55  {
56  /// Default value
57  bool def=
58  true;
59 
60  forEach(CRTP_THIS.serializableMembers(),
61  [&def](auto& s)
62  {
63  def&=
64  s.isDefault();
65  });
66 
67  return
68  def;
69  }
70 
71  /// Convert to YAML node
72  YAML::Node serialize(YAML::Node& node, ///< Resulting node
73  const bool& onlyNonDefault=false) ///< Prints only non-default
74  const
75  {
76  forEach(CRTP_THIS.serializableMembers(),
77  [&node,&onlyNonDefault](auto& s)
78  {
79  s.serialize(node,onlyNonDefault);
80  });
81 
82  return
83  node;
84  }
85 
86  /// Convert to YAML node
87  YAML::Node serialize(const bool& onlyNonDefault=false) ///< Prints only non-default
88  const
89  {
90  /// Resulting node
91  YAML::Node node;
92 
93  return
94  serialize(node,onlyNonDefault);
95  }
96 
97  /// Put the class to the default value
98  void putToDefault()
99  {
100  forEach(CRTP_THIS.serializableMembers(),
101  [](auto& s)
102  {
103  s.putToDefault();
104  });
105  }
106 
107  /// Check that no spurious subnodes are contained
108  void checkNoSpuriousSubNodes(const YAML::Node& node) ///< Node to be checked
109  {
110  /// Spurious list
111  std::string spurious;
112 
113  for(auto& subNode : node)
114  {
115  /// Mark found or not
116  bool found=
117  false;
118 
119  /// Expected name
120  const std::string name=
121  subNode.first.as<std::string>();
122 
123  forEach(CRTP_THIS.serializableMembers(),
124  [name,&found](auto& s)
125  {
126  found|=
127  s.name==name;
128  });
129 
130  if(not found)
131  {
132  if(spurious.size())
133  spurious+=
134  ", ";
135 
136  spurious+=
137  name;
138  }
139  }
140 
141  if(spurious.size())
142  CRASH<<"Spurious tags: "<<spurious;
143  }
144 
145  /// Convert from a YAML node
146  bool deSerialize(const YAML::Node& node) ///< Node from which to convert
147  {
148  if(not node.IsMap())
149  CRASH<<"Needs to be a map, is: "<<(node.IsNull()?"Null":(node.IsScalar()?"Scalar":(node.IsSequence()?"Sequence":"Unknown")));
150 
151  forEach(CRTP_THIS.serializableMembers(),
152  [&node](auto& s)
153  {
154  s.deSerialize(node);
155  });
156 
158 
159  return
160  true;
161  }
162 
163  /// Convert to binary
164  template <typename B=Binarizer>
165  Binarizer& binarize(B&& out={}) ///< Output
166  const
167  {
168  forEach(CRTP_THIS.serializableMembers(),
169  [&out](auto& s)
170  {
171  out.binarize(s);
172  });
173 
174  return
175  out;
176  }
177 
178  /// Convert from binary
179  template <typename B=Binarizer>
180  Binarizer& deBinarize(B&& in) ///< Input
181  {
182  forEach(CRTP_THIS.serializableMembers(),
183  [&in](auto& s)
184  {
185  in.deBinarize(s);
186  });
187 
188  return
189  in;
190  }
191  };
192 
193  /// Shortcut to define a serializable class
194 #define DEFINE_SERIALIZABLE_CLASS(T)
195  class T :
196  public SerializableClass<T>
197 
198  /// Defines a serializable class
199 #define SERIALIZABLE_CLASS(TYPE,NAME)
200  Serializable<TYPE> NAME{#NAME}
201 
202  /// Defines a list of serializable members
203 #define LIST_SERIALIZABLE_MEMBERS(...)
204  CONST_OR_NOT_SERIALIZABLE_MEMBERS( , __VA_ARGS__);
205  CONST_OR_NOT_SERIALIZABLE_MEMBERS(const, __VA_ARGS__);
206 }
207 
208 #endif
auto _serializableMembers(Ts &&...ts)
Helper function to create the serializable members.
Definition: Map.hpp:24
bool deSerialize(const YAML::Node &node)
Convert from a YAML node.
Definition: Map.hpp:146
#define CRTP_THIS
Access to the inheriting class.
Definition: CRTP.hpp:32
Binarizer & deBinarize(B &&in)
Convert from binary.
Definition: Map.hpp:180
#define CRASH
Initialize the crasher.
Definition: Crash.hpp:13
#define PROVIDE_CRTP_CAST_OPERATOR(CLASS)
Definition: CRTP.hpp:16
Binarizer & binarize(B &&out={}) const
Convert to binary.
Definition: Map.hpp:165
void clear()
Restart to write.
Definition: Binarize.hpp:204
YAML::Node serialize(YAML::Node &node, const bool &onlyNonDefault=false) const
Convert to YAML node.
Definition: Map.hpp:72
YAML::Node serialize(const bool &onlyNonDefault=false) const
Convert to YAML node.
Definition: Map.hpp:87
void checkNoSpuriousSubNodes(const YAML::Node &node)
Check that no spurious subnodes are contained.
Definition: Map.hpp:108
#define CONST_OR_NOT_SERIALIZABLE_MEMBERS(CONST,...)
Provides the class embedding with a serializableMemebers method.
Definition: Map.hpp:34
decltype(auto) operator+(T1 &&smet1, T2 &&smet2)
Implement smet1+smet2.
Definition: Add.hpp:87
bool isDefault() const
Check if the class is default.
Definition: Map.hpp:53
static bool decode(const Node &node, T &rhs)
Decode from a node.
Definition: Base.hpp:86
Provides serialization features to a class.
Definition: Map.hpp:46
void putToDefault()
Put the class to the default value.
Definition: Map.hpp:98