26 std::vector< boost::shared_ptr< ::msgpack::object_handle > >
handles_;
40 std::ifstream config_ifs_;
42 config_ifs_.open(file_name.c_str());
43 if (!config_ifs_.good())
45 std::string file_name_default = std::string(HUMOTO_DEFAULT_CONFIG_PREFIX) + file_name;
46 config_ifs_.open(file_name_default.c_str());
48 if (!config_ifs_.good())
50 HUMOTO_THROW_MSG(std::string(
"Could not open configuration file: ") + file_name.c_str());
54 std::stringstream str_stream;
55 str_stream << config_ifs_.rdbuf();
56 buffer_ = str_stream.str();
62 std::size_t buffer_offset = 0;
64 while (buffer_offset != buffer_.size())
66 handles_.push_back(boost::shared_ptr< ::msgpack::object_handle >(new ::msgpack::object_handle));
68 unpack(*handles_[handles_.size()-1], buffer_.data(), buffer_.size(), buffer_offset);
71 catch(
const std::exception &e)
73 HUMOTO_THROW_MSG(std::string(
"Failed to parse the configuration file: ") + e.what());
85 return(node_stack_.top());
95 explicit Reader(
const std::string& file_name)
119 if (node_stack_.size() == 0)
121 for (std::size_t i = 0; i < handles_.size(); ++i)
123 if (::msgpack::type::MAP == handles_[i]->
get().type)
125 if (child_name == handles_[i]->
get().via.map.ptr[0].key.as<std::string>())
127 if (::msgpack::type::MAP == handles_[i]->
get().via.map.ptr[0].val.type)
129 node_stack_.push( &(handles_[i]->
get().via.map.ptr[0].val) );
138 if (::msgpack::type::MAP == getCurrentNode()->type)
140 for (std::size_t i = 0; i < getCurrentNode()->via.map.size; ++i)
142 if (child_name == getCurrentNode()->via.map.ptr[i].key.as<std::string>())
144 node_stack_.push( &(getCurrentNode()->via.map.ptr[i].val) );
175 template <
typename t_Scalar,
179 const std::string & entry_name,
180 const bool crash_on_missing_entry =
false)
182 if (descend(entry_name))
184 HUMOTO_ASSERT( (::msgpack::type::ARRAY == getCurrentNode()->type),
185 "[Config] Entry is not an array.");
187 if (Eigen::Dynamic == t_rows)
189 entry.resize(getCurrentNode()->via.array.size);
193 HUMOTO_ASSERT( (static_cast<int>(getCurrentNode()->via.array.size) == t_rows),
194 "[Config] Wrong entry sequence size.");
197 for(
humoto::EigenIndex i = 0; i < (Eigen::Dynamic == t_rows ? entry.rows() : t_rows); ++i)
199 getCurrentNode()->via.array.ptr[i] >> entry[i];
206 if (crash_on_missing_entry)
208 HUMOTO_THROW_MSG(std::string(
"Configuration file does not contain entry '") + entry_name +
"'.");
227 template <
typename t_Scalar,
231 void readCompound( Eigen::Matrix<t_Scalar, t_rows, t_cols, t_flags> &entry,
232 const std::string& entry_name,
233 const bool crash_on_missing_entry =
false)
235 if (descend(entry_name))
240 readScalar(num_rows,
"rows",
true);
241 readScalar(num_cols,
"cols",
true);
245 readCompound(v,
"data",
true);
248 std::string(
"Inconsistent configuration file entry: ") + entry_name);
250 Eigen::Map< Eigen::Matrix<
double,
253 Eigen::RowMajor> > map(v.data(),
262 if (crash_on_missing_entry)
264 HUMOTO_THROW_MSG(std::string(
"Configuration file does not contain entry '") + entry_name +
"'.");
279 template <
typename t_VectorEntryType>
281 const std::string & entry_name,
282 const bool crash_on_missing_entry =
false)
284 if (descend(entry_name))
286 HUMOTO_ASSERT( (::msgpack::type::ARRAY == getCurrentNode()->type),
287 "[Config] Entry is not an array.");
289 entry.resize(getCurrentNode()->via.array.size);
291 for(std::size_t i = 0; i < entry.size(); ++i)
293 getCurrentNode()->via.array.ptr[i] >> entry[i];
300 if (crash_on_missing_entry)
302 HUMOTO_THROW_MSG(std::string(
"Configuration file does not contain entry '") + entry_name +
"'.");
318 template <
typename t_EntryType>
320 const std::string & entry_name,
321 const bool crash_on_missing_entry =
false)
323 if (descend(entry_name))
325 *getCurrentNode() >> entry;
330 if (crash_on_missing_entry)
332 HUMOTO_THROW_MSG(std::string(
"Configuration file does not contain entry '") + entry_name +
"'.");
348 template <
typename t_EnumerationType>
350 const std::string & entry_name,
351 const bool crash_on_missing_entry =
false)
353 if (descend(entry_name))
356 *getCurrentNode() >> tmp_value;
358 entry =
static_cast<t_EnumerationType
> (tmp_value);
364 if (crash_on_missing_entry)
366 HUMOTO_THROW_MSG(std::string(
"Configuration file does not contain entry '") + entry_name +
"'.");
std::vector< boost::shared_ptr< ::msgpack::object_handle > > handles_
#define HUMOTO_ASSERT(condition, message)
void readEnum(t_EnumerationType &entry, const std::string &entry_name, const bool crash_on_missing_entry=false)
Read configuration entry (an enum). This method is added since an explicit casting to integer is need...
#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
void readCompound(Eigen::Matrix< t_Scalar, t_rows, t_cols, t_flags > &entry, const std::string &entry_name, const bool crash_on_missing_entry=false)
Read a configuration entry (matrix)
bool descend(const std::string &child_name)
Descend to the entry with the given name.
void ascend()
Ascend from the current entry to its parent.
Reader(const std::string &file_name)
Constructor.
Reader()
Default constructor.
void readCompound(Eigen::Matrix< t_Scalar, t_rows, 1, t_flags > &entry, const std::string &entry_name, const bool crash_on_missing_entry=false)
Read configuration entry (vector)
std::stack< const ::msgpack::object *> node_stack_
Stack of nodes.
The root namespace of HuMoTo.
void openFile(const std::string &file_name)
open configuration file
const ::msgpack::object * getCurrentNode() const
Get current node.
Configuration reader class.
void readScalar(t_EntryType &entry, const std::string &entry_name, const bool crash_on_missing_entry=false)
Read configuration entry (scalar template)
void readCompound(std::vector< t_VectorEntryType > &entry, const std::string &entry_name, const bool crash_on_missing_entry=false)
Read configuration entry (std::vector)