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_COMPHELPER_NAMEDVALUECOLLECTION_HXX
21 #define INCLUDED_COMPHELPER_NAMEDVALUECOLLECTION_HXX
23 #include <comphelper/comphelperdllapi.h>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <com/sun/star/uno/Any.hxx>
27 #include <com/sun/star/beans/PropertyValue.hpp>
28 #include <com/sun/star/beans/NamedValue.hpp>
39 // = NamedValueCollection
41 struct NamedValueCollection_Impl
;
42 /** a collection of named values, packed in various formats.
44 class COMPHELPER_DLLPUBLIC NamedValueCollection
47 ::std::unique_ptr
< NamedValueCollection_Impl
> m_pImpl
;
50 NamedValueCollection();
52 NamedValueCollection( const NamedValueCollection
& _rCopySource
);
53 NamedValueCollection( NamedValueCollection
&& _rCopySource
);
55 NamedValueCollection
& operator=( const NamedValueCollection
& i_rCopySource
);
56 NamedValueCollection
& operator=( NamedValueCollection
&& i_rCopySource
);
58 /** constructs a collection
60 the wrapped elements of the collection. The <code>Any</code> might contain a sequence of
61 property values, a sequence of named values, or directly a property value or named value.
62 All other cases are worth an assertion in non-product builds.
64 NamedValueCollection( const css::uno::Any
& _rElements
);
66 /** constructs a collection
68 a sequence of Any's containing either PropertyValue's or NamedValue's.
70 NamedValueCollection( const css::uno::Sequence
< css::uno::Any
>& _rArguments
);
72 /** constructs a collection
74 a sequence of PropertyValues's
76 NamedValueCollection( const css::uno::Sequence
< css::beans::PropertyValue
>& _rArguments
);
78 /** constructs a collection
80 a sequence of NamedValue's
82 NamedValueCollection( const css::uno::Sequence
< css::beans::NamedValue
>& _rArguments
);
84 ~NamedValueCollection();
86 inline void assign( const css::uno::Sequence
< css::uno::Any
>& _rArguments
)
88 impl_assign( _rArguments
);
93 impl_assign( css::uno::Sequence
< css::beans::NamedValue
>() );
96 /** determines whether or not named values can be extracted from the given value
99 <TRUE/> if and only if the given <code>Any</code> contains a <code>NamedValue</code>, a
100 <code>PropertyValue</code>, or a sequence thereof.
102 static bool canExtractFrom( css::uno::Any
const & i_value
);
104 /// returns the number of elements in the collection
107 /// determines whether the collection is empty
110 /** returns the names of all elements in the collection
112 ::std::vector
< OUString
>
115 /** merges the content of another collection into |this|
116 @param _rAdditionalValues
117 the collection whose values are to be merged
118 @param _bOverwriteExisting
119 defines whether or not elements which are already present in |this|
120 should be overwritten (<TRUE/>) or preserved (<FALSE/>).
123 NamedValueCollection
&
125 const NamedValueCollection
& _rAdditionalValues
,
126 bool _bOverwriteExisting
129 /** retrieves a value with a given name from the collection, if it is present
131 @param _pAsciiValueName
132 the ASCII name of the value to retrieve
135 is the output parameter taking the desired value upon successful return. If
136 a value with the given name is not present in the collection, or if a wrong-typed
137 value is present, then this parameter will not be touched.
140 <TRUE/> if there is a value with the given name, which could successfully
141 be extraced. In this case, <arg>_out_rValue</arg> will contain the requested
143 <FALSE/>, if there is no value with the given name.
144 @throws IllegalArgumentException
145 in case there is a value with the given name, but it cannot legally assigned to
148 template < typename VALUE_TYPE
>
149 bool get_ensureType( const sal_Char
* _pAsciiValueName
, VALUE_TYPE
& _out_rValue
) const
151 return get_ensureType( OUString::createFromAscii( _pAsciiValueName
), &_out_rValue
, ::cppu::UnoType
< VALUE_TYPE
>::get() );
154 template < typename VALUE_TYPE
>
155 bool get_ensureType( const OUString
& _rValueName
, VALUE_TYPE
& _out_rValue
) const
157 return get_ensureType( _rValueName
, &_out_rValue
, ::cppu::UnoType
< VALUE_TYPE
>::get() );
160 /** retrieves a value with a given name, or defaults it to a given value, if it's not present
163 template < typename VALUE_TYPE
>
164 VALUE_TYPE
getOrDefault( const sal_Char
* _pAsciiValueName
, const VALUE_TYPE
& _rDefault
) const
166 return getOrDefault( OUString::createFromAscii( _pAsciiValueName
), _rDefault
);
169 template < typename VALUE_TYPE
>
170 VALUE_TYPE
getOrDefault( const OUString
& _rValueName
, const VALUE_TYPE
& _rDefault
) const
172 VALUE_TYPE
retVal( _rDefault
);
173 get_ensureType( _rValueName
, retVal
);
177 /** retrieves a (untyped) value with a given name
179 If the collection does not contain a value with the given name, an empty
182 const css::uno::Any
& get( const sal_Char
* _pAsciiValueName
) const
184 return get( OUString::createFromAscii( _pAsciiValueName
) );
187 /** retrieves a (untyped) value with a given name
189 If the collection does not contain a value with the given name, an empty
192 const css::uno::Any
& get( const OUString
& _rValueName
) const
194 return impl_get( _rValueName
);
197 /// determines whether a value with a given name is present in the collection
198 inline bool has( const sal_Char
* _pAsciiValueName
) const
200 return impl_has( OUString::createFromAscii( _pAsciiValueName
) );
203 /// determines whether a value with a given name is present in the collection
204 inline bool has( const OUString
& _rValueName
) const
206 return impl_has( _rValueName
);
209 /** puts a value into the collection
211 @return <TRUE/> if and only if a value was already present previously, in
212 which case it has been overwritten.
214 template < typename VALUE_TYPE
>
215 inline bool put( const sal_Char
* _pAsciiValueName
, const VALUE_TYPE
& _rValue
)
217 return impl_put( OUString::createFromAscii( _pAsciiValueName
), css::uno::makeAny( _rValue
) );
220 /** puts a value into the collection
222 @return <TRUE/> if and only if a value was already present previously, in
223 which case it has been overwritten.
225 template < typename VALUE_TYPE
>
226 inline bool put( const OUString
& _rValueName
, const VALUE_TYPE
& _rValue
)
228 return impl_put( _rValueName
, css::uno::makeAny( _rValue
) );
231 inline bool put( const sal_Char
* _pAsciiValueName
, const css::uno::Any
& _rValue
)
233 return impl_put( OUString::createFromAscii( _pAsciiValueName
), _rValue
);
236 inline bool put( const OUString
& _rValueName
, const css::uno::Any
& _rValue
)
238 return impl_put( _rValueName
, _rValue
);
241 /** removes the value with the given name from the collection
243 @return <TRUE/> if and only if a value with the given name existed in the collection.
245 inline bool remove( const sal_Char
* _pAsciiValueName
)
247 return impl_remove( OUString::createFromAscii( _pAsciiValueName
) );
250 /** removes the value with the given name from the collection
252 @return <TRUE/> if and only if a value with the given name existed in the collection.
254 inline bool remove( const OUString
& _rValueName
)
256 return impl_remove( _rValueName
);
259 /** transforms the collection to a sequence of PropertyValues
262 the number of elements in the sequence
264 sal_Int32
operator >>= ( css::uno::Sequence
< css::beans::PropertyValue
>& _out_rValues
) const;
266 /** transforms the collection to a sequence of NamedValues
269 the number of elements in the sequence
271 sal_Int32
operator >>= ( css::uno::Sequence
< css::beans::NamedValue
>& _out_rValues
) const;
273 /** transforms the collection into a sequence of PropertyValues
275 inline css::uno::Sequence
< css::beans::PropertyValue
>
276 getPropertyValues() const
278 css::uno::Sequence
< css::beans::PropertyValue
> aValues
;
283 /** returns a Sequence< Any >, containing PropertyValues
285 inline css::uno::Sequence
< css::uno::Any
>
286 getWrappedPropertyValues() const
288 return impl_wrap
< css::beans::PropertyValue
>();
291 /** returns a Sequence< Any >, containing NamedValues
293 inline css::uno::Sequence
< css::uno::Any
>
294 getWrappedNamedValues() const
296 return impl_wrap
< css::beans::NamedValue
>();
299 /** transforms the collection into a sequence of NamedValues
301 inline css::uno::Sequence
< css::beans::NamedValue
>
302 getNamedValues() const
304 css::uno::Sequence
< css::beans::NamedValue
> aValues
;
310 void impl_assign( const css::uno::Any
& i_rWrappedElements
);
311 void impl_assign( const css::uno::Sequence
< css::uno::Any
>& _rArguments
);
312 void impl_assign( const css::uno::Sequence
< css::beans::PropertyValue
>& _rArguments
);
313 void impl_assign( const css::uno::Sequence
< css::beans::NamedValue
>& _rArguments
);
316 const OUString
& _rValueName
,
317 void* _pValueLocation
,
318 const css::uno::Type
& _rExpectedValueType
322 impl_get( const OUString
& _rValueName
) const;
324 bool impl_has( const OUString
& _rValueName
) const;
326 bool impl_put( const OUString
& _rValueName
, const css::uno::Any
& _rValue
);
328 bool impl_remove( const OUString
& _rValueName
);
330 template< class VALUE_TYPE
>
331 css::uno::Sequence
< css::uno::Any
> impl_wrap() const
333 css::uno::Sequence
< VALUE_TYPE
> aValues
;
335 css::uno::Sequence
< css::uno::Any
> aWrappedValues( aValues
.getLength() );
337 css::uno::Any
* pO
= aWrappedValues
.getArray();
338 const VALUE_TYPE
* pV
= aValues
.getConstArray();
339 const sal_Int32 nLen
= aValues
.getLength();
340 for( sal_Int32 i
= 0; i
< nLen
; ++i
)
341 *(pO
++) = css::uno::makeAny
<VALUE_TYPE
>( *(pV
++) );
343 return aWrappedValues
;
348 } // namespace comphelper
351 #endif // INCLUDED_COMPHELPER_NAMEDVALUECOLLECTION_HXX
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */