1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MultiPropertySetHandler.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _MULTI_PROPERTY_SET_HANDLER_HXX
32 #define _MULTI_PROPERTY_SET_HANDLER_HXX
34 #include <rtl/ustring.hxx>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/beans/XMultiPropertySet.hpp>
39 /** @descr MultiPropertySetHandler handles the two slightly different
40 interfaces XPropertySet and XMultiPorpertySet for accessing
41 properties of an object.
43 It uses the classes PropertyWrapperBase and the template
44 PropertyWrapper for a type safe access to single properties.
46 The function class OUStringComparison is used by a STL map to
47 sort the properties by names.
50 /** @descr Base class for the templated property wrappers.
51 Having a common base class allows to set a variable to the
52 property's value without explicit knowledge of its type.
54 class PropertyWrapperBase
57 /** @descr Create a class instance and store the given name.
58 @param rName The name of the property.
60 PropertyWrapperBase (const ::rtl::OUString
& rName
)
63 virtual ~PropertyWrapperBase()
66 /** @descr Abstract interface of a method for setting a variables
67 value to that of the property.
69 virtual void SetValue (const ::com::sun::star::uno::Any
& rValue
) = 0;
71 const ::rtl::OUString msName
;
77 /** @descr For every property type there will be one instantiation of this
78 template class with its own and type specific version of SetValue.
80 template<class T
> class PropertyWrapper
: public PropertyWrapperBase
83 /** @descr Create a wrapper for a property of type T.
85 PropertyWrapper (const ::rtl::OUString
& rName
, T
& rValue
)
86 : PropertyWrapperBase (rName
),
90 /** descr Set the given value inside an Any to the variable referenced
93 virtual void SetValue (const ::com::sun::star::uno::Any
& rValue
)
99 /// Reference to a variable. Its value can be modified by a call to SetValue.
106 /** @descr Function object for comparing two OUStrings.
108 class OUStringComparison
111 /// Compare two strings. Returns true if the first is before the second.
112 inline bool operator() (const ::rtl::OUString
& a
, const ::rtl::OUString
& b
) const
114 return (a
.compareTo (b
) < 0);
121 /** @descr This class lets you get the values from an object that either
122 supports the interface XPropertySet or XMultiPropertySet. If it
123 supports both interfaces then XMultiPropertySet is preferred.
125 Using it works in three steps.
126 1. Create an instance and pass a reference to the object from which to
127 get the property values.
128 2. Make all properties whose values you want to get known to the object
129 by using the Add method. This creates instances of a template class
130 that stores the properties name and a reference to the variable in
131 which to store its value.
132 3. Finally call GetProperties to store the properties values into the
133 variables specified in step 2. This uses either the XPropertySet or
134 (preferred) the XMultiPropertySet interface.
136 class MultiPropertySetHandler
139 /** @descr Create a handler of the property set of the given
141 @param xObject A reference to any of the object's interfaces.
142 not neccessarily XPropertySet or XMultiPropertySet. It
143 is casted later to one of the two of them.
145 MultiPropertySetHandler (::com::sun::star::uno::Reference
<
146 ::com::sun::star::uno::XInterface
> xObject
);
147 ~MultiPropertySetHandler (void);
148 /** @descr Add a property to handle. The type given implicitely by the
149 reference to a variable is used to create an instance of
150 the PropertyWrapper template class.
151 @param sName Name of the property.
152 @param rValue Reference to a variable whose value is set by the
153 call to GetProperties to the property's value.
155 template<class T
> void Add (const ::rtl::OUString
& sName
, T
& rValue
)
157 aPropertyList
[sName
] = new PropertyWrapper
<T
> (sName
, rValue
);
160 /** @descr Try to get the values for all properties added with the Add
161 method. If possible it uses the XMultiPropertySet. If that fails
162 (i.e. for an UnknownPropertyExcption) or if the interface is not
163 supported it uses the XPropertySet interface.
164 @return If none of the two interfaces is supported or using them both
165 fails then FALSE is returned. Else True is returned.
167 inline BOOL
GetProperties (void);
170 /** @descr Try to use the XMultiPropertySet interface to get the property
172 @param rNameList A precomputed and sorted sequence of OUStrings
173 containing the properties names.
174 @return True if values could be derived.
176 inline BOOL
MultiGet (const ::com::sun::star::uno::Sequence
<
177 ::rtl::OUString
> & rNameList
);
179 /** @descr Try to use the XPropertySet interface to get the property
181 @param rNameList A precomputed and sorted sequence of OUStrings
182 containing the properties names.
183 @return True if values could be derived.
185 inline BOOL
SingleGet (const ::com::sun::star::uno::Sequence
<
186 ::rtl::OUString
> & rNameList
);
188 /** @descr STL map that maps from property names to polymorphic instances of
189 PropertyWrapper. It uses OUStringComparison for sorting
192 ::std::map
< ::rtl::OUString
, PropertyWrapperBase
*, OUStringComparison
> aPropertyList
;
194 /// The object from which to get the property values.
195 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> mxObject
;
200 //===== Inline implementation of the methods declared above ==========================
202 MultiPropertySetHandler::MultiPropertySetHandler (::com::sun::star::uno::Reference
<
203 ::com::sun::star::uno::XInterface
> xObject
)
211 MultiPropertySetHandler::~MultiPropertySetHandler (void)
213 ::std::map
< ::rtl::OUString
, PropertyWrapperBase
*, OUStringComparison
>::iterator I
;
214 for (I
=aPropertyList
.begin(); I
!=aPropertyList
.end(); I
++)
220 template<class T> void MultiPropertySetHandler::Add (const ::rtl::OUString & sName, T& pValue)
222 aPropertyList[sName] = new PropertyWrapper<T> (sName, pValue);
228 BOOL
MultiPropertySetHandler::GetProperties (void)
230 ::std::map
< ::rtl::OUString
, PropertyWrapperBase
*, OUStringComparison
>::iterator I
;
231 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> aNameList (aPropertyList
.size());
233 for (I
=aPropertyList
.begin(),i
=0; I
!=aPropertyList
.end(); I
++)
234 aNameList
[i
++] = I
->second
->msName
;
235 if ( ! MultiGet(aNameList
))
236 if ( ! SingleGet(aNameList
))
244 BOOL
MultiPropertySetHandler::MultiGet (const ::com::sun::star::uno::Sequence
<
245 ::rtl::OUString
> & rNameList
)
247 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XMultiPropertySet
> xMultiSet (
248 mxObject
, ::com::sun::star::uno::UNO_QUERY
);
252 ::std::map
< ::rtl::OUString
, PropertyWrapperBase
*, OUStringComparison
>::iterator I
;
254 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> aValueList
=
255 xMultiSet
->getPropertyValues (rNameList
);
256 for (I
=aPropertyList
.begin(),i
=0; I
!=aPropertyList
.end(); I
++)
257 I
->second
->SetValue (aValueList
[i
++]);
259 catch (::com::sun::star::beans::UnknownPropertyException e
)
272 BOOL
MultiPropertySetHandler::SingleGet (const ::com::sun::star::uno::Sequence
<
273 ::rtl::OUString
> & rNameList
)
275 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> xSingleSet (
276 mxObject
, ::com::sun::star::uno::UNO_QUERY
);
280 ::std::map
< ::rtl::OUString
, PropertyWrapperBase
*, OUStringComparison
>::iterator I
;
282 for (I
=aPropertyList
.begin(),i
=0; I
!=aPropertyList
.end(); I
++)
283 I
->second
->SetValue (xSingleSet
->getPropertyValue (rNameList
[i
++]));
285 catch (::com::sun::star::beans::UnknownPropertyException e
)