Skip to content

Commit bd0ed2b

Browse files
committed
+namevalue2 visitor
1 parent 01cca4f commit bd0ed2b

File tree

10 files changed

+539
-2
lines changed

10 files changed

+539
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ representation formats, in particular:
181181
<https://asherikov.github.io/ariles/2/group__rosparam.html>
182182

183183
* A set of flattened key-value pairs, output only, no dependencies:
184-
<https://asherikov.github.io/ariles/2/group__namevalue.html>
184+
<https://asherikov.github.io/ariles/2/group__namevalue2.html>
185185

186186
* `graphviz` dot files for diagram generation:
187187
<https://asherikov.github.io/ariles/2/group__graphviz.html>

extra_visitors/namevalue/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecated, use namevalue2.
2+
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set (TGT_ARILES_VISITOR_LIB "${PROJECT_NAME}_visitor_${ARILES_VISITOR}")
2+
3+
add_library(${TGT_ARILES_VISITOR_LIB} INTERFACE)
4+
5+
include(ariles_install_component)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
@file
3+
@author Alexander Sherikov
4+
5+
@copyright 2018-2024 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+
@defgroup namevalue2 NameValue2
13+
@ingroup config
14+
15+
@brief Generates a set of <std::string, double> pairs with flattened member names,
16+
e.g., <"ariles_class.class_member.real_member", 3.4>.
17+
*/
18+
19+
20+
#pragma once
21+
22+
#define ARILES2_VISITOR_INCLUDED_namevalue2
23+
24+
#include <ariles2/internal/helpers.h>
25+
#include <ariles2/visitors/config.h>
26+
27+
#include "./namevalue2/writer.h"
28+
29+
namespace ariles2
30+
{
31+
/**
32+
* @brief NameValue2 visitor.
33+
* @ingroup namevalue2
34+
*/
35+
struct ARILES2_VISIBILITY_ATTRIBUTE namevalue2
36+
{
37+
using Writer = ariles2::cfgwrite::Visitor<ns_namevalue2::Writer>;
38+
};
39+
} // namespace ariles2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
/**
2+
@file
3+
@author Alexander Sherikov
4+
5+
@copyright 2019 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+
#pragma once
12+
13+
#include <string>
14+
#include <vector>
15+
#include <utility>
16+
#include <memory>
17+
#include <boost/lexical_cast.hpp>
18+
19+
20+
namespace ariles2
21+
{
22+
namespace ns_namevalue2
23+
{
24+
class NameValueVector
25+
{
26+
public:
27+
using SharedPtr = std::shared_ptr<NameValueVector>;
28+
static inline SharedPtr make_shared()
29+
{
30+
return (std::make_shared<NameValueVector>());
31+
}
32+
33+
public:
34+
std::vector<std::pair<std::string, double>> name_value_pairs_;
35+
36+
public:
37+
std::string &name(const std::size_t index)
38+
{
39+
return (name_value_pairs_[index].first);
40+
}
41+
double &value(const std::size_t index)
42+
{
43+
return (name_value_pairs_[index].second);
44+
}
45+
46+
47+
void expand()
48+
{
49+
name_value_pairs_.resize(name_value_pairs_.size() + 1);
50+
}
51+
void reserve(const std::size_t size)
52+
{
53+
name_value_pairs_.reserve(size);
54+
}
55+
void clear()
56+
{
57+
name_value_pairs_.clear();
58+
}
59+
std::size_t size() const
60+
{
61+
return (name_value_pairs_.size());
62+
}
63+
};
64+
65+
66+
/**
67+
* @brief Configuration writer class
68+
*/
69+
template <class t_NameValueContainer>
70+
class ARILES2_VISIBILITY_ATTRIBUTE GenericWriter
71+
: public ariles2::write::Visitor,
72+
public serialization::NodeStackBase<serialization::Node<std::string>>
73+
{
74+
protected:
75+
std::size_t reserve_;
76+
77+
bool initialize_structure_;
78+
79+
const std::string separator_ = ".";
80+
const std::string bracket_left_ = "{";
81+
const std::string bracket_right_ = "}";
82+
83+
84+
public:
85+
std::shared_ptr<t_NameValueContainer> name_value_pairs_;
86+
std::size_t index_;
87+
88+
89+
protected:
90+
void expand()
91+
{
92+
if (index_ == name_value_pairs_->size())
93+
{
94+
name_value_pairs_->expand();
95+
}
96+
}
97+
98+
void expandReserve(const std::size_t size)
99+
{
100+
reserve_ += size;
101+
name_value_pairs_->reserve(reserve_);
102+
}
103+
104+
105+
public:
106+
explicit GenericWriter(const std::size_t reserve = 0)
107+
{
108+
name_value_pairs_ = std::make_shared<t_NameValueContainer>();
109+
110+
if (reserve > 0)
111+
{
112+
expandReserve(reserve);
113+
}
114+
reset();
115+
}
116+
117+
118+
explicit GenericWriter(
119+
const std::shared_ptr<t_NameValueContainer> &name_value_pairs,
120+
const std::size_t reserve = 0)
121+
{
122+
name_value_pairs_ = name_value_pairs;
123+
124+
if (reserve > 0)
125+
{
126+
expandReserve(reserve);
127+
}
128+
reset();
129+
}
130+
131+
132+
void flush()
133+
{
134+
}
135+
136+
137+
void reset(const bool initialize_structure = true)
138+
{
139+
if (initialize_structure)
140+
{
141+
name_value_pairs_->clear();
142+
}
143+
initialize_structure_ = initialize_structure;
144+
index_ = 0;
145+
reserve_ = 0;
146+
}
147+
148+
149+
virtual void startMap(const Parameters &, const std::size_t num_entries)
150+
{
151+
if (initialize_structure_)
152+
{
153+
expandReserve(num_entries);
154+
}
155+
}
156+
157+
virtual void startMapEntry(const std::string &map_name)
158+
{
159+
if (initialize_structure_)
160+
{
161+
if (empty())
162+
{
163+
emplace(map_name);
164+
}
165+
else
166+
{
167+
if (back().isArray())
168+
{
169+
concatWithNodeAndEmplace(
170+
bracket_left_,
171+
boost::lexical_cast<std::string>(back().index_),
172+
bracket_right_,
173+
separator_,
174+
map_name);
175+
}
176+
else
177+
{
178+
concatWithNodeAndEmplace(separator_, map_name);
179+
}
180+
}
181+
}
182+
}
183+
184+
virtual void endMapEntry()
185+
{
186+
if (initialize_structure_)
187+
{
188+
pop();
189+
}
190+
}
191+
192+
virtual void endMap()
193+
{
194+
}
195+
196+
197+
virtual bool startIteratedMap(const std::size_t /*num_entries*/, const Parameters &)
198+
{
199+
return (false);
200+
}
201+
202+
virtual void startArray(const std::size_t size, const bool /*compact*/ = false)
203+
{
204+
if (initialize_structure_)
205+
{
206+
expandReserve(size);
207+
if (back().isArray())
208+
{
209+
emplace(concatWithNode(std::string("_"), boost::lexical_cast<std::string>(back().index_)),
210+
0,
211+
size);
212+
}
213+
else
214+
{
215+
emplace(back().node_, 0, size);
216+
}
217+
}
218+
}
219+
220+
virtual void endArrayElement()
221+
{
222+
if (initialize_structure_)
223+
{
224+
shiftArray();
225+
}
226+
}
227+
228+
virtual void endArray()
229+
{
230+
if (initialize_structure_)
231+
{
232+
pop();
233+
}
234+
}
235+
236+
237+
#define ARILES2_BASIC_TYPE(type) \
238+
void writeElement(const type &element, const Parameters &) \
239+
{ \
240+
expand(); \
241+
if (initialize_structure_) \
242+
{ \
243+
name_value_pairs_->name(index_) = back().node_; \
244+
if (back().isArray()) \
245+
{ \
246+
name_value_pairs_->name(index_) += "_"; \
247+
name_value_pairs_->name(index_) += boost::lexical_cast<std::string>(back().index_); \
248+
} \
249+
} \
250+
name_value_pairs_->value(index_) = element; \
251+
++index_; \
252+
}
253+
254+
CPPUT_MACRO_SUBSTITUTE(ARILES2_BASIC_NUMERIC_TYPES_LIST)
255+
256+
#undef ARILES2_BASIC_TYPE
257+
258+
259+
void writeElement(const std::string & /*element*/, const Parameters &)
260+
{
261+
}
262+
};
263+
264+
265+
using Writer = GenericWriter<NameValueVector>;
266+
} // namespace ns_namevalue2
267+
} // namespace ariles2

tests/api_v2/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@ ariles_define_regression_test("232" "yaml_cpp") # No
8080
ariles_define_regression_test("233" "ANY" "${ARILES_TEST_ALL_VISITORS}") # subtree read
8181
ariles_define_regression_test("234" "ANY" "${ARILES_TEST_ALL_VISITORS}") # OptionalPointer
8282
ariles_define_regression_test("235" "ros2param") # Parameter declaration
83+
ariles_define_regression_test("236" "namevalue2") # namevalue2

tests/api_v2/all_enabled_visitors.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
// #include <ariles2/visitors/namevalue.h>
4949
// #endif
5050
//
51+
// #ifdef ARILES_VISITOR_namevalue2
52+
// #include <ariles2/visitors/namevalue2.h>
53+
// #endif
54+
//
5155
// #ifdef ARILES_VISITOR_graphviz
52-
// #include <ariles2/visitors/namevalue.h>
56+
// #include <ariles2/visitors/graphviz.h>
5357
// #endif

0 commit comments

Comments
 (0)