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 0 : struct NamedValueCollection_Impl
55 : {
56 : NamedValueRepository aValues;
57 : };
58 :
59 :
60 : //= NamedValueCollection
61 :
62 :
63 0 : NamedValueCollection::NamedValueCollection()
64 0 : :m_pImpl( new NamedValueCollection_Impl )
65 : {
66 0 : }
67 :
68 :
69 0 : NamedValueCollection::NamedValueCollection( const NamedValueCollection& _rCopySource )
70 0 : :m_pImpl( new NamedValueCollection_Impl )
71 : {
72 0 : *this = _rCopySource;
73 0 : }
74 :
75 :
76 0 : NamedValueCollection& NamedValueCollection::operator=( const NamedValueCollection& i_rCopySource )
77 : {
78 0 : m_pImpl->aValues = i_rCopySource.m_pImpl->aValues;
79 0 : return *this;
80 : }
81 :
82 :
83 0 : NamedValueCollection::NamedValueCollection( const Any& _rElements )
84 0 : :m_pImpl( new NamedValueCollection_Impl )
85 : {
86 0 : impl_assign( _rElements );
87 0 : }
88 :
89 :
90 0 : NamedValueCollection::NamedValueCollection( const Sequence< Any >& _rArguments )
91 0 : :m_pImpl( new NamedValueCollection_Impl )
92 : {
93 0 : impl_assign( _rArguments );
94 0 : }
95 :
96 :
97 0 : NamedValueCollection::NamedValueCollection( const Sequence< PropertyValue >& _rArguments )
98 0 : :m_pImpl( new NamedValueCollection_Impl )
99 : {
100 0 : impl_assign( _rArguments );
101 0 : }
102 :
103 :
104 0 : NamedValueCollection::NamedValueCollection( const Sequence< NamedValue >& _rArguments )
105 0 : :m_pImpl( new NamedValueCollection_Impl )
106 : {
107 0 : impl_assign( _rArguments );
108 0 : }
109 :
110 :
111 0 : NamedValueCollection::~NamedValueCollection()
112 : {
113 0 : }
114 :
115 :
116 0 : bool NamedValueCollection::canExtractFrom( ::com::sun::star::uno::Any const & i_value )
117 : {
118 0 : Type const & aValueType = i_value.getValueType();
119 0 : if ( aValueType.equals( ::cppu::UnoType< PropertyValue >::get() )
120 0 : || aValueType.equals( ::cppu::UnoType< NamedValue >::get() )
121 0 : || aValueType.equals( ::cppu::UnoType< Sequence< PropertyValue > >::get() )
122 0 : || aValueType.equals( ::cppu::UnoType< Sequence< NamedValue > >::get() )
123 : )
124 0 : return true;
125 0 : return false;
126 : }
127 :
128 :
129 0 : NamedValueCollection& NamedValueCollection::merge( const NamedValueCollection& _rAdditionalValues, bool _bOverwriteExisting )
130 : {
131 0 : for ( NamedValueRepository::const_iterator namedValue = _rAdditionalValues.m_pImpl->aValues.begin();
132 0 : namedValue != _rAdditionalValues.m_pImpl->aValues.end();
133 : ++namedValue
134 : )
135 : {
136 0 : if ( _bOverwriteExisting || !impl_has( namedValue->first ) )
137 0 : impl_put( namedValue->first, namedValue->second );
138 : }
139 :
140 0 : return *this;
141 : }
142 :
143 :
144 0 : size_t NamedValueCollection::size() const
145 : {
146 0 : return m_pImpl->aValues.size();
147 : }
148 :
149 :
150 0 : bool NamedValueCollection::empty() const
151 : {
152 0 : return m_pImpl->aValues.empty();
153 : }
154 :
155 :
156 0 : ::std::vector< OUString > NamedValueCollection::getNames() const
157 : {
158 0 : ::std::vector< OUString > aNames;
159 0 : for ( NamedValueRepository::const_iterator it = m_pImpl->aValues.begin(), end = m_pImpl->aValues.end(); it != end; ++it )
160 : {
161 0 : aNames.push_back( it->first );
162 : }
163 0 : return aNames;
164 : }
165 :
166 :
167 0 : void NamedValueCollection::impl_assign( const Any& i_rWrappedElements )
168 : {
169 0 : Sequence< NamedValue > aNamedValues;
170 0 : Sequence< PropertyValue > aPropertyValues;
171 0 : NamedValue aNamedValue;
172 0 : PropertyValue aPropertyValue;
173 :
174 0 : if ( i_rWrappedElements >>= aNamedValues )
175 0 : impl_assign( aNamedValues );
176 0 : else if ( i_rWrappedElements >>= aPropertyValues )
177 0 : impl_assign( aPropertyValues );
178 0 : else if ( i_rWrappedElements >>= aNamedValue )
179 0 : impl_assign( Sequence< NamedValue >( &aNamedValue, 1 ) );
180 0 : else if ( i_rWrappedElements >>= aPropertyValue )
181 0 : impl_assign( Sequence< PropertyValue >( &aPropertyValue, 1 ) );
182 : else
183 0 : SAL_WARN_IF( i_rWrappedElements.hasValue(), "comphelper", "NamedValueCollection::impl_assign(Any): unsupported type!" );
184 0 : }
185 :
186 :
187 0 : void NamedValueCollection::impl_assign( const Sequence< Any >& _rArguments )
188 : {
189 : {
190 0 : NamedValueRepository aEmpty;
191 0 : m_pImpl->aValues.swap( aEmpty );
192 : }
193 :
194 0 : PropertyValue aPropertyValue;
195 0 : NamedValue aNamedValue;
196 :
197 0 : const Any* pArgument = _rArguments.getConstArray();
198 0 : const Any* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
199 0 : for ( ; pArgument != pArgumentEnd; ++pArgument )
200 : {
201 0 : if ( *pArgument >>= aPropertyValue )
202 0 : m_pImpl->aValues[ aPropertyValue.Name ] = aPropertyValue.Value;
203 0 : else if ( *pArgument >>= aNamedValue )
204 0 : 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 0 : }
214 0 : }
215 :
216 :
217 0 : void NamedValueCollection::impl_assign( const Sequence< PropertyValue >& _rArguments )
218 : {
219 : {
220 0 : NamedValueRepository aEmpty;
221 0 : m_pImpl->aValues.swap( aEmpty );
222 : }
223 :
224 0 : const PropertyValue* pArgument = _rArguments.getConstArray();
225 0 : const PropertyValue* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
226 0 : for ( ; pArgument != pArgumentEnd; ++pArgument )
227 0 : m_pImpl->aValues[ pArgument->Name ] = pArgument->Value;
228 0 : }
229 :
230 :
231 0 : void NamedValueCollection::impl_assign( const Sequence< NamedValue >& _rArguments )
232 : {
233 : {
234 0 : NamedValueRepository aEmpty;
235 0 : m_pImpl->aValues.swap( aEmpty );
236 : }
237 :
238 0 : const NamedValue* pArgument = _rArguments.getConstArray();
239 0 : const NamedValue* pArgumentEnd = _rArguments.getConstArray() + _rArguments.getLength();
240 0 : for ( ; pArgument != pArgumentEnd; ++pArgument )
241 0 : m_pImpl->aValues[ pArgument->Name ] = pArgument->Value;
242 0 : }
243 :
244 :
245 0 : bool NamedValueCollection::get_ensureType( const OUString& _rValueName, void* _pValueLocation, const Type& _rExpectedValueType ) const
246 : {
247 0 : NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
248 0 : if ( pos != m_pImpl->aValues.end() )
249 : {
250 0 : if ( uno_type_assignData(
251 : _pValueLocation, _rExpectedValueType.getTypeLibType(),
252 0 : 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 0 : ) )
257 : // argument exists, and could be extracted
258 0 : 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 0 : return false;
273 : }
274 :
275 : namespace
276 : {
277 : class theEmptyDefault : public rtl::Static<Any, theEmptyDefault> {};
278 : }
279 :
280 :
281 0 : const Any& NamedValueCollection::impl_get( const OUString& _rValueName ) const
282 : {
283 0 : NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
284 0 : if ( pos != m_pImpl->aValues.end() )
285 0 : return pos->second;
286 :
287 0 : return theEmptyDefault::get();
288 : }
289 :
290 :
291 0 : bool NamedValueCollection::impl_has( const OUString& _rValueName ) const
292 : {
293 0 : NamedValueRepository::const_iterator pos = m_pImpl->aValues.find( _rValueName );
294 0 : return ( pos != m_pImpl->aValues.end() );
295 : }
296 :
297 :
298 0 : bool NamedValueCollection::impl_put( const OUString& _rValueName, const Any& _rValue )
299 : {
300 0 : bool bHas = impl_has( _rValueName );
301 0 : m_pImpl->aValues[ _rValueName ] = _rValue;
302 0 : return bHas;
303 : }
304 :
305 :
306 0 : bool NamedValueCollection::impl_remove( const OUString& _rValueName )
307 : {
308 0 : NamedValueRepository::iterator pos = m_pImpl->aValues.find( _rValueName );
309 0 : if ( pos == m_pImpl->aValues.end() )
310 0 : return false;
311 0 : m_pImpl->aValues.erase( pos );
312 0 : return true;
313 : }
314 :
315 :
316 : namespace
317 : {
318 : struct Value2PropertyValue : public ::std::unary_function< NamedValueRepository::value_type, PropertyValue >
319 : {
320 0 : PropertyValue operator()( const NamedValueRepository::value_type& _rValue )
321 : {
322 : return PropertyValue(
323 0 : _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 0 : sal_Int32 NamedValueCollection::operator >>= ( Sequence< PropertyValue >& _out_rValues ) const
338 : {
339 0 : _out_rValues.realloc( m_pImpl->aValues.size() );
340 0 : ::std::transform( m_pImpl->aValues.begin(), m_pImpl->aValues.end(), _out_rValues.getArray(), Value2PropertyValue() );
341 0 : 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: */
|