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