Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / comphelper / namedvaluecollection.hxx
blobb92646e40a114613a2c9731fa2a98bd30fb1372a
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 <vector>
31 #include <unordered_map>
33 namespace comphelper
37 // = NamedValueCollection
39 /** a collection of named values, packed in various formats.
41 class COMPHELPER_DLLPUBLIC NamedValueCollection
43 std::unordered_map< OUString, css::uno::Any > maValues;
44 public:
45 NamedValueCollection() = default;
47 NamedValueCollection( const NamedValueCollection& _rCopySource ) = default;
48 NamedValueCollection(NamedValueCollection&& _rCopySource) noexcept = default;
50 NamedValueCollection& operator=( const NamedValueCollection& i_rCopySource ) = default;
51 NamedValueCollection& operator=(NamedValueCollection&& i_rCopySource) noexcept = default;
53 /** constructs a collection
54 @param _rElements
55 the wrapped elements of the collection. The @c Any might contain a sequence of
56 property values, a sequence of named values, or directly a property value or named value.
57 All other cases are worth an assertion in non-product builds.
59 NamedValueCollection( const css::uno::Any& _rElements );
61 /** constructs a collection
62 @param _rArguments
63 a sequence of Any's containing either PropertyValue's or NamedValue's.
65 NamedValueCollection( const css::uno::Sequence< css::uno::Any >& _rArguments );
67 /** constructs a collection
68 @param _rArguments
69 a sequence of PropertyValues's
71 NamedValueCollection( const css::uno::Sequence< css::beans::PropertyValue >& _rArguments );
73 /** constructs a collection
74 @param _rArguments
75 a sequence of NamedValue's
77 NamedValueCollection( const css::uno::Sequence< css::beans::NamedValue >& _rArguments );
79 void assign( const css::uno::Sequence< css::uno::Any >& _rArguments )
81 impl_assign( _rArguments );
84 void clear()
86 impl_assign( css::uno::Sequence< css::beans::NamedValue >() );
89 /** determines whether or not named values can be extracted from the given value
91 @return
92 true if and only if the given @c Any contains a @c NamedValue, a
93 @c PropertyValue, or a sequence thereof.
95 static bool canExtractFrom( css::uno::Any const & i_value );
97 /// returns the number of elements in the collection
98 size_t size() const;
100 /// determines whether the collection is empty
101 bool empty() const;
103 /** returns the names of all elements in the collection
105 ::std::vector< OUString >
106 getNames() const;
108 /** merges the content of another collection into @c this
109 @param _rAdditionalValues
110 the collection whose values are to be merged
111 @param _bOverwriteExisting
112 defines whether or not elements which are already present in @c this
113 should be overwritten (true) or preserved (false).
114 @return @c *this
116 NamedValueCollection&
117 merge(
118 const NamedValueCollection& _rAdditionalValues,
119 bool _bOverwriteExisting
122 /** retrieves a value with a given name from the collection, if it is present
124 @param _pAsciiValueName
125 the ASCII name of the value to retrieve
127 @param _out_rValue
128 is the output parameter taking the desired value upon successful return. If
129 a value with the given name is not present in the collection, or if a wrong-typed
130 value is present, then this parameter will not be touched.
132 @retval
133 true if there is a value with the given name, which could successfully
134 be extracted. In this case, @c _out_rValue will contain the requested
135 value.
136 @retval
137 false, if there is no value with the given name.
139 @throws IllegalArgumentException
140 in case there is a value with the given name, but it cannot legally assigned to
141 _out_rValue.
143 template < typename VALUE_TYPE >
144 bool get_ensureType( const OUString& _rValueName, VALUE_TYPE& _out_rValue ) const
146 return get_ensureType( _rValueName, &_out_rValue, ::cppu::UnoType< VALUE_TYPE >::get() );
149 /** retrieves a value with a given name, or defaults it to a given value, if it's not present
150 in the collection
152 template < typename VALUE_TYPE >
153 VALUE_TYPE getOrDefault( const OUString& _rValueName, const VALUE_TYPE& _rDefault ) const
155 VALUE_TYPE retVal( _rDefault );
156 get_ensureType( _rValueName, retVal );
157 return retVal;
160 /** Retrieves a value with a given name, or defaults it to a given value, if it's not present
161 in the collection.
162 For when you only need a single value from a Sequence<PropertyValue>.
164 template < typename VALUE_TYPE >
165 static VALUE_TYPE getOrDefault( const css::uno::Sequence<css::beans::PropertyValue> & rPropSeq,
166 std::u16string_view _rValueName, const VALUE_TYPE& _rDefault )
168 VALUE_TYPE retVal( _rDefault );
169 get_ensureType( rPropSeq, _rValueName, &retVal, ::cppu::UnoType< VALUE_TYPE >::get() );
170 return retVal;
173 /** retrieves a (untyped) value with a given name
175 If the collection does not contain a value with the given name, an empty
176 Any is returned.
178 const css::uno::Any& get( const OUString& _rValueName ) const
180 return impl_get( _rValueName );
183 /** retrieves a (untyped) value with a given name. For when you only need a single value from a Sequence<PropertyValue>.
185 If the collection does not contain a value with the given name, an empty
186 Any is returned.
188 static const css::uno::Any& get( const css::uno::Sequence<css::beans::PropertyValue>& rPropSeq, std::u16string_view _rValueName );
190 /// determines whether a value with a given name is present in the collection
191 bool has( const OUString& _rValueName ) const
193 return impl_has( _rValueName );
196 /** puts a value into the collection
198 @return true if and only if a value was already present previously, in
199 which case it has been overwritten.
201 template < typename VALUE_TYPE >
202 bool put( const OUString& _rValueName, const VALUE_TYPE& _rValue )
204 return impl_put( _rValueName, css::uno::Any( _rValue ) );
207 bool put( const OUString& _rValueName, const css::uno::Any& _rValue )
209 return impl_put( _rValueName, _rValue );
212 /** removes the value with the given name from the collection
214 @return true if and only if a value with the given name existed in the collection.
216 bool remove( const OUString& _rValueName )
218 return impl_remove( _rValueName );
221 /** transforms the collection to a sequence of PropertyValues
223 @return
224 the number of elements in the sequence
226 sal_Int32 operator >>= ( css::uno::Sequence< css::beans::PropertyValue >& _out_rValues ) const;
228 /** transforms the collection to a sequence of NamedValues
230 @return
231 the number of elements in the sequence
233 sal_Int32 operator >>= ( css::uno::Sequence< css::beans::NamedValue >& _out_rValues ) const;
235 /** transforms the collection into a sequence of PropertyValues
237 css::uno::Sequence< css::beans::PropertyValue >
238 getPropertyValues() const
240 css::uno::Sequence< css::beans::PropertyValue > aValues;
241 *this >>= aValues;
242 return aValues;
245 /** returns a Sequence< Any >, containing PropertyValues
247 css::uno::Sequence< css::uno::Any >
248 getWrappedPropertyValues() const
250 return impl_wrap< css::beans::PropertyValue >();
253 /** returns a Sequence< Any >, containing NamedValues
255 css::uno::Sequence< css::uno::Any >
256 getWrappedNamedValues() const
258 return impl_wrap< css::beans::NamedValue >();
261 /** transforms the collection into a sequence of NamedValues
263 css::uno::Sequence< css::beans::NamedValue >
264 getNamedValues() const
266 css::uno::Sequence< css::beans::NamedValue > aValues;
267 *this >>= aValues;
268 return aValues;
271 private:
272 void impl_assign( const css::uno::Any& i_rWrappedElements );
273 void impl_assign( const css::uno::Sequence< css::uno::Any >& _rArguments );
274 void impl_assign( const css::uno::Sequence< css::beans::PropertyValue >& _rArguments );
275 void impl_assign( const css::uno::Sequence< css::beans::NamedValue >& _rArguments );
277 bool get_ensureType(
278 const OUString& _rValueName,
279 void* _pValueLocation,
280 const css::uno::Type& _rExpectedValueType
281 ) const;
283 static bool get_ensureType(
284 const css::uno::Sequence<css::beans::PropertyValue> & rPropSeq,
285 std::u16string_view _rValueName,
286 void* _pValueLocation,
287 const css::uno::Type& _rExpectedValueType
290 const css::uno::Any&
291 impl_get( const OUString& _rValueName ) const;
293 bool impl_has( const OUString& _rValueName ) const;
295 bool impl_put( const OUString& _rValueName, const css::uno::Any& _rValue );
297 bool impl_remove( const OUString& _rValueName );
299 template< class VALUE_TYPE >
300 css::uno::Sequence< css::uno::Any > impl_wrap() const
302 css::uno::Sequence< VALUE_TYPE > aValues;
303 *this >>= aValues;
304 css::uno::Sequence< css::uno::Any > aWrappedValues( aValues.getLength() );
306 css::uno::Any* pO = aWrappedValues.getArray();
307 const VALUE_TYPE* pV = aValues.getConstArray();
308 const sal_Int32 nLen = aValues.getLength();
309 for( sal_Int32 i = 0; i < nLen; ++i )
310 *(pO++) = css::uno::Any( *(pV++) );
312 return aWrappedValues;
317 } // namespace comphelper
320 #endif // INCLUDED_COMPHELPER_NAMEDVALUECOLLECTION_HXX
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */