OpenMPCD
Setting.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines the `OpenMPCD::ImplementationDetails::Configuration::Setting` class.
4  */
5 
6 #ifndef OPENMPCD_IMPLEMENTATIONDETAILS_CONFIGURATION_SETTING_HPP
7 #define OPENMPCD_IMPLEMENTATIONDETAILS_CONFIGURATION_SETTING_HPP
8 
10 
11 #include <libconfig.h++>
12 
13 namespace OpenMPCD
14 {
15 /**
16  * Namespace for implementation details that do not have to concern the users of the main functionality.
17  */
18 namespace ImplementationDetails
19 {
20 /**
21  * Namespace for implementation details of `OpenMPCD::Configuration`.
22  */
23 namespace Configuration
24 {
25 
26 /**
27  * Helper class to handle libconfig++ settings.
28  */
29 class Setting
30 {
31  private:
32  Setting(); ///< The constructor.
33 
34  public:
35  /**
36  * Creates a new setting with the given value.
37  * @throw InvalidCallException Throws if there already is an exception of the given name.
38  * @param[in,out] parent The parent setting.
39  * @param[in] name The setting name.
40  * @param[in] value The value for the new setting.
41  */
42  static void createSetting(libconfig::Setting* const parent, const std::string& name, const bool value)
43  {
44  throwIfNameInUse(*parent, name);
45 
46  libconfig::Setting& setting = parent->add(name, libconfig::Setting::TypeBoolean);
47  setting = value;
48  }
49 
50  /**
51  * Creates a new setting with the given value.
52  * @throw InvalidCallException Throws if there already is an exception of the given name.
53  * @param[in,out] parent The parent setting.
54  * @param[in] name The setting name.
55  * @param[in] value The value for the new setting.
56  */
57  static void createSetting(libconfig::Setting* const parent, const std::string& name, const int value)
58  {
59  throwIfNameInUse(*parent, name);
60 
61  libconfig::Setting& setting = parent->add(name, libconfig::Setting::TypeInt);
62  setting = value;
63  }
64 
65  /**
66  * Creates a new setting with the given value.
67  * @throw InvalidCallException Throws if there already is an exception of the given name.
68  * @param[in,out] parent The parent setting.
69  * @param[in] name The setting name.
70  * @param[in] value The value for the new setting.
71  */
72  static void createSetting(libconfig::Setting* const parent, const std::string& name, const long long value)
73  {
74  throwIfNameInUse(*parent, name);
75 
76  libconfig::Setting& setting = parent->add(name, libconfig::Setting::TypeInt64);
77  setting = value;
78  }
79 
80  /**
81  * Creates a new setting with the given value.
82  * @throw InvalidCallException Throws if there already is an exception of the given name.
83  * @param[in,out] parent The parent setting.
84  * @param[in] name The setting name.
85  * @param[in] value The value for the new setting.
86  */
87  static void createSetting(libconfig::Setting* const parent, const std::string& name, const double value)
88  {
89  throwIfNameInUse(*parent, name);
90 
91  libconfig::Setting& setting = parent->add(name, libconfig::Setting::TypeFloat);
92  setting = value;
93  }
94 
95  /**
96  * Creates a new setting with the given value.
97  * @throw InvalidCallException Throws if there already is an exception of the given name.
98  * @param[in,out] parent The parent setting.
99  * @param[in] name The setting name.
100  * @param[in] value The value for the new setting.
101  */
102  static void createSetting(libconfig::Setting* const parent, const std::string& name, const std::string& value)
103  {
104  throwIfNameInUse(*parent, name);
105 
106  libconfig::Setting& setting = parent->add(name, libconfig::Setting::TypeString);
107  setting = value;
108  }
109 
110  /**
111  * Creates a new setting with the given value.
112  * @throw InvalidCallException Throws if there already is an exception of the given name.
113  * @param[in,out] parent The parent setting.
114  * @param[in] name The setting name.
115  * @param[in] value The value for the new setting.
116  */
117  static void createSetting(libconfig::Setting* const parent, const std::string& name, const char* const value)
118  {
119  throwIfNameInUse(*parent, name);
120 
121  libconfig::Setting& setting = parent->add(name, libconfig::Setting::TypeString);
122  setting = value;
123  }
124 
125  private:
126  /**
127  * Throws `InvalidCallException` if the given parent setting has a child with the given name.
128  * @throw InvalidCallException Throws if the given parent setting has a child with the given name.
129  * @param[in] parent The parent to check.
130  * @param[in] name The name in question.
131  */
132  static void throwIfNameInUse(const libconfig::Setting& parent, const std::string& name)
133  {
134  if(parent.exists(name))
135  OPENMPCD_THROW(InvalidCallException, "Setting name already in use.");
136  }
137 }; //class Setting
138 
139 } //namespace Configuration
140 } //namespace ImplementationDetails
141 } //namespace OpenMPCD
142 
143 #endif
OpenMPCD::ImplementationDetails::Configuration::Setting::createSetting
static void createSetting(libconfig::Setting *const parent, const std::string &name, const bool value)
Creates a new setting with the given value.
Definition: Setting.hpp:42
OpenMPCD::InvalidCallException
Exception for a forbidden function call.
Definition: Exceptions.hpp:144
Exceptions.hpp
OPENMPCD_THROW
#define OPENMPCD_THROW(ExceptionType, message)
Throws the given ExceptionType, passing the given message along with file and line number information...
Definition: Exceptions.hpp:22
OpenMPCD::ImplementationDetails::Configuration::Setting::createSetting
static void createSetting(libconfig::Setting *const parent, const std::string &name, const char *const value)
Creates a new setting with the given value.
Definition: Setting.hpp:117
OpenMPCD::ImplementationDetails::Configuration::Setting::createSetting
static void createSetting(libconfig::Setting *const parent, const std::string &name, const int value)
Creates a new setting with the given value.
Definition: Setting.hpp:57
OpenMPCD::ImplementationDetails::Configuration::Setting
Helper class to handle libconfig++ settings.
Definition: Setting.hpp:29
OpenMPCD::ImplementationDetails::Configuration::Setting::createSetting
static void createSetting(libconfig::Setting *const parent, const std::string &name, const std::string &value)
Creates a new setting with the given value.
Definition: Setting.hpp:102
OpenMPCD::ImplementationDetails::Configuration::Setting::createSetting
static void createSetting(libconfig::Setting *const parent, const std::string &name, const double value)
Creates a new setting with the given value.
Definition: Setting.hpp:87
OpenMPCD::ImplementationDetails::Configuration::Setting::createSetting
static void createSetting(libconfig::Setting *const parent, const std::string &name, const long long value)
Creates a new setting with the given value.
Definition: Setting.hpp:72