Ocean
RegistryConfig.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_PLATFORM_WIN_REGISTRY_CONFIG_H
9 #define META_OCEAN_PLATFORM_WIN_REGISTRY_CONFIG_H
10 
11 #include "ocean/platform/win/Win.h"
13 
14 #include "ocean/base/Config.h"
15 
16 #include <map>
17 
18 namespace Ocean
19 {
20 
21 namespace Platform
22 {
23 
24 namespace Win
25 {
26 
27 /**
28  * This class implements an application or module configuration toolkit using the windows registry.
29  * Beware: In contrast to other implementations of Config, this class uses case-insensitive name matching for all values, due to the underlying case-insensitive key name matching of the Windows Registry.
30  * @ingroup platformwin
31  */
32 class OCEAN_PLATFORM_WIN_EXPORT RegistryConfig final : public Config
33 {
34  protected:
35 
36  // Forward declaration
37  class RegistryValue;
38 
39  /**
40  * Implements case-insensitive string comparison.
41  */
43  {
44  /**
45  * @param left The left string operand
46  * @param right The right string operand
47  * @return Returns true if lower case version of left operand is less than lower case version of right operand.
48  */
49  inline bool operator()(const std::string& left, const std::string& right) const;
50  };
51 
52  /**
53  * Definition of a map mapping value names to value objects using case-insensitive key comparison.
54  */
55  typedef std::map<std::string, RegistryValue, CaseInsensitiveCompare> Values;
56 
57  /**
58  * Definition of a registry value object.
59  */
60  class OCEAN_PLATFORM_WIN_EXPORT RegistryValue final : protected Value
61  {
62  friend class RegistryConfig;
63 
64  public:
65 
66  /**
67  * Creates a new value object.
68  */
70 
71  /**
72  * Creates a new value object.
73  * @param key Registry key
74  */
75  explicit RegistryValue(const HKEY key);
76 
77  /**
78  * Creates a new value object.
79  * @param key Registry key
80  * @param name The name of the value
81  */
82  explicit RegistryValue(const HKEY key, const std::string name);
83 
84  /**
85  * Destructs a value object.
86  */
87  ~RegistryValue() override;
88 
89  /**
90  * Returns the number of sub values.
91  * @see Config::Value::values().
92  */
93  unsigned int values() const override;
94 
95  /**
96  * Returns the number of sub values with a given name.
97  * @see Config::Value::values().
98  */
99  unsigned int values(const std::string& name) const override;
100 
101  /**
102  * Returns whether this value holds at least one specified sub value.
103  * @see Config::Value::exist().
104  */
105  bool exist(const std::string& name) const override;
106 
107  /**
108  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
109  * @see Config::Value::value().
110  */
111  RegistryValue& value(const std::string& name, const unsigned int index) override;
112 
113  /**
114  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
115  * @see Config::Value::value().
116  */
117  bool value(const std::string& name, const unsigned int index, Value** value) override;
118 
119  /**
120  * Returns a sub value specified by it's index.
121  * @see Config::Value::value().
122  */
123  RegistryValue& value(const unsigned int index, std::string& name) override;
124 
125  /**
126  * Returns a sub value specified by it's index.
127  * @see Config::Value::value().
128  */
129  bool value(const unsigned int index, std::string& name, Value** value) override;
130 
131  /**
132  * Returns this value as boolean.
133  * @see Config::Value::operator().
134  */
135  bool operator()(const bool value) const override;
136 
137  /**
138  * Returns this value as integer.
139  * @see Config::Value::operator().
140  */
141  int operator()(const int value) const override;
142 
143  /**
144  * Returns this value as number.
145  * @see Config::Value::operator().
146  */
147  double operator()(const double value) const override;
148 
149  /**
150  * Returns this value as string.
151  * @see Config::Value::operator().
152  */
153  std::string operator()(const std::string& value) const override;
154 
155  /**
156  * Sets this value as boolean.
157  * @see Config::Value::operator=().
158  */
159  bool operator=(const bool value) override;
160 
161  /**
162  * Sets this value as integer.
163  * @see Config::Value::operator=().
164  */
165  bool operator=(const int value) override;
166 
167  /**
168  * Sets this value as number.
169  * @see Config::Value::operator=().
170  */
171  bool operator=(const double value) override;
172 
173  /**
174  * Sets this value as string.
175  * @see Config::Value::operator=().
176  */
177  bool operator=(const std::string& value) override;
178 
179  /**
180  * Returns a sub value specified by it's name
181  * @see Config::Value::operator[]().
182  */
183  RegistryValue& operator[](const std::string& name) override;
184 
185  private:
186 
187  /**
188  * Returns the boolean value of this object.
189  * @return Parsed boolean value
190  */
191  bool boolValue() const;
192 
193  /**
194  * Returns the integer value of this object.
195  * @return Parsed integer value
196  */
197  int integerValue() const;
198 
199  /**
200  * Returns the number value of this object.
201  * @return Parsed number value
202  */
203  double numberValue() const;
204 
205  /**
206  * Writes this value object to the windows registry.
207  * @param key Registry key
208  * @param name Windows registry value name
209  * @return True, if succeeded
210  */
211  bool writeToRegistry(const HKEY key, const std::string& name) const;
212 
213  private:
214 
215  /// Value as string.
216  std::string string_;
217 
218  /// Sub values this value is a group.
220  };
221 
222  public:
223 
224  /**
225  * Creates a new configuration object by the registry base path.
226  * @param path Registry base path
227  * @param rootType The root type of the path
228  */
229  explicit RegistryConfig(const std::string& path, const Registry::RootType rootType = Registry::ROOT_CURRENT_USER);
230 
231  /**
232  * Destructs a configuration object.
233  */
234  ~RegistryConfig() override;
235 
236  /**
237  * Reads / loads all values of this configuration.
238  * @see Config::read().
239  */
240  bool read() override;
241 
242  /**
243  * Writes / saves all values of this configuration.
244  * @see Config::write().
245  */
246  bool write() override;
247 
248  /**
249  * Returns the number of sub values.
250  * @see Config::values().
251  */
252  unsigned int values() const override;
253 
254  /**
255  * Returns the number of sub values with a given name.
256  * @see Config::values().
257  */
258  unsigned int values(const std::string& name) const override;
259 
260  /**
261  * Returns whether this value holds at least one specified sub value.
262  * @see Config::exist().
263  */
264  bool exist(const std::string& name) const override;
265 
266  /**
267  * Returns a sub value specified by it's index.
268  * @see Config::value().
269  */
270  RegistryValue& value(const unsigned int index, std::string& name) override;
271 
272  /**
273  * Returns a sub value specified by it's index.
274  * @see Config::value().
275  */
276  bool value(const unsigned int index, std::string& name, Value** value) override;
277 
278  /**
279  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
280  * @see Config::value().
281  */
282  RegistryValue& value(const std::string& name, const unsigned int index) override;
283 
284  /**
285  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
286  * @see Config::value().
287  */
288  bool value(const std::string& name, const unsigned int index, Value** value) override;
289 
290  /**
291  * Returns a sub value specified by it's name
292  * @see Config::operator[]().
293  */
294  RegistryValue& operator[](const std::string& name) override;
295 
296  protected:
297 
298  /**
299  * Returns the default value holding no data.
300  * @return The default value holding no data.
301  */
303 
304  private:
305 
306  /// The root type of the registry path.
308 
309  /// Configuration registry path.
310  std::string path_;
311 
312  /// Registry values.
314 };
315 
316 inline bool RegistryConfig::CaseInsensitiveCompare::operator()(const std::string& left, const std::string& right) const
317 {
318  return String::toLower(left) < String::toLower(right);
319 }
320 
321 } // namespace Win
322 
323 } // namespace Platform
324 
325 } // namespace Ocean
326 
327 #endif // META_OCEAN_PLATFORM_WIN_REGISTRY_CONFIG_H
This class implements a configuration value.
Definition: Config.h:77
This class implements an application or module configuration toolkit.
Definition: Config.h:38
Definition of a registry value object.
Definition: RegistryConfig.h:61
RegistryValue & operator[](const std::string &name) override
Returns a sub value specified by it's name.
unsigned int values() const override
Returns the number of sub values.
unsigned int values(const std::string &name) const override
Returns the number of sub values with a given name.
~RegistryValue() override
Destructs a value object.
bool exist(const std::string &name) const override
Returns whether this value holds at least one specified sub value.
bool value(const std::string &name, const unsigned int index, Value **value) override
Returns a sub value specified by it's name and it's index if more than one value exists with the same...
bool boolValue() const
Returns the boolean value of this object.
double operator()(const double value) const override
Returns this value as number.
double numberValue() const
Returns the number value of this object.
std::string string_
Value as string.
Definition: RegistryConfig.h:216
int integerValue() const
Returns the integer value of this object.
bool value(const unsigned int index, std::string &name, Value **value) override
Returns a sub value specified by it's index.
bool operator=(const bool value) override
Sets this value as boolean.
bool operator=(const int value) override
Sets this value as integer.
RegistryValue(const HKEY key, const std::string name)
Creates a new value object.
Values subValues_
Sub values this value is a group.
Definition: RegistryConfig.h:219
std::string operator()(const std::string &value) const override
Returns this value as string.
int operator()(const int value) const override
Returns this value as integer.
RegistryValue & value(const std::string &name, const unsigned int index) override
Returns a sub value specified by it's name and it's index if more than one value exists with the same...
bool operator=(const double value) override
Sets this value as number.
bool writeToRegistry(const HKEY key, const std::string &name) const
Writes this value object to the windows registry.
RegistryValue(const HKEY key)
Creates a new value object.
bool operator=(const std::string &value) override
Sets this value as string.
bool operator()(const bool value) const override
Returns this value as boolean.
RegistryValue & value(const unsigned int index, std::string &name) override
Returns a sub value specified by it's index.
This class implements an application or module configuration toolkit using the windows registry.
Definition: RegistryConfig.h:33
Values values_
Registry values.
Definition: RegistryConfig.h:313
RegistryValue & value(const std::string &name, const unsigned int index) override
Returns a sub value specified by it's name and it's index if more than one value exists with the same...
RegistryConfig(const std::string &path, const Registry::RootType rootType=Registry::ROOT_CURRENT_USER)
Creates a new configuration object by the registry base path.
RegistryValue & operator[](const std::string &name) override
Returns a sub value specified by it's name.
~RegistryConfig() override
Destructs a configuration object.
bool value(const unsigned int index, std::string &name, Value **value) override
Returns a sub value specified by it's index.
const Registry::RootType rootType_
The root type of the registry path.
Definition: RegistryConfig.h:307
std::string path_
Configuration registry path.
Definition: RegistryConfig.h:310
unsigned int values(const std::string &name) const override
Returns the number of sub values with a given name.
bool exist(const std::string &name) const override
Returns whether this value holds at least one specified sub value.
std::map< std::string, RegistryValue, CaseInsensitiveCompare > Values
Definition of a map mapping value names to value objects using case-insensitive key comparison.
Definition: RegistryConfig.h:55
bool read() override
Reads / loads all values of this configuration.
unsigned int values() const override
Returns the number of sub values.
bool value(const std::string &name, const unsigned int index, Value **value) override
Returns a sub value specified by it's name and it's index if more than one value exists with the same...
bool write() override
Writes / saves all values of this configuration.
static RegistryValue & nullValue()
Returns the default value holding no data.
RegistryValue & value(const unsigned int index, std::string &name) override
Returns a sub value specified by it's index.
RootType
Definition of different registry root types.
Definition: Registry.h:36
@ ROOT_CURRENT_USER
Current user root type.
Definition: Registry.h:40
static std::string toLower(const std::string &value)
Converts a string to a string with lower characters only.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15
Implements case-insensitive string comparison.
Definition: RegistryConfig.h:43
bool operator()(const std::string &left, const std::string &right) const
Definition: RegistryConfig.h:316