Ariles
types.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4 
5  @copyright 2019 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 namespace ariles
14 {
15  template <template <class> class t_Pointer, class t_Base, class t_Instantiator>
17  {
18 #define ARILES_SECTION_ID "Any"
19 #include ARILES_INITIALIZE
20 
21  protected:
22  bool isConsitent() const
23  {
24  if (("" != id_) && (NULL != value_.get()))
25  {
26  return (true);
27  }
28 
29  if (("" == id_) && (NULL == value_.get()))
30  {
31  return (true);
32  }
33 
34  return (false);
35  }
36 
37 
38  public:
39  std::string id_;
40  t_Pointer<t_Base> value_;
41 
42 
43  public:
44  Any()
45  {
46  setDefaults();
47  }
48 
49 
50  Any(const std::string &id)
51  {
52  build(id);
53  }
54 
55 
56  void build(const std::string &id)
57  {
58  id_ = id;
59  value_ = t_Instantiator::instantiate(id_);
60  ARILES_ASSERT(NULL != value_.get(), "Could not instantiate class.");
61  }
62 
63 
64  bool isInitialized() const
65  {
66  return ("" != id_ && NULL != value_.get());
67  }
68 
69 
70  /// @{
71  /**
72  * @brief Cast methods are potentially dangerous, no id checks are
73  * performed. If value is not initialized the returned pointer may
74  * be NULL.
75  */
76  template <class t_Derived>
77  t_Derived *cast()
78  {
79  return (dynamic_cast<t_Derived *>(value_.get()));
80  }
81 
82 
83  template <class t_Derived>
84  const t_Derived *cast() const
85  {
86  return (dynamic_cast<const t_Derived *>(value_.get()));
87  }
88  /// @}
89 
90 
91  /// @{
92  /**
93  * @brief These casts succeed if the Ariles config section id
94  * matches the given string.
95  */
96  template <class t_Derived>
97  t_Derived *cast(const std::string &config_section_id)
98  {
99  if (true == isInitialized())
100  {
101  if (config_section_id == value_->getConfigSectionID())
102  {
103  return (dynamic_cast<t_Derived *>(value_.get()));
104  }
105  }
106  return (NULL);
107  }
108 
109 
110  template <class t_Derived>
111  const t_Derived *cast(const std::string &config_section_id) const
112  {
113  if (true == isInitialized())
114  {
115  if (config_section_id == value_->getConfigSectionID())
116  {
117  return (dynamic_cast<t_Derived *>(value_.get()));
118  }
119  }
120  return (NULL);
121  }
122  /// @}
123 
124 
126  {
127  ARILES_ASSERT(true == isInitialized(), "Not initialized");
128  return (value_.get());
129  }
130 
131 
132  const t_Base *operator->() const
133  {
134  ARILES_ASSERT(true == isInitialized(), "Not initialized");
135  return (value_.get());
136  }
137 
138 
139  t_Base &operator*()
140  {
141  ARILES_ASSERT(true == isInitialized(), "Not initialized");
142  return (*value_);
143  }
144 
145 
146  const t_Base &operator*() const
147  {
148  ARILES_ASSERT(true == isInitialized(), "Not initialized");
149  return (*value_);
150  }
151 
152 
153  // Ariles methods
154 
156  {
157  ARILES_ASSERT(
158  true == isConsitent(),
159  "Could not write config: entry is in an inconsistent (partially initialized) state.");
160 
162  if (true == isInitialized())
163  {
164  ARILES_WRITE_NAMED_ENTRY(*value_, "value");
165  }
166  }
167 
168 
170  {
171  if (true == reader(id_, "id", parameters))
172  {
173  build(id_);
174  ariles::ConfigurableFlags param = parameters;
176  reader(*value_, "value", parameters);
177  }
178  }
179 
180 
182  {
183  if (true == isInitialized())
184  {
185  value_->finalize();
186  }
187  }
188 
189  void setDefaults()
190  {
191  id_ = "";
192  value_.reset();
193  }
194 
195 
196  std::size_t getNumberOfEntries() const
197  {
198  return (2);
199  }
200 
201 
202  template <class t_Other>
203  bool arilesCompare(const t_Other &other, const ariles::ComparisonParameters &param) const
204  {
206  if (true == param.compare_number_of_entries_)
207  {
208  if (getNumberOfEntries() != other.getNumberOfEntries())
209  {
210  return (false);
211  }
212  }
213 
214  if (this->id_ != other.id_)
215  {
216  return (false);
217  }
218 
219  if (NULL != value_.get() && NULL != other.value_.get())
220  {
221  ariles::compare::Visitor visitor;
222  return (visitor.compare(*value_, *(other.value_), param));
223  }
224  else
225  {
226  return (false);
227  }
228  }
229  };
230 } // namespace ariles
231 
232 
233 namespace ariles
234 {
235  template <class t_Pointer>
237  {
238 #define ARILES_SECTION_ID "NonNullPointer"
239 #include ARILES_INITIALIZE
240 
241  public:
242  typedef t_Pointer BasePointer;
243  typedef PointerHandler<t_Pointer> Handler;
244 
245 
246  public:
247  t_Pointer value_;
248 
249 
250  public:
252  {
253  setDefaults();
254  }
255 
256 
257  NonNullPointer(const t_Pointer &value)
258  {
259  value_ = value;
260  }
261 
262 
263  operator BasePointer &()
264  {
265  return (value_);
266  }
267 
268  operator const BasePointer &() const
269  {
270  return (value_);
271  }
272 
273 
274  NonNullPointer(const typename Handler::Value &value)
275  {
276  Handler::allocate(value_);
277  *value_ = value;
278  }
279 
280 
281  virtual ~NonNullPointer()
282  {
283  }
284 
285 
286  typename Handler::Value *operator->() const
287  {
288  ARILES_ASSERT(false == isNull(), "Not initialized");
289  return (value_.get());
290  }
291 
292 
293  typename Handler::Value &operator*() const
294  {
295  ARILES_ASSERT(false == isNull(), "Not initialized");
296  return (*value_);
297  }
298 
299 
300  void writeConfigEntries(ariles::WriterBase &writer, const ariles::ConfigurableFlags &parameters) const
301  {
302  ARILES_ASSERT(false == isNull(), "Could not write config: entry is not initialized");
303  value_->writeConfigEntries(writer, parameters);
304  }
305 
306 
308  {
309  Handler::allocate(value_);
310  value_->readConfigEntries(reader, parameters);
311  }
312 
313 
315  {
316  ARILES_ASSERT(false == isNull(), "Not initialized");
317  }
318 
319 
320  void setDefaults()
321  {
322  Handler::allocate(value_);
323  value_->setDefaults();
324  }
325 
326 
327  std::size_t getNumberOfEntries() const
328  {
329  ARILES_ASSERT(false == isNull(), "Not initialized");
330  return (value_->getNumberOfEntries());
331  }
332 
333 
334  bool isNull() const
335  {
336  return (Handler::isNull(value_));
337  }
338 
339 
340  template <class t_Other>
341  bool arilesCompare(const t_Other &other, const ariles::ComparisonParameters &param) const
342  {
344  return (value_->arilesCompare(*other.value_, param));
345  }
346  };
347 } // namespace ariles
#define ARILES_TRACE_FUNCTION
Definition: trace.h:118
t_Pointer< t_Base > value_
Definition: types.h:40
bool isInitialized() const
Definition: types.h:64
bool arilesCompare(const t_Other &other, const ariles::ComparisonParameters &param) const
Definition: types.h:341
bool compare(const t_Left &left, const t_Right &right, const Parameters &param)
Definition: compare.h:67
NonNullPointer(const typename Handler::Value &value)
Definition: types.h:274
void arilesFinalize()
Definition: types.h:181
t_Derived * cast(const std::string &config_section_id)
These casts succeed if the Ariles config section id matches the given string.
Definition: types.h:97
void writeConfigEntries(ariles::WriterBase &writer, const ariles::ConfigurableFlags &param) const
Definition: types.h:155
void setDefaults()
Definition: types.h:189
Handler::Value * operator->() const
Definition: types.h:286
t_Pointer value_
Definition: types.h:247
void writeConfigEntries(ariles::WriterBase &writer, const ariles::ConfigurableFlags &parameters) const
Definition: types.h:300
const t_Derived * cast() const
Definition: types.h:84
std::size_t getNumberOfEntries() const
Get number of entries in the corresponding configuration node.
Definition: types.h:196
t_Pointer BasePointer
Definition: types.h:242
void readConfigEntries(ariles::ReaderBase &reader, const ariles::ConfigurableFlags &parameters)
Definition: types.h:307
std::size_t getNumberOfEntries() const
Get number of entries in the corresponding configuration node.
Definition: types.h:327
NonNullPointer(const t_Pointer &value)
Definition: types.h:257
t_Derived * cast()
Cast methods are potentially dangerous, no id checks are performed. If value is not initialized the r...
Definition: types.h:77
std::string id_
Definition: types.h:39
void readConfigEntries(ariles::ReaderBase &reader, const ariles::ConfigurableFlags &parameters)
Definition: types.h:169
const t_Derived * cast(const std::string &config_section_id) const
Definition: types.h:111
bool isNull() const
Definition: types.h:334
#define ARILES_WRITE_NAMED_ENTRY(entry, name)
Definition: ariles.h:38
bool isConsitent() const
Definition: types.h:22
bool arilesCompare(const t_Other &other, const ariles::ComparisonParameters &param) const
Definition: types.h:203
virtual ~NonNullPointer()
Definition: types.h:281
Default configurable base.
Definition: ariles.h:348
#define ARILES_WRITE_ENTRY_(entry)
Definition: ariles.h:40
#define ARILES_VISIBILITY_ATTRIBUTE
Definition: helpers.h:69
PointerHandler< t_Pointer > Handler
Definition: types.h:243
t_Base * operator->()
Definition: types.h:125
Definition: basic.h:17
void arilesFinalize()
Definition: types.h:314
const t_Base * operator->() const
Definition: types.h:132
Any(const std::string &id)
Definition: types.h:50
void build(const std::string &id)
Definition: types.h:56