Ariles
Loading...
Searching...
No Matches
demo_api_v2.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// cppcheck-suppress-file duplInheritedMember
11
12
13// ============================================================================
14// HEADER INCLUSION
15// ============================================================================
16
17// `visitor` is an Ariles component which provides integration with a particular
18// 3rd party library.
22
23// `adapter` is an Ariles component which adds support for serialization of
24// certain type(s), e.g. Eigen types or Boost pointers.
26#include <ariles2/adapters/eigen.h>
29#include <ariles2/ariles.h>
30
31
32
33// ===============================================================
34// DEFINING TYPES
35// ===============================================================
36namespace demo
37{
39 // must inherit from ariles2::DefaultBase
41 {
42// Declare entries, in this case two numbers
43#define ARILES2_ENTRIES(v) \
44 ARILES2_TYPED_ENTRY(v, real_member, double) \
45 ARILES2_TYPED_ENTRY_(v, integer_member, int)
46// underscore ^ indicates that the name of the entry must be
47// 'integer_member_' instead of 'integer_member', this is useful if your
48// naming convention requires trailing underscores for member variables.
49
50// Initialize ariles
51#include ARILES2_INITIALIZE
52
53 public:
54 virtual ~ArilesBaseClass() = default; // added to suppress compiler warnings
55
56 // This method is called every time you deserialize a class. If
57 // omitted, the default automatically generated method is used.
58 void arilesVisit(const ariles2::Defaults & /*visitor*/, const ariles2::Defaults::Parameters & /*param*/)
59 {
60 real_member = 0.0;
61 integer_member_ = 12;
62 }
63 };
64
65
67 {
68 public:
69 // Eigen types are supported too, see below
70 Eigen::Vector3d eigen_vector_;
71 };
72
73
74 class MyClass : public ArilesBaseClass, // no need to inherit from ariles2::DefaultBase directly.
76 {
77// Declare entries, in this case we indicate inheritance from another
78// Ariles class (ArilesBaseClass) and a member from a non-Ariles class
79// (NonArilesBaseClass)
80#define ARILES2_ENTRIES(v) \
81 ARILES2_PARENT(v, ArilesBaseClass) \
82 ARILES2_ENTRY_(v, eigen_vector)
83 // In this case ^ Ariles should not declare the inherited
84 // member, therefore we use 'ARILES2_ENTRY_' instead of 'ARILES2_TYPED_ENTRY_'.
85
86#include ARILES2_INITIALIZE
87
88
89 public:
90 ~MyClass() override = default; // added to suppress compiler warnings
91
92
94 {
95 // If you use your own method to initialize member variables,
96 // it is up to you to properly initialize all entries and
97 // parent classes.
98 // all parents at once
99 arilesVisitParents(visitor, param);
100 // or one by one (either option is sufficient)
101 ArilesBaseClass::arilesVisit(visitor, param);
102
103 // custom default values for some members
104 real_member = 100.0;
105 eigen_vector_.setZero();
106 }
107 };
108
109
111 {
112 // Some of the standard containers can be used with Ariles types.
113#define ARILES2_ENTRIES(v) \
114 ARILES2_TYPED_ENTRY_(v, my_class_vector, std::vector<MyClass>) \
115 ARILES2_TYPED_ENTRY_(v, ptr, std::shared_ptr<MyClass>)
116#include ARILES2_INITIALIZE
117 };
118} // namespace demo
119
120
121// ===============================================================
122// SERIALIZATION & DESERIALIZATION
123// ===============================================================
124
125#include <iostream> // std::cout
126
127int main()
128{
129 try
130 {
131 demo::MyContainerClass my_container_class;
132
133 // access members as usual
134 my_container_class.my_class_vector_.size();
135 my_container_class.my_class_vector_.emplace_back();
136 ariles2::apply<ariles2::Defaults>(my_container_class.my_class_vector_[0]);
137
138
139 // YAML
140 /*
141 * When you serialize `my_container_class` to YAML you get the following:
142 * -----
143 MyContainerClass:
144 my_class_vector:
145 - real_member: 100
146 integer_member: 12
147 eigen_vector: [0, 0, 0]
148 * -----
149 * Note that the trailing underscores are omitted for all members. This
150 * applies to all supported representations.
151 */
152 {
153 // You can read and write YAML configuration files as follows:
154 ariles2::apply<ariles2::yaml_cpp::Writer>("config.yaml", my_container_class);
155 ariles2::apply<ariles2::yaml_cpp::Reader>("config.yaml", my_container_class);
156
157 // Sometimes it may be useful to dump configuration to std::cout
158 ariles2::apply<ariles2::yaml_cpp::Writer>(std::cout, my_container_class);
159
160 // Adjust visitor parameters
162 parameters.get<ariles2::write::Parameters>().allow_missing_entries_ = true;
163 ariles2::apply<ariles2::yaml_cpp::Writer>(std::cout, my_container_class, parameters);
164
165 // In some situations it is more convenient to instantiate Reader and
166 // Writer classes explicitly, e.g., if you keep configurations of several
167 // classes in the same file
168 ariles2::yaml_cpp::Writer writer("config.yaml");
169 ariles2::apply(writer, my_container_class);
170
171 ariles2::yaml_cpp::Reader reader("config.yaml");
172 ariles2::apply(reader, my_container_class);
173 }
174
175
176 // ROS parameter server
177 {
178 ros::NodeHandle nh;
179
180 // read/write
181 ariles2::apply<ariles2::rosparam::Writer>(nh, my_container_class);
182 ariles2::apply<ariles2::rosparam::Reader>(nh, my_container_class);
183 // parameters can be uploaded to parameter server in advance using
184 // roslaunch, see http://wiki.ros.org/roslaunch/XML/rosparam
185
186 // read/write with namespace
187 ariles2::apply<ariles2::rosparam::Writer>(nh, my_container_class, "/some_namespace/");
188 ariles2::apply<ariles2::rosparam::Reader>(nh, my_container_class, "/some_namespace/");
189
190 // Reader / Writer classes
191 ariles2::rosparam::Writer writer(nh);
192 ariles2::apply(writer, my_container_class);
193
194 ariles2::rosparam::Reader reader(nh);
195 ariles2::apply(reader, my_container_class);
196 }
197
198
199 // Octave
200 {
201 // Octave visitor supports only writing
202 ariles2::apply<ariles2::octave::Writer>("debug.m", my_container_class);
203 // the generated file can later be loaded in Octave with
204 // 'source debug.m' for debugging
205 }
206 }
207 catch (const std::exception &e)
208 {
209 std::cout << e.what() << std::endl;
210 return (EXIT_FAILURE);
211 }
212
213
214 return (EXIT_SUCCESS);
215}
void arilesVisit(const ariles2::Defaults &, const ariles2::Defaults::Parameters &)
virtual ~ArilesBaseClass()=default
void arilesVisit(const ariles2::Defaults &visitor, const ariles2::Defaults::Parameters &param)
~MyClass() override=default
Eigen::Vector3d eigen_vector_
int main()
t_Visitor::ReturnType apply(t_Visitor &visitor, t_Ariles &ariles_class, t_Subtree &&subtree, const typename t_Visitor::Parameters &param)
Definition common.h:165