Ariles
reader_compact.cpp
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4 
5  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
6  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
7 
8  @copyright 2017-2018 Alexander Sherikov, Licensed under the Apache License, Version 2.0.
9  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
10 
11  @brief
12 */
13 
14 #include <sstream>
15 #include <iostream>
16 #include <msgpack.hpp>
17 
19 
20 
21 namespace ariles2
22 {
23  namespace ns_msgpack_compact
24  {
26  }
27 } // namespace ariles2
28 
29 
30 namespace ariles2
31 {
32  namespace ns_msgpack_compact
33  {
34  namespace impl
35  {
37  {
38  public:
39  std::string buffer_;
40 
41  ::msgpack::object_handle handle_;
42 
43 
44  public:
45  template <class... t_Args>
46  explicit Reader(t_Args &&...args) : FileVisitorImplementation(std::forward<t_Args>(args)...)
47  {
48  initialize();
49  }
50 
51 
52  /**
53  * @brief open configuration file
54  */
55  void initialize()
56  {
57  std::stringstream str_stream;
58  str_stream << input_stream_->rdbuf();
59  buffer_ = str_stream.str();
60 
61  try
62  {
63  unpack(handle_, buffer_.data(), buffer_.size(), nullptr);
64  emplace(&handle_.get());
65  }
66  catch (const std::exception &e)
67  {
68  CPPUT_THROW("Failed to parse the configuration file: ", e.what());
69  }
70  }
71 
72 
73  /**
74  * @brief Get current node
75  *
76  * @return pointer to the current node
77  */
78  const ::msgpack::object &getRawNode(const std::size_t depth)
79  {
80  if (node_stack_[depth].isArray())
81  {
82  return (getRawNode(depth - 1).via.array.ptr[node_stack_[depth].index_]);
83  }
84  return (*node_stack_[depth].node_);
85  }
86 
87 
88  const ::msgpack::object &getRawNode()
89  {
90  return (getRawNode(node_stack_.size() - 1));
91  }
92  };
93  } // namespace impl
94  } // namespace ns_msgpack_compact
95 } // namespace ariles2
96 
97 
98 
99 namespace ariles2
100 {
101  namespace ns_msgpack_compact
102  {
103  Reader::Reader(const std::string &file_name)
104  {
105  makeImplPtr(file_name);
106  }
107 
108 
109  Reader::Reader(std::istream &input_stream)
110  {
111  makeImplPtr(input_stream);
112  }
113 
114 
115  void Reader::startMap(const SizeLimitEnforcementType limit_type, const std::size_t min, const std::size_t max)
116  {
117  const std::size_t size = impl_->getRawNode().via.array.size;
118  checkSize(limit_type, size, min, max);
119  impl_->emplace(0, size);
120  }
121 
122  bool Reader::startMapEntry(const std::string &)
123  {
124  if (impl_->back().isArray())
125  {
127  }
128  return (true);
129  }
130 
132  {
133  if (impl_->back().isArray())
134  {
135  endArrayElement();
136  }
137  }
138 
140  {
141  CPPUT_ASSERT(
142  impl_->back().isCompleted(), "Some entries were not parsed, which is not allowed by this visitor.");
143  impl_->pop();
144  }
145 
146 
147  std::size_t Reader::startArray()
148  {
149  const std::size_t size = impl_->getRawNode().via.array.size;
150  impl_->emplace(0, size);
151 
152  return (size);
153  }
154 
155 
157  {
158  impl_->pop();
159  }
160 
161 
163  {
164  CPPUT_ASSERT(
165  impl_->back().index_ < impl_->back().size_,
166  "Internal error: array has more elements than expected.");
167  }
168 
169 
171  {
172  impl_->shiftArray();
173  }
174 
175 
176 #define ARILES2_BASIC_TYPE(type) \
177  void Reader::readElement(type &element) \
178  { \
179  impl_->getRawNode() >> element; \
180  }
181 
183 
184 #undef ARILES2_BASIC_TYPE
185  } // namespace ns_msgpack_compact
186 } // namespace ariles2
void endMapEntry()
endMapEntry from the current entry to its parent.
void startMap(const SizeLimitEnforcementType limit_type=SIZE_LIMIT_NONE, const std::size_t min=0, const std::size_t max=0)
bool startMapEntry(const std::string &)
startMapEntry to the entry with the given name
Reader(const std::string &file_name)
Constructor.
const ::msgpack::object & getRawNode()
const ::msgpack::object & getRawNode(const std::size_t depth)
Get current node.
void initialize()
open configuration file
void checkSize(const SizeLimitEnforcementType limit_type, const std::size_t size=0, const std::size_t min=0, const std::size_t max=0) const
Definition: read.h:47
#define CPPUT_THROW(...)
Definition: exception.h:19
#define CPPUT_ASSERT(condition,...)
Definition: exception.h:32
#define ARILES2_BASIC_TYPES_LIST
Definition: helpers.h:72
#define CPPUT_MACRO_SUBSTITUTE(macro)
Definition: misc.h:21
Definition: basic.h:17