Ariles
demo_api_v1.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 <iostream> // std::cout
13 
14 
15 // ============================================================================
16 // HEADER INCLUSION
17 // ============================================================================
18 
19 /*
20  * Version I: selective inclusion
21  * ------------------------------
22  */
23 #include <ariles/internal/build_config.h>
24 
25 // Visitor (deprecated: `bridge`) is an Ariles component which provides
26 // integration with a particular 3rd party library.
28 #include "ariles/bridges/ros.h"
29 #include "ariles/bridges/octave.h"
30 
31 // `adapter` is an Ariles component which adds support for serialization of
32 // certain type(s), e.g. Eigen types or Boost pointers.
33 #include "ariles/adapters_all.h"
34 #include "ariles/ariles.h"
35 
36 
37 
38 // ===============================================================
39 // DEFINING TYPES
40 // ===============================================================
41 namespace demo
42 {
44  // must inherit from ariles::ConfigurableBase
46  {
47 // Unique entry name, to be safe use only alphanumeric characters and underscores
48 #define ARILES_SECTION_ID "ArilesBaseClass"
49 
50 // Declare entries, in this case two numbers
51 #define ARILES_ENTRIES \
52  ARILES_TYPED_ENTRY(real_member, double) \
53  ARILES_TYPED_ENTRY_(integer_member, int)
54 // underscore ^ indicates that the name of the entry must be
55 // 'integer_member_' instead of 'integer_member', this is useful if your
56 // naming convention requires trailining underscores for member variables.
57 
58 // Initialize ariles
59 #include ARILES_INITIALIZE
60 
61  public:
62  virtual ~ArilesBaseClass(){}; // added to suppress compiler warnings
63 
64  // setDefaults() is a method which is called every time you deserialize
65  // a class. You can implement it manually as here, or request its
66  // automatic generation using ARILES_AUTO_DEFAULTS as demonstrated for
67  // other classes below.
68  virtual void setDefaults()
69  {
70  real_member = 0.0;
71  integer_member_ = 12;
72  }
73  };
74 
75 
77  {
78  public:
79  // Eigen types are supported too, see below
80  Eigen::Vector3d eigen_vector_;
81  };
82 
83 
84  class MyClass : public ArilesBaseClass, // no need to inherit from ConfigurableBase directly.
85  public NonArilesBaseClass
86  {
87 #define ARILES_SECTION_ID "MyClass"
88 
89 // Declare entries, in this case we indicate inheritance from another
90 // Ariles class (ArilesBaseClass) and a member from a non-Ariles class
91 // (NonArilesBaseClass)
92 #define ARILES_ENTRIES \
93  ARILES_PARENT(ArilesBaseClass) \
94  ARILES_ENTRY_(eigen_vector)
95  // In this case ^ Ariles should not declare the inherited
96  // member, therefore we use 'ARILES_ENTRY_' instead of 'ARILES_TYPED_ENTRY_'.
97 
98 #include ARILES_INITIALIZE
99 
100 
101  public:
102  virtual ~MyClass(){}; // added to suppress compiler warnings
103 
104  virtual void setDefaults()
105  {
106  // If you implement setDefaults() manually, it is up to you to
107  // properly initialize all entries and parent classes.
109 
110  // custom default values for some members
111  real_member = 100.0;
112  eigen_vector_.setZero();
113  }
114  };
115 
116 
118  {
119 #define ARILES_SECTION_ID "MyContainerClass"
120 
121 #define ARILES_AUTO_DEFAULTS // Generate setDefaults() automatically
122 
123  // Some of the standard containers can be used with Ariles types.
124 #define ARILES_ENTRIES ARILES_TYPED_ENTRY_(myclass_vector, std::vector<MyClass>)
125 
126 #include ARILES_INITIALIZE
127 
128  public:
129  /*
130  * Optional method, which is called automatically after parsing a
131  * configuration. This method can be used to perform data
132  * consistency checks or initialize parameters which are not stored
133  * in the configuration.
134  * Bug: all ariles classes are `finalize()`d after reading a
135  * configuration, but manual call to `finalize()` does not
136  * guarantee that, fixed in APIv2.
137  */
138  void finalize()
139  {
140  if (myclass_vector_.size() > 2)
141  {
142  std::cout << "myclass_vector contains more than two elements" << std::endl;
143  }
144  }
145  };
146 } // namespace demo
147 
148 
149 // ===============================================================
150 // SERIALIZATION & DESERIALIZATION
151 // ===============================================================
152 
153 int main()
154 {
155  demo::MyContainerClass my_container_class;
156 
157  // access members as usual
158  my_container_class.myclass_vector_.size();
159  my_container_class.myclass_vector_.push_back(demo::MyClass());
160  my_container_class.myclass_vector_[0].setDefaults();
161 
162 
163  // YAML
164  /*
165  * When you serialize `my_container_class` to YAML you get the following:
166  * -----
167  MyContainerClass:
168  myclass_vector:
169  - real_member: 100
170  integer_member: 12
171  eigen_vector: [0, 0, 0]
172  * -----
173  * Note that the trailing underscores are omitted for all members. This
174  * applies to all supported representations.
175  */
176  {
177  // You can read and write YAML configuration files as follows:
178  my_container_class.writeConfig<ariles::yaml_cpp>("config.yaml");
179  my_container_class.readConfig<ariles::yaml_cpp>("config.yaml");
180 
181  // Sometimes it may be useful to dump configuration to std::cout
182  my_container_class.writeConfig<ariles::yaml_cpp>(std::cout);
183 
184  // In some situations it is more convenient to instantiate Reader and
185  // Writer classes explicitly, e.g., if you keep configurations of several
186  // classses in the same file
187  ariles::yaml_cpp::Writer writer("config.yaml");
188  my_container_class.writeConfig(writer);
189 
190  ariles::yaml_cpp::Reader reader("config.yaml");
191  my_container_class.readConfig(reader);
192  }
193 
194 
195  // ROS parameter server
196  {
197  ros::NodeHandle nh;
198 
199  // read/write
200  my_container_class.writeConfig<ariles::ros>(nh);
201  my_container_class.readConfig<ariles::ros>(nh);
202  // parameters can be uploaded to parameter server in advance using
203  // roslaunch, see http://wiki.ros.org/roslaunch/XML/rosparam
204 
205  // read/write with namespace
206  my_container_class.writeConfig<ariles::ros>(nh, "/some_namespace/");
207  my_container_class.readConfig<ariles::ros>(nh, "/some_namespace/");
208 
209  // Reader / Wrter classes
210  ariles::ros::Writer writer(nh);
211  my_container_class.writeConfig(writer);
212 
213  ariles::ros::Reader reader(nh);
214  my_container_class.readConfig(reader);
215  }
216 
217 
218  // Octave
219  {
220  // Only writing of octave script is supported
221  my_container_class.writeConfig<ariles::octave>("debug.m");
222  // the generated file can later be loaded in Octave with
223  // 'source debug.m' for debugging
224  }
225 
226 
227  return (0);
228 }
ROS parameter server visitor.
Definition: ros.h:67
void readConfig(ariles::ReaderBase &reader, const ariles::ConfigurableFlags &param)
Read configuration (assuming the configuration node to be in the root).
Definition: ariles.h:129
YAML C++11 visitor.
Definition: yaml_cpp.h:65
Octave visitor.
Definition: octave.h:90
virtual ~MyClass()
virtual void setDefaults()
Definition: ariles.h:79
void writeConfig(t_WriterInitializer &writer_initializer) const
Definition: ariles.h:244
virtual void setDefaults()
Definition: demo_api_v1.cpp:68
Eigen::Vector3d eigen_vector_
Definition: demo_api_v1.cpp:80
Default configurable base.
Definition: ariles.h:348
int main()
virtual ~ArilesBaseClass()
Definition: demo_api_v1.cpp:62
virtual void setDefaults()