humoto
writer.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
5  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
6 
7  @brief
8 */
9 
10 #pragma once
11 
12 namespace humoto
13 {
14  namespace config
15  {
16  namespace msgpack
17  {
18  /**
19  * @brief Configuration writer class
20  */
22  {
23  private:
24  /// output file stream
25  std::ofstream config_ofs_;
26 
27  ::msgpack::packer< std::ofstream > *packer_;
28 
29 
30  public:
31  /**
32  * @brief Constructor
33  *
34  * @param[in] file_name
35  */
36  explicit Writer(const std::string& file_name)
37  {
38  config_ofs_.open(file_name.c_str());
39 
40  if (!config_ofs_.good())
41  {
42  HUMOTO_THROW_MSG(std::string("Could not open configuration file for writing: ") + file_name.c_str());
43  }
44 
45  packer_ = new ::msgpack::packer< std::ofstream >(config_ofs_);
46  }
47 
48 
49  /**
50  * @brief Destructor
51  */
53  {
54  delete packer_;
55  }
56 
57 
58  /**
59  * @brief Starts a nested map in the configuration file
60  *
61  * @param[in] map_name name of the map
62  * @param[in] num_entries number of child entries
63  */
64  void descend(const std::string &map_name, const std::size_t num_entries)
65  {
66  packer_->pack(map_name);
67  packer_->pack_map(num_entries);
68  }
69 
70 
71  /**
72  * @brief Starts a nested map in the configuration file
73  */
74  void initRoot()
75  {
76  packer_->pack_map(1);
77  }
78 
79 
80  /**
81  * @brief Ends a nested map in the configuration file
82  */
83  void ascend()
84  {
85  }
86 
87 
88  /**
89  * @brief Write a configuration entry (vector)
90  *
91  * @tparam t_Derived Eigen template parameter
92  *
93  * @param[in] entry data
94  * @param[in] entry_name name
95  */
96  template < typename t_Scalar,
97  int t_rows,
98  int t_flags>
99  void writeCompound( const Eigen::Matrix<t_Scalar, t_rows, 1, t_flags> &entry,
100  const std::string & entry_name)
101  {
102  HUMOTO_ASSERT(entry.size() <= std::numeric_limits<uint32_t>::max(), "Vector is too long.");
103 
104  packer_->pack(entry_name);
105  packer_->pack_array(entry.size());
106  for (humoto::EigenIndex i = 0; i < entry.rows(); ++i)
107  {
108  packer_->pack(entry[i]);
109  }
110  }
111 
112 
113  /**
114  * @brief Write a configuration entry (matrix)
115  *
116  * @tparam t_Scalar Eigen template parameter
117  * @tparam t_rows Eigen template parameter
118  * @tparam t_cols Eigen template parameter
119  * @tparam t_flags Eigen template parameter
120  *
121  * @param[in] entry data
122  * @param[in] entry_name name
123  */
124  template < typename t_Scalar,
125  int t_rows,
126  int t_cols,
127  int t_flags>
128  void writeCompound( const Eigen::Matrix<t_Scalar, t_rows, t_cols, t_flags> &entry,
129  const std::string& entry_name)
130  {
131  HUMOTO_ASSERT(entry.size() <= std::numeric_limits<uint32_t>::max(), "Matrix is too large.");
132 
133  descend(entry_name, 3);
134 
135  writeScalar(entry.cols(), "cols");
136  writeScalar(entry.rows(), "rows");
137 
138 
139  packer_->pack("data");
140  packer_->pack_array(entry.size());
141 
142  for (humoto::EigenIndex i = 0; i < entry.size(); ++i)
143  {
144  packer_->pack(entry.data()[i]);
145  }
146 
147  ascend();
148  }
149 
150 
151  /**
152  * @brief Write configuration entry (std::vector<std::vector<std::string>>)
153  *
154  * @tparam t_VectorEntryType type of the entry of std::vector
155  *
156  * @param[in] entry configuration parameter
157  * @param[in] entry_name name of the configuration parameter
158  */
159  template <typename t_VectorEntryType>
160  void writeCompound( const std::vector< std::vector<t_VectorEntryType> > & entry,
161  const std::string & entry_name) const
162  {
163  HUMOTO_ASSERT(entry.size() <= std::numeric_limits<uint32_t>::max(), "Vector is too long.");
164 
165  packer_->pack(entry_name);
166  packer_->pack_array(entry.size());
167  for(std::size_t i = 0; i < entry.size(); ++i)
168  {
169  packer_->pack(entry[i]);
170  }
171  }
172 
173 
174  /**
175  * @brief Read configuration entry (std::vector)
176  *
177  * @tparam t_VectorEntryType type of the entry of std::vector
178  *
179  * @param[in] entry data
180  * @param[in] entry_name name
181  */
182  template <typename t_VectorEntryType>
183  void writeCompound( const std::vector<t_VectorEntryType> & entry,
184  const std::string & entry_name)
185  {
186  packer_->pack(entry_name);
187  packer_->pack(entry);
188  }
189 
190 
191  /**
192  * @brief Write a configuration entry (enum)
193  *
194  * @tparam t_EnumType type of the enum
195  *
196  * @param[in] entry data
197  * @param[in] entry_name name
198  */
199  template <typename t_EnumType>
200  void writeEnum( const t_EnumType entry,
201  const std::string & entry_name)
202  {
203  packer_->pack(entry_name);
204  packer_->pack_int64(entry);
205  }
206 
207 
208  /**
209  * @brief Write a configuration entry (scalar template)
210  *
211  * @tparam t_EntryType type of the entry
212  *
213  * @param[in] entry_name name
214  * @param[in] entry data
215  */
216  template <typename t_EntryType>
217  void writeScalar( const t_EntryType entry,
218  const std::string & entry_name)
219  {
220  packer_->pack(entry_name);
221  packer_->pack(entry);
222  }
223 
224 
225  /**
226  * @brief Flush the configuration to the file
227  */
228  void flush()
229  {
230  config_ofs_.flush();
231  }
232  };
233  }
234  }
235 }
void flush()
Flush the configuration to the file.
Definition: writer.h:228
Writer(const std::string &file_name)
Constructor.
Definition: writer.h:36
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:99
void ascend()
Ends a nested map in the configuration file.
Definition: writer.h:83
#define HUMOTO_LOCAL
Definition: export_import.h:26
void descend(const std::string &map_name, const std::size_t num_entries)
Starts a nested map in the configuration file.
Definition: writer.h:64
#define HUMOTO_ASSERT(condition, message)
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:160
#define HUMOTO_THROW_MSG(s)
HUMOTO_THROW_MSG throws an error message concatenated with the name of the function (if supported)...
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:128
::msgpack::packer< std::ofstream > * packer_
Definition: writer.h:27
void writeEnum(const t_EnumType entry, const std::string &entry_name)
Write a configuration entry (enum)
Definition: writer.h:200
void writeScalar(const t_EntryType entry, const std::string &entry_name)
Write a configuration entry (scalar template)
Definition: writer.h:217
The root namespace of HuMoTo.
Definition: config.h:12
void writeCompound(const std::vector< t_VectorEntryType > &entry, const std::string &entry_name)
Read configuration entry (std::vector)
Definition: writer.h:183
std::ofstream config_ofs_
output file stream
Definition: writer.h:25
void initRoot()
Starts a nested map in the configuration file.
Definition: writer.h:74
Configuration writer class.
Definition: writer.h:21