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 ariles2
16 {
17  namespace ns_rosparam
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_rosparam
31 } // namespace ariles2
32 
33 
34 namespace ariles2
35 {
36  namespace ns_rosparam
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::startMapEntry(const std::string &map_name)
57  {
58  if (impl_->node_stack_.empty())
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 
70  {
71  impl_->node_stack_.pop_back();
72  }
73 
74 
75  void Writer::startArray(const std::size_t size, const bool /*compact*/)
76  {
77  impl_->getRawNode().setSize(size);
78  impl_->node_stack_.push_back(NodeWrapper(0, size));
79  }
80 
82  {
83  ARILES2_ASSERT(
84  impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
85  "Internal error: namevalue.has more elements than expected.");
86  }
87 
89  {
90  ARILES2_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: expected array.");
91  ++impl_->node_stack_.back().index_;
92  }
93 
95  {
96  impl_->node_stack_.pop_back();
97  }
98 
99 
100 
101  void Writer::writeElement(const bool &element, const Parameters &)
102  {
103  impl_->getRawNode() = element;
104  }
105 
106 
107  void Writer::writeElement(const std::string &element, const Parameters &)
108  {
109  impl_->getRawNode() = element;
110  }
111 
112 
113  void Writer::startRoot(const std::string &name, const Parameters &)
114  {
115  ARILES2_TRACE_FUNCTION;
116 
117  impl_->root_name_ = "";
118  impl_->root_value_.clear();
119 
120  if (true == name.empty())
121  {
122  startMapEntry("ariles");
123  }
124  else
125  {
126  startMapEntry(name);
127  }
128  }
129 
130  void Writer::endRoot(const std::string & /*name*/)
131  {
132  ARILES2_TRACE_FUNCTION;
133  endMapEntry();
134  }
135 
136 
137 #define ARILES2_BASIC_TYPE(type) \
138  void Writer::writeElement(const type &element, const Parameters &) \
139  { \
140  impl_->getRawNode() = element; \
141  }
142 
144 
145 #undef ARILES2_BASIC_TYPE
146 
147 
148 
149 #define ARILES2_BASIC_TYPE(type) \
150  void Writer::writeElement(const type &element, const Parameters &) \
151  { \
152  ARILES2_ASSERT( \
153  static_cast<int64_t>(element) <= std::numeric_limits<int>::max() \
154  && static_cast<int64_t>(element) >= static_cast<int64_t>(std::numeric_limits<int>::min()), \
155  "Value is out of range."); \
156  impl_->getRawNode() = static_cast<int>(element); \
157  }
158 
160 
161 #undef ARILES2_BASIC_TYPE
162 
163 
164 #define ARILES2_BASIC_TYPE(type) \
165  void Writer::writeElement(const type &element, const Parameters &) \
166  { \
167  ARILES2_ASSERT( \
168  static_cast<uint64_t>(element) <= static_cast<uint64_t>(std::numeric_limits<int>::max()), \
169  "Value is too large."); \
170  impl_->getRawNode() = static_cast<int>(element); \
171  }
172 
174 
175 #undef ARILES2_BASIC_TYPE
176  } // namespace ns_rosparam
177 } // namespace ariles2
ariles2
Definition: basic.h:16
ariles2::ns_rosparam::Writer::endArray
void endArray()
Definition: writer.cpp:94
ariles2::ns_rosparam::impl::Writer
Definition: writer.cpp:21
ariles2::ns_rosparam::Writer::endRoot
void endRoot(const std::string &name)
Definition: writer.cpp:130
ariles2::ns_rosparam::ARILES2_MACRO_SUBSTITUTE
ARILES2_MACRO_SUBSTITUTE(ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST) ARILES2_MACRO_SUBSTITUTE(ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST) ARILES2_MACRO_SUBSTITUTE(ARILES2_BASIC_REAL_TYPES_LIST) void Reader
Definition: reader.cpp:188
ariles2::ns_rosparam::Writer::startRoot
void startRoot(const std::string &name, const Parameters &)
Definition: writer.cpp:113
ariles2::ns_rosparam::Writer::flush
void flush()
Flush the configuration to the output.
Definition: writer.cpp:44
ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST
#define ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:101
ariles2::ns_rosparam::ImplBase
Definition: common.h:28
ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST
#define ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:107
ariles2::ns_rosparam::Writer::endMapEntry
void endMapEntry()
Definition: writer.cpp:69
ariles2::serialization::PIMPLVisitor< write::Visitor, impl::Writer >::Impl
impl::Writer Impl
Definition: serialization.h:124
ARILES2_VISIBILITY_ATTRIBUTE
#define ARILES2_VISIBILITY_ATTRIBUTE
Definition: helpers.h:138
ariles2::ns_rosparam::Writer::startMapEntry
void startMapEntry(const std::string &map_name)
Starts a nested map in the configuration file.
Definition: writer.cpp:56
common.h
ariles2::serialization::PIMPLVisitor< write::Visitor, impl::Writer >::impl_
ImplPtr impl_
Definition: serialization.h:128
ariles2::ns_rosparam::NodeWrapper
serialization::Node< XmlRpc::XmlRpcValue * > NodeWrapper
Definition: common.h:25
ariles2::ns_rosparam::Writer::startArrayElement
void startArrayElement()
Definition: writer.cpp:81
ariles2::ns_rosparam::Writer::endArrayElement
void endArrayElement()
Definition: writer.cpp:88
ariles2::ns_rosparam::Writer::Writer
Writer(const ::ros::NodeHandle &nh)
Definition: writer.cpp:38
ARILES2_BASIC_REAL_TYPES_LIST
#define ARILES2_BASIC_REAL_TYPES_LIST
Definition: helpers.h:118
ariles2::write::VisitorBase< Visitor, Parameters >::writeElement
void writeElement(const std::complex< t_Scalar > &entry, const Parameters &param)
Definition: write.h:285
ariles2::ns_rosparam::Writer::startArray
void startArray(const std::size_t size, const bool=false)
Definition: writer.cpp:75
ariles2::ns_rosparam::impl::Writer::Writer
Writer(const ::ros::NodeHandle &nh)
Definition: writer.cpp:24
ariles2::serialization::PIMPLVisitor< write::Visitor, impl::Writer >::ImplPtr
ARILES2_SHARED_PTR< impl::Writer > ImplPtr
Definition: serialization.h:125