Ariles
writer.cpp
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4 
5  @copyright 2018 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 
12 #include "common.h"
13 
14 
15 namespace ariles
16 {
17  namespace ns_ros
18  {
19  namespace impl
20  {
22  {
23  public:
24  explicit Writer(const ::ros::NodeHandle &nh)
25  {
26  nh_ = nh;
27  }
28  };
29  } // namespace impl
30  } // namespace ns_ros
31 } // namespace ariles
32 
33 
34 namespace ariles
35 {
36  namespace ns_ros
37  {
38  Writer::Writer(const ::ros::NodeHandle &nh)
39  {
40  impl_ = ImplPtr(new Impl(nh));
41  }
42 
43 
45  {
46  if (XmlRpc::XmlRpcValue::TypeInvalid == impl_->root_value_.getType())
47  {
48  impl_->root_value_ = "";
49  }
50  impl_->nh_.setParam(impl_->root_name_, impl_->root_value_);
51  impl_->root_name_.clear();
52  }
53 
54 
55 
56  void Writer::descend(const std::string &map_name)
57  {
58  if (0 == impl_->node_stack_.size())
59  {
60  impl_->root_name_ = map_name;
61  impl_->node_stack_.push_back(&impl_->root_value_);
62  }
63  else
64  {
65  impl_->node_stack_.push_back(NodeWrapper(&(impl_->getRawNode()[map_name])));
66  }
67  }
68 
69 
71  {
72  impl_->node_stack_.pop_back();
73  }
74 
75 
76  void Writer::startArray(const std::size_t size, const bool /*compact*/)
77  {
78  impl_->getRawNode().setSize(size);
79  impl_->node_stack_.push_back(NodeWrapper(0, size));
80  }
81 
83  {
84  ARILES_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: expected array.");
85  ARILES_ASSERT(
86  impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
87  "Internal error: array has more elements than expected.");
88  ++impl_->node_stack_.back().index_;
89  }
90 
92  {
93  impl_->node_stack_.pop_back();
94  }
95 
96 
97 
98  void Writer::writeElement(const bool &element)
99  {
100  impl_->getRawNode() = element;
101  }
102 
103 
104  void Writer::writeElement(const std::string &element)
105  {
106  impl_->getRawNode() = element;
107  }
108 
109 
110  void Writer::startRoot(const std::string &name)
111  {
113 
114  impl_->root_name_ = "";
115  impl_->root_value_.clear();
116 
117  if (true == name.empty())
118  {
119  descend("ariles");
120  }
121  else
122  {
123  descend(name);
124  }
125  }
126 
127  void Writer::endRoot(const std::string & /*name*/)
128  {
130  ascend();
131  }
132 
133 
134 #define ARILES_BASIC_TYPE(type) \
135  void Writer::writeElement(const type &element) \
136  { \
137  impl_->getRawNode() = element; \
138  }
139 
141 
142 #undef ARILES_BASIC_TYPE
143 
144 
145 
146 #define ARILES_BASIC_TYPE(type) \
147  void Writer::writeElement(const type &element) \
148  { \
149  ARILES_ASSERT( \
150  static_cast<int64_t>(element) <= std::numeric_limits<int>::max() \
151  && static_cast<int64_t>(element) >= static_cast<int64_t>(std::numeric_limits<int>::min()), \
152  "Value is out of range."); \
153  impl_->getRawNode() = static_cast<int>(element); \
154  }
155 
157 
158 #undef ARILES_BASIC_TYPE
159 
160 
161 #define ARILES_BASIC_TYPE(type) \
162  void Writer::writeElement(const type &element) \
163  { \
164  ARILES_ASSERT( \
165  static_cast<uint64_t>(element) <= static_cast<uint64_t>(std::numeric_limits<int>::max()), \
166  "Value is too large."); \
167  impl_->getRawNode() = static_cast<int>(element); \
168  }
169 
171 
172 #undef ARILES_BASIC_TYPE
173  } // namespace ns_ros
174 } // namespace ariles
#define ARILES_TRACE_FUNCTION
Definition: trace.h:118
#define ARILES_BASIC_UNSIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:86
Writer(const ::ros::NodeHandle &nh)
Definition: writer.cpp:38
void startRoot(const std::string &name)
Definition: writer.cpp:110
ariles::Node< XmlRpc::XmlRpcValue * > NodeWrapper
Definition: common.h:25
#define ARILES_BASIC_SIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:80
void startArray(const std::size_t size, const bool=false)
Definition: writer.cpp:76
#define ARILES_BASIC_REAL_TYPES_LIST
Definition: helpers.h:96
ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_SIGNED_INTEGER_TYPES_LIST) ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_UNSIGNED_INTEGER_TYPES_LIST) ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_REAL_TYPES_LIST) void Reader
Definition: reader.cpp:172
void flush()
Flush the configuration to the output.
Definition: writer.cpp:44
Writer(const ::ros::NodeHandle &nh)
Definition: writer.cpp:24
#define ARILES_VISIBILITY_ATTRIBUTE
Definition: helpers.h:69
ARILES_SHARED_PTR< impl::Writer > ImplPtr
Definition: ros.h:31
Definition: basic.h:17
void endRoot(const std::string &name)
Definition: writer.cpp:127
void descend(const std::string &map_name)
Starts a nested map in the configuration file.
Definition: writer.cpp:56