Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / include / comphelper / namedvaluecollection.hxx
blobb7e64810d20b9ffdf608f6d42e75663bd18538fc
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>
35 namespace comphelper
39 // = NamedValueCollection
41 struct NamedValueCollection_Impl;
42 /** a collection of named values, packed in various formats.
44 class COMPHELPER_DLLPUBLIC NamedValueCollection
46 private:
47 ::std::unique_ptr< NamedValueCollection_Impl > m_pImpl;
49 public:
50 NamedValueCollection();
52 NamedValueCollection( const NamedValueCollection& _rCopySource );
54 NamedValueCollection& operator=( const NamedValueCollection& i_rCopySource );
56 /** constructs a collection
57 @param _rElements
58 the wrapped elements of the collection. The <code>Any</code> might contain a sequence of
59 property values, a sequence of named values, or directly a property value or named value.
60 All other cases are worth an assertion in non-product builds.
62 NamedValueCollection( const css::uno::Any& _rElements );
64 /** constructs a collection
65 @param _rArguments
66 a sequence of Any's containing either PropertyValue's or NamedValue's.
68 NamedValueCollection( const css::uno::Sequence< css::uno::Any >& _rArguments );
70 /** constructs a collection
71 @param _rArguments
72 a sequence of PropertyValues's
74 NamedValueCollection( const css::uno::Sequence< css::beans::PropertyValue >& _rArguments );
76 /** constructs a collection
77 @param _rArguments
78 a sequence of NamedValue's
80 NamedValueCollection( const css::uno::Sequence< css::beans::NamedValue >& _rArguments );
82 ~NamedValueCollection();
84 inline void assign( const css::uno::Sequence< css::uno::Any >& _rArguments )
86 impl_assign( _rArguments );
89 inline void clear()
91 impl_assign( css::uno::Sequence< css::beans::NamedValue >() );
94 /** determines whether or not named values can be extracted from the given value
96 @return
97 <TRUE/> if and only if the given <code>Any</code> contains a <code>NamedValue</code>, a
98 <code>PropertyValue</code>, or a sequence thereof.
100 static bool canExtractFrom( css::uno::Any const & i_value );
102 /// returns the number of elements in the collection
103 size_t size() const;
105 /// determines whether the collection is empty
106 bool empty() const;
108 /** returns the names of all elements in the collection
110 ::std::vector< OUString >
111 getNames() const;
113 /** merges the content of another collection into |this|
114 @param _rAdditionalValues
115 the collection whose values are to be merged
116 @param _bOverwriteExisting
117 defines whether or not elements which are already present in |this|
118 should be overwritten (<TRUE/>) or preserved (<FALSE/>).
119 @return |*this|
121 NamedValueCollection&
122 merge(
123 const NamedValueCollection& _rAdditionalValues,
124 bool _bOverwriteExisting
127 /** retrieves a value with a given name from the collection, if it is present
129 @param _pAsciiValueName
130 the ASCII name of the value to retrieve
132 @param _out_rValue
133 is the output parameter taking the desired value upon successful return. If
134 a value with the given name is not present in the collection, or if a wrong-typed
135 value is present, then this parameter will not be touched.
137 @return
138 <TRUE/> if there is a value with the given name, which could successfully
139 be extraced. In this case, <arg>_out_rValue</arg> will contain the requested
140 value.<br/>
141 <FALSE/>, if there is no value with the given name.
142 @throws IllegalArgumentException
143 in case there is a value with the given name, but it cannot legally assigned to
144 _out_rValue.
146 template < typename VALUE_TYPE >
147 bool get_ensureType( const sal_Char* _pAsciiValueName, VALUE_TYPE& _out_rValue ) const
149 return get_ensureType( OUString::createFromAscii( _pAsciiValueName ), &_out_rValue, ::cppu::UnoType< VALUE_TYPE >::get() );
152 template < typename VALUE_TYPE >
153 bool get_ensureType( const OUString& _rValueName, VALUE_TYPE& _out_rValue ) const
155 return get_ensureType( _rValueName, &_out_rValue, ::cppu::UnoType< VALUE_TYPE >::get() );
158 /** retrieves a value with a given name, or defaults it to a given value, if its not present
159 in the collection
161 template < typename VALUE_TYPE >
162 VALUE_TYPE getOrDefault( const sal_Char* _pAsciiValueName, const VALUE_TYPE& _rDefault ) const
164 return getOrDefault( OUString::createFromAscii( _pAsciiValueName ), _rDefault );
167 template < typename VALUE_TYPE >
168 VALUE_TYPE getOrDefault( const OUString& _rValueName, const VALUE_TYPE& _rDefault ) const
170 VALUE_TYPE retVal( _rDefault );
171 get_ensureType( _rValueName, retVal );
172 return retVal;
175 /** retrieves a (untyped) value with a given name
177 If the collection does not contain a value with the given name, an empty
178 Any is returned.
180 const css::uno::Any& get( const sal_Char* _pAsciiValueName ) const
182 return get( OUString::createFromAscii( _pAsciiValueName ) );
185 /** retrieves a (untyped) value with a given name
187 If the collection does not contain a value with the given name, an empty
188 Any is returned.
190 const css::uno::Any& get( const OUString& _rValueName ) const
192 return impl_get( _rValueName );
195 /// determines whether a value with a given name is present in the collection
196 inline bool has( const sal_Char* _pAsciiValueName ) const
198 return impl_has( OUString::createFromAscii( _pAsciiValueName ) );
201 /// determines whether a value with a given name is present in the collection
202 inline bool has( const OUString& _rValueName ) const
204 return impl_has( _rValueName );
207 /** puts a value into the collection
209 @return <TRUE/> if and only if a value was already present previously, in
210 which case it has been overwritten.
212 template < typename VALUE_TYPE >
213 inline bool put( const sal_Char* _pAsciiValueName, const VALUE_TYPE& _rValue )
215 return impl_put( OUString::createFromAscii( _pAsciiValueName ), css::uno::makeAny( _rValue ) );
218 /** puts a value into the collection
220 @return <TRUE/> if and only if a value was already present previously, in
221 which case it has been overwritten.
223 template < typename VALUE_TYPE >
224 inline bool put( const OUString& _rValueName, const VALUE_TYPE& _rValue )
226 return impl_put( _rValueName, css::uno::makeAny( _rValue ) );
229 inline bool put( const sal_Char* _pAsciiValueName, const css::uno::Any& _rValue )
231 return impl_put( OUString::createFromAscii( _pAsciiValueName ), _rValue );
234 inline bool put( const OUString& _rValueName, const css::uno::Any& _rValue )
236 return impl_put( _rValueName, _rValue );
239 /** removes the value with the given name from the collection
241 @return <TRUE/> if and only if a value with the given name existed in the collection.
243 inline bool remove( const sal_Char* _pAsciiValueName )
245 return impl_remove( OUString::createFromAscii( _pAsciiValueName ) );
248 /** removes the value with the given name from the collection
250 @return <TRUE/> if and only if a value with the given name existed in the collection.
252 inline bool remove( const OUString& _rValueName )
254 return impl_remove( _rValueName );
257 /** transforms the collection to a sequence of PropertyValues
259 @return
260 the number of elements in the sequence
262 sal_Int32 operator >>= ( css::uno::Sequence< css::beans::PropertyValue >& _out_rValues ) const;
264 /** transforms the collection to a sequence of NamedValues
266 @return
267 the number of elements in the sequence
269 sal_Int32 operator >>= ( css::uno::Sequence< css::beans::NamedValue >& _out_rValues ) const;
271 /** transforms the collection into a sequence of PropertyValues
273 inline css::uno::Sequence< css::beans::PropertyValue >
274 getPropertyValues() const
276 css::uno::Sequence< css::beans::PropertyValue > aValues;
277 *this >>= aValues;
278 return aValues;
281 /** returns a Sequence< Any >, containing PropertyValues
283 inline css::uno::Sequence< css::uno::Any >
284 getWrappedPropertyValues() const
286 return impl_wrap< css::beans::PropertyValue >();
289 /** returns a Sequence< Any >, containing NamedValues
291 inline css::uno::Sequence< css::uno::Any >
292 getWrappedNamedValues() const
294 return impl_wrap< css::beans::NamedValue >();
297 /** transforms the collection into a sequence of NamedValues
299 inline css::uno::Sequence< css::beans::NamedValue >
300 getNamedValues() const
302 css::uno::Sequence< css::beans::NamedValue > aValues;
303 *this >>= aValues;
304 return aValues;
307 private:
308 void impl_assign( const css::uno::Any& i_rWrappedElements );
309 void impl_assign( const css::uno::Sequence< css::uno::Any >& _rArguments );
310 void impl_assign( const css::uno::Sequence< css::beans::PropertyValue >& _rArguments );
311 void impl_assign( const css::uno::Sequence< css::beans::NamedValue >& _rArguments );
313 bool get_ensureType(
314 const OUString& _rValueName,
315 void* _pValueLocation,
316 const css::uno::Type& _rExpectedValueType
317 ) const;
319 const css::uno::Any&
320 impl_get( const OUString& _rValueName ) const;
322 bool impl_has( const OUString& _rValueName ) const;
324 bool impl_put( const OUString& _rValueName, const css::uno::Any& _rValue );
326 bool impl_remove( const OUString& _rValueName );
328 template< class VALUE_TYPE >
329 css::uno::Sequence< css::uno::Any > impl_wrap() const
331 css::uno::Sequence< VALUE_TYPE > aValues;
332 *this >>= aValues;
333 css::uno::Sequence< css::uno::Any > aWrappedValues( aValues.getLength() );
335 css::uno::Any* pO = aWrappedValues.getArray();
336 const VALUE_TYPE* pV = aValues.getConstArray();
337 const sal_Int32 nLen = aValues.getLength();
338 for( sal_Int32 i = 0; i < nLen; ++i )
339 *(pO++) = css::uno::makeAny<VALUE_TYPE>( *(pV++) );
341 return aWrappedValues;
346 } // namespace comphelper
349 #endif // INCLUDED_COMPHELPER_NAMEDVALUECOLLECTION_HXX
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */