LibreOffice Module comphelper (master)  1
namedvaluecollection.cxx
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 
21 
22 #include <com/sun/star/beans/NamedValue.hpp>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <com/sun/star/beans/PropertyState.hpp>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 
27 #include <rtl/instance.hxx>
28 #include <sal/log.hxx>
29 
30 #include <algorithm>
31 #include <unordered_map>
32 
33 namespace comphelper
34 {
35 
36 
37  using ::com::sun::star::uno::Any;
38  using ::com::sun::star::uno::Sequence;
39  using ::com::sun::star::beans::PropertyValue;
40  using ::com::sun::star::beans::NamedValue;
41  using ::com::sun::star::uno::Type;
42  using ::com::sun::star::uno::cpp_acquire;
43  using ::com::sun::star::uno::cpp_release;
44  using ::com::sun::star::uno::cpp_queryInterface;
45  using ::com::sun::star::lang::IllegalArgumentException;
46  using ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
47 
48  NamedValueCollection::NamedValueCollection( const Any& _rElements )
49  {
50  impl_assign( _rElements );
51  }
52 
53 
54  NamedValueCollection::NamedValueCollection( const Sequence< Any >& _rArguments )
55  {
56  impl_assign( _rArguments );
57  }
58 
59 
60  NamedValueCollection::NamedValueCollection( const Sequence< PropertyValue >& _rArguments )
61  {
62  impl_assign( _rArguments );
63  }
64 
65 
66  NamedValueCollection::NamedValueCollection( const Sequence< NamedValue >& _rArguments )
67  {
68  impl_assign( _rArguments );
69  }
70 
71 
72  bool NamedValueCollection::canExtractFrom( css::uno::Any const & i_value )
73  {
74  Type const & aValueType = i_value.getValueType();
75  return aValueType.equals( ::cppu::UnoType< PropertyValue >::get() )
76  || aValueType.equals( ::cppu::UnoType< NamedValue >::get() )
77  || aValueType.equals( ::cppu::UnoType< Sequence< PropertyValue > >::get() )
78  || aValueType.equals( ::cppu::UnoType< Sequence< NamedValue > >::get() );
79  }
80 
81 
82  NamedValueCollection& NamedValueCollection::merge( const NamedValueCollection& _rAdditionalValues, bool _bOverwriteExisting )
83  {
84  for (auto const& value : _rAdditionalValues.maValues)
85  {
86  if ( _bOverwriteExisting || !impl_has( value.first ) )
87  impl_put( value.first, value.second );
88  }
89 
90  return *this;
91  }
92 
93 
95  {
96  return maValues.size();
97  }
98 
99 
101  {
102  return maValues.empty();
103  }
104 
105 
106  std::vector< OUString > NamedValueCollection::getNames() const
107  {
108  std::vector< OUString > aNames;
109  for (auto const& value : maValues)
110  {
111  aNames.push_back( value.first );
112  }
113  return aNames;
114  }
115 
116 
117  void NamedValueCollection::impl_assign( const Any& i_rWrappedElements )
118  {
119  Sequence< NamedValue > aNamedValues;
120  Sequence< PropertyValue > aPropertyValues;
121  NamedValue aNamedValue;
122  PropertyValue aPropertyValue;
123 
124  if ( i_rWrappedElements >>= aNamedValues )
125  impl_assign( aNamedValues );
126  else if ( i_rWrappedElements >>= aPropertyValues )
127  impl_assign( aPropertyValues );
128  else if ( i_rWrappedElements >>= aNamedValue )
129  impl_assign( Sequence< NamedValue >( &aNamedValue, 1 ) );
130  else if ( i_rWrappedElements >>= aPropertyValue )
131  impl_assign( Sequence< PropertyValue >( &aPropertyValue, 1 ) );
132  else
133  SAL_WARN_IF( i_rWrappedElements.hasValue(), "comphelper", "NamedValueCollection::impl_assign(Any): unsupported type!" );
134  }
135 
136 
137  void NamedValueCollection::impl_assign( const Sequence< Any >& _rArguments )
138  {
139  maValues.clear();
140 
141  PropertyValue aPropertyValue;
142  NamedValue aNamedValue;
143 
144  for ( auto const & argument : _rArguments )
145  {
146  if ( argument >>= aPropertyValue )
147  maValues[ aPropertyValue.Name ] = aPropertyValue.Value;
148  else if ( argument >>= aNamedValue )
149  maValues[ aNamedValue.Name ] = aNamedValue.Value;
150  else
151  {
152  SAL_WARN_IF(
153  argument.hasValue(), "comphelper",
154  ("NamedValueCollection::impl_assign: encountered a value"
155  " type which I cannot handle: "
156  + argument.getValueTypeName()));
157  }
158  }
159  }
160 
161 
162  void NamedValueCollection::impl_assign( const Sequence< PropertyValue >& _rArguments )
163  {
164  maValues.clear();
165 
166  for ( auto const & argument : _rArguments )
167  maValues[ argument.Name ] = argument.Value;
168  }
169 
170 
171  void NamedValueCollection::impl_assign( const Sequence< NamedValue >& _rArguments )
172  {
173  maValues.clear();
174 
175  for ( auto const & argument : _rArguments )
176  maValues[ argument.Name ] = argument.Value;
177  }
178 
179 
180  bool NamedValueCollection::get_ensureType( const OUString& _rValueName, void* _pValueLocation, const Type& _rExpectedValueType ) const
181  {
182  auto pos = maValues.find( _rValueName );
183  if ( pos != maValues.end() )
184  {
185  if ( uno_type_assignData(
186  _pValueLocation, _rExpectedValueType.getTypeLibType(),
187  const_cast< void* >( pos->second.getValue() ), pos->second.getValueType().getTypeLibType(),
188  reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
189  reinterpret_cast< uno_AcquireFunc >( cpp_acquire ),
190  reinterpret_cast< uno_ReleaseFunc >( cpp_release )
191  ) )
192  // argument exists, and could be extracted
193  return true;
194 
195  // argument exists, but is of wrong type
196  throw IllegalArgumentException(
197  "Invalid value type for '" + _rValueName
198  + "'.\nExpected: " + _rExpectedValueType.getTypeName()
199  + "\nFound: " + pos->second.getValueType().getTypeName(),
200  nullptr, 0 );
201  }
202 
203  // argument does not exist
204  return false;
205  }
206 
207  namespace
208  {
209  class theEmptyDefault : public rtl::Static<Any, theEmptyDefault> {};
210  }
211 
212 
213  const Any& NamedValueCollection::impl_get( const OUString& _rValueName ) const
214  {
215  auto pos = maValues.find( _rValueName );
216  if ( pos != maValues.end() )
217  return pos->second;
218 
219  return theEmptyDefault::get();
220  }
221 
222 
223  bool NamedValueCollection::impl_has( const OUString& _rValueName ) const
224  {
225  auto pos = maValues.find( _rValueName );
226  return ( pos != maValues.end() );
227  }
228 
229 
230  bool NamedValueCollection::impl_put( const OUString& _rValueName, const Any& _rValue )
231  {
232  bool bHas = impl_has( _rValueName );
233  maValues[ _rValueName ] = _rValue;
234  return bHas;
235  }
236 
237 
238  bool NamedValueCollection::impl_remove( const OUString& _rValueName )
239  {
240  auto pos = maValues.find( _rValueName );
241  if ( pos == maValues.end() )
242  return false;
243  maValues.erase( pos );
244  return true;
245  }
246 
247 
248  sal_Int32 NamedValueCollection::operator >>= ( Sequence< PropertyValue >& _out_rValues ) const
249  {
250  _out_rValues.realloc( maValues.size() );
251  std::transform( maValues.begin(), maValues.end(), _out_rValues.getArray(),
252  [](const std::pair< OUString, css::uno::Any >& _rValue)
253  { return PropertyValue( _rValue.first, 0, _rValue.second, PropertyState_DIRECT_VALUE ); } );
254  return _out_rValues.getLength();
255  }
256 
257 
258  sal_Int32 NamedValueCollection::operator >>= ( Sequence< NamedValue >& _out_rValues ) const
259  {
260  _out_rValues.realloc( maValues.size() );
261  std::transform( maValues.begin(), maValues.end(), _out_rValues.getArray(),
262  [](const std::pair< OUString, css::uno::Any >& _rValue)
263  { return NamedValue( _rValue.first, _rValue.second ); } );
264  return _out_rValues.getLength();
265  }
266 
267 
268 } // namespace comphelper
269 
270 
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
bool impl_put(const OUString &_rValueName, const css::uno::Any &_rValue)
::std::vector< OUString > getNames() const
returns the names of all elements in the collection
bool impl_remove(const OUString &_rValueName)
sal_Bool SAL_CALL uno_type_assignData(void *pDest, typelib_TypeDescriptionReference *pDestType, void *pSource, typelib_TypeDescriptionReference *pSourceType, uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
a collection of named values, packed in various formats.
bool impl_has(const OUString &_rValueName) const
const css::uno::Any & impl_get(const OUString &_rValueName) const
bool empty() const
determines whether the collection is empty
size_t pos
std::unordered_map< OUString, css::uno::Any > maValues
NamedValueCollection & merge(const NamedValueCollection &_rAdditionalValues, bool _bOverwriteExisting)
merges the content of another collection into this
sal_Int32 operator>>=(css::uno::Sequence< css::beans::PropertyValue > &_out_rValues) const
transforms the collection to a sequence of PropertyValues
bool get_ensureType(const char *_pAsciiValueName, VALUE_TYPE &_out_rValue) const
retrieves a value with a given name from the collection, if it is present
::boost::spirit::classic::rule< ScannerT > argument
PropertyValueVector_t aPropertyValues
#define SAL_WARN_IF(condition, area, stream)
Any value
static bool canExtractFrom(css::uno::Any const &i_value)
determines whether or not named values can be extracted from the given value
void impl_assign(const css::uno::Any &i_rWrappedElements)
size_t size() const
returns the number of elements in the collection