Ariles
serialization.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4 
5  @copyright 2017-2020 Alexander Sherikov, Licensed under the Apache License, Version 2.0.
6  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
7 
8  @brief
9 */
10 
11 #pragma once
12 
13 #include "common.h"
14 
15 /**
16 @defgroup serialization Serialization
17 
18 @brief Serialization.
19 */
20 
21 namespace ariles2
22 {
23  /// @ingroup serialization
24  namespace serialization
25  {
27  {
28  public:
29  bool sloppy_maps_; /// Treat key values in maps as entry names if they are strings
30  bool sloppy_pairs_; /// Treat first entry in an std::pair as entry name if it is a string
31  bool explicit_matrix_size_; /// Specify matrix size even if it is known to be constant
32  bool fallback_to_string_floats_; /// Allow saving floats as strings if necessary
33  bool flat_matrices_; /// Save matrix as a single vector
34  bool allow_missing_entries_; /// Do not treat missing entries as errors
35  bool persistent_structure_; /// Hint: expect Ariles classes with constant number of entries
36 
37 
38  public:
39  explicit Parameters(const bool override_parameters = true) : visitor::Parameters(override_parameters)
40  {
41  sloppy_maps_ = false;
42  sloppy_pairs_ = false;
43  explicit_matrix_size_ = false;
45  flat_matrices_ = true;
46  allow_missing_entries_ = false;
47  persistent_structure_ = false;
48  }
49  };
50 
51 
52  template <class t_RawNode>
53  class Node
54  {
55  public:
56  enum class Type
57  {
58  UNDEFINED = 0,
59  GENERIC = 1,
60  ARRAY = 2,
61  MATRIX = 3,
62  VECTOR = 4,
63  ITERATED_MAP = 5
64  };
65 
66 
67  public:
68  t_RawNode node_;
69  std::size_t index_;
70  std::size_t size_;
72 
73 
74  public:
75  explicit Node(const Type type = Type::GENERIC)
76  {
78  type_ = type;
79  }
80 
81  explicit Node(t_RawNode node, const Type type = Type::GENERIC) : node_(node)
82  {
84  type_ = type;
85  index_ = 0;
86  size_ = 0;
87  }
88 
89  Node(const std::size_t index, const std::size_t size) : index_(index), size_(size)
90  {
92  type_ = Type::ARRAY; // NOLINT
93  } // NOLINT
94 
95  Node(t_RawNode node, const std::size_t index, const std::size_t size)
96  : node_(node), index_(index), size_(size)
97  {
100  }
101 
102  bool isMatrix() const
103  {
104  return (Type::MATRIX == type_);
105  }
106 
107  bool isVector() const
108  {
109  return (Type::VECTOR == type_);
110  }
111 
112  bool isArray() const
113  {
114  return (Type::ARRAY == type_);
115  }
116 
117  bool isCompleted() const
118  {
119  return (index_ >= size_);
120  }
121  };
122 
123 
124  template <class t_Node>
126  {
127  public:
128  std::vector<t_Node> node_stack_;
129 
130  public:
131  [[nodiscard]] t_Node &back()
132  {
133  return (node_stack_.back());
134  }
135 
136  [[nodiscard]] const t_Node &back() const
137  {
138  return (node_stack_.back());
139  }
140 
141  void clear()
142  {
143  node_stack_.clear();
144  }
145 
146  template <class... t_Args>
147  void emplace(t_Args &&...args)
148  {
149  node_stack_.emplace_back(std::forward<t_Args>(args)...);
150  }
151 
152  void pop()
153  {
154  node_stack_.pop_back();
155  }
156 
157  void shiftArray()
158  {
159  CPPUT_ASSERT(back().isArray(), "Internal error: expected array.");
160  ++back().index_;
161  }
162 
163  bool empty() const
164  {
165  return (node_stack_.empty());
166  }
167 
168 
169  template <typename... t_String>
170  std::string concatWithNode(t_String &&...strings) const
171  {
172  return (cpput::concat::simple(back().node_, std::forward<t_String>(strings)...));
173  }
174 
175  template <typename... t_String>
176  void concatWithNodeAndEmplace(t_String &&...strings)
177  {
178  emplace(concatWithNode(std::forward<t_String>(strings)...));
179  }
180  };
181 
182 
183  template <class t_Visitor, class t_Implementation>
184  class PIMPLVisitor : public t_Visitor
185  {
186  protected:
187  using Impl = t_Implementation;
188  using ImplPtr = std::shared_ptr<t_Implementation>;
189 
190  protected:
192 
193  private:
196 
197  protected:
200 
201  template <class... t_Args>
202  void makeImplPtr(t_Args &&...args)
203  {
204  impl_ = std::make_shared<Impl>(std::forward<t_Args>(args)...);
205  }
206  };
207 
208 
209  template <class t_Derived, class t_Parameters>
211  } // namespace serialization
212 } // namespace ariles2
std::string concatWithNode(t_String &&...strings) const
void concatWithNodeAndEmplace(t_String &&...strings)
Node(t_RawNode node, const Type type=Type::GENERIC)
Definition: serialization.h:81
Node(t_RawNode node, const std::size_t index, const std::size_t size)
Definition: serialization.h:95
Node(const Type type=Type::GENERIC)
Definition: serialization.h:75
Node(const std::size_t index, const std::size_t size)
Definition: serialization.h:89
std::shared_ptr< t_Implementation > ImplPtr
PIMPLVisitor(const PIMPLVisitor &)
void makeImplPtr(t_Args &&...args)
PIMPLVisitor & operator=(const PIMPLVisitor &)
bool fallback_to_string_floats_
Specify matrix size even if it is known to be constant.
Definition: serialization.h:32
bool persistent_structure_
Do not treat missing entries as errors.
Definition: serialization.h:35
bool allow_missing_entries_
Save matrix as a single vector.
Definition: serialization.h:34
bool flat_matrices_
Allow saving floats as strings if necessary.
Definition: serialization.h:33
bool sloppy_pairs_
Treat key values in maps as entry names if they are strings.
Definition: serialization.h:30
Parameters(const bool override_parameters=true)
Hint: expect Ariles classes with constant number of entries.
Definition: serialization.h:39
bool explicit_matrix_size_
Treat first entry in an std::pair as entry name if it is a string.
Definition: serialization.h:31
#define CPPUT_ASSERT(condition,...)
Definition: exception.h:32
Definition: basic.h:17
std::string simple(t_String &&...strings)
Definition: concat.h:38
#define CPPUT_TRACE_FUNCTION
Definition: trace.h:126