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 ariles
22 {
23  namespace ns_msgpack_compact
24  {
26  }
27 } // namespace ariles
28 
29 
30 namespace ariles
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(), 0);
62  node_stack_.push_back(NodeWrapper(&handle_.get()));
63  }
64  catch (const std::exception &e)
65  {
66  ARILES_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  else
83  {
84  return (*node_stack_[depth].node_);
85  }
86  }
87 
88 
89  const ::msgpack::object &getRawNode()
90  {
91  return (getRawNode(node_stack_.size() - 1));
92  }
93  };
94  } // namespace impl
95  } // namespace ns_msgpack_compact
96 } // namespace ariles
97 
98 
99 
100 namespace ariles
101 {
102  namespace ns_msgpack_compact
103  {
104  Reader::Reader(const std::string &file_name)
105  {
106  std::ifstream config_ifs;
107  read::Visitor::openFile(config_ifs, file_name);
108  impl_ = ImplPtr(new Impl());
109  impl_->initialize(config_ifs);
110  }
111 
112 
113  Reader::Reader(std::istream &input_stream)
114  {
115  impl_ = ImplPtr(new Impl());
116  impl_->initialize(input_stream);
117  }
118 
119 
120  std::size_t Reader::getMapSize(const bool /*expect_empty*/)
121  {
122  return (impl_->getRawNode().via.array.size);
123  }
124 
125  std::size_t Reader::startMapImpl(const std::size_t size)
126  {
127  impl_->node_stack_.push_back(NodeWrapper(0, size));
128  return (size);
129  }
130 
131 
133  {
134  ARILES_ASSERT(
135  true == impl_->node_stack_.back().isAllParsed(),
136  "Some entries were not parsed, which is not allowed by this visitor.");
137  impl_->node_stack_.pop_back();
138  }
139 
140 
142  {
143  if (true == impl_->node_stack_.back().isArray())
144  {
145  shiftArray();
146  }
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  ARILES_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: expected array.");
168  ARILES_ASSERT(
169  impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
170  "Internal error: array has more elements than expected.");
171  ++impl_->node_stack_.back().index_;
172  }
173 
174 
175 #define ARILES_BASIC_TYPE(type) \
176  void Reader::readElement(type &element) \
177  { \
178  impl_->getRawNode() >> element; \
179  }
180 
182 
183 #undef ARILES_BASIC_TYPE
184  } // namespace ns_msgpack_compact
185 } // namespace ariles
std::size_t startMapImpl(const std::size_t size)
std::vector< NodeWrapper > node_stack_
Stack of nodes.
Reader(const std::string &file_name)
Constructor.
void initialize(std::istream &input_stream)
open configuration file
const ::msgpack::object & getRawNode()
static void openFile(std::ifstream &config_ifs, const std::string &file_name)
open configuration file
Definition: read.h:81
const ::msgpack::object & getRawNode(const std::size_t depth)
Get current node.
#define ARILES_BASIC_TYPES_LIST
Definition: helpers.h:105
void ascend()
Ascend from the current entry to its parent.
std::size_t getMapSize(const bool)
ariles::Node< const ::msgpack::object * > NodeWrapper
ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_NUMERIC_TYPES_LIST) void Writer
Definition: writer.cpp:224
#define ARILES_VISIBILITY_ATTRIBUTE
Definition: helpers.h:69
Definition: basic.h:17