bump product version to 4.2.0.1
[LibreOffice.git] / include / comphelper / configuration.hxx
blob9be907c2ef698274d8e73af197c4a449b6f84e3a
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 <boost/noncopyable.hpp>
16 #include <boost/optional.hpp>
17 #include <boost/shared_ptr.hpp>
18 #include <com/sun/star/uno/Any.hxx>
19 #include <com/sun/star/uno/Reference.hxx>
20 #include <comphelper/comphelperdllapi.h>
21 #include <comphelper/processfactory.hxx>
22 #include <sal/types.h>
24 namespace com { namespace sun { namespace star {
25 namespace configuration { class XReadWriteAccess; }
26 namespace container {
27 class XHierarchicalNameAccess;
28 class XHierarchicalNameReplace;
29 class XNameAccess;
30 class XNameContainer;
32 namespace uno { class XComponentContext; }
33 } } }
35 namespace comphelper {
37 namespace detail { class ConfigurationWrapper; }
39 /// A batch of configuration changes that is committed as a whole.
40 ///
41 /// Client code needs to call commit explicitly; otherwise the changes are lost
42 /// when the instance is destroyed.
43 ///
44 /// This is the only class from this header file that client code should use
45 /// directly.
46 class COMPHELPER_DLLPUBLIC ConfigurationChanges: private boost::noncopyable {
47 public:
48 static boost::shared_ptr< ConfigurationChanges > create(
49 com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
50 const & context = comphelper::getProcessComponentContext());
52 ~ConfigurationChanges();
54 void commit() const;
56 private:
57 SAL_DLLPRIVATE ConfigurationChanges(
58 com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
59 const & context);
61 SAL_DLLPRIVATE void setPropertyValue(
62 OUString const & path, com::sun::star::uno::Any const & value)
63 const;
65 SAL_DLLPRIVATE com::sun::star::uno::Reference<
66 com::sun::star::container::XHierarchicalNameReplace >
67 getGroup(OUString const & path) const;
69 SAL_DLLPRIVATE
70 com::sun::star::uno::Reference< com::sun::star::container::XNameContainer >
71 getSet(OUString const & path) const;
73 com::sun::star::uno::Reference<
74 com::sun::star::configuration::XReadWriteAccess > access_;
76 friend class detail::ConfigurationWrapper;
79 namespace detail {
81 /// @internal
82 class COMPHELPER_DLLPUBLIC ConfigurationWrapper: private boost::noncopyable {
83 public:
84 static ConfigurationWrapper const & get(
85 com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
86 const & context);
88 SAL_DLLPRIVATE explicit ConfigurationWrapper(
89 com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
90 const & context);
92 SAL_DLLPRIVATE ~ConfigurationWrapper();
94 com::sun::star::uno::Any getPropertyValue(OUString const & path) const;
96 void setPropertyValue(
97 boost::shared_ptr< ConfigurationChanges > const & batch,
98 OUString const & path, com::sun::star::uno::Any const & value)
99 const;
101 com::sun::star::uno::Any getLocalizedPropertyValue(
102 OUString const & path) const;
104 void setLocalizedPropertyValue(
105 boost::shared_ptr< ConfigurationChanges > const & batch,
106 OUString const & path, com::sun::star::uno::Any const & value)
107 const;
109 com::sun::star::uno::Reference<
110 com::sun::star::container::XHierarchicalNameAccess >
111 getGroupReadOnly(OUString const & path) const;
113 com::sun::star::uno::Reference<
114 com::sun::star::container::XHierarchicalNameReplace >
115 getGroupReadWrite(
116 boost::shared_ptr< ConfigurationChanges > const & batch,
117 OUString const & path) const;
119 com::sun::star::uno::Reference< com::sun::star::container::XNameAccess >
120 getSetReadOnly(OUString const & path) const;
122 com::sun::star::uno::Reference< com::sun::star::container::XNameContainer >
123 getSetReadWrite(
124 boost::shared_ptr< ConfigurationChanges > const & batch,
125 OUString const & path) const;
127 boost::shared_ptr< ConfigurationChanges > createChanges() const;
129 private:
130 com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
131 context_;
133 com::sun::star::uno::Reference<
134 com::sun::star::container::XHierarchicalNameAccess > access_;
137 /// @internal
138 template< typename T > struct Convert: private boost::noncopyable {
139 static com::sun::star::uno::Any toAny(T const & value)
140 { return com::sun::star::uno::makeAny(value); }
142 static T fromAny(com::sun::star::uno::Any const & value)
143 { return value.get< T >(); }
145 private:
146 Convert(); // not defined
147 ~Convert(); // not defined
150 /// @internal
151 template< typename T > struct Convert< boost::optional< T > >:
152 private boost::noncopyable
154 static com::sun::star::uno::Any toAny(boost::optional< T > const & value) {
155 return value
156 ? com::sun::star::uno::makeAny(value.get())
157 : com::sun::star::uno::Any();
160 static boost::optional< T > fromAny(com::sun::star::uno::Any const & value)
162 return value.hasValue()
163 ? boost::optional< T >(value.get< T >()) : boost::optional< T >();
166 private:
167 Convert(); // not defined
168 ~Convert(); // not defined
173 /// A type-safe wrapper around a (non-localized) configuration property.
175 /// Automatically generated headers for the various configuration properties
176 /// derive from this template and make available its member functions to access
177 /// each given configuration property.
178 template< typename T, typename U > struct ConfigurationProperty:
179 private boost::noncopyable
181 /// Get the value of the given (non-localized) configuration property.
183 /// For nillable properties, U is of type boost::optional<U'>.
184 static U get(
185 com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
186 const & context = comphelper::getProcessComponentContext())
188 // Folding this into one statement causes a bogus error at least with
189 // Red Hat GCC 4.6.2-1:
190 com::sun::star::uno::Any a(
191 detail::ConfigurationWrapper::get(context).getPropertyValue(
192 T::path()));
193 return detail::Convert< U >::fromAny(a);
196 /// Set the value of the given (non-localized) configuration property, via a
197 /// given changes batch.
199 /// For nillable properties, U is of type boost::optional<U'>.
200 static void set(
201 U const & value,
202 boost::shared_ptr< ConfigurationChanges > const & batch,
203 com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
204 const & context = comphelper::getProcessComponentContext())
206 detail::ConfigurationWrapper::get(context).setPropertyValue(
207 batch, T::path(), detail::Convert< U >::toAny(value));
210 private:
211 ConfigurationProperty(); // not defined
212 ~ConfigurationProperty(); // not defined
215 /// A type-safe wrapper around a localized configuration property.
217 /// Automatically generated headers for the various localized configuration
218 /// properties derive from this template and make available its member functions
219 /// to access each given localized configuration property.
220 template< typename T, typename U > struct ConfigurationLocalizedProperty:
221 private boost::noncopyable
223 /// Get the value of the given localized configuration property, for the
224 /// locale currently set at the
225 /// com.sun.star.configuration.theDefaultProvider.
227 /// For nillable properties, U is of type boost::optional<U'>.
228 static U get(
229 com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
230 const & context = comphelper::getProcessComponentContext())
232 // Folding this into one statement causes a bogus error at least with
233 // Red Hat GCC 4.6.2-1:
234 com::sun::star::uno::Any a(
235 detail::ConfigurationWrapper::get(context).
236 getLocalizedPropertyValue(T::path()));
237 return detail::Convert< U >::fromAny(a);
240 /// Set the value of the given localized configuration property, for the
241 /// locale currently set at the
242 /// com.sun.star.configuration.theDefaultProvider, via a given changes
243 /// batch.
245 /// For nillable properties, U is of type boost::optional<U'>.
246 static void set(
247 U const & value,
248 boost::shared_ptr< ConfigurationChanges > const & batch,
249 com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
250 const & context = comphelper::getProcessComponentContext())
252 detail::ConfigurationWrapper::get(context).setLocalizedPropertyValue(
253 batch, T::path(), detail::Convert< U >::toAny(value));
256 private:
257 ConfigurationLocalizedProperty(); // not defined
258 ~ConfigurationLocalizedProperty(); // not defined
261 /// A type-safe wrapper around a configuration group.
263 /// Automatically generated headers for the various configuration groups derive
264 /// from this template and make available its member functions to access each
265 /// given configuration group.
266 template< typename T > struct ConfigurationGroup: private boost::noncopyable {
267 /// Get read-only access to the given configuration group.
268 static com::sun::star::uno::Reference<
269 com::sun::star::container::XHierarchicalNameAccess >
270 get(com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
271 const & context = comphelper::getProcessComponentContext())
273 return detail::ConfigurationWrapper::get(context).getGroupReadOnly(
274 T::path());
277 /// Get read/write access to the given configuration group, storing any
278 /// modifications via the given changes batch.
279 static com::sun::star::uno::Reference<
280 com::sun::star::container::XHierarchicalNameReplace >
281 get(boost::shared_ptr< ConfigurationChanges > const & batch,
282 com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
283 const & context = comphelper::getProcessComponentContext())
285 return detail::ConfigurationWrapper::get(context).getGroupReadWrite(
286 batch, T::path());
289 private:
290 ConfigurationGroup(); // not defined
291 ~ConfigurationGroup(); // not defined
294 /// A type-safe wrapper around a configuration set.
296 /// Automatically generated headers for the various configuration sets derive
297 /// from this template and make available its member functions to access each
298 /// given configuration set.
299 template< typename T > struct ConfigurationSet: private boost::noncopyable {
300 /// Get read-only access to the given configuration set.
301 static
302 com::sun::star::uno::Reference< com::sun::star::container::XNameAccess >
303 get(com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
304 const & context = comphelper::getProcessComponentContext())
306 return detail::ConfigurationWrapper::get(context).getSetReadOnly(
307 T::path());
310 /// Get read/write access to the given configuration set, storing any
311 /// modifications via the given changes batch.
312 static
313 com::sun::star::uno::Reference< com::sun::star::container::XNameContainer >
314 get(boost::shared_ptr< ConfigurationChanges > const & batch,
315 com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
316 const & context = comphelper::getProcessComponentContext())
318 return detail::ConfigurationWrapper::get(context).getSetReadWrite(
319 batch, T::path());
322 private:
323 ConfigurationSet(); // not defined
324 ~ConfigurationSet(); // not defined
329 #endif
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */