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 <optional>
16 #include <string_view>
17 
18 #include <com/sun/star/uno/Any.hxx>
19 #include <com/sun/star/uno/Reference.h>
22 #include <sal/types.h>
23 #include <memory>
24 
25 namespace com::sun::star {
26  namespace configuration { class XReadWriteAccess; }
27  namespace container {
28  class XHierarchicalNameAccess;
29  class XHierarchicalNameReplace;
30  class XNameAccess;
31  class XNameContainer;
32  }
33  namespace uno { class XComponentContext; }
34 }
35 
36 namespace comphelper {
37 
38 namespace detail { class ConfigurationWrapper; }
39 
48 public:
49  static std::shared_ptr<ConfigurationChanges> create();
50 
52 
53  void commit() const;
54 
55 private:
57  ConfigurationChanges& operator=(const ConfigurationChanges&) = delete;
58 
59  SAL_DLLPRIVATE ConfigurationChanges(
60  css::uno::Reference< css::uno::XComponentContext >
61  const & context);
62 
63  SAL_DLLPRIVATE void setPropertyValue(
64  OUString const & path, css::uno::Any const & value)
65  const;
66 
67  SAL_DLLPRIVATE css::uno::Reference<
68  css::container::XHierarchicalNameReplace >
69  getGroup(OUString const & path) const;
70 
71  SAL_DLLPRIVATE
72  css::uno::Reference< css::container::XNameContainer >
73  getSet(OUString const & path) const;
74 
75  css::uno::Reference<
76  css::configuration::XReadWriteAccess > access_;
77 
79 };
80 
81 namespace detail {
82 
85 public:
86  static ConfigurationWrapper const & get();
87 
88  bool isReadOnly(OUString const & path) const;
89 
90  css::uno::Any getPropertyValue(OUString const & path) const;
91 
92  static void setPropertyValue(
93  std::shared_ptr< ConfigurationChanges > const & batch,
94  OUString const & path, css::uno::Any const & value);
95 
96  css::uno::Any getLocalizedPropertyValue(
97  std::u16string_view path) const;
98 
99  static void setLocalizedPropertyValue(
100  std::shared_ptr< ConfigurationChanges > const & batch,
101  OUString const & path, css::uno::Any const & value);
102 
103  css::uno::Reference<
104  css::container::XHierarchicalNameAccess >
105  getGroupReadOnly(OUString const & path) const;
106 
107  static css::uno::Reference<
108  css::container::XHierarchicalNameReplace >
109  getGroupReadWrite(
110  std::shared_ptr< ConfigurationChanges > const & batch,
111  OUString const & path);
112 
113  css::uno::Reference< css::container::XNameAccess >
114  getSetReadOnly(OUString const & path) const;
115 
116  static css::uno::Reference< css::container::XNameContainer >
117  getSetReadWrite(
118  std::shared_ptr< ConfigurationChanges > const & batch,
119  OUString const & path);
120 
121  std::shared_ptr< ConfigurationChanges > createChanges() const;
122 
123 private:
124  SAL_DLLPRIVATE explicit ConfigurationWrapper();
125 
126  SAL_DLLPRIVATE ~ConfigurationWrapper();
127 
128  ConfigurationWrapper(const ConfigurationWrapper&) = delete;
129  ConfigurationWrapper& operator=(const ConfigurationWrapper&) = delete;
130 
131  css::uno::Reference< css::uno::XComponentContext > context_;
132 
133  css::uno::Reference< css::configuration::XReadWriteAccess > access_;
134  // should really be a css.configuration.ReadOnlyAccess (with added
135  // css.beans.XHierarchicalPropertySetInfo), but then
136  // configmgr::Access::asProperty() would report all properties as
137  // READONLY, so isReadOnly() would not work
138 };
139 
141 template< typename T > struct Convert {
142  static css::uno::Any toAny(T const & value)
143  { return css::uno::Any(value); }
144 
145  static T fromAny(css::uno::Any const & value)
146  { return value.get< T >(); }
147 
148 private:
149  Convert(const Convert&) = delete;
150  Convert& operator=(const Convert&) = delete;
151 
152  Convert() = delete;
153  ~Convert() = delete;
154 };
155 
157 template< typename T > struct Convert< std::optional< T > >
158 {
159  static css::uno::Any toAny(std::optional< T > const & value) {
160  return value
161  ? css::uno::Any(*value)
162  : css::uno::Any();
163  }
164 
165  static std::optional< T > fromAny(css::uno::Any const & value)
166  {
167  return value.hasValue()
168  ? std::optional< T >(value.get< T >()) : std::optional< T >();
169  }
170 
171 private:
172  Convert(const Convert&) = delete;
173  Convert& operator=(const Convert&) = delete;
174 
175  Convert() = delete;
176  ~Convert() = delete;
177 };
178 
179 }
180 
186 template< typename T, typename U > struct ConfigurationProperty
187 {
190  static bool isReadOnly()
191  {
192  return detail::ConfigurationWrapper::get().isReadOnly(T::path());
193  }
194 
198  static U get()
199  {
200  // Folding this into one statement causes a bogus error at least with
201  // Red Hat GCC 4.6.2-1:
202  css::uno::Any a(
204  T::path()));
206  }
207 
212  static void set(
213  U const & value,
214  std::shared_ptr< ConfigurationChanges > const & batch)
215  {
217  batch, T::path(), detail::Convert< U >::toAny(value));
218  }
219 
220 private:
223 
224  ConfigurationProperty() = delete;
225  ~ConfigurationProperty() = delete;
226 };
227 
233 template< typename T, typename U > struct ConfigurationLocalizedProperty
234 {
240  static U get()
241  {
242  // Folding this into one statement causes a bogus error at least with
243  // Red Hat GCC 4.6.2-1:
244  css::uno::Any a(
246  getLocalizedPropertyValue(T::path()));
248  }
249 
256  static void set(
257  U const & value,
258  std::shared_ptr< ConfigurationChanges > const & batch)
259  {
261  batch, T::path(), detail::Convert< U >::toAny(value));
262  }
263 
264 private:
267 
270 };
271 
277 template< typename T > struct ConfigurationGroup {
279  static css::uno::Reference<
280  css::container::XHierarchicalNameAccess >
281  get()
282  {
284  T::path());
285  }
286 
289  static css::uno::Reference<
290  css::container::XHierarchicalNameReplace >
291  get(std::shared_ptr< ConfigurationChanges > const & batch)
292  {
294  batch, T::path());
295  }
296 
297 private:
298  ConfigurationGroup(const ConfigurationGroup&) = delete;
300 
301  ConfigurationGroup() = delete;
302  ~ConfigurationGroup() = delete;
303 };
304 
310 template< typename T > struct ConfigurationSet {
312  static
313  css::uno::Reference< css::container::XNameAccess >
314  get()
315  {
317  T::path());
318  }
319 
322  static
323  css::uno::Reference< css::container::XNameContainer >
324  get(std::shared_ptr< ConfigurationChanges > const & batch)
325  {
327  batch, T::path());
328  }
329 
330 private:
331  ConfigurationSet(const ConfigurationSet&) = delete;
332  ConfigurationSet& operator=(const ConfigurationSet&) = delete;
333 
334  ConfigurationSet() = delete;
335  ~ConfigurationSet() = delete;
336 };
337 
338 }
339 
340 #endif
341 
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
A type-safe wrapper around a localized configuration property.
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...
ConfigurationLocalizedProperty & operator=(const ConfigurationLocalizedProperty &)=delete
static void setPropertyValue(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path, css::uno::Any const &value)
A type-safe wrapper around a configuration set.
static css::uno::Reference< css::container::XHierarchicalNameReplace > getGroupReadWrite(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path)
Convert & operator=(const Convert &)=delete
ConfigurationProperty & operator=(const ConfigurationProperty &)=delete
A type-safe wrapper around a configuration group.
static T fromAny(css::uno::Any const &value)
exports com.sun.star. container
css::uno::Reference< css::configuration::XReadWriteAccess > access_
bool getPropertyValue(ValueType &rValue, css::uno::Reference< css::beans::XPropertySet > const &xPropSet, OUString const &propName)
static ConfigurationWrapper const & get()
static css::uno::Any toAny(T const &value)
css::uno::Reference< css::configuration::XReadWriteAccess > access_
Reference< deployment::XPackageRegistry > create(Reference< deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, Reference< XComponentContext > const &xComponentContext)
A type-safe wrapper around a (non-localized) configuration property.
css::uno::Reference< css::container::XNameAccess > getSetReadOnly(OUString const &path) const
static css::uno::Any toAny(std::optional< T > const &value)
ConfigurationGroup & operator=(const ConfigurationGroup &)=delete
uno_Any a
bool setPropertyValue(uno::Sequence< beans::PropertyValue > &aProp, const OUString &aName, const uno::Any &aValue)
static void setLocalizedPropertyValue(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path, css::uno::Any const &value)
exports com.sun.star. configuration
A batch of configuration changes that is committed as a whole.
css::uno::Reference< css::uno::XComponentContext > context_
Any value
bool isReadOnly(OUString const &path) const
static std::optional< T > fromAny(css::uno::Any const &value)
ConfigurationSet & operator=(const ConfigurationSet &)=delete
#define COMPHELPER_DLLPUBLIC
static css::uno::Reference< css::container::XNameContainer > getSetReadWrite(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path)
css::uno::Reference< css::container::XHierarchicalNameAccess > getGroupReadOnly(OUString const &path) const
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...