bump product version to 4.2.0.1
[LibreOffice.git] / include / comphelper / namedvaluecollection.hxx
blobc625f24cdcbc72d542177f4cd31e1064dc92cf6d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
30 #include <memory>
31 #include <algorithm>
32 #include <vector>
34 //........................................................................
35 namespace comphelper
37 //........................................................................
39 // ====================================================================
40 // = NamedValueCollection
41 // ====================================================================
42 struct NamedValueCollection_Impl;
43 /** a collection of named values, packed in various formats.
45 class COMPHELPER_DLLPUBLIC NamedValueCollection
47 private:
48 ::std::auto_ptr< NamedValueCollection_Impl > m_pImpl;
50 public:
51 NamedValueCollection();
53 NamedValueCollection( const NamedValueCollection& _rCopySource );
55 NamedValueCollection& operator=( const NamedValueCollection& i_rCopySource );
57 /** constructs a collection
58 @param _rElements
59 the wrapped elements of the collection. The <code>Any</code> might contain a sequence of
60 property values, a sequence of named values, or directly a property value or named value.
61 All other cases are worth an assertion in non-product builds.
63 NamedValueCollection( const ::com::sun::star::uno::Any& _rElements );
65 /** constructs a collection
66 @param _rArguments
67 a sequence of Any's containing either PropertyValue's or NamedValue's.
69 NamedValueCollection( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& _rArguments );
71 /** constructs a collection
72 @param _rArguments
73 a sequence of PropertyValues's
75 NamedValueCollection( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rArguments );
77 /** constructs a collection
78 @param _rArguments
79 a sequence of NamedValue's
81 NamedValueCollection( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& _rArguments );
83 ~NamedValueCollection();
85 inline void assign( const ::com::sun::star::uno::Any& i_rWrappedElements )
87 impl_assign( i_rWrappedElements );
90 inline void assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& _rArguments )
92 impl_assign( _rArguments );
95 inline void assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rArguments )
97 impl_assign( _rArguments );
100 inline void assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& _rArguments )
102 impl_assign( _rArguments );
105 inline void clear()
107 impl_assign( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >() );
110 /** determines whether or not named values can be extracted from the given value
112 @return
113 <TRUE/> if and only if the given <code>Any</code> contains a <code>NamedValue</code>, a
114 <code>PropertyValue</code>, or a sequence thereof.
116 static bool canExtractFrom( ::com::sun::star::uno::Any const & i_value );
118 /// returns the number of elements in the collection
119 size_t size() const;
121 /// determines whether the collection is empty
122 bool empty() const;
124 /** returns the names of all elements in the collection
126 ::std::vector< OUString >
127 getNames() const;
129 /** merges the content of another collection into |this|
130 @param _rAdditionalValues
131 the collection whose values are to be merged
132 @param _bOverwriteExisting
133 defines whether or not elements which are already present in |this|
134 should be overwritten (<TRUE/>) or preserved (<FALSE/>).
135 @return |*this|
137 NamedValueCollection&
138 merge(
139 const NamedValueCollection& _rAdditionalValues,
140 bool _bOverwriteExisting
143 /** retrieves a value with a given name from the collection, if it is present
145 @param _pAsciiValueName
146 the ASCII name of the value to retrieve
148 @param _out_rValue
149 is the output parameter taking the desired value upon successful return. If
150 a value with the given name is not present in the collection, or if a wrong-typed
151 value is present, then this parameter will not be touched.
153 @return
154 <TRUE/> if there is a value with the given name, which could successfully
155 be extraced. In this case, <arg>_out_rValue</arg> will contain the requested
156 value.<br/>
157 <FALSE/>, if there is no value with the given name.
158 @throws IllegalArgumentException
159 in case there is a value with the given name, but it cannot legally assigned to
160 _out_rValue.
162 template < typename VALUE_TYPE >
163 bool get_ensureType( const sal_Char* _pAsciiValueName, VALUE_TYPE& _out_rValue ) const
165 return get_ensureType( OUString::createFromAscii( _pAsciiValueName ), &_out_rValue, ::cppu::UnoType< VALUE_TYPE >::get() );
168 template < typename VALUE_TYPE >
169 bool get_ensureType( const OUString& _rValueName, VALUE_TYPE& _out_rValue ) const
171 return get_ensureType( _rValueName, &_out_rValue, ::cppu::UnoType< VALUE_TYPE >::get() );
174 /** retrieves a value with a given name, or defaults it to a given value, if its not present
175 in the colllection
177 template < typename VALUE_TYPE >
178 VALUE_TYPE getOrDefault( const sal_Char* _pAsciiValueName, const VALUE_TYPE& _rDefault ) const
180 return getOrDefault( OUString::createFromAscii( _pAsciiValueName ), _rDefault );
183 template < typename VALUE_TYPE >
184 VALUE_TYPE getOrDefault( const OUString& _rValueName, const VALUE_TYPE& _rDefault ) const
186 VALUE_TYPE retVal( _rDefault );
187 get_ensureType( _rValueName, retVal );
188 return retVal;
191 /** retrieves a (untyped) value with a given name
193 If the collection does not contain a value with the given name, an empty
194 Any is returned.
196 const ::com::sun::star::uno::Any& get( const sal_Char* _pAsciiValueName ) const
198 return get( OUString::createFromAscii( _pAsciiValueName ) );
201 /** retrieves a (untyped) value with a given name
203 If the collection does not contain a value with the given name, an empty
204 Any is returned.
206 const ::com::sun::star::uno::Any& get( const OUString& _rValueName ) const
208 return impl_get( _rValueName );
211 /// determines whether a value with a given name is present in the collection
212 inline bool has( const sal_Char* _pAsciiValueName ) const
214 return impl_has( OUString::createFromAscii( _pAsciiValueName ) );
217 /// determines whether a value with a given name is present in the collection
218 inline bool has( const OUString& _rValueName ) const
220 return impl_has( _rValueName );
223 /** puts a value into the collection
225 @return <TRUE/> if and only if a value was already present previously, in
226 which case it has been overwritten.
228 template < typename VALUE_TYPE >
229 inline bool put( const sal_Char* _pAsciiValueName, const VALUE_TYPE& _rValue )
231 return impl_put( OUString::createFromAscii( _pAsciiValueName ), ::com::sun::star::uno::makeAny( _rValue ) );
234 /** puts a value into the collection
236 @return <TRUE/> if and only if a value was already present previously, in
237 which case it has been overwritten.
239 template < typename VALUE_TYPE >
240 inline bool put( const OUString& _rValueName, const VALUE_TYPE& _rValue )
242 return impl_put( _rValueName, ::com::sun::star::uno::makeAny( _rValue ) );
245 inline bool put( const sal_Char* _pAsciiValueName, const ::com::sun::star::uno::Any& _rValue )
247 return impl_put( OUString::createFromAscii( _pAsciiValueName ), _rValue );
250 inline bool put( const OUString& _rValueName, const ::com::sun::star::uno::Any& _rValue )
252 return impl_put( _rValueName, _rValue );
255 /** removes the value with the given name from the collection
257 @return <TRUE/> if and only if a value with the given name existed in the collection.
259 inline bool remove( const sal_Char* _pAsciiValueName )
261 return impl_remove( OUString::createFromAscii( _pAsciiValueName ) );
264 /** removes the value with the given name from the collection
266 @return <TRUE/> if and only if a value with the given name existed in the collection.
268 inline bool remove( const OUString& _rValueName )
270 return impl_remove( _rValueName );
273 /** transforms the collection to a sequence of PropertyValues
275 @return
276 the number of elements in the sequence
278 sal_Int32 operator >>= ( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _out_rValues ) const;
280 /** transforms the collection to a sequence of NamedValues
282 @return
283 the number of elements in the sequence
285 sal_Int32 operator >>= ( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& _out_rValues ) const;
287 /** transforms the collection into a sequence of PropertyValues
289 inline ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >
290 getPropertyValues() const
292 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aValues;
293 *this >>= aValues;
294 return aValues;
297 /** returns a Sequence< Any >, containing PropertyValues
299 inline ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >
300 getWrappedPropertyValues() const
302 return impl_wrap< ::com::sun::star::beans::PropertyValue >();
305 /** returns a Sequence< Any >, containing NamedValues
307 inline ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >
308 getWrappedNamedValues() const
310 return impl_wrap< ::com::sun::star::beans::NamedValue >();
313 /** transforms the collection into a sequence of NamedValues
315 inline ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >
316 getNamedValues() const
318 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > aValues;
319 *this >>= aValues;
320 return aValues;
323 private:
324 void impl_assign( const ::com::sun::star::uno::Any& i_rWrappedElements );
325 void impl_assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& _rArguments );
326 void impl_assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rArguments );
327 void impl_assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& _rArguments );
329 bool get_ensureType(
330 const OUString& _rValueName,
331 void* _pValueLocation,
332 const ::com::sun::star::uno::Type& _rExpectedValueType
333 ) const;
335 const ::com::sun::star::uno::Any&
336 impl_get( const OUString& _rValueName ) const;
338 bool impl_has( const OUString& _rValueName ) const;
340 bool impl_put( const OUString& _rValueName, const ::com::sun::star::uno::Any& _rValue );
342 bool impl_remove( const OUString& _rValueName );
344 template< class VALUE_TYPE >
345 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > impl_wrap() const
347 ::com::sun::star::uno::Sequence< VALUE_TYPE > aValues;
348 *this >>= aValues;
349 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > aWrappedValues( aValues.getLength() );
351 ::com::sun::star::uno::Any* pO = aWrappedValues.getArray();
352 const VALUE_TYPE* pV = aValues.getConstArray();
353 const sal_Int32 nLen = aValues.getLength();
354 for( sal_Int32 i = 0; i < nLen; ++i )
355 *(pO++) = ::com::sun::star::uno::makeAny<VALUE_TYPE>( *(pV++) );
357 return aWrappedValues;
361 //........................................................................
362 } // namespace comphelper
363 //........................................................................
365 #endif // INCLUDED_COMPHELPER_NAMEDVALUECOLLECTION_HXX
367 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */