Line data Source code
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 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef _MULTI_PROPERTY_SET_HANDLER_HXX
21 : #define _MULTI_PROPERTY_SET_HANDLER_HXX
22 :
23 : #include <rtl/ustring.hxx>
24 : #include <com/sun/star/beans/XPropertySet.hpp>
25 : #include <com/sun/star/beans/XMultiPropertySet.hpp>
26 :
27 :
28 : /** @descr MultiPropertySetHandler handles the two slightly different
29 : interfaces XPropertySet and XMultiPorpertySet for accessing
30 : properties of an object.
31 :
32 : It uses the classes PropertyWrapperBase and the template
33 : PropertyWrapper for a type safe access to single properties.
34 :
35 : The function class OUStringComparison is used by a STL map to
36 : sort the properties by names.
37 : */
38 :
39 : /** @descr Base class for the templated property wrappers.
40 : Having a common base class allows to set a variable to the
41 : property's value without explicit knowledge of its type.
42 : */
43 : class PropertyWrapperBase
44 : {
45 : public:
46 : /** @descr Create a class instance and store the given name.
47 : @param rName The name of the property.
48 : */
49 0 : PropertyWrapperBase (const ::rtl::OUString & rName)
50 0 : : msName (rName)
51 0 : {}
52 0 : virtual ~PropertyWrapperBase()
53 0 : {}
54 :
55 : /** @descr Abstract interface of a method for setting a variables
56 : value to that of the property.
57 : */
58 : virtual void SetValue (const ::com::sun::star::uno::Any & rValue) = 0;
59 :
60 : const ::rtl::OUString msName;
61 : };
62 :
63 :
64 :
65 :
66 : /** @descr For every property type there will be one instantiation of this
67 : template class with its own and type specific version of SetValue.
68 : */
69 0 : template<class T> class PropertyWrapper : public PropertyWrapperBase
70 : {
71 : public:
72 : /** @descr Create a wrapper for a property of type T.
73 : */
74 0 : PropertyWrapper (const ::rtl::OUString & rName, T & rValue)
75 : : PropertyWrapperBase (rName),
76 0 : mrValue (rValue)
77 0 : {}
78 :
79 : /** descr Set the given value inside an Any to the variable referenced
80 : by the data member.
81 : */
82 0 : virtual void SetValue (const ::com::sun::star::uno::Any & rValue)
83 : {
84 0 : rValue >>= mrValue;
85 0 : }
86 :
87 : private:
88 : /// Reference to a variable. Its value can be modified by a call to SetValue.
89 : T & mrValue;
90 : };
91 :
92 :
93 :
94 :
95 : /** @descr Function object for comparing two OUStrings.
96 : */
97 : class OUStringComparison
98 : {
99 : public:
100 : /// Compare two strings. Returns true if the first is before the second.
101 0 : inline bool operator() (const ::rtl::OUString & a, const ::rtl::OUString & b) const
102 : {
103 0 : return (a.compareTo (b) < 0);
104 : }
105 : };
106 :
107 :
108 :
109 :
110 : /** @descr This class lets you get the values from an object that either
111 : supports the interface XPropertySet or XMultiPropertySet. If it
112 : supports both interfaces then XMultiPropertySet is preferred.
113 :
114 : Using it works in three steps.
115 : 1. Create an instance and pass a reference to the object from which to
116 : get the property values.
117 : 2. Make all properties whose values you want to get known to the object
118 : by using the Add method. This creates instances of a template class
119 : that stores the properties name and a reference to the variable in
120 : which to store its value.
121 : 3. Finally call GetProperties to store the properties values into the
122 : variables specified in step 2. This uses either the XPropertySet or
123 : (preferred) the XMultiPropertySet interface.
124 : */
125 : class MultiPropertySetHandler
126 : {
127 : public:
128 : /** @descr Create a handler of the property set of the given
129 : object.
130 : @param xObject A reference to any of the object's interfaces.
131 : not neccessarily XPropertySet or XMultiPropertySet. It
132 : is casted later to one of the two of them.
133 : */
134 : MultiPropertySetHandler (::com::sun::star::uno::Reference<
135 : ::com::sun::star::uno::XInterface> xObject);
136 : ~MultiPropertySetHandler (void);
137 : /** @descr Add a property to handle. The type given implicitely by the
138 : reference to a variable is used to create an instance of
139 : the PropertyWrapper template class.
140 : @param sName Name of the property.
141 : @param rValue Reference to a variable whose value is set by the
142 : call to GetProperties to the property's value.
143 : */
144 0 : template<class T> void Add (const ::rtl::OUString & sName, T& rValue)
145 : {
146 0 : aPropertyList[sName] = new PropertyWrapper<T> (sName, rValue);
147 0 : }
148 :
149 : /** @descr Try to get the values for all properties added with the Add
150 : method. If possible it uses the XMultiPropertySet. If that fails
151 : (i.e. for an UnknownPropertyExcption) or if the interface is not
152 : supported it uses the XPropertySet interface.
153 : @return If none of the two interfaces is supported or using them both
154 : fails then sal_False is returned. Else True is returned.
155 : */
156 : inline sal_Bool GetProperties (void);
157 :
158 : private:
159 : /** @descr Try to use the XMultiPropertySet interface to get the property
160 : values.
161 : @param rNameList A precomputed and sorted sequence of OUStrings
162 : containing the properties names.
163 : @return True if values could be derived.
164 : */
165 : inline sal_Bool MultiGet (const ::com::sun::star::uno::Sequence<
166 : ::rtl::OUString> & rNameList);
167 :
168 : /** @descr Try to use the XPropertySet interface to get the property
169 : values.
170 : @param rNameList A precomputed and sorted sequence of OUStrings
171 : containing the properties names.
172 : @return True if values could be derived.
173 : */
174 : inline sal_Bool SingleGet (const ::com::sun::star::uno::Sequence<
175 : ::rtl::OUString> & rNameList);
176 :
177 : /** @descr STL map that maps from property names to polymorphic instances of
178 : PropertyWrapper. It uses OUStringComparison for sorting
179 : the property names.
180 : */
181 : ::std::map< ::rtl::OUString, PropertyWrapperBase*, OUStringComparison> aPropertyList;
182 :
183 : /// The object from which to get the property values.
184 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> mxObject;
185 : };
186 :
187 :
188 :
189 : //===== Inline implementation of the methods declared above ==========================
190 :
191 0 : MultiPropertySetHandler::MultiPropertySetHandler (::com::sun::star::uno::Reference<
192 : ::com::sun::star::uno::XInterface> xObject)
193 0 : : mxObject (xObject)
194 : {
195 0 : }
196 :
197 :
198 :
199 :
200 0 : MultiPropertySetHandler::~MultiPropertySetHandler (void)
201 : {
202 0 : ::std::map< ::rtl::OUString, PropertyWrapperBase*, OUStringComparison>::iterator I;
203 0 : for (I=aPropertyList.begin(); I!=aPropertyList.end(); ++I)
204 0 : delete I->second;
205 0 : }
206 :
207 :
208 :
209 0 : sal_Bool MultiPropertySetHandler::GetProperties (void)
210 : {
211 0 : ::std::map< ::rtl::OUString, PropertyWrapperBase*, OUStringComparison>::iterator I;
212 0 : ::com::sun::star::uno::Sequence< ::rtl::OUString> aNameList (aPropertyList.size());
213 : int i;
214 0 : for (I=aPropertyList.begin(),i=0; I!=aPropertyList.end(); ++I)
215 0 : aNameList[i++] = I->second->msName;
216 0 : if ( ! MultiGet(aNameList))
217 0 : if ( ! SingleGet(aNameList))
218 0 : return sal_False;
219 0 : return sal_True;
220 : }
221 :
222 :
223 :
224 :
225 0 : sal_Bool MultiPropertySetHandler::MultiGet (const ::com::sun::star::uno::Sequence<
226 : ::rtl::OUString> & rNameList)
227 : {
228 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XMultiPropertySet> xMultiSet (
229 0 : mxObject, ::com::sun::star::uno::UNO_QUERY);
230 0 : if (xMultiSet.is())
231 : try
232 : {
233 0 : ::std::map< ::rtl::OUString, PropertyWrapperBase*, OUStringComparison>::iterator I;
234 : int i;
235 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any> aValueList =
236 0 : xMultiSet->getPropertyValues (rNameList);
237 0 : for (I=aPropertyList.begin(),i=0; I!=aPropertyList.end(); ++I)
238 0 : I->second->SetValue (aValueList[i++]);
239 : }
240 0 : catch (const ::com::sun::star::beans::UnknownPropertyException&)
241 : {
242 0 : return sal_False;
243 : }
244 : else
245 0 : return sal_False;
246 :
247 0 : return sal_True;
248 : }
249 :
250 :
251 :
252 :
253 0 : sal_Bool MultiPropertySetHandler::SingleGet (const ::com::sun::star::uno::Sequence<
254 : ::rtl::OUString> & rNameList)
255 : {
256 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> xSingleSet (
257 0 : mxObject, ::com::sun::star::uno::UNO_QUERY);
258 0 : if (xSingleSet.is())
259 : try
260 : {
261 0 : ::std::map< ::rtl::OUString, PropertyWrapperBase*, OUStringComparison>::iterator I;
262 : int i;
263 0 : for (I=aPropertyList.begin(),i=0; I!=aPropertyList.end(); ++I)
264 0 : I->second->SetValue (xSingleSet->getPropertyValue (rNameList[i++]));
265 : }
266 0 : catch (const ::com::sun::star::beans::UnknownPropertyException&)
267 : {
268 0 : return sal_False;
269 : }
270 : else
271 0 : return sal_False;
272 :
273 0 : return sal_True;
274 : }
275 :
276 :
277 : #endif
278 :
279 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|