nss: upgrade to release 3.73
[LibreOffice.git] / include / comphelper / configuration.hxx
blobe56951281f9e34a2e3b0541c15039e9fbe01501b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #ifndef INCLUDED_COMPHELPER_CONFIGURATION_HXX
11 #define INCLUDED_COMPHELPER_CONFIGURATION_HXX
13 #include <sal/config.h>
15 #include <optional>
16 #include <com/sun/star/uno/Any.hxx>
17 #include <com/sun/star/uno/Reference.h>
18 #include <comphelper/comphelperdllapi.h>
19 #include <comphelper/processfactory.hxx>
20 #include <sal/types.h>
21 #include <memory>
23 namespace com::sun::star {
24 namespace configuration { class XReadWriteAccess; }
25 namespace container {
26 class XHierarchicalNameAccess;
27 class XHierarchicalNameReplace;
28 class XNameAccess;
29 class XNameContainer;
31 namespace uno { class XComponentContext; }
34 namespace comphelper {
36 namespace detail { class ConfigurationWrapper; }
38 /// A batch of configuration changes that is committed as a whole.
39 ///
40 /// Client code needs to call commit explicitly; otherwise the changes are lost
41 /// when the instance is destroyed.
42 ///
43 /// This is the only class from this header file that client code should use
44 /// directly.
45 class COMPHELPER_DLLPUBLIC ConfigurationChanges {
46 public:
47 static std::shared_ptr<ConfigurationChanges> create(
48 css::uno::Reference< css::uno::XComponentContext >
49 const & context = comphelper::getProcessComponentContext());
51 ~ConfigurationChanges();
53 void commit() const;
55 private:
56 ConfigurationChanges(const ConfigurationChanges&) = delete;
57 ConfigurationChanges& operator=(const ConfigurationChanges&) = delete;
59 SAL_DLLPRIVATE ConfigurationChanges(
60 css::uno::Reference< css::uno::XComponentContext >
61 const & context);
63 SAL_DLLPRIVATE void setPropertyValue(
64 OUString const & path, css::uno::Any const & value)
65 const;
67 SAL_DLLPRIVATE css::uno::Reference<
68 css::container::XHierarchicalNameReplace >
69 getGroup(OUString const & path) const;
71 SAL_DLLPRIVATE
72 css::uno::Reference< css::container::XNameContainer >
73 getSet(OUString const & path) const;
75 css::uno::Reference<
76 css::configuration::XReadWriteAccess > access_;
78 friend class detail::ConfigurationWrapper;
81 namespace detail {
83 /// @internal
84 class COMPHELPER_DLLPUBLIC ConfigurationWrapper {
85 public:
86 static ConfigurationWrapper const & get(
87 css::uno::Reference< css::uno::XComponentContext >
88 const & context);
90 SAL_DLLPRIVATE explicit ConfigurationWrapper(
91 css::uno::Reference< css::uno::XComponentContext >
92 const & context);
94 SAL_DLLPRIVATE ~ConfigurationWrapper();
96 bool isReadOnly(OUString const & path) const;
98 css::uno::Any getPropertyValue(OUString const & path) const;
100 static void setPropertyValue(
101 std::shared_ptr< ConfigurationChanges > const & batch,
102 OUString const & path, css::uno::Any const & value);
104 css::uno::Any getLocalizedPropertyValue(
105 OUString const & path) const;
107 static void setLocalizedPropertyValue(
108 std::shared_ptr< ConfigurationChanges > const & batch,
109 OUString const & path, css::uno::Any const & value);
111 css::uno::Reference<
112 css::container::XHierarchicalNameAccess >
113 getGroupReadOnly(OUString const & path) const;
115 static css::uno::Reference<
116 css::container::XHierarchicalNameReplace >
117 getGroupReadWrite(
118 std::shared_ptr< ConfigurationChanges > const & batch,
119 OUString const & path);
121 css::uno::Reference< css::container::XNameAccess >
122 getSetReadOnly(OUString const & path) const;
124 static css::uno::Reference< css::container::XNameContainer >
125 getSetReadWrite(
126 std::shared_ptr< ConfigurationChanges > const & batch,
127 OUString const & path);
129 std::shared_ptr< ConfigurationChanges > createChanges() const;
131 private:
132 ConfigurationWrapper(const ConfigurationWrapper&) = delete;
133 ConfigurationWrapper& operator=(const ConfigurationWrapper&) = delete;
135 css::uno::Reference< css::uno::XComponentContext > context_;
137 css::uno::Reference< css::configuration::XReadWriteAccess > access_;
138 // should really be a css.configuration.ReadOnlyAccess (with added
139 // css.beans.XHierarchicalPropertySetInfo), but then
140 // configmgr::Access::asProperty() would report all properties as
141 // READONLY, so isReadOnly() would not work
144 /// @internal
145 template< typename T > struct Convert {
146 static css::uno::Any toAny(T const & value)
147 { return css::uno::makeAny(value); }
149 static T fromAny(css::uno::Any const & value)
150 { return value.get< T >(); }
152 private:
153 Convert(const Convert&) = delete;
154 Convert& operator=(const Convert&) = delete;
156 Convert() = delete;
157 ~Convert() = delete;
160 /// @internal
161 template< typename T > struct Convert< std::optional< T > >
163 static css::uno::Any toAny(std::optional< T > const & value) {
164 return value
165 ? css::uno::makeAny(*value)
166 : css::uno::Any();
169 static std::optional< T > fromAny(css::uno::Any const & value)
171 return value.hasValue()
172 ? std::optional< T >(value.get< T >()) : std::optional< T >();
175 private:
176 Convert(const Convert&) = delete;
177 Convert& operator=(const Convert&) = delete;
179 Convert() = delete;
180 ~Convert() = delete;
185 /// A type-safe wrapper around a (non-localized) configuration property.
187 /// Automatically generated headers for the various configuration properties
188 /// derive from this template and make available its member functions to access
189 /// each given configuration property.
190 template< typename T, typename U > struct ConfigurationProperty
192 /// Get the read-only status of the given (non-localized) configuration
193 /// property.
194 static bool isReadOnly(
195 css::uno::Reference<css::uno::XComponentContext> const & context
196 = comphelper::getProcessComponentContext())
198 return detail::ConfigurationWrapper::get(context).isReadOnly(T::path());
201 /// Get the value of the given (non-localized) configuration property.
203 /// For nillable properties, U is of type std::optional<U'>.
204 static U get(
205 css::uno::Reference< css::uno::XComponentContext >
206 const & context = comphelper::getProcessComponentContext())
208 // Folding this into one statement causes a bogus error at least with
209 // Red Hat GCC 4.6.2-1:
210 css::uno::Any a(
211 detail::ConfigurationWrapper::get(context).getPropertyValue(
212 T::path()));
213 return detail::Convert< U >::fromAny(a);
216 /// Set the value of the given (non-localized) configuration property, via a
217 /// given changes batch.
219 /// For nillable properties, U is of type std::optional<U'>.
220 static void set(
221 U const & value,
222 std::shared_ptr< ConfigurationChanges > const & batch)
224 comphelper::detail::ConfigurationWrapper::setPropertyValue(
225 batch, T::path(), detail::Convert< U >::toAny(value));
228 private:
229 ConfigurationProperty(const ConfigurationProperty&) = delete;
230 ConfigurationProperty& operator=(const ConfigurationProperty&) = delete;
232 ConfigurationProperty() = delete;
233 ~ConfigurationProperty() = delete;
236 /// A type-safe wrapper around a localized configuration property.
238 /// Automatically generated headers for the various localized configuration
239 /// properties derive from this template and make available its member functions
240 /// to access each given localized configuration property.
241 template< typename T, typename U > struct ConfigurationLocalizedProperty
243 /// Get the value of the given localized configuration property, for the
244 /// locale currently set at the
245 /// com.sun.star.configuration.theDefaultProvider.
247 /// For nillable properties, U is of type std::optional<U'>.
248 static U get(css::uno::Reference< css::uno::XComponentContext > const & context)
250 // Folding this into one statement causes a bogus error at least with
251 // Red Hat GCC 4.6.2-1:
252 css::uno::Any a(
253 detail::ConfigurationWrapper::get(context).
254 getLocalizedPropertyValue(T::path()));
255 return detail::Convert< U >::fromAny(a);
258 /// Set the value of the given localized configuration property, for the
259 /// locale currently set at the
260 /// com.sun.star.configuration.theDefaultProvider, via a given changes
261 /// batch.
263 /// For nillable properties, U is of type std::optional<U'>.
264 static void set(
265 U const & value,
266 std::shared_ptr< ConfigurationChanges > const & batch)
268 comphelper::detail::ConfigurationWrapper::setLocalizedPropertyValue(
269 batch, T::path(), detail::Convert< U >::toAny(value));
272 private:
273 ConfigurationLocalizedProperty(const ConfigurationLocalizedProperty&) = delete;
274 ConfigurationLocalizedProperty& operator=(const ConfigurationLocalizedProperty&) = delete;
276 ConfigurationLocalizedProperty() = delete;
277 ~ConfigurationLocalizedProperty() = delete;
280 /// A type-safe wrapper around a configuration group.
282 /// Automatically generated headers for the various configuration groups derive
283 /// from this template and make available its member functions to access each
284 /// given configuration group.
285 template< typename T > struct ConfigurationGroup {
286 /// Get read-only access to the given configuration group.
287 static css::uno::Reference<
288 css::container::XHierarchicalNameAccess >
289 get(css::uno::Reference< css::uno::XComponentContext >
290 const & context = comphelper::getProcessComponentContext())
292 return detail::ConfigurationWrapper::get(context).getGroupReadOnly(
293 T::path());
296 /// Get read/write access to the given configuration group, storing any
297 /// modifications via the given changes batch.
298 static css::uno::Reference<
299 css::container::XHierarchicalNameReplace >
300 get(std::shared_ptr< ConfigurationChanges > const & batch)
302 return comphelper::detail::ConfigurationWrapper::getGroupReadWrite(
303 batch, T::path());
306 private:
307 ConfigurationGroup(const ConfigurationGroup&) = delete;
308 ConfigurationGroup& operator=(const ConfigurationGroup&) = delete;
310 ConfigurationGroup() = delete;
311 ~ConfigurationGroup() = delete;
314 /// A type-safe wrapper around a configuration set.
316 /// Automatically generated headers for the various configuration sets derive
317 /// from this template and make available its member functions to access each
318 /// given configuration set.
319 template< typename T > struct ConfigurationSet {
320 /// Get read-only access to the given configuration set.
321 static
322 css::uno::Reference< css::container::XNameAccess >
323 get(css::uno::Reference< css::uno::XComponentContext >
324 const & context = comphelper::getProcessComponentContext())
326 return detail::ConfigurationWrapper::get(context).getSetReadOnly(
327 T::path());
330 /// Get read/write access to the given configuration set, storing any
331 /// modifications via the given changes batch.
332 static
333 css::uno::Reference< css::container::XNameContainer >
334 get(std::shared_ptr< ConfigurationChanges > const & batch)
336 return comphelper::detail::ConfigurationWrapper::getSetReadWrite(
337 batch, T::path());
340 private:
341 ConfigurationSet(const ConfigurationSet&) = delete;
342 ConfigurationSet& operator=(const ConfigurationSet&) = delete;
344 ConfigurationSet() = delete;
345 ~ConfigurationSet() = delete;
350 #endif
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */