humoto
writer.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Jan Michalczyk
4  @author Alexander Sherikov
5  @copyright 2014-2017 INRIA. 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 namespace humoto
14 {
15  namespace config
16  {
17  namespace yaml
18  {
19  /**
20  * @brief Configuration writer class
21  */
23  {
24  private:
25  /// output file stream
26  std::ofstream config_ofs_;
27 
28  /// instance of YAML emitter, is destroyed and reinitialized by flush()
29  YAML::Emitter *emitter_;
30 
31 
32  private:
33  /**
34  * @brief Initialize emitter
35  */
36  void initEmitter()
37  {
38  emitter_ = new YAML::Emitter;
39  emitter_->SetDoublePrecision(std::numeric_limits<double>::digits10);
40  if (config_ofs_.tellp() != 0)
41  {
42  *emitter_ << YAML::Newline;
43  }
44  *emitter_ << YAML::BeginMap;
45  }
46 
47 
48  /**
49  * @brief Destroy emitter
50  */
52  {
53  *emitter_ << YAML::EndMap;
54  config_ofs_ << emitter_->c_str();
55  delete emitter_;
56  }
57 
58 
59  public:
60  /**
61  * @brief Constructor
62  *
63  * @param[in] file_name
64  */
65  explicit Writer(const std::string& file_name) :
66  config_ofs_(file_name.c_str())
67  {
68  initEmitter();
69  }
70 
71 
72  /**
73  * @brief Destructor
74  */
76  {
77  delete emitter_;
78  }
79 
80 
81  /**
82  * @brief Starts a nested map in the configuration file
83  *
84  * @param[in] map_name name of the map
85  * @param[in] num_entries number of child entries
86  */
87  void descend(const std::string &map_name, const std::size_t num_entries)
88  {
89  *emitter_ << YAML::Key << map_name;
90  *emitter_ << YAML::Value;
91  *emitter_ << YAML::BeginMap;
92  }
93 
94 
95  /**
96  * @brief Starts a nested map in the configuration file
97  */
98  void initRoot()
99  {
100  }
101 
102 
103  /**
104  * @brief Ends a nested map in the configuration file
105  */
106  void ascend()
107  {
108  *emitter_ << YAML::EndMap;
109  }
110 
111 
112  /**
113  * @brief Write a configuration entry (vector)
114  *
115  * @tparam t_Derived Eigen template parameter
116  *
117  * @param[in] entry data
118  * @param[in] entry_name name
119  */
120  template < typename t_Scalar,
121  int t_rows,
122  int t_flags>
123  void writeCompound( const Eigen::Matrix<t_Scalar, t_rows, 1, t_flags> &entry,
124  const std::string & entry_name)
125  {
126  // this is
127  *emitter_ << YAML::Key << entry_name;
128  *emitter_ << YAML::Value << YAML::Flow;
129  *emitter_ << YAML::BeginSeq;
130 
131  for(humoto::EigenIndex i = 0; i < entry.rows(); ++i)
132  {
133  *emitter_ << entry(i);
134  }
135 
136  *emitter_ << YAML::EndSeq;
137  }
138 
139 
140 
141  /**
142  * @brief Write a configuration entry (matrix)
143  *
144  * @tparam t_Scalar Eigen template parameter
145  * @tparam t_rows Eigen template parameter
146  * @tparam t_cols Eigen template parameter
147  * @tparam t_flags Eigen template parameter
148  *
149  * @param[in] entry data
150  * @param[in] entry_name name
151  */
152  template < typename t_Scalar,
153  int t_rows,
154  int t_cols,
155  int t_flags>
156  void writeCompound( const Eigen::Matrix<t_Scalar, t_rows, t_cols, t_flags> &entry,
157  const std::string& entry_name)
158  {
159  descend(entry_name, 3);
160 
161  writeScalar(entry.cols(), "cols");
162  writeScalar(entry.rows(), "rows");
163 
164 
165  *emitter_ << YAML::Key << "data";
166  *emitter_ << YAML::Value << YAML::Flow;
167  *emitter_ << YAML::BeginSeq;
168 
169  for(humoto::EigenIndex i = 0; i < entry.cols(); ++i)
170  {
171  for(humoto::EigenIndex j = 0; j < entry.rows(); ++j)
172  {
173  *emitter_ << entry(j, i);
174  }
175  }
176 
177  *emitter_ << YAML::EndSeq;
178 
179  ascend();
180  }
181 
182 
183  /**
184  * @brief Write configuration entry (std::vector<std::vector<std::string>>)
185  *
186  * @tparam t_VectorEntryType type of the entry of std::vector
187  *
188  * @param[in] entry configuration parameter
189  * @param[in] entry_name name of the configuration parameter
190  */
191  template<typename t_VectorEntryType>
192  void writeCompound( const std::vector< std::vector<t_VectorEntryType> > & entry,
193  const std::string & entry_name) const
194  {
195  *emitter_ << YAML::Key << entry_name;
196  *emitter_ << YAML::Value << YAML::Flow;
197  *emitter_ << YAML::BeginSeq;
198 
199  for(std::size_t i = 0; i < entry.size(); ++i)
200  {
201  for(std::size_t j = 0; j < entry[i].size(); ++j)
202  {
203  *emitter_ << entry[i][j];
204  }
205  }
206 
207  *emitter_ << YAML::EndSeq;
208  }
209 
210 
211  /**
212  * @brief Read configuration entry (std::vector)
213  *
214  * @tparam t_VectorEntryType type of the entry of std::vector
215  *
216  * @param[in] entry data
217  * @param[in] entry_name name
218  */
219  template <typename t_VectorEntryType>
220  void writeCompound( const std::vector<t_VectorEntryType> & entry,
221  const std::string & entry_name)
222  {
223  *emitter_ << YAML::Key << entry_name;
224  *emitter_ << YAML::Value << YAML::Flow;
225  *emitter_ << YAML::BeginSeq;
226 
227  for(std::size_t i = 0; i < entry.size(); ++i)
228  {
229  *emitter_ << entry[i];
230  }
231 
232  *emitter_ << YAML::EndSeq;
233  }
234 
235 
236  /**
237  * @brief Write a configuration entry (enum)
238  *
239  * @tparam t_EnumType type of the enum
240  *
241  * @param[in] entry data
242  * @param[in] entry_name name
243  */
244  template <typename t_EnumType>
245  void writeEnum( const t_EnumType entry,
246  const std::string & entry_name)
247  {
248  *emitter_ << YAML::Key << entry_name << YAML::Value << entry;
249  }
250 
251 
252  /**
253  * @brief Write a configuration entry (scalar template)
254  *
255  * @tparam t_EntryType type of the entry
256  *
257  * @param[in] entry_name name
258  * @param[in] entry data
259  */
260  template <typename t_EntryType>
261  void writeScalar( const t_EntryType entry,
262  const std::string & entry_name)
263  {
264  *emitter_ << YAML::Key << entry_name << YAML::Value << entry;
265  }
266 
267 
268  /**
269  * @brief Flush the configuration to the file
270  */
271  void flush()
272  {
273  destroyEmitter();
274  initEmitter();
275  }
276  };
277  }
278  }
279 }
YAML::Emitter * emitter_
instance of YAML emitter, is destroyed and reinitialized by flush()
Definition: writer.h:29
void descend(const std::string &map_name, const std::size_t num_entries)
Starts a nested map in the configuration file.
Definition: writer.h:87
void ascend()
Ends a nested map in the configuration file.
Definition: writer.h:106
#define HUMOTO_LOCAL
Definition: export_import.h:26
~Writer()
Destructor.
Definition: writer.h:75
void destroyEmitter()
Destroy emitter.
Definition: writer.h:51
Writer(const std::string &file_name)
Constructor.
Definition: writer.h:65
EIGEN_DEFAULT_DENSE_INDEX_TYPE EigenIndex
Definition: utility.h:26
void writeCompound(const Eigen::Matrix< t_Scalar, t_rows, t_cols, t_flags > &entry, const std::string &entry_name)
Write a configuration entry (matrix)
Definition: writer.h:156
void flush()
Flush the configuration to the file.
Definition: writer.h:271
Configuration writer class.
Definition: writer.h:22
void writeCompound(const std::vector< t_VectorEntryType > &entry, const std::string &entry_name)
Read configuration entry (std::vector)
Definition: writer.h:220
void writeCompound(const Eigen::Matrix< t_Scalar, t_rows, 1, t_flags > &entry, const std::string &entry_name)
Write a configuration entry (vector)
Definition: writer.h:123
The root namespace of HuMoTo.
Definition: config.h:12
void initEmitter()
Initialize emitter.
Definition: writer.h:36
void writeEnum(const t_EnumType entry, const std::string &entry_name)
Write a configuration entry (enum)
Definition: writer.h:245
void writeCompound(const std::vector< std::vector< t_VectorEntryType > > &entry, const std::string &entry_name) const
Write configuration entry (std::vector<std::vector<std::string>>)
Definition: writer.h:192
void initRoot()
Starts a nested map in the configuration file.
Definition: writer.h:98
std::ofstream config_ofs_
output file stream
Definition: writer.h:26
void writeScalar(const t_EntryType entry, const std::string &entry_name)
Write a configuration entry (scalar template)
Definition: writer.h:261