Ice 3.8
C++ API Reference
Loading...
Searching...
No Matches
Properties.h
1// Copyright (c) ZeroC, Inc.
2
3#ifndef ICE_PROPERTIES_H
4#define ICE_PROPERTIES_H
5
6#include "Config.h"
7#include "Ice/BuiltinSequences.h"
8#include "Ice/PropertyDict.h"
9#include "PropertiesF.h"
10#include "StringConverter.h"
11
12#include <mutex>
13#include <set>
14#include <string>
15#include <string_view>
16
17namespace Ice
18{
19 /// Represents a set of properties used to configure Ice and Ice-based applications. A property is a key/value pair,
20 /// where both the key and the value are strings. By convention, property keys should have the form
21 /// `application-name>[.category[.sub-category]].name`.
22 /// @remark This class is thread-safe: multiple threads can safely read and write the properties without their own
23 /// synchronization.
24 /// @headerfile Ice/Ice.h
25 class ICE_API Properties final
26 {
27 public:
28 /// Default constructor.
29 Properties() = default;
30
31 /// Constructs an empty property set with a list of opt-in prefixes.
32 /// @param optInPrefixes The list of opt-in prefixes allowed in the property set.
33 /// @remark This constructor is used by services such as IceGrid and IceStorm to parse properties with the
34 /// `IceGrid` and `IceStorm` prefixes.
35 explicit Properties(std::vector<std::string> optInPrefixes) : _optInPrefixes(std::move(optInPrefixes)) {}
36
37 /// Copy constructor.
38 /// @param source The property set to copy.
39 Properties(const Properties& source);
40
41 /// Constructs a property from command-line arguments and a default property set.
42 /// @param args The command-line arguments. Property arguments are removed from this sequence.
43 /// @param defaults The default property set.
44 Properties(StringSeq& args, const PropertiesPtr& defaults);
45
46 Properties& operator=(const Properties& rhs) = delete;
47
48 /// Gets a property by key.
49 /// @param key The property key.
50 /// @return The property value, or the empty string if the property is not set.
51 /// @see #setProperty
52 std::string getProperty(std::string_view key);
53
54 /// Gets an Ice property by key.
55 /// @param key The property key.
56 /// @return The property value, or the default value for this property if the property is not set.
57 /// @throws PropertyException Thrown when the property is not a known Ice property.
58 /// @see #setProperty
59 std::string getIceProperty(std::string_view key);
60
61 /// Gets a property by key.
62 /// @param key The property key.
63 /// @param value The default value to return if the property is not set.
64 /// @return The property value or the default value if the property is not set.
65 /// @see #setProperty
66 std::string getPropertyWithDefault(std::string_view key, std::string_view value);
67
68 /// Gets a property as an integer.
69 /// @param key The property key.
70 /// @return The property value interpreted as an integer, or 0 if the property is not set.
71 /// @throws PropertyException Thrown when the property value is not a valid integer.
72 /// @see #setProperty
73 int getPropertyAsInt(std::string_view key);
74
75 /// Gets an Ice property as an integer.
76 /// @param key The property key.
77 /// @return The property value interpreted as an integer, or the default value if the property is not set.
78 /// @throws PropertyException Thrown when the property is not a known Ice property or the value is not a valid
79 /// integer.
80 /// @see #setProperty
81 int getIcePropertyAsInt(std::string_view key);
82
83 /// Gets a property as an integer.
84 /// @param key The property key.
85 /// @param value The default value to return if the property does not exist.
86 /// @return The property value interpreted as an integer, or the default value of the property is not set.
87 /// @throws PropertyException Thrown when the property value is not a valid integer.
88 /// @see #setProperty
89 int getPropertyAsIntWithDefault(std::string_view key, int value);
90
91 /// Gets a property as a list of strings. The strings must be separated by whitespace or comma. The strings in
92 /// the list can contain whitespace and commas if they are enclosed in single or double quotes. If quotes are
93 /// mismatched, an empty list is returned. Within single quotes or double quotes, you can escape the quote in
94 /// question with a backslash, e.g. O'Reilly can be written as O'Reilly, "O'Reilly" or 'O\'Reilly'.
95 /// @param key The property key.
96 /// @return The property value interpreted as a list of strings, or an empty list if the property is not set.
97 /// @see #setProperty
98 StringSeq getPropertyAsList(std::string_view key);
99
100 /// Gets an Ice property as a list of strings. The strings must be separated by whitespace or comma. The strings
101 /// in the list can contain whitespace and commas if they are enclosed in single or double quotes. If quotes are
102 /// mismatched, the default list is returned. Within single quotes or double quotes, you can escape the quote
103 /// in question with a backslash, e.g. O'Reilly can be written as O'Reilly, "O'Reilly" or 'O\'Reilly'.
104 /// @param key The property key.
105 /// @return The property value interpreted as list of strings, or the default value if the property is not set.
106 /// @throws PropertyException If the property is not a known Ice property.
107 /// @see #setProperty
108 StringSeq getIcePropertyAsList(std::string_view key);
109
110 /// Gets a property as a list of strings. The strings must be separated by whitespace or comma. The strings in
111 /// the list can contain whitespace and commas if they are enclosed in single or double quotes. If quotes are
112 /// mismatched, the default list is returned. Within single quotes or double quotes, you can escape the quote
113 /// in question with a backslash, e.g. O'Reilly can be written as O'Reilly, "O'Reilly" or 'O\'Reilly'.
114 /// @param key The property key.
115 /// @param value The default value to use if the property is not set.
116 /// @return The property value interpreted as list of strings, or the default value if the property is not set.
117 /// @see #setProperty
118 StringSeq getPropertyAsListWithDefault(std::string_view key, const StringSeq& value);
119
120 /// Gets all properties whose keys begins with @p prefix. If @p prefix is the empty string, then all properties
121 /// are returned.
122 /// @param prefix The prefix to search for (empty string if none).
123 /// @return The matching property set.
124 PropertyDict getPropertiesForPrefix(std::string_view prefix);
125
126 /// Sets a property. To unset a property, set it to the empty string.
127 /// @param key The property key.
128 /// @param value The property value.
129 /// @see #getProperty
130 void setProperty(std::string_view key, std::string_view value);
131
132 /// Gets a sequence of command-line options that is equivalent to this property set. Each element of the
133 /// returned sequence is a command-line option of the form `--key=value`.
134 /// @return The command line options for this property set.
136
137 /// Converts a sequence of command-line options into properties. All options that start with `--prefix.` are
138 /// converted into properties. If the prefix is empty, all options that begin with `--` are converted to
139 /// properties.
140 /// @param prefix The property prefix, or the empty string to convert all options starting with `--`.
141 /// @param options The command-line options.
142 /// @return The command-line options that do not start with the specified prefix, in their original order.
143 StringSeq parseCommandLineOptions(std::string_view prefix, const StringSeq& options);
144
145 /// Converts a sequence of command-line options into properties. All options that start with one of the
146 /// reserved Ice prefixes (`--Ice`, `--IceSSL`, etc.) are converted into properties.
147 /// @param options The command-line options.
148 /// @return The command-line options that do not start with one of the reserved prefixes, in their original
149 /// order.
151
152 /// Loads properties from a file.
153 /// @param file The property file.
154 void load(std::string_view file);
155
156 /// Creates a copy of this property set.
157 /// @return A copy of this property set.
158 PropertiesPtr clone() { return std::make_shared<Properties>(*this); }
159
160 /// Gets the properties that were never read.
161 /// @return A list of unused properties.
162 std::set<std::string> getUnusedProperties();
163
164 /// Parses a sequence of options into a map of key value pairs starting with a prefix. The options are expected
165 /// to be of the form `--key=value`.
166 /// @param prefix The prefix to match.
167 /// @param options The options to parse.
168 /// @return A pair containing a map of matched key value pairs and a sequence of unmatched options.
169 static std::pair<PropertyDict, StringSeq> parseOptions(std::string_view prefix, const StringSeq& options);
170
171 private:
172 static std::optional<std::pair<std::string, std::string>>
173 parseLine(std::string_view, const StringConverterPtr&);
174
175 void loadArgs(StringSeq&);
176
177 void loadConfig();
178
179 struct PropertyValue
180 {
181 PropertyValue() : used(false) {}
182
183 PropertyValue(std::string v, bool u) : value(std::move(v)), used(u) {}
184
185 std::string value;
186 bool used;
187 };
188 std::map<std::string, PropertyValue, std::less<>> _properties;
189 // List of "opt-in" property prefixes to allow in the property set. Setting a property for a property prefix
190 // that is opt-in (eg. IceGrid, IceStorm, Glacier2, etc.) but not in this list is considered an error.
191 std::vector<std::string> _optInPrefixes;
192 mutable std::mutex _mutex;
193 };
194
195 /// Creates an empty property set.
196 /// @return A new empty property set.
198
199 /// Creates a property set initialized from command-line arguments and a default property set.
200 /// @param seq The command-line arguments. This function parses arguments starting with `--` and one of the
201 /// reserved prefixes (Ice, IceSSL, etc.) as properties and removes these elements from the list. If there is an
202 /// argument starting with `--Ice.Config`, this function loads the specified configuration file. When the same
203 /// property is set in a configuration file and through a command-line argument, the command-line setting takes
204 /// precedence.
205 /// @param defaults Default values for the property set. Settings in configuration files and the arguments override
206 /// these defaults.
207 /// @return A new property set initialized with the properties that were removed from the argument vector.
208 ICE_API PropertiesPtr createProperties(StringSeq& seq, const PropertiesPtr& defaults = nullptr);
209
210 /// Creates a property set initialized from command-line arguments and a default property set.
211 /// @param argc The number of arguments in @p argv. When this function parses properties from @p argv, it
212 /// reshuffles the arguments so that the remaining arguments start at the beginning of @p argv, and updates @p argc.
213 /// @param argv The command-line arguments. This function parses arguments starting with `--` and one of the
214 /// reserved prefixes (Ice, IceSSL, etc.) as properties. If there is an argument starting with `--Ice.Config`, this
215 /// function loads the specified configuration file. When the same property is set in a configuration file and
216 /// through a command-line argument, the command-line setting takes precedence.
217 /// @param defaults Default values for the property set. Settings in configuration files and the arguments override
218 /// these defaults.
219 /// @return A new property set initialized with the properties that were removed from the argument vector.
220 ICE_API PropertiesPtr createProperties(int& argc, const char* argv[], const PropertiesPtr& defaults = nullptr);
221
222 /// @copydoc createProperties(int&, const char*[], const PropertiesPtr&)
223 inline PropertiesPtr createProperties(int& argc, char* argv[], const PropertiesPtr& defaults = nullptr)
224 {
225 return createProperties(argc, const_cast<const char**>(argv), defaults);
226 }
227
228#if defined(_WIN32) || defined(ICE_DOXYGEN)
229 /// @copydoc createProperties(int&, const char*[], const PropertiesPtr&)
230 /// @remark Windows only.
231 ICE_API PropertiesPtr createProperties(int& argc, const wchar_t* argv[], const PropertiesPtr& defaults = nullptr);
232
233 /// @copydoc createProperties(int&, const char*[], const PropertiesPtr&)
234 /// @remark Windows only.
235 inline PropertiesPtr createProperties(int& argc, wchar_t* argv[], const PropertiesPtr& defaults = nullptr)
236 {
237 return createProperties(argc, const_cast<const wchar_t**>(argv), defaults);
238 }
239#endif
240}
241
242#endif
Properties(std::vector< std::string > optInPrefixes)
Constructs an empty property set with a list of opt-in prefixes.
Definition Properties.h:35
std::string getPropertyWithDefault(std::string_view key, std::string_view value)
Gets a property by key.
StringSeq parseIceCommandLineOptions(const StringSeq &options)
Converts a sequence of command-line options into properties.
std::set< std::string > getUnusedProperties()
Gets the properties that were never read.
Properties(StringSeq &args, const PropertiesPtr &defaults)
Constructs a property from command-line arguments and a default property set.
StringSeq getPropertyAsListWithDefault(std::string_view key, const StringSeq &value)
Gets a property as a list of strings.
std::string getProperty(std::string_view key)
Gets a property by key.
void load(std::string_view file)
Loads properties from a file.
void setProperty(std::string_view key, std::string_view value)
Sets a property.
int getPropertyAsIntWithDefault(std::string_view key, int value)
Gets a property as an integer.
PropertyDict getPropertiesForPrefix(std::string_view prefix)
Gets all properties whose keys begins with prefix.
StringSeq getPropertyAsList(std::string_view key)
Gets a property as a list of strings.
std::string getIceProperty(std::string_view key)
Gets an Ice property by key.
int getIcePropertyAsInt(std::string_view key)
Gets an Ice property as an integer.
StringSeq parseCommandLineOptions(std::string_view prefix, const StringSeq &options)
Converts a sequence of command-line options into properties.
PropertiesPtr clone()
Creates a copy of this property set.
Definition Properties.h:158
StringSeq getCommandLineOptions()
Gets a sequence of command-line options that is equivalent to this property set.
StringSeq getIcePropertyAsList(std::string_view key)
Gets an Ice property as a list of strings.
int getPropertyAsInt(std::string_view key)
Gets a property as an integer.
Properties(const Properties &source)
Copy constructor.
Properties()=default
Default constructor.
static std::pair< PropertyDict, StringSeq > parseOptions(std::string_view prefix, const StringSeq &options)
Parses a sequence of options into a map of key value pairs starting with a prefix.
std::shared_ptr< Properties > PropertiesPtr
A shared pointer to a Properties.
Definition PropertiesF.h:13
std::shared_ptr< StringConverter > StringConverterPtr
A shared pointer to a StringConverter.
PropertiesPtr createProperties()
Creates an empty property set.
std::vector< std::string > StringSeq
A sequence of strings.
std::map< std::string, std::string, std::less<> > PropertyDict
A simple collection of properties, represented as a dictionary of key/value pairs.
The Ice RPC framework.
Definition SampleEvent.h:59