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  /// Stack of nodes.
44  std::vector<NodeWrapper> node_stack_;
45 
46 
47  public:
48  /**
49  * @brief open configuration file
50  *
51  * @param[in] input_stream
52  */
53  void initialize(std::istream &input_stream)
54  {
55  std::stringstream str_stream;
56  str_stream << input_stream.rdbuf();
57  buffer_ = str_stream.str();
58 
59  try
60  {
61  unpack(handle_, buffer_.data(), buffer_.size(), NULL);
62  node_stack_.push_back(NodeWrapper(&handle_.get()));
63  }
64  catch (const std::exception &e)
65  {
66  ARILES2_THROW(std::string("Failed to parse the configuration file: ") + e.what());
67  }
68  }
69 
70 
71  /**
72  * @brief Get current node
73  *
74  * @return pointer to the current node
75  */
76  const ::msgpack::object &getRawNode(const std::size_t depth)
77  {
78  if (node_stack_[depth].isArray())
79  {
80  return (getRawNode(depth - 1).via.array.ptr[node_stack_[depth].index_]);
81  }
82  return (*node_stack_[depth].node_);
83  }
84 
85 
86  const ::msgpack::object &getRawNode()
87  {
88  return (getRawNode(node_stack_.size() - 1));
89  }
90  };
91  } // namespace impl
92  } // namespace ns_msgpack_compact
93 } // namespace ariles2
94 
95 
96 
97 namespace ariles2
98 {
99  namespace ns_msgpack_compact
100  {
101  Reader::Reader(const std::string &file_name)
102  {
103  std::ifstream config_ifs;
104  read::Visitor::openFile(config_ifs, file_name);
105  impl_ = ImplPtr(new Impl());
106  impl_->initialize(config_ifs);
107  }
108 
109 
110  Reader::Reader(std::istream &input_stream)
111  {
112  impl_ = ImplPtr(new Impl());
113  impl_->initialize(input_stream);
114  }
115 
116 
117  void Reader::startMap(const SizeLimitEnforcementType limit_type, const std::size_t min, const std::size_t max)
118  {
119  const std::size_t size = impl_->getRawNode().via.array.size;
120  checkSize(limit_type, size, min, max);
121  impl_->node_stack_.push_back(NodeWrapper(0, size));
122  }
123 
124  bool Reader::startMapEntry(const std::string &)
125  {
126  if (true == impl_->node_stack_.back().isArray())
127  {
129  }
130  return (true);
131  }
132 
134  {
135  if (true == impl_->node_stack_.back().isArray())
136  {
137  endArrayElement();
138  }
139  }
140 
142  {
143  ARILES2_ASSERT(
144  true == impl_->node_stack_.back().isAllParsed(),
145  "Some entries were not parsed, which is not allowed by this visitor.");
146  impl_->node_stack_.pop_back();
147  }
148 
149 
150  std::size_t Reader::startArray()
151  {
152  std::size_t size = impl_->getRawNode().via.array.size;
153  impl_->node_stack_.push_back(NodeWrapper(0, size));
154 
155  return (size);
156  }
157 
158 
160  {
161  impl_->node_stack_.pop_back();
162  }
163 
164 
166  {
167  ARILES2_ASSERT(
168  impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
169  "Internal error: namevalue.has more elements than expected.");
170  }
171 
172 
174  {
175  ARILES2_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: expected array.");
176  ++impl_->node_stack_.back().index_;
177  }
178 
179 
180 #define ARILES2_BASIC_TYPE(type) \
181  void Reader::readElement(type &element) \
182  { \
183  impl_->getRawNode() >> element; \
184  }
185 
187 
188 #undef ARILES2_BASIC_TYPE
189  } // namespace ns_msgpack_compact
190 } // namespace ariles2
ariles2::ns_msgpack_compact::Reader::startArray
std::size_t startArray()
Definition: reader_compact.cpp:150
ariles2::ns_msgpack_compact::Reader::startMap
void startMap(const SizeLimitEnforcementType limit_type=SIZE_LIMIT_NONE, const std::size_t min=0, const std::size_t max=0)
Definition: reader_compact.cpp:117
ariles2
Definition: basic.h:16
ariles2::ns_msgpack_compact::impl::Reader
Definition: reader_compact.cpp:36
ariles2::ns_msgpack_compact::Reader::endArray
void endArray()
Definition: reader_compact.cpp:159
ariles2::ns_msgpack_compact::impl::Reader::buffer_
std::string buffer_
Definition: reader_compact.cpp:39
ariles2::read::Visitor::checkSize
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:62
ariles2::ns_msgpack_compact::Reader::startMapEntry
bool startMapEntry(const std::string &)
startMapEntry to the entry with the given name
Definition: reader_compact.cpp:124
ariles2::ns_msgpack_compact::impl::Reader::handle_
::msgpack::object_handle handle_
Definition: reader_compact.cpp:41
ARILES2_BASIC_TYPES_LIST
#define ARILES2_BASIC_TYPES_LIST
Definition: helpers.h:131
ariles2::read::Visitor::SizeLimitEnforcementType
SizeLimitEnforcementType
Definition: read.h:47
ariles2::ns_msgpack_compact::impl::Reader::node_stack_
std::vector< NodeWrapper > node_stack_
Stack of nodes.
Definition: reader_compact.cpp:44
ariles2::ns_msgpack_compact::NodeWrapper
serialization::Node< const ::msgpack::object * > NodeWrapper
Definition: reader_compact.cpp:25
ariles2::ns_octave::ARILES2_MACRO_SUBSTITUTE
ARILES2_MACRO_SUBSTITUTE(ARILES2_BASIC_NUMERIC_TYPES_LIST) void Writer
Definition: writer.cpp:255
ariles2::ns_msgpack_compact::Reader::Reader
Reader(const std::string &file_name)
Constructor.
Definition: reader_compact.cpp:101
ariles2::ns_msgpack_compact::impl::Reader::initialize
void initialize(std::istream &input_stream)
open configuration file
Definition: reader_compact.cpp:53
msgpack.h
ariles2::ns_msgpack_compact::impl::Reader::getRawNode
const ::msgpack::object & getRawNode(const std::size_t depth)
Get current node.
Definition: reader_compact.cpp:76
ariles2::ns_msgpack_compact::Reader::endMap
void endMap()
Definition: reader_compact.cpp:141
ariles2::read::Visitor::openFile
static void openFile(std::ifstream &config_ifs, const std::string &file_name)
open configuration file
Definition: read.h:95
ariles2::serialization::PIMPLVisitor< read::Visitor, impl::Reader >::Impl
impl::Reader Impl
Definition: serialization.h:124
ARILES2_VISIBILITY_ATTRIBUTE
#define ARILES2_VISIBILITY_ATTRIBUTE
Definition: helpers.h:138
ariles2::ns_msgpack_compact::Reader::endMapEntry
void endMapEntry()
endMapEntry from the current entry to its parent.
Definition: reader_compact.cpp:133
ariles2::serialization::PIMPLVisitor< read::Visitor, impl::Reader >::impl_
ImplPtr impl_
Definition: serialization.h:128
ariles2::ns_msgpack_compact::impl::Reader::getRawNode
const ::msgpack::object & getRawNode()
Definition: reader_compact.cpp:86
ariles2::ns_msgpack_compact::Reader::endArrayElement
void endArrayElement()
Definition: reader_compact.cpp:173
ariles2::serialization::Node
Definition: serialization.h:49
ariles2::ns_msgpack_compact::Reader::startArrayElement
void startArrayElement()
Definition: reader_compact.cpp:165
ariles2::serialization::PIMPLVisitor< read::Visitor, impl::Reader >::ImplPtr
ARILES2_SHARED_PTR< impl::Reader > ImplPtr
Definition: serialization.h:125