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 INCLUDED_XMLOFF_SOURCE_CHART_MULTIPROPERTYSETHANDLER_HXX
21 : #define INCLUDED_XMLOFF_SOURCE_CHART_MULTIPROPERTYSETHANDLER_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 : /** @descr MultiPropertySetHandler handles the two slightly different
28 : interfaces XPropertySet and XMultiPorpertySet for accessing
29 : properties of an object.
30 :
31 : It uses the classes PropertyWrapperBase and the template
32 : PropertyWrapper for a type safe access to single properties.
33 :
34 : The function class OUStringComparison is used by a STL map to
35 : sort the properties by names.
36 : */
37 :
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.
41 : */
42 : class PropertyWrapperBase
43 : {
44 : public:
45 : /** @descr Create a class instance and store the given name.
46 : @param rName The name of the property.
47 : */
48 0 : PropertyWrapperBase (const OUString & rName)
49 0 : : msName (rName)
50 0 : {}
51 0 : virtual ~PropertyWrapperBase()
52 0 : {}
53 :
54 : /** @descr Abstract interface of a method for setting a variables
55 : value to that of the property.
56 : */
57 : virtual void SetValue (const ::com::sun::star::uno::Any & rValue) = 0;
58 :
59 : const OUString msName;
60 : };
61 :
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.
64 : */
65 0 : template<class T> class PropertyWrapper : public PropertyWrapperBase
66 : {
67 : public:
68 : /** @descr Create a wrapper for a property of type T.
69 : */
70 0 : PropertyWrapper (const OUString & rName, T & rValue)
71 : : PropertyWrapperBase (rName),
72 0 : mrValue (rValue)
73 0 : {}
74 :
75 : /** descr Set the given value inside an Any to the variable referenced
76 : by the data member.
77 : */
78 0 : virtual void SetValue (const ::com::sun::star::uno::Any & rValue) SAL_OVERRIDE
79 : {
80 0 : rValue >>= mrValue;
81 0 : }
82 :
83 : private:
84 : /// Reference to a variable. Its value can be modified by a call to SetValue.
85 : T & mrValue;
86 : };
87 :
88 : /** @descr Function object for comparing two OUStrings.
89 : */
90 : class OUStringComparison
91 : {
92 : public:
93 : /// Compare two strings. Returns true if the first is before the second.
94 0 : inline bool operator() (const OUString & a, const OUString & b) const
95 : {
96 0 : return (a.compareTo (b) < 0);
97 : }
98 : };
99 :
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.
103 :
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.
114 : */
115 : class MultiPropertySetHandler
116 : {
117 : public:
118 : /** @descr Create a handler of the property set of the given
119 : object.
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.
123 : */
124 : MultiPropertySetHandler (::com::sun::star::uno::Reference<
125 : ::com::sun::star::uno::XInterface> xObject);
126 : ~MultiPropertySetHandler (void);
127 : /** @descr Add a property to handle. The type given implicitely 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.
133 : */
134 0 : template<class T> void Add (const OUString & sName, T& rValue)
135 : {
136 0 : aPropertyList[sName] = new PropertyWrapper<T> (sName, rValue);
137 0 : }
138 :
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.
145 : */
146 : inline sal_Bool GetProperties (void);
147 :
148 : private:
149 : /** @descr Try to use the XMultiPropertySet interface to get the property
150 : values.
151 : @param rNameList A precomputed and sorted sequence of OUStrings
152 : containing the properties names.
153 : @return True if values could be derived.
154 : */
155 : inline sal_Bool MultiGet (const ::com::sun::star::uno::Sequence<
156 : OUString> & rNameList);
157 :
158 : /** @descr Try to use the XPropertySet interface to get the property
159 : values.
160 : @param rNameList A precomputed and sorted sequence of OUStrings
161 : containing the properties names.
162 : @return True if values could be derived.
163 : */
164 : inline sal_Bool SingleGet (const ::com::sun::star::uno::Sequence<
165 : OUString> & rNameList);
166 :
167 : /** @descr STL map that maps from property names to polymorphic instances of
168 : PropertyWrapper. It uses OUStringComparison for sorting
169 : the property names.
170 : */
171 : ::std::map< OUString, PropertyWrapperBase*, OUStringComparison> aPropertyList;
172 :
173 : /// The object from which to get the property values.
174 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> mxObject;
175 : };
176 :
177 0 : MultiPropertySetHandler::MultiPropertySetHandler (::com::sun::star::uno::Reference<
178 : ::com::sun::star::uno::XInterface> xObject)
179 0 : : mxObject (xObject)
180 : {
181 0 : }
182 :
183 0 : MultiPropertySetHandler::~MultiPropertySetHandler (void)
184 : {
185 0 : ::std::map< OUString, PropertyWrapperBase*, OUStringComparison>::iterator I;
186 0 : for (I=aPropertyList.begin(); I!=aPropertyList.end(); ++I)
187 0 : delete I->second;
188 0 : }
189 :
190 0 : sal_Bool MultiPropertySetHandler::GetProperties (void)
191 : {
192 0 : ::std::map< OUString, PropertyWrapperBase*, OUStringComparison>::iterator I;
193 0 : ::com::sun::star::uno::Sequence< OUString> aNameList (aPropertyList.size());
194 : int i;
195 0 : for (I=aPropertyList.begin(),i=0; I!=aPropertyList.end(); ++I)
196 0 : aNameList[i++] = I->second->msName;
197 0 : if ( ! MultiGet(aNameList))
198 0 : if ( ! SingleGet(aNameList))
199 0 : return sal_False;
200 0 : return sal_True;
201 : }
202 :
203 0 : sal_Bool MultiPropertySetHandler::MultiGet (const ::com::sun::star::uno::Sequence<
204 : OUString> & rNameList)
205 : {
206 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XMultiPropertySet> xMultiSet (
207 0 : mxObject, ::com::sun::star::uno::UNO_QUERY);
208 0 : if (xMultiSet.is())
209 : try
210 : {
211 0 : ::std::map< OUString, PropertyWrapperBase*, OUStringComparison>::iterator I;
212 : int i;
213 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any> aValueList =
214 0 : xMultiSet->getPropertyValues (rNameList);
215 0 : for (I=aPropertyList.begin(),i=0; I!=aPropertyList.end(); ++I)
216 0 : I->second->SetValue (aValueList[i++]);
217 : }
218 0 : catch (const ::com::sun::star::beans::UnknownPropertyException&)
219 : {
220 0 : return sal_False;
221 : }
222 : else
223 0 : return sal_False;
224 :
225 0 : return sal_True;
226 : }
227 :
228 0 : sal_Bool MultiPropertySetHandler::SingleGet (const ::com::sun::star::uno::Sequence<
229 : OUString> & rNameList)
230 : {
231 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> xSingleSet (
232 0 : mxObject, ::com::sun::star::uno::UNO_QUERY);
233 0 : if (xSingleSet.is())
234 : try
235 : {
236 0 : ::std::map< OUString, PropertyWrapperBase*, OUStringComparison>::iterator I;
237 : int i;
238 0 : for (I=aPropertyList.begin(),i=0; I!=aPropertyList.end(); ++I)
239 0 : I->second->SetValue (xSingleSet->getPropertyValue (rNameList[i++]));
240 : }
241 0 : catch (const ::com::sun::star::beans::UnknownPropertyException&)
242 : {
243 0 : return sal_False;
244 : }
245 : else
246 0 : return sal_False;
247 :
248 0 : return sal_True;
249 : }
250 :
251 : #endif
252 :
253 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|