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