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 62660 : struct NamedValueCollection_Impl
55 : {
56 : NamedValueRepository aValues;
57 : };
58 :
59 :
60 : //= NamedValueCollection
61 :
62 :
63 5238 : NamedValueCollection::NamedValueCollection()
64 5238 : :m_pImpl( new NamedValueCollection_Impl )
65 : {
66 5238 : }
67 :
68 :
69 2274 : NamedValueCollection::NamedValueCollection( const NamedValueCollection& _rCopySource )
70 2274 : :m_pImpl( new NamedValueCollection_Impl )
71 : {
72 2274 : *this = _rCopySource;
73 2274 : }
74 :
75 :
76 2376 : NamedValueCollection& NamedValueCollection::operator=( const NamedValueCollection& i_rCopySource )
77 : {
78 2376 : m_pImpl->aValues = i_rCopySource.m_pImpl->aValues;
79 2376 : return *this;
80 : }
81 :
82 :
83 2916 : NamedValueCollection::NamedValueCollection( const Any& _rElements )
84 2916 : :m_pImpl( new NamedValueCollection_Impl )
85 : {
86 2916 : impl_assign( _rElements );
87 2916 : }
88 :
89 :
90 2795 : NamedValueCollection::NamedValueCollection( const Sequence< Any >& _rArguments )
91 2795 : :m_pImpl( new NamedValueCollection_Impl )
92 : {
93 2795 : impl_assign( _rArguments );
94 2795 : }
95 :
96 :
97 18225 : NamedValueCollection::NamedValueCollection( const Sequence< PropertyValue >& _rArguments )
98 18225 : :m_pImpl( new NamedValueCollection_Impl )
99 : {
100 18225 : impl_assign( _rArguments );
101 18225 : }
102 :
103 :
104 32 : NamedValueCollection::NamedValueCollection( const Sequence< NamedValue >& _rArguments )
105 32 : :m_pImpl( new NamedValueCollection_Impl )
106 : {
107 32 : impl_assign( _rArguments );
108 32 : }
109 :
110 :
111 31180 : NamedValueCollection::~NamedValueCollection()
112 : {
113 31180 : }
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 94 : NamedValueCollection& NamedValueCollection::merge( const NamedValueCollection& _rAdditionalValues, bool _bOverwriteExisting )
130 : {
131 660 : for ( NamedValueRepository::const_iterator namedValue = _rAdditionalValues.m_pImpl->aValues.begin();
132 440 : namedValue != _rAdditionalValues.m_pImpl->aValues.end();
133 : ++namedValue
134 : )
135 : {
136 126 : if ( _bOverwriteExisting || !impl_has( namedValue->first ) )
137 126 : impl_put( namedValue->first, namedValue->second );
138 : }
139 :
140 94 : 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 2916 : void NamedValueCollection::impl_assign( const Any& i_rWrappedElements )
168 : {
169 2916 : Sequence< NamedValue > aNamedValues;
170 5832 : Sequence< PropertyValue > aPropertyValues;
171 5832 : NamedValue aNamedValue;
172 5832 : PropertyValue aPropertyValue;
173 :
174 2916 : if ( i_rWrappedElements >>= aNamedValues )
175 29 : impl_assign( aNamedValues );
176 2887 : else if ( i_rWrappedElements >>= aPropertyValues )
177 2373 : impl_assign( aPropertyValues );
178 514 : else if ( i_rWrappedElements >>= aNamedValue )
179 0 : impl_assign( Sequence< NamedValue >( &aNamedValue, 1 ) );
180 514 : else if ( i_rWrappedElements >>= aPropertyValue )
181 0 : impl_assign( Sequence< PropertyValue >( &aPropertyValue, 1 ) );
182 : else
183 2916 : SAL_WARN_IF( i_rWrappedElements.hasValue(), "comphelper", "NamedValueCollection::impl_assign(Any): unsupported type!" );
184 2916 : }
185 :
186 :
187 2801 : void NamedValueCollection::impl_assign( const Sequence< Any >& _rArguments )
188 : {
189 : {
190 2801 : NamedValueRepository aEmpty;
191 2801 : m_pImpl->aValues.swap( aEmpty );
192 : }
193 :
194 2801 : PropertyValue aPropertyValue;
195 5602 : NamedValue aNamedValue;
196 :
197 2801 : const Any* pArgument = _rArguments.getConstArray();
198 2801 : const Any* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
199 3710 : for ( ; pArgument != pArgumentEnd; ++pArgument )
200 : {
201 909 : if ( *pArgument >>= aPropertyValue )
202 901 : m_pImpl->aValues[ aPropertyValue.Name ] = aPropertyValue.Value;
203 8 : else if ( *pArgument >>= aNamedValue )
204 8 : 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 2801 : }
214 2801 : }
215 :
216 :
217 20598 : void NamedValueCollection::impl_assign( const Sequence< PropertyValue >& _rArguments )
218 : {
219 : {
220 20598 : NamedValueRepository aEmpty;
221 20598 : m_pImpl->aValues.swap( aEmpty );
222 : }
223 :
224 20598 : const PropertyValue* pArgument = _rArguments.getConstArray();
225 20598 : const PropertyValue* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
226 163537 : for ( ; pArgument != pArgumentEnd; ++pArgument )
227 142939 : m_pImpl->aValues[ pArgument->Name ] = pArgument->Value;
228 20598 : }
229 :
230 :
231 64 : void NamedValueCollection::impl_assign( const Sequence< NamedValue >& _rArguments )
232 : {
233 : {
234 64 : NamedValueRepository aEmpty;
235 64 : m_pImpl->aValues.swap( aEmpty );
236 : }
237 :
238 64 : const NamedValue* pArgument = _rArguments.getConstArray();
239 64 : const NamedValue* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
240 237 : for ( ; pArgument != pArgumentEnd; ++pArgument )
241 173 : m_pImpl->aValues[ pArgument->Name ] = pArgument->Value;
242 64 : }
243 :
244 :
245 47556 : bool NamedValueCollection::get_ensureType( const OUString& _rValueName, void* _pValueLocation, const Type& _rExpectedValueType ) const
246 : {
247 47556 : NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
248 47556 : if ( pos != m_pImpl->aValues.end() )
249 : {
250 14501 : if ( uno_type_assignData(
251 : _pValueLocation, _rExpectedValueType.getTypeLibType(),
252 29002 : 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 29002 : ) )
257 : // argument exists, and could be extracted
258 14501 : 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 33055 : return false;
273 : }
274 :
275 : namespace
276 : {
277 : class theEmptyDefault : public rtl::Static<Any, theEmptyDefault> {};
278 : }
279 :
280 :
281 9908 : const Any& NamedValueCollection::impl_get( const OUString& _rValueName ) const
282 : {
283 9908 : NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
284 9908 : if ( pos != m_pImpl->aValues.end() )
285 1461 : return pos->second;
286 :
287 8447 : return theEmptyDefault::get();
288 : }
289 :
290 :
291 22200 : bool NamedValueCollection::impl_has( const OUString& _rValueName ) const
292 : {
293 22200 : NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
294 22200 : return ( pos != m_pImpl->aValues.end() );
295 : }
296 :
297 :
298 17432 : bool NamedValueCollection::impl_put( const OUString& _rValueName, const Any& _rValue )
299 : {
300 17432 : bool bHas = impl_has( _rValueName );
301 17432 : m_pImpl->aValues[ _rValueName ] = _rValue;
302 17432 : return bHas;
303 : }
304 :
305 :
306 35425 : bool NamedValueCollection::impl_remove( const OUString& _rValueName )
307 : {
308 35425 : NamedValueRepository::iterator pos = m_pImpl->aValues.find( _rValueName );
309 35425 : if ( pos == m_pImpl->aValues.end() )
310 32235 : return false;
311 3190 : m_pImpl->aValues.erase( pos );
312 3190 : return true;
313 : }
314 :
315 :
316 : namespace
317 : {
318 : struct Value2PropertyValue : public ::std::unary_function< NamedValueRepository::value_type, PropertyValue >
319 : {
320 46480 : PropertyValue operator()( const NamedValueRepository::value_type& _rValue )
321 : {
322 : return PropertyValue(
323 46480 : _rValue.first, 0, _rValue.second, PropertyState_DIRECT_VALUE );
324 : }
325 : };
326 :
327 : struct Value2NamedValue : public ::std::unary_function< NamedValueRepository::value_type, NamedValue >
328 : {
329 169 : NamedValue operator()( const NamedValueRepository::value_type& _rValue )
330 : {
331 169 : return NamedValue( _rValue.first, _rValue.second );
332 : }
333 : };
334 : }
335 :
336 :
337 10507 : sal_Int32 NamedValueCollection::operator >>= ( Sequence< PropertyValue >& _out_rValues ) const
338 : {
339 10507 : _out_rValues.realloc( m_pImpl->aValues.size() );
340 10507 : ::std::transform( m_pImpl->aValues.begin(), m_pImpl->aValues.end(), _out_rValues.getArray(), Value2PropertyValue() );
341 10507 : return _out_rValues.getLength();
342 : }
343 :
344 :
345 31 : sal_Int32 NamedValueCollection::operator >>= ( Sequence< NamedValue >& _out_rValues ) const
346 : {
347 31 : _out_rValues.realloc( m_pImpl->aValues.size() );
348 31 : ::std::transform( m_pImpl->aValues.begin(), m_pImpl->aValues.end(), _out_rValues.getArray(), Value2NamedValue() );
349 31 : return _out_rValues.getLength();
350 : }
351 :
352 :
353 : } // namespace comphelper
354 :
355 :
356 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|