1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #include <comphelper/namedvaluecollection.hxx>
22 #include <com/sun/star/beans/NamedValue.hpp>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <com/sun/star/beans/PropertyState.hpp>
26 #include <rtl/ustrbuf.hxx>
27 #include <rtl/instance.hxx>
29 #include <boost/unordered_map.hpp>
33 //........................................................................
36 //........................................................................
38 using ::com::sun::star::uno::Any
;
39 using ::com::sun::star::uno::Sequence
;
40 using ::com::sun::star::beans::PropertyValue
;
41 using ::com::sun::star::beans::NamedValue
;
42 using ::com::sun::star::uno::Type
;
43 using ::com::sun::star::uno::cpp_acquire
;
44 using ::com::sun::star::uno::cpp_release
;
45 using ::com::sun::star::uno::cpp_queryInterface
;
46 using ::com::sun::star::lang::IllegalArgumentException
;
47 using ::com::sun::star::beans::PropertyState_DIRECT_VALUE
;
49 //====================================================================
50 //= NamedValueCollection_Impl
51 //====================================================================
52 typedef ::boost::unordered_map
< OUString
, Any
, OUStringHash
> NamedValueRepository
;
54 struct NamedValueCollection_Impl
56 NamedValueRepository aValues
;
59 //====================================================================
60 //= NamedValueCollection
61 //====================================================================
62 //--------------------------------------------------------------------
63 NamedValueCollection::NamedValueCollection()
64 :m_pImpl( new NamedValueCollection_Impl
)
68 //--------------------------------------------------------------------
69 NamedValueCollection::NamedValueCollection( const NamedValueCollection
& _rCopySource
)
70 :m_pImpl( new NamedValueCollection_Impl
)
75 //--------------------------------------------------------------------
76 NamedValueCollection
& NamedValueCollection::operator=( const NamedValueCollection
& i_rCopySource
)
78 m_pImpl
->aValues
= i_rCopySource
.m_pImpl
->aValues
;
82 //--------------------------------------------------------------------
83 NamedValueCollection::NamedValueCollection( const Any
& _rElements
)
84 :m_pImpl( new NamedValueCollection_Impl
)
86 impl_assign( _rElements
);
89 //--------------------------------------------------------------------
90 NamedValueCollection::NamedValueCollection( const Sequence
< Any
>& _rArguments
)
91 :m_pImpl( new NamedValueCollection_Impl
)
93 impl_assign( _rArguments
);
96 //--------------------------------------------------------------------
97 NamedValueCollection::NamedValueCollection( const Sequence
< PropertyValue
>& _rArguments
)
98 :m_pImpl( new NamedValueCollection_Impl
)
100 impl_assign( _rArguments
);
103 //--------------------------------------------------------------------
104 NamedValueCollection::NamedValueCollection( const Sequence
< NamedValue
>& _rArguments
)
105 :m_pImpl( new NamedValueCollection_Impl
)
107 impl_assign( _rArguments
);
110 //--------------------------------------------------------------------
111 NamedValueCollection::~NamedValueCollection()
115 //--------------------------------------------------------------------
116 bool NamedValueCollection::canExtractFrom( ::com::sun::star::uno::Any
const & i_value
)
118 Type
const & aValueType
= i_value
.getValueType();
119 if ( aValueType
.equals( ::cppu::UnoType
< PropertyValue
>::get() )
120 || aValueType
.equals( ::cppu::UnoType
< NamedValue
>::get() )
121 || aValueType
.equals( ::cppu::UnoType
< Sequence
< PropertyValue
> >::get() )
122 || aValueType
.equals( ::cppu::UnoType
< Sequence
< NamedValue
> >::get() )
128 //--------------------------------------------------------------------
129 NamedValueCollection
& NamedValueCollection::merge( const NamedValueCollection
& _rAdditionalValues
, bool _bOverwriteExisting
)
131 for ( NamedValueRepository::const_iterator namedValue
= _rAdditionalValues
.m_pImpl
->aValues
.begin();
132 namedValue
!= _rAdditionalValues
.m_pImpl
->aValues
.end();
136 if ( _bOverwriteExisting
|| !impl_has( namedValue
->first
) )
137 impl_put( namedValue
->first
, namedValue
->second
);
143 //--------------------------------------------------------------------
144 size_t NamedValueCollection::size() const
146 return m_pImpl
->aValues
.size();
149 //--------------------------------------------------------------------
150 bool NamedValueCollection::empty() const
152 return m_pImpl
->aValues
.empty();
155 //--------------------------------------------------------------------
156 ::std::vector
< OUString
> NamedValueCollection::getNames() const
158 ::std::vector
< OUString
> aNames
;
159 for ( NamedValueRepository::const_iterator it
= m_pImpl
->aValues
.begin(), end
= m_pImpl
->aValues
.end(); it
!= end
; ++it
)
161 aNames
.push_back( it
->first
);
166 //--------------------------------------------------------------------
167 void NamedValueCollection::impl_assign( const Any
& i_rWrappedElements
)
169 Sequence
< NamedValue
> aNamedValues
;
170 Sequence
< PropertyValue
> aPropertyValues
;
171 NamedValue aNamedValue
;
172 PropertyValue aPropertyValue
;
174 if ( i_rWrappedElements
>>= aNamedValues
)
175 impl_assign( aNamedValues
);
176 else if ( i_rWrappedElements
>>= aPropertyValues
)
177 impl_assign( aPropertyValues
);
178 else if ( i_rWrappedElements
>>= aNamedValue
)
179 impl_assign( Sequence
< NamedValue
>( &aNamedValue
, 1 ) );
180 else if ( i_rWrappedElements
>>= aPropertyValue
)
181 impl_assign( Sequence
< PropertyValue
>( &aPropertyValue
, 1 ) );
183 SAL_WARN_IF( i_rWrappedElements
.hasValue(), "comphelper", "NamedValueCollection::impl_assign(Any): unsupported type!" );
186 //--------------------------------------------------------------------
187 void NamedValueCollection::impl_assign( const Sequence
< Any
>& _rArguments
)
190 NamedValueRepository aEmpty
;
191 m_pImpl
->aValues
.swap( aEmpty
);
194 PropertyValue aPropertyValue
;
195 NamedValue aNamedValue
;
197 const Any
* pArgument
= _rArguments
.getConstArray();
198 const Any
* pArgumentEnd
= _rArguments
.getConstArray() + _rArguments
.getLength();
199 for ( ; pArgument
!= pArgumentEnd
; ++pArgument
)
201 if ( *pArgument
>>= aPropertyValue
)
202 m_pImpl
->aValues
[ aPropertyValue
.Name
] = aPropertyValue
.Value
;
203 else if ( *pArgument
>>= aNamedValue
)
204 m_pImpl
->aValues
[ aNamedValue
.Name
] = aNamedValue
.Value
;
208 pArgument
->hasValue(), "comphelper",
209 ("NamedValueCollection::impl_assign: encountered a value"
210 " type which I cannot handle: "
211 + pArgument
->getValueTypeName()));
216 //--------------------------------------------------------------------
217 void NamedValueCollection::impl_assign( const Sequence
< PropertyValue
>& _rArguments
)
220 NamedValueRepository aEmpty
;
221 m_pImpl
->aValues
.swap( aEmpty
);
224 const PropertyValue
* pArgument
= _rArguments
.getConstArray();
225 const PropertyValue
* pArgumentEnd
= _rArguments
.getConstArray() + _rArguments
.getLength();
226 for ( ; pArgument
!= pArgumentEnd
; ++pArgument
)
227 m_pImpl
->aValues
[ pArgument
->Name
] = pArgument
->Value
;
230 //--------------------------------------------------------------------
231 void NamedValueCollection::impl_assign( const Sequence
< NamedValue
>& _rArguments
)
234 NamedValueRepository aEmpty
;
235 m_pImpl
->aValues
.swap( aEmpty
);
238 const NamedValue
* pArgument
= _rArguments
.getConstArray();
239 const NamedValue
* pArgumentEnd
= _rArguments
.getConstArray() + _rArguments
.getLength();
240 for ( ; pArgument
!= pArgumentEnd
; ++pArgument
)
241 m_pImpl
->aValues
[ pArgument
->Name
] = pArgument
->Value
;
244 //--------------------------------------------------------------------
245 bool NamedValueCollection::get_ensureType( const OUString
& _rValueName
, void* _pValueLocation
, const Type
& _rExpectedValueType
) const
247 NamedValueRepository::const_iterator pos
= m_pImpl
->aValues
.find( _rValueName
);
248 if ( pos
!= m_pImpl
->aValues
.end() )
250 if ( uno_type_assignData(
251 _pValueLocation
, _rExpectedValueType
.getTypeLibType(),
252 const_cast< void* >( pos
->second
.getValue() ), pos
->second
.getValueType().getTypeLibType(),
253 reinterpret_cast< uno_QueryInterfaceFunc
>( cpp_queryInterface
),
254 reinterpret_cast< uno_AcquireFunc
>( cpp_acquire
),
255 reinterpret_cast< uno_ReleaseFunc
>( cpp_release
)
257 // argument exists, and could be extracted
260 // argument exists, but is of wrong type
261 OUStringBuffer aBuffer
;
262 aBuffer
.appendAscii( "Invalid value type for '" );
263 aBuffer
.append ( _rValueName
);
264 aBuffer
.appendAscii( "'.\nExpected: " );
265 aBuffer
.append ( _rExpectedValueType
.getTypeName() );
266 aBuffer
.appendAscii( "\nFound: " );
267 aBuffer
.append ( pos
->second
.getValueType().getTypeName() );
268 throw IllegalArgumentException( aBuffer
.makeStringAndClear(), NULL
, 0 );
271 // argument does not exist
277 class theEmptyDefault
: public rtl::Static
<Any
, theEmptyDefault
> {};
280 //--------------------------------------------------------------------
281 const Any
& NamedValueCollection::impl_get( const OUString
& _rValueName
) const
283 NamedValueRepository::const_iterator pos
= m_pImpl
->aValues
.find( _rValueName
);
284 if ( pos
!= m_pImpl
->aValues
.end() )
287 return theEmptyDefault::get();
290 //--------------------------------------------------------------------
291 bool NamedValueCollection::impl_has( const OUString
& _rValueName
) const
293 NamedValueRepository::const_iterator pos
= m_pImpl
->aValues
.find( _rValueName
);
294 return ( pos
!= m_pImpl
->aValues
.end() );
297 //--------------------------------------------------------------------
298 bool NamedValueCollection::impl_put( const OUString
& _rValueName
, const Any
& _rValue
)
300 bool bHas
= impl_has( _rValueName
);
301 m_pImpl
->aValues
[ _rValueName
] = _rValue
;
305 //--------------------------------------------------------------------
306 bool NamedValueCollection::impl_remove( const OUString
& _rValueName
)
308 NamedValueRepository::iterator pos
= m_pImpl
->aValues
.find( _rValueName
);
309 if ( pos
== m_pImpl
->aValues
.end() )
311 m_pImpl
->aValues
.erase( pos
);
315 //--------------------------------------------------------------------
318 struct Value2PropertyValue
: public ::std::unary_function
< NamedValueRepository::value_type
, PropertyValue
>
320 PropertyValue
operator()( const NamedValueRepository::value_type
& _rValue
)
322 return PropertyValue(
323 _rValue
.first
, 0, _rValue
.second
, PropertyState_DIRECT_VALUE
);
327 struct Value2NamedValue
: public ::std::unary_function
< NamedValueRepository::value_type
, NamedValue
>
329 NamedValue
operator()( const NamedValueRepository::value_type
& _rValue
)
331 return NamedValue( _rValue
.first
, _rValue
.second
);
336 //--------------------------------------------------------------------
337 sal_Int32
NamedValueCollection::operator >>= ( Sequence
< PropertyValue
>& _out_rValues
) const
339 _out_rValues
.realloc( m_pImpl
->aValues
.size() );
340 ::std::transform( m_pImpl
->aValues
.begin(), m_pImpl
->aValues
.end(), _out_rValues
.getArray(), Value2PropertyValue() );
341 return _out_rValues
.getLength();
344 //--------------------------------------------------------------------
345 sal_Int32
NamedValueCollection::operator >>= ( Sequence
< NamedValue
>& _out_rValues
) const
347 _out_rValues
.realloc( m_pImpl
->aValues
.size() );
348 ::std::transform( m_pImpl
->aValues
.begin(), m_pImpl
->aValues
.end(), _out_rValues
.getArray(), Value2NamedValue() );
349 return _out_rValues
.getLength();
352 //........................................................................
353 } // namespace comphelper
354 //........................................................................
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */