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 
165  const css::uno::Any& get( const OUString& _rValueName ) const
166  {
167  return impl_get( _rValueName );
168  }
169 
171  bool has( const OUString& _rValueName ) const
172  {
173  return impl_has( _rValueName );
174  }
175 
181  template < typename VALUE_TYPE >
182  bool put( const OUString& _rValueName, const VALUE_TYPE& _rValue )
183  {
184  return impl_put( _rValueName, css::uno::makeAny( _rValue ) );
185  }
186 
187  bool put( const OUString& _rValueName, const css::uno::Any& _rValue )
188  {
189  return impl_put( _rValueName, _rValue );
190  }
191 
196  bool remove( const OUString& _rValueName )
197  {
198  return impl_remove( _rValueName );
199  }
200 
206  sal_Int32 operator >>= ( css::uno::Sequence< css::beans::PropertyValue >& _out_rValues ) const;
207 
213  sal_Int32 operator >>= ( css::uno::Sequence< css::beans::NamedValue >& _out_rValues ) const;
214 
217  css::uno::Sequence< css::beans::PropertyValue >
219  {
220  css::uno::Sequence< css::beans::PropertyValue > aValues;
221  *this >>= aValues;
222  return aValues;
223  }
224 
227  css::uno::Sequence< css::uno::Any >
229  {
230  return impl_wrap< css::beans::PropertyValue >();
231  }
232 
235  css::uno::Sequence< css::uno::Any >
237  {
238  return impl_wrap< css::beans::NamedValue >();
239  }
240 
243  css::uno::Sequence< css::beans::NamedValue >
245  {
246  css::uno::Sequence< css::beans::NamedValue > aValues;
247  *this >>= aValues;
248  return aValues;
249  }
250 
251  private:
252  void impl_assign( const css::uno::Any& i_rWrappedElements );
253  void impl_assign( const css::uno::Sequence< css::uno::Any >& _rArguments );
254  void impl_assign( const css::uno::Sequence< css::beans::PropertyValue >& _rArguments );
255  void impl_assign( const css::uno::Sequence< css::beans::NamedValue >& _rArguments );
256 
257  bool get_ensureType(
258  const OUString& _rValueName,
259  void* _pValueLocation,
260  const css::uno::Type& _rExpectedValueType
261  ) const;
262 
263  const css::uno::Any&
264  impl_get( const OUString& _rValueName ) const;
265 
266  bool impl_has( const OUString& _rValueName ) const;
267 
268  bool impl_put( const OUString& _rValueName, const css::uno::Any& _rValue );
269 
270  bool impl_remove( const OUString& _rValueName );
271 
272  template< class VALUE_TYPE >
273  css::uno::Sequence< css::uno::Any > impl_wrap() const
274  {
275  css::uno::Sequence< VALUE_TYPE > aValues;
276  *this >>= aValues;
277  css::uno::Sequence< css::uno::Any > aWrappedValues( aValues.getLength() );
278 
279  css::uno::Any* pO = aWrappedValues.getArray();
280  const VALUE_TYPE* pV = aValues.getConstArray();
281  const sal_Int32 nLen = aValues.getLength();
282  for( sal_Int32 i = 0; i < nLen; ++i )
283  *(pO++) = css::uno::makeAny<VALUE_TYPE>( *(pV++) );
284 
285  return aWrappedValues;
286  }
287  };
288 
289 
290 } // namespace comphelper
291 
292 
293 #endif // INCLUDED_COMPHELPER_NAMEDVALUECOLLECTION_HXX
294 
295 /* 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