1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_XMLOFF_SOURCE_CHART_MULTIPROPERTYSETHANDLER_HXX
21 #define INCLUDED_XMLOFF_SOURCE_CHART_MULTIPROPERTYSETHANDLER_HXX
23 #include <rtl/ustring.hxx>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/beans/XMultiPropertySet.hpp>
27 /** @descr MultiPropertySetHandler handles the two slightly different
28 interfaces XPropertySet and XMultiPorpertySet for accessing
29 properties of an object.
31 It uses the classes PropertyWrapperBase and the template
32 PropertyWrapper for a type safe access to single properties.
34 The function class OUStringComparison is used by a STL map to
35 sort the properties by names.
38 /** @descr Base class for the templated property wrappers.
39 Having a common base class allows to set a variable to the
40 property's value without explicit knowledge of its type.
42 class PropertyWrapperBase
45 /** @descr Create a class instance and store the given name.
46 @param rName The name of the property.
48 explicit PropertyWrapperBase (const OUString
& rName
)
51 virtual ~PropertyWrapperBase()
54 /** @descr Abstract interface of a method for setting a variables
55 value to that of the property.
57 virtual void SetValue (const css::uno::Any
& rValue
) = 0;
59 const OUString msName
;
62 /** @descr For every property type there will be one instantiation of this
63 template class with its own and type specific version of SetValue.
65 template<class T
> class PropertyWrapper
: public PropertyWrapperBase
68 /** @descr Create a wrapper for a property of type T.
70 PropertyWrapper (const OUString
& rName
, T
& rValue
)
71 : PropertyWrapperBase (rName
),
75 /** descr Set the given value inside an Any to the variable referenced
78 virtual void SetValue (const css::uno::Any
& rValue
) override
84 /// Reference to a variable. Its value can be modified by a call to SetValue.
88 /** @descr Function object for comparing two OUStrings.
90 class OUStringComparison
93 /// Compare two strings. Returns true if the first is before the second.
94 inline bool operator() (const OUString
& a
, const OUString
& b
) const
96 return (a
.compareTo (b
) < 0);
100 /** @descr This class lets you get the values from an object that either
101 supports the interface XPropertySet or XMultiPropertySet. If it
102 supports both interfaces then XMultiPropertySet is preferred.
104 Using it works in three steps.
105 1. Create an instance and pass a reference to the object from which to
106 get the property values.
107 2. Make all properties whose values you want to get known to the object
108 by using the Add method. This creates instances of a template class
109 that stores the properties name and a reference to the variable in
110 which to store its value.
111 3. Finally call GetProperties to store the properties values into the
112 variables specified in step 2. This uses either the XPropertySet or
113 (preferred) the XMultiPropertySet interface.
115 class MultiPropertySetHandler
118 /** @descr Create a handler of the property set of the given
120 @param xObject A reference to any of the object's interfaces.
121 not necessarily XPropertySet or XMultiPropertySet. It
122 is casted later to one of the two of them.
124 explicit MultiPropertySetHandler (css::uno::Reference
<
125 css::uno::XInterface
> xObject
);
126 ~MultiPropertySetHandler();
127 /** @descr Add a property to handle. The type given implicitly by the
128 reference to a variable is used to create an instance of
129 the PropertyWrapper template class.
130 @param sName Name of the property.
131 @param rValue Reference to a variable whose value is set by the
132 call to GetProperties to the property's value.
134 template<class T
> void Add (const OUString
& sName
, T
& rValue
)
136 aPropertyList
[sName
] = new PropertyWrapper
<T
> (sName
, rValue
);
139 /** @descr Try to get the values for all properties added with the Add
140 method. If possible it uses the XMultiPropertySet. If that fails
141 (i.e. for an UnknownPropertyExcption) or if the interface is not
142 supported it uses the XPropertySet interface.
143 @return If none of the two interfaces is supported or using them both
144 fails then sal_False is returned. Else True is returned.
146 inline bool GetProperties();
149 /** @descr Try to use the XMultiPropertySet interface to get the property
151 @param rNameList A precomputed and sorted sequence of OUStrings
152 containing the properties names.
153 @return True if values could be derived.
155 inline bool MultiGet (const css::uno::Sequence
<
156 OUString
> & rNameList
);
158 /** @descr Try to use the XPropertySet interface to get the property
160 @param rNameList A precomputed and sorted sequence of OUStrings
161 containing the properties names.
162 @return True if values could be derived.
164 inline bool SingleGet (const css::uno::Sequence
<
165 OUString
> & rNameList
);
167 /** @descr STL map that maps from property names to polymorphic instances of
168 PropertyWrapper. It uses OUStringComparison for sorting
171 ::std::map
< OUString
, PropertyWrapperBase
*, OUStringComparison
> aPropertyList
;
173 /// The object from which to get the property values.
174 css::uno::Reference
< css::uno::XInterface
> mxObject
;
177 MultiPropertySetHandler::MultiPropertySetHandler (css::uno::Reference
<
178 css::uno::XInterface
> xObject
)
183 MultiPropertySetHandler::~MultiPropertySetHandler()
185 ::std::map
< OUString
, PropertyWrapperBase
*, OUStringComparison
>::iterator I
;
186 for (I
=aPropertyList
.begin(); I
!=aPropertyList
.end(); ++I
)
190 bool MultiPropertySetHandler::GetProperties()
192 ::std::map
< OUString
, PropertyWrapperBase
*, OUStringComparison
>::iterator I
;
193 css::uno::Sequence
< OUString
> aNameList (aPropertyList
.size());
195 for (I
=aPropertyList
.begin(),i
=0; I
!=aPropertyList
.end(); ++I
)
196 aNameList
[i
++] = I
->second
->msName
;
197 if ( ! MultiGet(aNameList
))
198 if ( ! SingleGet(aNameList
))
203 bool MultiPropertySetHandler::MultiGet (const css::uno::Sequence
<
204 OUString
> & rNameList
)
206 css::uno::Reference
< css::beans::XMultiPropertySet
> xMultiSet (
207 mxObject
, css::uno::UNO_QUERY
);
211 ::std::map
< OUString
, PropertyWrapperBase
*, OUStringComparison
>::iterator I
;
213 css::uno::Sequence
< css::uno::Any
> aValueList
=
214 xMultiSet
->getPropertyValues (rNameList
);
215 for (I
=aPropertyList
.begin(),i
=0; I
!=aPropertyList
.end(); ++I
)
216 I
->second
->SetValue (aValueList
[i
++]);
218 catch (const css::beans::UnknownPropertyException
&)
228 bool MultiPropertySetHandler::SingleGet (const css::uno::Sequence
<
229 OUString
> & rNameList
)
231 css::uno::Reference
< css::beans::XPropertySet
> xSingleSet (
232 mxObject
, css::uno::UNO_QUERY
);
236 ::std::map
< OUString
, PropertyWrapperBase
*, OUStringComparison
>::iterator I
;
238 for (I
=aPropertyList
.begin(),i
=0; I
!=aPropertyList
.end(); ++I
)
239 I
->second
->SetValue (xSingleSet
->getPropertyValue (rNameList
[i
++]));
241 catch (const css::beans::UnknownPropertyException
&)
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */