LibreOffice Module comphelper (master)  1
namedvaluecollection.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #ifndef INCLUDED_COMPHELPER_NAMEDVALUECOLLECTION_HXX
21 #define INCLUDED_COMPHELPER_NAMEDVALUECOLLECTION_HXX
22 
24 
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>
29 
30 #include <vector>
31 #include <unordered_map>
32 
33 namespace comphelper
34 {
35 
36 
37  // = NamedValueCollection
38 
42  {
43  std::unordered_map< OUString, css::uno::Any > maValues;
44  public:
45  NamedValueCollection() = default;
46 
47  NamedValueCollection( const NamedValueCollection& _rCopySource ) = default;
48  NamedValueCollection(NamedValueCollection&& _rCopySource) noexcept = default;
49 
50  NamedValueCollection& operator=( const NamedValueCollection& i_rCopySource ) = default;
51  NamedValueCollection& operator=(NamedValueCollection&& i_rCopySource) noexcept = default;
52 
59  NamedValueCollection( const css::uno::Any& _rElements );
60 
65  NamedValueCollection( const css::uno::Sequence< css::uno::Any >& _rArguments );
66 
71  NamedValueCollection( const css::uno::Sequence< css::beans::PropertyValue >& _rArguments );
72 
77  NamedValueCollection( const css::uno::Sequence< css::beans::NamedValue >& _rArguments );
78 
79  void assign( const css::uno::Sequence< css::uno::Any >& _rArguments )
80  {
81  impl_assign( _rArguments );
82  }
83 
84  void clear()
85  {
86  impl_assign( css::uno::Sequence< css::beans::NamedValue >() );
87  }
88 
95  static bool canExtractFrom( css::uno::Any const & i_value );
96 
98  size_t size() const;
99 
101  bool empty() const;
102 
105  ::std::vector< OUString >
106  getNames() const;
107 
117  merge(
118  const NamedValueCollection& _rAdditionalValues,
119  bool _bOverwriteExisting
120  );
121 
143  template < typename VALUE_TYPE >
144  bool get_ensureType( const OUString& _rValueName, VALUE_TYPE& _out_rValue ) const
145  {
146  return get_ensureType( _rValueName, &_out_rValue, ::cppu::UnoType< VALUE_TYPE >::get() );
147  }
148 
152  template < typename VALUE_TYPE >
153  VALUE_TYPE getOrDefault( const OUString& _rValueName, const VALUE_TYPE& _rDefault ) const
154  {
155  VALUE_TYPE retVal( _rDefault );
156  get_ensureType( _rValueName, retVal );
157  return retVal;
158  }
159 
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 )
167  {
168  VALUE_TYPE retVal( _rDefault );
169  get_ensureType( rPropSeq, _rValueName, &retVal, ::cppu::UnoType< VALUE_TYPE >::get() );
170  return retVal;
171  }
172 
178  const css::uno::Any& get( const OUString& _rValueName ) const
179  {
180  return impl_get( _rValueName );
181  }
182 
188  static const css::uno::Any& get( const css::uno::Sequence<css::beans::PropertyValue>& rPropSeq, std::u16string_view _rValueName );
189 
191  bool has( const OUString& _rValueName ) const
192  {
193  return impl_has( _rValueName );
194  }
195 
201  template < typename VALUE_TYPE >
202  bool put( const OUString& _rValueName, const VALUE_TYPE& _rValue )
203  {
204  return impl_put( _rValueName, css::uno::Any( _rValue ) );
205  }
206 
207  bool put( const OUString& _rValueName, const css::uno::Any& _rValue )
208  {
209  return impl_put( _rValueName, _rValue );
210  }
211 
216  bool remove( const OUString& _rValueName )
217  {
218  return impl_remove( _rValueName );
219  }
220 
226  sal_Int32 operator >>= ( css::uno::Sequence< css::beans::PropertyValue >& _out_rValues ) const;
227 
233  sal_Int32 operator >>= ( css::uno::Sequence< css::beans::NamedValue >& _out_rValues ) const;
234 
237  css::uno::Sequence< css::beans::PropertyValue >
239  {
240  css::uno::Sequence< css::beans::PropertyValue > aValues;
241  *this >>= aValues;
242  return aValues;
243  }
244 
247  css::uno::Sequence< css::uno::Any >
249  {
250  return impl_wrap< css::beans::PropertyValue >();
251  }
252 
255  css::uno::Sequence< css::uno::Any >
257  {
258  return impl_wrap< css::beans::NamedValue >();
259  }
260 
263  css::uno::Sequence< css::beans::NamedValue >
265  {
266  css::uno::Sequence< css::beans::NamedValue > aValues;
267  *this >>= aValues;
268  return aValues;
269  }
270 
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 );
276 
277  bool get_ensureType(
278  const OUString& _rValueName,
279  void* _pValueLocation,
280  const css::uno::Type& _rExpectedValueType
281  ) const;
282 
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
288  );
289 
290  const css::uno::Any&
291  impl_get( const OUString& _rValueName ) const;
292 
293  bool impl_has( const OUString& _rValueName ) const;
294 
295  bool impl_put( const OUString& _rValueName, const css::uno::Any& _rValue );
296 
297  bool impl_remove( const OUString& _rValueName );
298 
299  template< class VALUE_TYPE >
300  css::uno::Sequence< css::uno::Any > impl_wrap() const
301  {
302  css::uno::Sequence< VALUE_TYPE > aValues;
303  *this >>= aValues;
304  css::uno::Sequence< css::uno::Any > aWrappedValues( aValues.getLength() );
305 
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++) );
311 
312  return aWrappedValues;
313  }
314  };
315 
316 
317 } // namespace comphelper
318 
319 
320 #endif // INCLUDED_COMPHELPER_NAMEDVALUECOLLECTION_HXX
321 
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< css::beans::NamedValue > getNamedValues() const
transforms the collection into a sequence of NamedValues
css::uno::Sequence< css::uno::Any > getWrappedPropertyValues() const
returns a Sequence< Any >, containing PropertyValues
bool get_ensureType(const OUString &_rValueName, VALUE_TYPE &_out_rValue) const
retrieves a value with a given name from the collection, if it is present
a collection of named values, packed in various formats.
css::uno::Sequence< css::uno::Any > impl_wrap() const
VALUE_TYPE getOrDefault(const OUString &_rValueName, const VALUE_TYPE &_rDefault) const
retrieves a value with a given name, or defaults it to a given value, if it's not present in the coll...
std::unordered_map< OUString, css::uno::Any > maValues
bool put(const OUString &_rValueName, const css::uno::Any &_rValue)
int i
css::uno::Sequence< css::uno::Any > getWrappedNamedValues() const
returns a Sequence< Any >, containing NamedValues
css::uno::Sequence< css::beans::PropertyValue > getPropertyValues() const
transforms the collection into a sequence of PropertyValues
size
bool put(const OUString &_rValueName, const VALUE_TYPE &_rValue)
puts a value into the collection
bool has(const OUString &_rValueName) const
determines whether a value with a given name is present in the collection
#define COMPHELPER_DLLPUBLIC
static VALUE_TYPE getOrDefault(const css::uno::Sequence< css::beans::PropertyValue > &rPropSeq, std::u16string_view _rValueName, const VALUE_TYPE &_rDefault)
Retrieves a value with a given name, or defaults it to a given value, if it's not present in the coll...