Line data Source code
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 : #include <comphelper/namedvaluecollection.hxx>
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 :
26 : #include <rtl/ustrbuf.hxx>
27 : #include <rtl/instance.hxx>
28 :
29 : #include <boost/unordered_map.hpp>
30 : #include <functional>
31 : #include <algorithm>
32 :
33 : //........................................................................
34 : namespace comphelper
35 : {
36 : //........................................................................
37 :
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;
48 :
49 : //====================================================================
50 : //= NamedValueCollection_Impl
51 : //====================================================================
52 : typedef ::boost::unordered_map< OUString, Any, OUStringHash > NamedValueRepository;
53 :
54 35665 : struct NamedValueCollection_Impl
55 : {
56 : NamedValueRepository aValues;
57 : };
58 :
59 : //====================================================================
60 : //= NamedValueCollection
61 : //====================================================================
62 : //--------------------------------------------------------------------
63 2955 : NamedValueCollection::NamedValueCollection()
64 2955 : :m_pImpl( new NamedValueCollection_Impl )
65 : {
66 2955 : }
67 :
68 : //--------------------------------------------------------------------
69 1476 : NamedValueCollection::NamedValueCollection( const NamedValueCollection& _rCopySource )
70 1476 : :m_pImpl( new NamedValueCollection_Impl )
71 : {
72 1476 : *this = _rCopySource;
73 1476 : }
74 :
75 : //--------------------------------------------------------------------
76 1572 : NamedValueCollection& NamedValueCollection::operator=( const NamedValueCollection& i_rCopySource )
77 : {
78 1572 : m_pImpl->aValues = i_rCopySource.m_pImpl->aValues;
79 1572 : return *this;
80 : }
81 :
82 : //--------------------------------------------------------------------
83 1952 : NamedValueCollection::NamedValueCollection( const Any& _rElements )
84 1952 : :m_pImpl( new NamedValueCollection_Impl )
85 : {
86 1952 : impl_assign( _rElements );
87 1952 : }
88 :
89 : //--------------------------------------------------------------------
90 1427 : NamedValueCollection::NamedValueCollection( const Sequence< Any >& _rArguments )
91 1427 : :m_pImpl( new NamedValueCollection_Impl )
92 : {
93 1427 : impl_assign( _rArguments );
94 1427 : }
95 :
96 : //--------------------------------------------------------------------
97 10100 : NamedValueCollection::NamedValueCollection( const Sequence< PropertyValue >& _rArguments )
98 10100 : :m_pImpl( new NamedValueCollection_Impl )
99 : {
100 10100 : impl_assign( _rArguments );
101 10100 : }
102 :
103 : //--------------------------------------------------------------------
104 5 : NamedValueCollection::NamedValueCollection( const Sequence< NamedValue >& _rArguments )
105 5 : :m_pImpl( new NamedValueCollection_Impl )
106 : {
107 5 : impl_assign( _rArguments );
108 5 : }
109 :
110 : //--------------------------------------------------------------------
111 17750 : NamedValueCollection::~NamedValueCollection()
112 : {
113 17750 : }
114 :
115 : //--------------------------------------------------------------------
116 1 : bool NamedValueCollection::canExtractFrom( ::com::sun::star::uno::Any const & i_value )
117 : {
118 1 : Type const & aValueType = i_value.getValueType();
119 2 : if ( aValueType.equals( ::cppu::UnoType< PropertyValue >::get() )
120 1 : || aValueType.equals( ::cppu::UnoType< NamedValue >::get() )
121 1 : || aValueType.equals( ::cppu::UnoType< Sequence< PropertyValue > >::get() )
122 2 : || aValueType.equals( ::cppu::UnoType< Sequence< NamedValue > >::get() )
123 : )
124 1 : return true;
125 0 : return false;
126 : }
127 :
128 : //--------------------------------------------------------------------
129 28 : NamedValueCollection& NamedValueCollection::merge( const NamedValueCollection& _rAdditionalValues, bool _bOverwriteExisting )
130 : {
131 327 : for ( NamedValueRepository::const_iterator namedValue = _rAdditionalValues.m_pImpl->aValues.begin();
132 218 : namedValue != _rAdditionalValues.m_pImpl->aValues.end();
133 : ++namedValue
134 : )
135 : {
136 81 : if ( _bOverwriteExisting || !impl_has( namedValue->first ) )
137 81 : impl_put( namedValue->first, namedValue->second );
138 : }
139 :
140 28 : return *this;
141 : }
142 :
143 : //--------------------------------------------------------------------
144 1 : size_t NamedValueCollection::size() const
145 : {
146 1 : return m_pImpl->aValues.size();
147 : }
148 :
149 : //--------------------------------------------------------------------
150 1 : bool NamedValueCollection::empty() const
151 : {
152 1 : return m_pImpl->aValues.empty();
153 : }
154 :
155 : //--------------------------------------------------------------------
156 1 : ::std::vector< OUString > NamedValueCollection::getNames() const
157 : {
158 1 : ::std::vector< OUString > aNames;
159 4 : for ( NamedValueRepository::const_iterator it = m_pImpl->aValues.begin(), end = m_pImpl->aValues.end(); it != end; ++it )
160 : {
161 3 : aNames.push_back( it->first );
162 : }
163 1 : return aNames;
164 : }
165 :
166 : //--------------------------------------------------------------------
167 1952 : void NamedValueCollection::impl_assign( const Any& i_rWrappedElements )
168 : {
169 1952 : Sequence< NamedValue > aNamedValues;
170 3904 : Sequence< PropertyValue > aPropertyValues;
171 3904 : NamedValue aNamedValue;
172 3904 : PropertyValue aPropertyValue;
173 :
174 1952 : if ( i_rWrappedElements >>= aNamedValues )
175 19 : impl_assign( aNamedValues );
176 1933 : else if ( i_rWrappedElements >>= aPropertyValues )
177 1451 : impl_assign( aPropertyValues );
178 482 : else if ( i_rWrappedElements >>= aNamedValue )
179 0 : impl_assign( Sequence< NamedValue >( &aNamedValue, 1 ) );
180 482 : else if ( i_rWrappedElements >>= aPropertyValue )
181 0 : impl_assign( Sequence< PropertyValue >( &aPropertyValue, 1 ) );
182 : else
183 1952 : SAL_WARN_IF( i_rWrappedElements.hasValue(), "comphelper", "NamedValueCollection::impl_assign(Any): unsupported type!" );
184 1952 : }
185 :
186 : //--------------------------------------------------------------------
187 1428 : void NamedValueCollection::impl_assign( const Sequence< Any >& _rArguments )
188 : {
189 : {
190 1428 : NamedValueRepository aEmpty;
191 1428 : m_pImpl->aValues.swap( aEmpty );
192 : }
193 :
194 1428 : PropertyValue aPropertyValue;
195 2856 : NamedValue aNamedValue;
196 :
197 1428 : const Any* pArgument = _rArguments.getConstArray();
198 1428 : const Any* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
199 1883 : for ( ; pArgument != pArgumentEnd; ++pArgument )
200 : {
201 455 : if ( *pArgument >>= aPropertyValue )
202 452 : m_pImpl->aValues[ aPropertyValue.Name ] = aPropertyValue.Value;
203 3 : else if ( *pArgument >>= aNamedValue )
204 3 : m_pImpl->aValues[ aNamedValue.Name ] = aNamedValue.Value;
205 : else
206 : {
207 : SAL_WARN_IF(
208 : pArgument->hasValue(), "comphelper",
209 : ("NamedValueCollection::impl_assign: encountered a value"
210 : " type which I cannot handle: "
211 : + pArgument->getValueTypeName()));
212 : }
213 1428 : }
214 1428 : }
215 :
216 : //--------------------------------------------------------------------
217 11551 : void NamedValueCollection::impl_assign( const Sequence< PropertyValue >& _rArguments )
218 : {
219 : {
220 11551 : NamedValueRepository aEmpty;
221 11551 : m_pImpl->aValues.swap( aEmpty );
222 : }
223 :
224 11551 : const PropertyValue* pArgument = _rArguments.getConstArray();
225 11551 : const PropertyValue* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
226 93802 : for ( ; pArgument != pArgumentEnd; ++pArgument )
227 82251 : m_pImpl->aValues[ pArgument->Name ] = pArgument->Value;
228 11551 : }
229 :
230 : //--------------------------------------------------------------------
231 25 : void NamedValueCollection::impl_assign( const Sequence< NamedValue >& _rArguments )
232 : {
233 : {
234 25 : NamedValueRepository aEmpty;
235 25 : m_pImpl->aValues.swap( aEmpty );
236 : }
237 :
238 25 : const NamedValue* pArgument = _rArguments.getConstArray();
239 25 : const NamedValue* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
240 33 : for ( ; pArgument != pArgumentEnd; ++pArgument )
241 8 : m_pImpl->aValues[ pArgument->Name ] = pArgument->Value;
242 25 : }
243 :
244 : //--------------------------------------------------------------------
245 25395 : bool NamedValueCollection::get_ensureType( const OUString& _rValueName, void* _pValueLocation, const Type& _rExpectedValueType ) const
246 : {
247 25395 : NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
248 25395 : if ( pos != m_pImpl->aValues.end() )
249 : {
250 7227 : if ( uno_type_assignData(
251 : _pValueLocation, _rExpectedValueType.getTypeLibType(),
252 14454 : 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 )
256 14454 : ) )
257 : // argument exists, and could be extracted
258 7227 : return true;
259 :
260 : // argument exists, but is of wrong type
261 0 : OUStringBuffer aBuffer;
262 0 : aBuffer.appendAscii( "Invalid value type for '" );
263 0 : aBuffer.append ( _rValueName );
264 0 : aBuffer.appendAscii( "'.\nExpected: " );
265 0 : aBuffer.append ( _rExpectedValueType.getTypeName() );
266 0 : aBuffer.appendAscii( "\nFound: " );
267 0 : aBuffer.append ( pos->second.getValueType().getTypeName() );
268 0 : throw IllegalArgumentException( aBuffer.makeStringAndClear(), NULL, 0 );
269 : }
270 :
271 : // argument does not exist
272 18168 : return false;
273 : }
274 :
275 : namespace
276 : {
277 : class theEmptyDefault : public rtl::Static<Any, theEmptyDefault> {};
278 : }
279 :
280 : //--------------------------------------------------------------------
281 5667 : const Any& NamedValueCollection::impl_get( const OUString& _rValueName ) const
282 : {
283 5667 : NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
284 5667 : if ( pos != m_pImpl->aValues.end() )
285 522 : return pos->second;
286 :
287 5145 : return theEmptyDefault::get();
288 : }
289 :
290 : //--------------------------------------------------------------------
291 11817 : bool NamedValueCollection::impl_has( const OUString& _rValueName ) const
292 : {
293 11817 : NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
294 11817 : return ( pos != m_pImpl->aValues.end() );
295 : }
296 :
297 : //--------------------------------------------------------------------
298 9588 : bool NamedValueCollection::impl_put( const OUString& _rValueName, const Any& _rValue )
299 : {
300 9588 : bool bHas = impl_has( _rValueName );
301 9588 : m_pImpl->aValues[ _rValueName ] = _rValue;
302 9588 : return bHas;
303 : }
304 :
305 : //--------------------------------------------------------------------
306 22077 : bool NamedValueCollection::impl_remove( const OUString& _rValueName )
307 : {
308 22077 : NamedValueRepository::iterator pos = m_pImpl->aValues.find( _rValueName );
309 22077 : if ( pos == m_pImpl->aValues.end() )
310 20045 : return false;
311 2032 : m_pImpl->aValues.erase( pos );
312 2032 : return true;
313 : }
314 :
315 : //--------------------------------------------------------------------
316 : namespace
317 : {
318 : struct Value2PropertyValue : public ::std::unary_function< NamedValueRepository::value_type, PropertyValue >
319 : {
320 24856 : PropertyValue operator()( const NamedValueRepository::value_type& _rValue )
321 : {
322 : return PropertyValue(
323 24856 : _rValue.first, 0, _rValue.second, PropertyState_DIRECT_VALUE );
324 : }
325 : };
326 :
327 : struct Value2NamedValue : public ::std::unary_function< NamedValueRepository::value_type, NamedValue >
328 : {
329 0 : NamedValue operator()( const NamedValueRepository::value_type& _rValue )
330 : {
331 0 : return NamedValue( _rValue.first, _rValue.second );
332 : }
333 : };
334 : }
335 :
336 : //--------------------------------------------------------------------
337 6060 : sal_Int32 NamedValueCollection::operator >>= ( Sequence< PropertyValue >& _out_rValues ) const
338 : {
339 6060 : _out_rValues.realloc( m_pImpl->aValues.size() );
340 6060 : ::std::transform( m_pImpl->aValues.begin(), m_pImpl->aValues.end(), _out_rValues.getArray(), Value2PropertyValue() );
341 6060 : return _out_rValues.getLength();
342 : }
343 :
344 : //--------------------------------------------------------------------
345 0 : sal_Int32 NamedValueCollection::operator >>= ( Sequence< NamedValue >& _out_rValues ) const
346 : {
347 0 : _out_rValues.realloc( m_pImpl->aValues.size() );
348 0 : ::std::transform( m_pImpl->aValues.begin(), m_pImpl->aValues.end(), _out_rValues.getArray(), Value2NamedValue() );
349 0 : return _out_rValues.getLength();
350 : }
351 :
352 : //........................................................................
353 : } // namespace comphelper
354 : //........................................................................
355 :
356 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|