MRPT  2.0.4
CConfigFilePrefixer.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 
13 namespace mrpt::config
14 {
15 /** A wrapper for other CConfigFileBase-based objects that prefixes a given
16  * token to every key and/or section.
17  * If, for example, your code expect:
18  * \code
19  * [params1]
20  * foo = 34.0
21  * bar = /dev/ttyUSB0
22  * \endcode
23  *
24  * Using this class with key entries prefix "s1_" will enable the same
25  * existing code to transparently parse this file content:
26  *
27  * \code
28  * [params1]
29  * s1_foo = 34.0
30  * s1_bar = /dev/ttyUSB0
31  * \endcode
32  *
33  * See: \ref config_file_format
34  * \sa CConfigFileBase
35  * \ingroup mrpt_config_grp
36  */
38 {
39  private:
40  /** The object we are wrapping */
43  void ensureIsBound() const;
44 
45  protected:
46  void writeString(
47  const std::string& section, const std::string& name,
48  const std::string& str) override;
49  std::string readString(
50  const std::string& section, const std::string& name,
51  const std::string& defaultStr,
52  bool failIfNotFound = false) const override;
53 
54  public:
55  /** Unbound constructor: must bind this object to CConfigFileBase before
56  * usage with \a bind() and \a setPrefixes() */
58  /** Construct and bind to (wrap) a given object with given prefix texts */
60  const CConfigFileBase& o, const std::string& prefix_sections,
61  const std::string& prefix_keys);
62 
63  /** Make this object to wrap the given existing CConfigFileBase object. Can
64  * be changed at any moment after construction */
65  void bind(const CConfigFileBase& o);
66 
67  /** Change the prefix for sections and keys. Can be called at any moment. */
68  void setPrefixes(
69  const std::string& prefix_sections, const std::string& prefix_keys);
70 
71  std::string getSectionPrefix() const;
72  std::string getKeyPrefix() const;
73  /** Returns the currently-bounded config source, or nullptr if none. */
75 
76  ~CConfigFilePrefixer() override;
77 
78  // See base class docs
79  void getAllSections(std::vector<std::string>& sections) const override;
80  void getAllKeys(const std::string& section, std::vector<std::string>& keys)
81  const override;
82  void clear() override;
83 
84 }; // End of class def.
85 } // namespace mrpt::config
CConfigFileBase * getBoundConfigFileBase() const
Returns the currently-bounded config source, or nullptr if none.
CConfigFilePrefixer()
Unbound constructor: must bind this object to CConfigFileBase before usage with bind() and setPrefixe...
void writeString(const std::string &section, const std::string &name, const std::string &str) override
A virtual method to write a generic string.
void clear() override
Empties the "config file".
This class allows loading and storing values and vectors of different types from a configuration text...
A wrapper for other CConfigFileBase-based objects that prefixes a given token to every key and/or sec...
void getAllSections(std::vector< std::string > &sections) const override
Returns a list with all the section names.
void bind(const CConfigFileBase &o)
Make this object to wrap the given existing CConfigFileBase object.
std::string readString(const std::string &section, const std::string &name, const std::string &defaultStr, bool failIfNotFound=false) const override
A virtual method to read a generic string.
CConfigFileBase * m_bound_object
The object we are wrapping.
void getAllKeys(const std::string &section, std::vector< std::string > &keys) const override
Returs a list with all the keys into a section.
void setPrefixes(const std::string &prefix_sections, const std::string &prefix_keys)
Change the prefix for sections and keys.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020