Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <comphelper/namedvaluecollection.hxx>
30 : :
31 : : #include <com/sun/star/beans/NamedValue.hpp>
32 : : #include <com/sun/star/lang/IllegalArgumentException.hpp>
33 : : #include <com/sun/star/beans/PropertyState.hpp>
34 : :
35 : : #include <rtl/ustrbuf.hxx>
36 : : #include <rtl/strbuf.hxx>
37 : : #include <rtl/instance.hxx>
38 : : #include <osl/diagnose.h>
39 : :
40 : : #include <boost/unordered_map.hpp>
41 : : #include <functional>
42 : : #include <algorithm>
43 : :
44 : : //........................................................................
45 : : namespace comphelper
46 : : {
47 : : //........................................................................
48 : :
49 : : /** === begin UNO using === **/
50 : : using ::com::sun::star::uno::Any;
51 : : using ::com::sun::star::uno::Sequence;
52 : : using ::com::sun::star::beans::PropertyValue;
53 : : using ::com::sun::star::beans::NamedValue;
54 : : using ::com::sun::star::uno::Type;
55 : : using ::com::sun::star::uno::cpp_acquire;
56 : : using ::com::sun::star::uno::cpp_release;
57 : : using ::com::sun::star::uno::cpp_queryInterface;
58 : : using ::com::sun::star::lang::IllegalArgumentException;
59 : : using ::com::sun::star::beans::NamedValue;
60 : : using ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
61 : : /** === end UNO using === **/
62 : :
63 : : //====================================================================
64 : : //= NamedValueCollection_Impl
65 : : //====================================================================
66 : : typedef ::boost::unordered_map< ::rtl::OUString, Any, ::rtl::OUStringHash > NamedValueRepository;
67 : :
68 [ + - ]: 68835 : struct NamedValueCollection_Impl
69 : : {
70 : : NamedValueRepository aValues;
71 : : };
72 : :
73 : : //====================================================================
74 : : //= NamedValueCollection
75 : : //====================================================================
76 : : //--------------------------------------------------------------------
77 : 5539 : NamedValueCollection::NamedValueCollection()
78 [ + - ]: 5539 : :m_pImpl( new NamedValueCollection_Impl )
79 : : {
80 : 5539 : }
81 : :
82 : : //--------------------------------------------------------------------
83 : 2808 : NamedValueCollection::NamedValueCollection( const NamedValueCollection& _rCopySource )
84 [ + - ]: 2808 : :m_pImpl( new NamedValueCollection_Impl )
85 : : {
86 [ + - ]: 2808 : *this = _rCopySource;
87 : 2808 : }
88 : :
89 : : //--------------------------------------------------------------------
90 : 2996 : NamedValueCollection& NamedValueCollection::operator=( const NamedValueCollection& i_rCopySource )
91 : : {
92 : 2996 : m_pImpl->aValues = i_rCopySource.m_pImpl->aValues;
93 : 2996 : return *this;
94 : : }
95 : :
96 : : //--------------------------------------------------------------------
97 : 3743 : NamedValueCollection::NamedValueCollection( const Any& _rElements )
98 [ + - ]: 3743 : :m_pImpl( new NamedValueCollection_Impl )
99 : : {
100 [ + - ]: 3743 : impl_assign( _rElements );
101 : 3743 : }
102 : :
103 : : //--------------------------------------------------------------------
104 : 3921 : NamedValueCollection::NamedValueCollection( const Sequence< Any >& _rArguments )
105 [ + - ]: 3921 : :m_pImpl( new NamedValueCollection_Impl )
106 : : {
107 [ + - ]: 3921 : impl_assign( _rArguments );
108 : 3921 : }
109 : :
110 : : //--------------------------------------------------------------------
111 : 18546 : NamedValueCollection::NamedValueCollection( const Sequence< PropertyValue >& _rArguments )
112 [ + - ]: 18546 : :m_pImpl( new NamedValueCollection_Impl )
113 : : {
114 [ + - ]: 18546 : impl_assign( _rArguments );
115 : 18546 : }
116 : :
117 : : //--------------------------------------------------------------------
118 : 9 : NamedValueCollection::NamedValueCollection( const Sequence< NamedValue >& _rArguments )
119 [ + - ]: 9 : :m_pImpl( new NamedValueCollection_Impl )
120 : : {
121 [ + - ]: 9 : impl_assign( _rArguments );
122 : 9 : }
123 : :
124 : : //--------------------------------------------------------------------
125 : 34269 : NamedValueCollection::~NamedValueCollection()
126 : : {
127 : 34269 : }
128 : :
129 : : //--------------------------------------------------------------------
130 : 2 : bool NamedValueCollection::canExtractFrom( ::com::sun::star::uno::Any const & i_value )
131 : : {
132 : 2 : Type const & aValueType = i_value.getValueType();
133 [ + - ][ + - : 8 : if ( aValueType.equals( ::cppu::UnoType< PropertyValue >::get() )
+ - + - +
- ]
134 : 2 : || aValueType.equals( ::cppu::UnoType< NamedValue >::get() )
135 : 2 : || aValueType.equals( ::cppu::UnoType< Sequence< PropertyValue > >::get() )
136 : 2 : || aValueType.equals( ::cppu::UnoType< Sequence< NamedValue > >::get() )
137 : : )
138 : 2 : return true;
139 : 2 : return false;
140 : : }
141 : :
142 : : //--------------------------------------------------------------------
143 : 14 : NamedValueCollection& NamedValueCollection::merge( const NamedValueCollection& _rAdditionalValues, bool _bOverwriteExisting )
144 : : {
145 [ + - ][ + + ]: 220 : for ( NamedValueRepository::const_iterator namedValue = _rAdditionalValues.m_pImpl->aValues.begin();
146 [ + - ]: 110 : namedValue != _rAdditionalValues.m_pImpl->aValues.end();
147 : : ++namedValue
148 : : )
149 : : {
150 [ - + ][ # # ]: 96 : if ( _bOverwriteExisting || !impl_has( namedValue->first ) )
[ # # ][ # # ]
[ + - ]
151 [ + - ][ + - ]: 96 : impl_put( namedValue->first, namedValue->second );
[ + - ]
152 : : }
153 : :
154 : 14 : return *this;
155 : : }
156 : :
157 : : //--------------------------------------------------------------------
158 : 2 : size_t NamedValueCollection::size() const
159 : : {
160 : 2 : return m_pImpl->aValues.size();
161 : : }
162 : :
163 : : //--------------------------------------------------------------------
164 : 2 : bool NamedValueCollection::empty() const
165 : : {
166 : 2 : return m_pImpl->aValues.empty();
167 : : }
168 : :
169 : : //--------------------------------------------------------------------
170 : 2 : ::std::vector< ::rtl::OUString > NamedValueCollection::getNames() const
171 : : {
172 : 2 : ::std::vector< ::rtl::OUString > aNames;
173 [ + - ][ + + ]: 8 : for ( NamedValueRepository::const_iterator it = m_pImpl->aValues.begin(), end = m_pImpl->aValues.end(); it != end; ++it )
[ + - ]
174 : : {
175 [ + - ][ + - ]: 6 : aNames.push_back( it->first );
176 : : }
177 : 2 : return aNames;
178 : : }
179 : :
180 : : //--------------------------------------------------------------------
181 : 3743 : void NamedValueCollection::impl_assign( const Any& i_rWrappedElements )
182 : : {
183 [ + - ]: 3743 : Sequence< NamedValue > aNamedValues;
184 [ + - ]: 3743 : Sequence< PropertyValue > aPropertyValues;
185 : 3743 : NamedValue aNamedValue;
186 : 3743 : PropertyValue aPropertyValue;
187 : :
188 [ + + ][ + - ]: 3743 : if ( i_rWrappedElements >>= aNamedValues )
189 [ + - ]: 36 : impl_assign( aNamedValues );
190 [ + - ][ + + ]: 3707 : else if ( i_rWrappedElements >>= aPropertyValues )
191 [ + - ]: 2763 : impl_assign( aPropertyValues );
192 [ + - ][ - + ]: 944 : else if ( i_rWrappedElements >>= aNamedValue )
193 [ # # ][ # # ]: 0 : impl_assign( Sequence< NamedValue >( &aNamedValue, 1 ) );
[ # # ]
194 [ + - ][ - + ]: 944 : else if ( i_rWrappedElements >>= aPropertyValue )
195 [ # # ][ # # ]: 0 : impl_assign( Sequence< PropertyValue >( &aPropertyValue, 1 ) );
[ # # ]
196 : : else
197 [ + - ][ + - ]: 3743 : OSL_ENSURE( !i_rWrappedElements.hasValue(), "NamedValueCollection::impl_assign(Any): unsupported type!" );
198 : 3743 : }
199 : :
200 : : //--------------------------------------------------------------------
201 : 3923 : void NamedValueCollection::impl_assign( const Sequence< Any >& _rArguments )
202 : : {
203 : : {
204 [ + - ]: 3923 : NamedValueRepository aEmpty;
205 [ + - ][ + - ]: 3923 : m_pImpl->aValues.swap( aEmpty );
206 : : }
207 : :
208 : 3923 : PropertyValue aPropertyValue;
209 : 3923 : NamedValue aNamedValue;
210 : :
211 : 3923 : const Any* pArgument = _rArguments.getConstArray();
212 : 3923 : const Any* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
213 [ + + ]: 7479 : for ( ; pArgument != pArgumentEnd; ++pArgument )
214 : : {
215 [ + - ][ + + ]: 3556 : if ( *pArgument >>= aPropertyValue )
216 [ + - ]: 1654 : m_pImpl->aValues[ aPropertyValue.Name ] = aPropertyValue.Value;
217 [ + - ][ + - ]: 1902 : else if ( *pArgument >>= aNamedValue )
218 [ + - ]: 1902 : m_pImpl->aValues[ aNamedValue.Name ] = aNamedValue.Value;
219 : : #if OSL_DEBUG_LEVEL > 0
220 : : else if ( pArgument->hasValue() )
221 : : {
222 : : ::rtl::OStringBuffer message;
223 : : message.append( "NamedValueCollection::impl_assign: encountered a value type which I cannot handle:\n" );
224 : : message.append( ::rtl::OUStringToOString( pArgument->getValueTypeName(), RTL_TEXTENCODING_ASCII_US ) );
225 : : OSL_FAIL( message.getStr() );
226 : : }
227 : : #endif
228 : 3923 : }
229 : 3923 : }
230 : :
231 : : //--------------------------------------------------------------------
232 : 21309 : void NamedValueCollection::impl_assign( const Sequence< PropertyValue >& _rArguments )
233 : : {
234 : : {
235 [ + - ]: 21309 : NamedValueRepository aEmpty;
236 [ + - ][ + - ]: 21309 : m_pImpl->aValues.swap( aEmpty );
237 : : }
238 : :
239 : 21309 : const PropertyValue* pArgument = _rArguments.getConstArray();
240 : 21309 : const PropertyValue* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
241 [ + + ]: 168243 : for ( ; pArgument != pArgumentEnd; ++pArgument )
242 : 146934 : m_pImpl->aValues[ pArgument->Name ] = pArgument->Value;
243 : 21309 : }
244 : :
245 : : //--------------------------------------------------------------------
246 : 47 : void NamedValueCollection::impl_assign( const Sequence< NamedValue >& _rArguments )
247 : : {
248 : : {
249 [ + - ]: 47 : NamedValueRepository aEmpty;
250 [ + - ][ + - ]: 47 : m_pImpl->aValues.swap( aEmpty );
251 : : }
252 : :
253 : 47 : const NamedValue* pArgument = _rArguments.getConstArray();
254 : 47 : const NamedValue* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
255 [ + + ]: 60 : for ( ; pArgument != pArgumentEnd; ++pArgument )
256 : 13 : m_pImpl->aValues[ pArgument->Name ] = pArgument->Value;
257 : 47 : }
258 : :
259 : : //--------------------------------------------------------------------
260 : 47551 : bool NamedValueCollection::get_ensureType( const ::rtl::OUString& _rValueName, void* _pValueLocation, const Type& _rExpectedValueType ) const
261 : : {
262 [ + - ]: 47551 : NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
263 [ + + ][ + - ]: 47551 : if ( pos != m_pImpl->aValues.end() )
264 : : {
265 [ + - ]: 15567 : if ( uno_type_assignData(
266 : : _pValueLocation, _rExpectedValueType.getTypeLibType(),
267 [ + - ][ + - ]: 31134 : const_cast< void* >( pos->second.getValue() ), pos->second.getValueType().getTypeLibType(),
268 : : reinterpret_cast< uno_QueryInterfaceFunc >( cpp_queryInterface ),
269 : : reinterpret_cast< uno_AcquireFunc >( cpp_acquire ),
270 : : reinterpret_cast< uno_ReleaseFunc >( cpp_release )
271 : 31134 : ) )
272 : : // argument exists, and could be extracted
273 : 15567 : return true;
274 : :
275 : : // argument exists, but is of wrong type
276 : 0 : ::rtl::OUStringBuffer aBuffer;
277 [ # # ]: 0 : aBuffer.appendAscii( "Invalid value type for '" );
278 [ # # ]: 0 : aBuffer.append ( _rValueName );
279 [ # # ]: 0 : aBuffer.appendAscii( "'.\nExpected: " );
280 [ # # ]: 0 : aBuffer.append ( _rExpectedValueType.getTypeName() );
281 [ # # ]: 0 : aBuffer.appendAscii( "\nFound: " );
282 [ # # ][ # # ]: 0 : aBuffer.append ( pos->second.getValueType().getTypeName() );
283 [ # # ][ # # ]: 0 : throw IllegalArgumentException( aBuffer.makeStringAndClear(), NULL, 0 );
[ # # ]
284 : : }
285 : :
286 : : // argument does not exist
287 : 47551 : return false;
288 : : }
289 : :
290 : : namespace
291 : : {
292 : : class theEmptyDefault : public rtl::Static<Any, theEmptyDefault> {};
293 : : }
294 : :
295 : : //--------------------------------------------------------------------
296 : 13480 : const Any& NamedValueCollection::impl_get( const ::rtl::OUString& _rValueName ) const
297 : : {
298 [ + - ]: 13480 : NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
299 [ + + ][ + - ]: 13480 : if ( pos != m_pImpl->aValues.end() )
300 [ + - ]: 1019 : return pos->second;
301 : :
302 : 13480 : return theEmptyDefault::get();
303 : : }
304 : :
305 : : //--------------------------------------------------------------------
306 : 24094 : bool NamedValueCollection::impl_has( const ::rtl::OUString& _rValueName ) const
307 : : {
308 [ + - ]: 24094 : NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
309 [ + - ]: 24094 : return ( pos != m_pImpl->aValues.end() );
310 : : }
311 : :
312 : : //--------------------------------------------------------------------
313 : 19981 : bool NamedValueCollection::impl_put( const ::rtl::OUString& _rValueName, const Any& _rValue )
314 : : {
315 : 19981 : bool bHas = impl_has( _rValueName );
316 : 19981 : m_pImpl->aValues[ _rValueName ] = _rValue;
317 : 19981 : return bHas;
318 : : }
319 : :
320 : : //--------------------------------------------------------------------
321 : 52648 : bool NamedValueCollection::impl_remove( const ::rtl::OUString& _rValueName )
322 : : {
323 [ + - ]: 52648 : NamedValueRepository::iterator pos = m_pImpl->aValues.find( _rValueName );
324 [ + - ][ + + ]: 52648 : if ( pos == m_pImpl->aValues.end() )
325 : 46129 : return false;
326 [ + - ]: 6519 : m_pImpl->aValues.erase( pos );
327 : 52648 : return true;
328 : : }
329 : :
330 : : //--------------------------------------------------------------------
331 : : namespace
332 : : {
333 : : struct Value2PropertyValue : public ::std::unary_function< NamedValueRepository::value_type, PropertyValue >
334 : : {
335 : 50434 : PropertyValue operator()( const NamedValueRepository::value_type& _rValue )
336 : : {
337 : : return PropertyValue(
338 : 50434 : _rValue.first, 0, _rValue.second, PropertyState_DIRECT_VALUE );
339 : : }
340 : : };
341 : :
342 : : struct Value2NamedValue : public ::std::unary_function< NamedValueRepository::value_type, NamedValue >
343 : : {
344 : 0 : NamedValue operator()( const NamedValueRepository::value_type& _rValue )
345 : : {
346 : 0 : return NamedValue( _rValue.first, _rValue.second );
347 : : }
348 : : };
349 : : }
350 : :
351 : : //--------------------------------------------------------------------
352 : 12744 : sal_Int32 NamedValueCollection::operator >>= ( Sequence< PropertyValue >& _out_rValues ) const
353 : : {
354 : 12744 : _out_rValues.realloc( m_pImpl->aValues.size() );
355 [ + - ][ + - ]: 12744 : ::std::transform( m_pImpl->aValues.begin(), m_pImpl->aValues.end(), _out_rValues.getArray(), Value2PropertyValue() );
[ + - ][ + - ]
356 : 12744 : return _out_rValues.getLength();
357 : : }
358 : :
359 : : //--------------------------------------------------------------------
360 : 0 : sal_Int32 NamedValueCollection::operator >>= ( Sequence< NamedValue >& _out_rValues ) const
361 : : {
362 : 0 : _out_rValues.realloc( m_pImpl->aValues.size() );
363 [ # # ][ # # ]: 0 : ::std::transform( m_pImpl->aValues.begin(), m_pImpl->aValues.end(), _out_rValues.getArray(), Value2NamedValue() );
[ # # ][ # # ]
364 : 0 : return _out_rValues.getLength();
365 : : }
366 : :
367 : : //........................................................................
368 : : } // namespace comphelper
369 : : //........................................................................
370 : :
371 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|