LibreOffice Module comphelper (master) 1
configuration.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
10#ifndef INCLUDED_COMPHELPER_CONFIGURATION_HXX
11#define INCLUDED_COMPHELPER_CONFIGURATION_HXX
12
13#include <sal/config.h>
14
15#include <com/sun/star/uno/Any.hxx>
16#include <com/sun/star/uno/Reference.h>
19#include <sal/types.h>
20#include <memory>
21#include <mutex>
22#include <optional>
23#include <string_view>
24#include <unordered_map>
25
26namespace com::sun::star {
27 namespace configuration { class XReadWriteAccess; }
28 namespace container {
29 class XHierarchicalNameAccess;
30 class XHierarchicalNameReplace;
31 class XNameAccess;
32 class XNameContainer;
33 }
34 namespace uno { class XComponentContext; }
35 namespace util {
36 class XChangesListener;
37 class XChangesNotifier;
38 }
39}
40
41namespace comphelper {
42
43namespace detail { class ConfigurationWrapper; }
44
53public:
54 static std::shared_ptr<ConfigurationChanges> create();
55
57
58 void commit() const;
59
60private:
63
64 SAL_DLLPRIVATE ConfigurationChanges(
65 css::uno::Reference< css::uno::XComponentContext >
66 const & context);
67
68 SAL_DLLPRIVATE void setPropertyValue(
69 OUString const & path, css::uno::Any const & value)
70 const;
71
72 SAL_DLLPRIVATE css::uno::Reference<
73 css::container::XHierarchicalNameReplace >
74 getGroup(OUString const & path) const;
75
76 SAL_DLLPRIVATE
77 css::uno::Reference< css::container::XNameContainer >
78 getSet(OUString const & path) const;
79
80 css::uno::Reference<
81 css::configuration::XReadWriteAccess > access_;
82
84};
85
86namespace detail {
87
88class ConfigurationChangesListener;
89
93public:
94 static ConfigurationWrapper const & get();
95
96 bool isReadOnly(OUString const & path) const;
97
98 css::uno::Any getPropertyValue(OUString const & path) const;
99
100 static void setPropertyValue(
101 std::shared_ptr< ConfigurationChanges > const & batch,
102 OUString const & path, css::uno::Any const & value);
103
104 css::uno::Any getLocalizedPropertyValue(
105 std::u16string_view path) const;
106
107 static void setLocalizedPropertyValue(
108 std::shared_ptr< ConfigurationChanges > const & batch,
109 OUString const & path, css::uno::Any const & value);
110
111 css::uno::Reference<
112 css::container::XHierarchicalNameAccess >
113 getGroupReadOnly(OUString const & path) const;
114
115 static css::uno::Reference<
116 css::container::XHierarchicalNameReplace >
117 getGroupReadWrite(
118 std::shared_ptr< ConfigurationChanges > const & batch,
119 OUString const & path);
120
121 css::uno::Reference< css::container::XNameAccess >
122 getSetReadOnly(OUString const & path) const;
123
124 static css::uno::Reference< css::container::XNameContainer >
125 getSetReadWrite(
126 std::shared_ptr< ConfigurationChanges > const & batch,
127 OUString const & path);
128
129 std::shared_ptr< ConfigurationChanges > createChanges() const;
130
131private:
132 SAL_DLLPRIVATE explicit ConfigurationWrapper();
133
134 SAL_DLLPRIVATE ~ConfigurationWrapper();
135
138
139 css::uno::Reference< css::uno::XComponentContext > context_;
140
141 css::uno::Reference< css::configuration::XReadWriteAccess > access_;
142 // should really be a css.configuration.ReadOnlyAccess (with added
143 // css.beans.XHierarchicalPropertySetInfo), but then
144 // configmgr::Access::asProperty() would report all properties as
145 // READONLY, so isReadOnly() would not work
146
149 mutable std::unordered_map<OUString, css::uno::Any> maPropertyCache;
150 css::uno::Reference< css::util::XChangesNotifier > maNotifier;
151 css::uno::Reference< css::util::XChangesListener > maListener;
152};
153
155template< typename T > struct Convert {
156 static css::uno::Any toAny(T const & value)
157 { return css::uno::Any(value); }
158
159 static T fromAny(css::uno::Any const & value)
160 { return value.get< T >(); }
161
162private:
163 Convert(const Convert&) = delete;
164 Convert& operator=(const Convert&) = delete;
165
166 Convert() = delete;
167 ~Convert() = delete;
168};
169
171template< typename T > struct Convert< std::optional< T > >
172{
173 static css::uno::Any toAny(std::optional< T > const & value) {
174 return value
175 ? css::uno::Any(*value)
176 : css::uno::Any();
177 }
178
179 static std::optional< T > fromAny(css::uno::Any const & value)
180 {
181 return value.hasValue()
182 ? std::optional< T >(value.get< T >()) : std::optional< T >();
183 }
184
185private:
186 Convert(const Convert&) = delete;
187 Convert& operator=(const Convert&) = delete;
188
189 Convert() = delete;
190 ~Convert() = delete;
191};
192
193}
194
200template< typename T, typename U > struct ConfigurationProperty
201{
204 static bool isReadOnly()
205 {
207 }
208
212 static U get()
213 {
214 // Folding this into one statement causes a bogus error at least with
215 // Red Hat GCC 4.6.2-1:
216 css::uno::Any a(
218 T::path()));
220 }
221
226 static void set(
227 U const & value,
228 std::shared_ptr< ConfigurationChanges > const & batch)
229 {
231 batch, T::path(), detail::Convert< U >::toAny(value));
232 }
233
234private:
237
240};
241
247template< typename T, typename U > struct ConfigurationLocalizedProperty
248{
254 static U get()
255 {
256 // Folding this into one statement causes a bogus error at least with
257 // Red Hat GCC 4.6.2-1:
258 css::uno::Any a(
260 getLocalizedPropertyValue(T::path()));
262 }
263
270 static void set(
271 U const & value,
272 std::shared_ptr< ConfigurationChanges > const & batch)
273 {
275 batch, T::path(), detail::Convert< U >::toAny(value));
276 }
277
278private:
281
284};
285
291template< typename T > struct ConfigurationGroup {
293 static css::uno::Reference<
294 css::container::XHierarchicalNameAccess >
296 {
298 T::path());
299 }
300
303 static css::uno::Reference<
304 css::container::XHierarchicalNameReplace >
305 get(std::shared_ptr< ConfigurationChanges > const & batch)
306 {
308 batch, T::path());
309 }
310
311private:
314
317};
318
324template< typename T > struct ConfigurationSet {
326 static
327 css::uno::Reference< css::container::XNameAccess >
329 {
331 T::path());
332 }
333
336 static
337 css::uno::Reference< css::container::XNameContainer >
338 get(std::shared_ptr< ConfigurationChanges > const & batch)
339 {
341 batch, T::path());
342 }
343
344private:
347
350};
351
352}
353
354#endif
355
356/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
A batch of configuration changes that is committed as a whole.
css::uno::Reference< css::configuration::XReadWriteAccess > access_
ConfigurationChanges(const ConfigurationChanges &)=delete
ConfigurationChanges & operator=(const ConfigurationChanges &)=delete
ConfigurationWrapper(const ConfigurationWrapper &)=delete
css::uno::Reference< css::container::XHierarchicalNameAccess > getGroupReadOnly(OUString const &path) const
bool isReadOnly(OUString const &path) const
ConfigurationWrapper & operator=(const ConfigurationWrapper &)=delete
css::uno::Reference< css::configuration::XReadWriteAccess > access_
static void setPropertyValue(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path, css::uno::Any const &value)
css::uno::Reference< css::util::XChangesListener > maListener
css::uno::Reference< css::util::XChangesNotifier > maNotifier
css::uno::Reference< css::uno::XComponentContext > context_
static void setLocalizedPropertyValue(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path, css::uno::Any const &value)
std::unordered_map< OUString, css::uno::Any > maPropertyCache
css::uno::Reference< css::container::XNameAccess > getSetReadOnly(OUString const &path) const
static css::uno::Reference< css::container::XNameContainer > getSetReadWrite(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path)
static ConfigurationWrapper const & get()
static css::uno::Reference< css::container::XHierarchicalNameReplace > getGroupReadWrite(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path)
#define COMPHELPER_DLLPUBLIC
Any value
uno_Any a
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
VBAHELPER_DLLPUBLIC bool setPropertyValue(css::uno::Sequence< css::beans::PropertyValue > &aProp, const OUString &aName, const css::uno::Any &aValue)
bool getPropertyValue(ValueType &rValue, css::uno::Reference< css::beans::XPropertySet > const &xPropSet, OUString const &propName)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
std::mutex mutex
Definition: random.cxx:41
A type-safe wrapper around a configuration group.
static css::uno::Reference< css::container::XHierarchicalNameAccess > get()
Get read-only access to the given configuration group.
ConfigurationGroup & operator=(const ConfigurationGroup &)=delete
static css::uno::Reference< css::container::XHierarchicalNameReplace > get(std::shared_ptr< ConfigurationChanges > const &batch)
Get read/write access to the given configuration group, storing any modifications via the given chang...
ConfigurationGroup(const ConfigurationGroup &)=delete
A type-safe wrapper around a localized configuration property.
ConfigurationLocalizedProperty & operator=(const ConfigurationLocalizedProperty &)=delete
ConfigurationLocalizedProperty(const ConfigurationLocalizedProperty &)=delete
static U get()
Get the value of the given localized configuration property, for the locale currently set at the com....
static void set(U const &value, std::shared_ptr< ConfigurationChanges > const &batch)
Set the value of the given localized configuration property, for the locale currently set at the com....
A type-safe wrapper around a (non-localized) configuration property.
ConfigurationProperty & operator=(const ConfigurationProperty &)=delete
static U get()
Get the value of the given (non-localized) configuration property.
static bool isReadOnly()
Get the read-only status of the given (non-localized) configuration property.
static void set(U const &value, std::shared_ptr< ConfigurationChanges > const &batch)
Set the value of the given (non-localized) configuration property, via a given changes batch.
ConfigurationProperty(const ConfigurationProperty &)=delete
A type-safe wrapper around a configuration set.
ConfigurationSet & operator=(const ConfigurationSet &)=delete
ConfigurationSet(const ConfigurationSet &)=delete
static css::uno::Reference< css::container::XNameAccess > get()
Get read-only access to the given configuration set.
static css::uno::Reference< css::container::XNameContainer > get(std::shared_ptr< ConfigurationChanges > const &batch)
Get read/write access to the given configuration set, storing any modifications via the given changes...
Convert & operator=(const Convert &)=delete
static css::uno::Any toAny(std::optional< T > const &value)
static std::optional< T > fromAny(css::uno::Any const &value)
Convert(const Convert &)=delete
static css::uno::Any toAny(T const &value)
static T fromAny(css::uno::Any const &value)
Convert & operator=(const Convert &)=delete