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 #define ARILES_API_VERSION 2
17 
18 // `visitor` is an Ariles component which provides integration with a particular
19 // 3rd party library.
21 #include <ariles/visitors/ros.h>
22 #include <ariles/visitors/octave.h>
23 
24 // `adapter` is an Ariles component which adds support for serialization of
25 // certain type(s), e.g. Eigen types or Boost pointers.
26 #include <ariles/adapters_all.h>
27 #include <ariles/ariles2.h>
28 
29 
30 
31 // ===============================================================
32 // DEFINING TYPES
33 // ===============================================================
34 namespace demo
35 {
36  class ArilesBaseClass
37  // must inherit from ariles::DefaultBase
38  : public ariles::DefaultBase
39  {
40 // Declare entries, in this case two numbers
41 #define ARILES_ENTRIES \
42  ARILES_TYPED_ENTRY(real_member, double) \
43  ARILES_TYPED_ENTRY_(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 trailining underscores for member variables.
47 
48 // Initialize ariles
49 #include ARILES_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 ariles::Defaults & /*visitor*/, const ariles::Defaults::Parameters & /*param*/)
57  {
58  real_member = 0.0;
59  integer_member_ = 12;
60  }
61  };
62 
63 
64  class NonArilesBaseClass
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 ConfigurableBase 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 ARILES_ENTRIES \
79  ARILES_PARENT(ArilesBaseClass) \
80  ARILES_ENTRY_(eigen_vector)
81  // In this case ^ Ariles should not declare the inherited
82  // member, therefore we use 'ARILES_ENTRY_' instead of 'ARILES_TYPED_ENTRY_'.
83 
84 #include ARILES_INITIALIZE
85 
86 
87  public:
88  virtual ~MyClass(){}; // added to suppress compiler warnings
89 
90 
91  void arilesVisit(const ariles::Defaults &visitor, const ariles::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  ArilesBaseClass::arilesVisit(visitor, param);
97 
98  // custom default values for some members
99  real_member = 100.0;
100  eigen_vector_.setZero();
101  }
102  };
103 
104 
105  class MyContainerClass : public ariles::DefaultBase
106  {
107  // Some of the standard containers can be used with Ariles types.
108 #define ARILES_ENTRIES ARILES_TYPED_ENTRY_(myclass_vector, std::vector<MyClass>)
109 #include ARILES_INITIALIZE
110  };
111 } // namespace demo
112 
113 
114 // ===============================================================
115 // SERIALIZATION & DESERIALIZATION
116 // ===============================================================
117 
118 #include <iostream> // std::cout
119 
120 int main()
121 {
122  demo::MyContainerClass my_container_class;
123 
124  // access members as usual
125  my_container_class.myclass_vector_.size();
126  my_container_class.myclass_vector_.push_back(demo::MyClass());
127  ariles::apply<ariles::Defaults>(my_container_class.myclass_vector_[0]);
128 
129 
130  // YAML
131  /*
132  * When you serialize `my_container_class` to YAML you get the following:
133  * -----
134  MyContainerClass:
135  myclass_vector:
136  - real_member: 100
137  integer_member: 12
138  eigen_vector: [0, 0, 0]
139  * -----
140  * Note that the trailing underscores are omitted for all members. This
141  * applies to all supported representations.
142  */
143  {
144  // You can read and write YAML configuration files as follows:
145  ariles::apply<ariles::yaml_cpp::Writer>("config.yaml", my_container_class);
146  ariles::apply<ariles::yaml_cpp::Reader>("config.yaml", my_container_class);
147 
148  // Sometimes it may be useful to dump configuration to std::cout
149  ariles::apply<ariles::yaml_cpp::Writer>(std::cout, my_container_class);
150 
151  // In some situations it is more convenient to instantiate Reader and
152  // Writer classes explicitly, e.g., if you keep configurations of several
153  // classses in the same file
154  ariles::yaml_cpp::Writer writer("config.yaml");
155  ariles::apply(writer, my_container_class);
156 
157  ariles::yaml_cpp::Reader reader("config.yaml");
158  ariles::apply(reader, my_container_class);
159  }
160 
161 
162  // ROS parameter server
163  {
164  ros::NodeHandle nh;
165 
166  // read/write
167  ariles::apply<ariles::ros::Writer>(nh, my_container_class);
168  ariles::apply<ariles::ros::Reader>(nh, my_container_class);
169  // parameters can be uploaded to parameter server in advance using
170  // roslaunch, see http://wiki.ros.org/roslaunch/XML/rosparam
171 
172  // read/write with namespace
173  ariles::apply<ariles::ros::Writer>(nh, my_container_class, "/some_namespace/");
174  ariles::apply<ariles::ros::Reader>(nh, my_container_class, "/some_namespace/");
175 
176  // Reader / Wrter classes
177  ariles::ros::Writer writer(nh);
178  ariles::apply(writer, my_container_class);
179 
180  ariles::ros::Reader reader(nh);
181  ariles::apply(reader, my_container_class);
182  }
183 
184 
185  // Octave
186  {
187  // Octave visitor supports only writing
188  ariles::apply<ariles::octave::Writer>("debug.m", my_container_class);
189  // the generated file can later be loaded in Octave with
190  // 'source debug.m' for debugging
191  }
192 
193 
194  return (0);
195 }
void apply(t_Visitor &visitor, t_Ariles &ariles_class, const std::string &name, const typename t_Visitor::Parameters &param, ARILES_IS_BASE_ENABLER(ariles::visitor::Visitor, t_Visitor))
Definition: common.h:96
void arilesVisit(const ariles::Defaults &visitor, const ariles::Defaults::Parameters &param)
Definition: demo_api_v2.cpp:91
virtual ~MyClass()
Definition: demo_api_v2.cpp:88
void arilesVisit(const ariles::Defaults &, const ariles::Defaults::Parameters &)
Definition: demo_api_v2.cpp:56
Eigen::Vector3d eigen_vector_
Definition: demo_api_v1.cpp:80
int main()
virtual ~ArilesBaseClass()
Definition: demo_api_v2.cpp:52