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 COMPHELPER_NAMEDVALUECOLLECTION_HXX
30 : #define COMPHELPER_NAMEDVALUECOLLECTION_HXX
31 :
32 : #include <comphelper/comphelperdllapi.h>
33 :
34 : #include <com/sun/star/uno/Sequence.hxx>
35 : #include <com/sun/star/uno/Any.hxx>
36 : #include <com/sun/star/beans/PropertyValue.hpp>
37 : #include <com/sun/star/beans/NamedValue.hpp>
38 :
39 : #include <memory>
40 : #include <algorithm>
41 : #include <vector>
42 :
43 : //........................................................................
44 : namespace comphelper
45 : {
46 : //........................................................................
47 :
48 : // ====================================================================
49 : // = NamedValueCollection
50 : // ====================================================================
51 : struct NamedValueCollection_Impl;
52 : /** a collection of named values, packed in various formats.
53 : */
54 : class COMPHELPER_DLLPUBLIC NamedValueCollection
55 : {
56 : private:
57 : ::std::auto_ptr< NamedValueCollection_Impl > m_pImpl;
58 :
59 : public:
60 : NamedValueCollection();
61 :
62 : NamedValueCollection( const NamedValueCollection& _rCopySource );
63 :
64 : NamedValueCollection& operator=( const NamedValueCollection& i_rCopySource );
65 :
66 : /** constructs a collection
67 : @param _rElements
68 : the wrapped elements of the collection. The <code>Any</code> might contain a sequence of
69 : property values, a sequence of named values, or directly a property value or named value.
70 : All other cases are worth an assertion in non-product builds.
71 : */
72 : NamedValueCollection( const ::com::sun::star::uno::Any& _rElements );
73 :
74 : /** constructs a collection
75 : @param _rArguments
76 : a sequence of Any's containing either PropertyValue's or NamedValue's.
77 : */
78 : NamedValueCollection( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& _rArguments );
79 :
80 : /** constructs a collection
81 : @param _rArguments
82 : a sequence of PropertyValues's
83 : */
84 : NamedValueCollection( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rArguments );
85 :
86 : /** constructs a collection
87 : @param _rArguments
88 : a sequence of NamedValue's
89 : */
90 : NamedValueCollection( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& _rArguments );
91 :
92 : ~NamedValueCollection();
93 :
94 : inline void assign( const ::com::sun::star::uno::Any& i_rWrappedElements )
95 : {
96 : impl_assign( i_rWrappedElements );
97 : }
98 :
99 0 : inline void assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& _rArguments )
100 : {
101 0 : impl_assign( _rArguments );
102 0 : }
103 :
104 : inline void assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rArguments )
105 : {
106 : impl_assign( _rArguments );
107 : }
108 :
109 : inline void assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& _rArguments )
110 : {
111 : impl_assign( _rArguments );
112 : }
113 :
114 0 : inline void clear()
115 : {
116 0 : impl_assign( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >() );
117 0 : }
118 :
119 : /** determines whether or not named values can be extracted from the given value
120 :
121 : @return
122 : <TRUE/> if and only if the given <code>Any</code> contains a <code>NamedValue</code>, a
123 : <code>PropertyValue</code>, or a sequence thereof.
124 : */
125 : static bool canExtractFrom( ::com::sun::star::uno::Any const & i_value );
126 :
127 : /// returns the number of elements in the collection
128 : size_t size() const;
129 :
130 : /// determines whether the collection is empty
131 : bool empty() const;
132 :
133 : /** returns the names of all elements in the collection
134 : */
135 : ::std::vector< ::rtl::OUString >
136 : getNames() const;
137 :
138 : /** merges the content of another collection into |this|
139 : @param _rAdditionalValues
140 : the collection whose values are to be merged
141 : @param _bOverwriteExisting
142 : defines whether or not elements which are already present in |this|
143 : should be overwritten (<TRUE/>) or preserved (<FALSE/>).
144 : @return |*this|
145 : */
146 : NamedValueCollection&
147 : merge(
148 : const NamedValueCollection& _rAdditionalValues,
149 : bool _bOverwriteExisting
150 : );
151 :
152 : /** retrieves a value with a given name from the collection, if it is present
153 :
154 : @param _pAsciiValueName
155 : the ASCII name of the value to retrieve
156 :
157 : @param _out_rValue
158 : is the output parameter taking the desired value upon successful return. If
159 : a value with the given name is not present in the collection, or if a wrong-typed
160 : value is present, then this parameter will not be touched.
161 :
162 : @return
163 : <TRUE/> if there is a value with the given name, which could successfully
164 : be extraced. In this case, <arg>_out_rValue</arg> will contain the requested
165 : value.<br/>
166 : <FALSE/>, if there is no value with the given name.
167 : @throws IllegalArgumentException
168 : in case there is a value with the given name, but it cannot legally assigned to
169 : _out_rValue.
170 : */
171 : template < typename VALUE_TYPE >
172 399 : bool get_ensureType( const sal_Char* _pAsciiValueName, VALUE_TYPE& _out_rValue ) const
173 : {
174 399 : return get_ensureType( ::rtl::OUString::createFromAscii( _pAsciiValueName ), &_out_rValue, ::cppu::UnoType< VALUE_TYPE >::get() );
175 : }
176 :
177 : template < typename VALUE_TYPE >
178 5709 : bool get_ensureType( const ::rtl::OUString& _rValueName, VALUE_TYPE& _out_rValue ) const
179 : {
180 5709 : return get_ensureType( _rValueName, &_out_rValue, ::cppu::UnoType< VALUE_TYPE >::get() );
181 : }
182 :
183 : /** retrieves a value with a given name, or defaults it to a given value, if its not present
184 : in the colllection
185 : */
186 : template < typename VALUE_TYPE >
187 5709 : VALUE_TYPE getOrDefault( const sal_Char* _pAsciiValueName, const VALUE_TYPE& _rDefault ) const
188 : {
189 5709 : return getOrDefault( ::rtl::OUString::createFromAscii( _pAsciiValueName ), _rDefault );
190 : }
191 :
192 : template < typename VALUE_TYPE >
193 5709 : VALUE_TYPE getOrDefault( const ::rtl::OUString& _rValueName, const VALUE_TYPE& _rDefault ) const
194 : {
195 5709 : VALUE_TYPE retVal( _rDefault );
196 5709 : get_ensureType( _rValueName, retVal );
197 5709 : return retVal;
198 : }
199 :
200 : /** retrieves a (untyped) value with a given name
201 :
202 : If the collection does not contain a value with the given name, an empty
203 : Any is returned.
204 : */
205 2274 : const ::com::sun::star::uno::Any& get( const sal_Char* _pAsciiValueName ) const
206 : {
207 2274 : return get( ::rtl::OUString::createFromAscii( _pAsciiValueName ) );
208 : }
209 :
210 : /** retrieves a (untyped) value with a given name
211 :
212 : If the collection does not contain a value with the given name, an empty
213 : Any is returned.
214 : */
215 2274 : const ::com::sun::star::uno::Any& get( const ::rtl::OUString& _rValueName ) const
216 : {
217 2274 : return impl_get( _rValueName );
218 : }
219 :
220 : /// determines whether a value with a given name is present in the collection
221 481 : inline bool has( const sal_Char* _pAsciiValueName ) const
222 : {
223 481 : return impl_has( ::rtl::OUString::createFromAscii( _pAsciiValueName ) );
224 : }
225 :
226 : /// determines whether a value with a given name is present in the collection
227 0 : inline bool has( const ::rtl::OUString& _rValueName ) const
228 : {
229 0 : return impl_has( _rValueName );
230 : }
231 :
232 : /** puts a value into the collection
233 :
234 : @return <TRUE/> if and only if a value was already present previously, in
235 : which case it has been overwritten.
236 : */
237 : template < typename VALUE_TYPE >
238 1392 : inline bool put( const sal_Char* _pAsciiValueName, const VALUE_TYPE& _rValue )
239 : {
240 1392 : return impl_put( ::rtl::OUString::createFromAscii( _pAsciiValueName ), ::com::sun::star::uno::makeAny( _rValue ) );
241 : }
242 :
243 : /** puts a value into the collection
244 :
245 : @return <TRUE/> if and only if a value was already present previously, in
246 : which case it has been overwritten.
247 : */
248 : template < typename VALUE_TYPE >
249 0 : inline bool put( const ::rtl::OUString& _rValueName, const VALUE_TYPE& _rValue )
250 : {
251 0 : return impl_put( _rValueName, ::com::sun::star::uno::makeAny( _rValue ) );
252 : }
253 :
254 240 : inline bool put( const sal_Char* _pAsciiValueName, const ::com::sun::star::uno::Any& _rValue )
255 : {
256 240 : return impl_put( ::rtl::OUString::createFromAscii( _pAsciiValueName ), _rValue );
257 : }
258 :
259 0 : inline bool put( const ::rtl::OUString& _rValueName, const ::com::sun::star::uno::Any& _rValue )
260 : {
261 0 : return impl_put( _rValueName, _rValue );
262 : }
263 :
264 : /** removes the value with the given name from the collection
265 :
266 : @return <TRUE/> if and only if a value with the given name existed in the collection.
267 : */
268 8144 : inline bool remove( const sal_Char* _pAsciiValueName )
269 : {
270 8144 : return impl_remove( ::rtl::OUString::createFromAscii( _pAsciiValueName ) );
271 : }
272 :
273 : /** removes the value with the given name from the collection
274 :
275 : @return <TRUE/> if and only if a value with the given name existed in the collection.
276 : */
277 0 : inline bool remove( const ::rtl::OUString& _rValueName )
278 : {
279 0 : return impl_remove( _rValueName );
280 : }
281 :
282 : /** transforms the collection to a sequence of PropertyValues
283 :
284 : @return
285 : the number of elements in the sequence
286 : */
287 : sal_Int32 operator >>= ( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _out_rValues ) const;
288 :
289 : /** transforms the collection to a sequence of NamedValues
290 :
291 : @return
292 : the number of elements in the sequence
293 : */
294 : sal_Int32 operator >>= ( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& _out_rValues ) const;
295 :
296 : /** transforms the collection into a sequence of PropertyValues
297 : */
298 : inline ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >
299 1501 : getPropertyValues() const
300 : {
301 1501 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aValues;
302 1501 : *this >>= aValues;
303 1501 : return aValues;
304 : }
305 :
306 : /** returns a Sequence< Any >, containing PropertyValues
307 : */
308 : inline ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >
309 224 : getWrappedPropertyValues() const
310 : {
311 224 : return impl_wrap< ::com::sun::star::beans::PropertyValue >();
312 : }
313 :
314 : /** returns a Sequence< Any >, containing NamedValues
315 : */
316 : inline ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >
317 0 : getWrappedNamedValues() const
318 : {
319 0 : return impl_wrap< ::com::sun::star::beans::NamedValue >();
320 : }
321 :
322 : /** transforms the collection into a sequence of NamedValues
323 : */
324 : inline ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >
325 0 : getNamedValues() const
326 : {
327 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > aValues;
328 0 : *this >>= aValues;
329 0 : return aValues;
330 : }
331 :
332 : private:
333 : void impl_assign( const ::com::sun::star::uno::Any& i_rWrappedElements );
334 : void impl_assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& _rArguments );
335 : void impl_assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rArguments );
336 : void impl_assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& _rArguments );
337 :
338 : bool get_ensureType(
339 : const ::rtl::OUString& _rValueName,
340 : void* _pValueLocation,
341 : const ::com::sun::star::uno::Type& _rExpectedValueType
342 : ) const;
343 :
344 : const ::com::sun::star::uno::Any&
345 : impl_get( const ::rtl::OUString& _rValueName ) const;
346 :
347 : bool impl_has( const ::rtl::OUString& _rValueName ) const;
348 :
349 : bool impl_put( const ::rtl::OUString& _rValueName, const ::com::sun::star::uno::Any& _rValue );
350 :
351 : bool impl_remove( const ::rtl::OUString& _rValueName );
352 :
353 : template< class VALUE_TYPE >
354 224 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > impl_wrap() const
355 : {
356 224 : ::com::sun::star::uno::Sequence< VALUE_TYPE > aValues;
357 224 : *this >>= aValues;
358 224 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > aWrappedValues( aValues.getLength() );
359 224 : ::com::sun::star::uno::Any (* const makeAny)(const VALUE_TYPE&) = ::com::sun::star::uno::makeAny< VALUE_TYPE >;
360 224 : ::std::transform(
361 : aValues.getConstArray(),
362 : aValues.getConstArray() + aValues.getLength(),
363 : aWrappedValues.getArray(),
364 : makeAny
365 : );
366 224 : return aWrappedValues;
367 : }
368 : };
369 :
370 : //........................................................................
371 : } // namespace comphelper
372 : //........................................................................
373 :
374 : #endif // COMPHELPER_NAMEDVALUECOLLECTION_HXX
375 :
376 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|