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>
28 #include <sal/log.hxx>
32 #include <unordered_map>
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 typedef std::unordered_map
< OUString
, Any
, OUStringHash
> NamedValueRepository
;
51 struct NamedValueCollection_Impl
53 NamedValueRepository aValues
;
56 NamedValueCollection::NamedValueCollection()
57 :m_pImpl( new NamedValueCollection_Impl
)
62 NamedValueCollection::NamedValueCollection( const NamedValueCollection
& _rCopySource
)
63 :m_pImpl( new NamedValueCollection_Impl
)
69 NamedValueCollection
& NamedValueCollection::operator=( const NamedValueCollection
& i_rCopySource
)
71 m_pImpl
->aValues
= i_rCopySource
.m_pImpl
->aValues
;
76 NamedValueCollection::NamedValueCollection( const Any
& _rElements
)
77 :m_pImpl( new NamedValueCollection_Impl
)
79 impl_assign( _rElements
);
83 NamedValueCollection::NamedValueCollection( const Sequence
< Any
>& _rArguments
)
84 :m_pImpl( new NamedValueCollection_Impl
)
86 impl_assign( _rArguments
);
90 NamedValueCollection::NamedValueCollection( const Sequence
< PropertyValue
>& _rArguments
)
91 :m_pImpl( new NamedValueCollection_Impl
)
93 impl_assign( _rArguments
);
97 NamedValueCollection::NamedValueCollection( const Sequence
< NamedValue
>& _rArguments
)
98 :m_pImpl( new NamedValueCollection_Impl
)
100 impl_assign( _rArguments
);
104 NamedValueCollection::~NamedValueCollection()
109 bool NamedValueCollection::canExtractFrom( ::com::sun::star::uno::Any
const & i_value
)
111 Type
const & aValueType
= i_value
.getValueType();
112 if ( aValueType
.equals( ::cppu::UnoType
< PropertyValue
>::get() )
113 || aValueType
.equals( ::cppu::UnoType
< NamedValue
>::get() )
114 || aValueType
.equals( ::cppu::UnoType
< Sequence
< PropertyValue
> >::get() )
115 || aValueType
.equals( ::cppu::UnoType
< Sequence
< NamedValue
> >::get() )
122 NamedValueCollection
& NamedValueCollection::merge( const NamedValueCollection
& _rAdditionalValues
, bool _bOverwriteExisting
)
124 for ( NamedValueRepository::const_iterator namedValue
= _rAdditionalValues
.m_pImpl
->aValues
.begin();
125 namedValue
!= _rAdditionalValues
.m_pImpl
->aValues
.end();
129 if ( _bOverwriteExisting
|| !impl_has( namedValue
->first
) )
130 impl_put( namedValue
->first
, namedValue
->second
);
137 size_t NamedValueCollection::size() const
139 return m_pImpl
->aValues
.size();
143 bool NamedValueCollection::empty() const
145 return m_pImpl
->aValues
.empty();
149 ::std::vector
< OUString
> NamedValueCollection::getNames() const
151 ::std::vector
< OUString
> aNames
;
152 for ( NamedValueRepository::const_iterator it
= m_pImpl
->aValues
.begin(), end
= m_pImpl
->aValues
.end(); it
!= end
; ++it
)
154 aNames
.push_back( it
->first
);
160 void NamedValueCollection::impl_assign( const Any
& i_rWrappedElements
)
162 Sequence
< NamedValue
> aNamedValues
;
163 Sequence
< PropertyValue
> aPropertyValues
;
164 NamedValue aNamedValue
;
165 PropertyValue aPropertyValue
;
167 if ( i_rWrappedElements
>>= aNamedValues
)
168 impl_assign( aNamedValues
);
169 else if ( i_rWrappedElements
>>= aPropertyValues
)
170 impl_assign( aPropertyValues
);
171 else if ( i_rWrappedElements
>>= aNamedValue
)
172 impl_assign( Sequence
< NamedValue
>( &aNamedValue
, 1 ) );
173 else if ( i_rWrappedElements
>>= aPropertyValue
)
174 impl_assign( Sequence
< PropertyValue
>( &aPropertyValue
, 1 ) );
176 SAL_WARN_IF( i_rWrappedElements
.hasValue(), "comphelper", "NamedValueCollection::impl_assign(Any): unsupported type!" );
180 void NamedValueCollection::impl_assign( const Sequence
< Any
>& _rArguments
)
183 NamedValueRepository aEmpty
;
184 m_pImpl
->aValues
.swap( aEmpty
);
187 PropertyValue aPropertyValue
;
188 NamedValue aNamedValue
;
190 const Any
* pArgument
= _rArguments
.getConstArray();
191 const Any
* pArgumentEnd
= _rArguments
.getConstArray() + _rArguments
.getLength();
192 for ( ; pArgument
!= pArgumentEnd
; ++pArgument
)
194 if ( *pArgument
>>= aPropertyValue
)
195 m_pImpl
->aValues
[ aPropertyValue
.Name
] = aPropertyValue
.Value
;
196 else if ( *pArgument
>>= aNamedValue
)
197 m_pImpl
->aValues
[ aNamedValue
.Name
] = aNamedValue
.Value
;
201 pArgument
->hasValue(), "comphelper",
202 ("NamedValueCollection::impl_assign: encountered a value"
203 " type which I cannot handle: "
204 + pArgument
->getValueTypeName()));
210 void NamedValueCollection::impl_assign( const Sequence
< PropertyValue
>& _rArguments
)
213 NamedValueRepository aEmpty
;
214 m_pImpl
->aValues
.swap( aEmpty
);
217 const PropertyValue
* pArgument
= _rArguments
.getConstArray();
218 const PropertyValue
* pArgumentEnd
= _rArguments
.getConstArray() + _rArguments
.getLength();
219 for ( ; pArgument
!= pArgumentEnd
; ++pArgument
)
220 m_pImpl
->aValues
[ pArgument
->Name
] = pArgument
->Value
;
224 void NamedValueCollection::impl_assign( const Sequence
< NamedValue
>& _rArguments
)
227 NamedValueRepository aEmpty
;
228 m_pImpl
->aValues
.swap( aEmpty
);
231 const NamedValue
* pArgument
= _rArguments
.getConstArray();
232 const NamedValue
* pArgumentEnd
= _rArguments
.getConstArray() + _rArguments
.getLength();
233 for ( ; pArgument
!= pArgumentEnd
; ++pArgument
)
234 m_pImpl
->aValues
[ pArgument
->Name
] = pArgument
->Value
;
238 bool NamedValueCollection::get_ensureType( const OUString
& _rValueName
, void* _pValueLocation
, const Type
& _rExpectedValueType
) const
240 NamedValueRepository::const_iterator pos
= m_pImpl
->aValues
.find( _rValueName
);
241 if ( pos
!= m_pImpl
->aValues
.end() )
243 if ( uno_type_assignData(
244 _pValueLocation
, _rExpectedValueType
.getTypeLibType(),
245 const_cast< void* >( pos
->second
.getValue() ), pos
->second
.getValueType().getTypeLibType(),
246 reinterpret_cast< uno_QueryInterfaceFunc
>( cpp_queryInterface
),
247 reinterpret_cast< uno_AcquireFunc
>( cpp_acquire
),
248 reinterpret_cast< uno_ReleaseFunc
>( cpp_release
)
250 // argument exists, and could be extracted
253 // argument exists, but is of wrong type
254 OUStringBuffer aBuffer
;
255 aBuffer
.appendAscii( "Invalid value type for '" );
256 aBuffer
.append ( _rValueName
);
257 aBuffer
.appendAscii( "'.\nExpected: " );
258 aBuffer
.append ( _rExpectedValueType
.getTypeName() );
259 aBuffer
.appendAscii( "\nFound: " );
260 aBuffer
.append ( pos
->second
.getValueType().getTypeName() );
261 throw IllegalArgumentException( aBuffer
.makeStringAndClear(), NULL
, 0 );
264 // argument does not exist
270 class theEmptyDefault
: public rtl::Static
<Any
, theEmptyDefault
> {};
274 const Any
& NamedValueCollection::impl_get( const OUString
& _rValueName
) const
276 NamedValueRepository::const_iterator pos
= m_pImpl
->aValues
.find( _rValueName
);
277 if ( pos
!= m_pImpl
->aValues
.end() )
280 return theEmptyDefault::get();
284 bool NamedValueCollection::impl_has( const OUString
& _rValueName
) const
286 NamedValueRepository::const_iterator pos
= m_pImpl
->aValues
.find( _rValueName
);
287 return ( pos
!= m_pImpl
->aValues
.end() );
291 bool NamedValueCollection::impl_put( const OUString
& _rValueName
, const Any
& _rValue
)
293 bool bHas
= impl_has( _rValueName
);
294 m_pImpl
->aValues
[ _rValueName
] = _rValue
;
299 bool NamedValueCollection::impl_remove( const OUString
& _rValueName
)
301 NamedValueRepository::iterator pos
= m_pImpl
->aValues
.find( _rValueName
);
302 if ( pos
== m_pImpl
->aValues
.end() )
304 m_pImpl
->aValues
.erase( pos
);
311 struct Value2PropertyValue
: public ::std::unary_function
< NamedValueRepository::value_type
, PropertyValue
>
313 PropertyValue
operator()( const NamedValueRepository::value_type
& _rValue
)
315 return PropertyValue(
316 _rValue
.first
, 0, _rValue
.second
, PropertyState_DIRECT_VALUE
);
320 struct Value2NamedValue
: public ::std::unary_function
< NamedValueRepository::value_type
, NamedValue
>
322 NamedValue
operator()( const NamedValueRepository::value_type
& _rValue
)
324 return NamedValue( _rValue
.first
, _rValue
.second
);
330 sal_Int32
NamedValueCollection::operator >>= ( Sequence
< PropertyValue
>& _out_rValues
) const
332 _out_rValues
.realloc( m_pImpl
->aValues
.size() );
333 ::std::transform( m_pImpl
->aValues
.begin(), m_pImpl
->aValues
.end(), _out_rValues
.getArray(), Value2PropertyValue() );
334 return _out_rValues
.getLength();
338 sal_Int32
NamedValueCollection::operator >>= ( Sequence
< NamedValue
>& _out_rValues
) const
340 _out_rValues
.realloc( m_pImpl
->aValues
.size() );
341 ::std::transform( m_pImpl
->aValues
.begin(), m_pImpl
->aValues
.end(), _out_rValues
.getArray(), Value2NamedValue() );
342 return _out_rValues
.getLength();
346 } // namespace comphelper
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */