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