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 :
21 : #include <comphelper/MasterPropertySet.hxx>
22 : #include <comphelper/MasterPropertySetInfo.hxx>
23 : #include <comphelper/ChainablePropertySet.hxx>
24 : #include <comphelper/ChainablePropertySetInfo.hxx>
25 : #include <comphelper/solarmutex.hxx>
26 :
27 : #include <boost/scoped_ptr.hpp>
28 :
29 :
30 :
31 : class AutoOGuardArray
32 : {
33 : boost::scoped_ptr< osl::Guard< comphelper::SolarMutex > > * mpGuardArray;
34 :
35 : public:
36 : AutoOGuardArray( sal_Int32 nNumElements );
37 : ~AutoOGuardArray();
38 :
39 0 : boost::scoped_ptr< osl::Guard< comphelper::SolarMutex > > & operator[] ( sal_Int32 i ) { return mpGuardArray[i]; }
40 : };
41 :
42 0 : AutoOGuardArray::AutoOGuardArray( sal_Int32 nNumElements ) : mpGuardArray(new boost::scoped_ptr< osl::Guard< comphelper::SolarMutex > >[nNumElements])
43 : {
44 0 : }
45 :
46 0 : AutoOGuardArray::~AutoOGuardArray()
47 : {
48 : //!! release scoped_ptr's and thus the mutexes locks
49 0 : delete [] mpGuardArray;
50 :
51 0 : }
52 :
53 :
54 :
55 : using namespace ::comphelper;
56 : using namespace ::com::sun::star;
57 : using namespace ::com::sun::star::uno;
58 : using namespace ::com::sun::star::lang;
59 : using namespace ::com::sun::star::beans;
60 :
61 :
62 10172 : SlaveData::SlaveData ( ChainablePropertySet *pSlave)
63 : : mpSlave ( pSlave )
64 : , mxSlave ( pSlave )
65 10172 : , mbInit ( false )
66 : {
67 10172 : }
68 :
69 10172 : MasterPropertySet::MasterPropertySet( comphelper::MasterPropertySetInfo* pInfo, comphelper::SolarMutex* pMutex )
70 : throw()
71 : : mpInfo ( pInfo )
72 : , mpMutex ( pMutex )
73 : , mnLastId ( 0 )
74 10172 : , mxInfo ( pInfo )
75 : {
76 10172 : }
77 :
78 10172 : MasterPropertySet::~MasterPropertySet()
79 20344 : throw()
80 : {
81 10172 : SlaveMap::iterator aEnd = maSlaveMap.end(), aIter = maSlaveMap.begin();
82 30516 : while (aIter != aEnd )
83 : {
84 10172 : delete (*aIter).second;
85 10172 : ++aIter;
86 : }
87 10172 : }
88 :
89 : // XPropertySet
90 492 : Reference< XPropertySetInfo > SAL_CALL MasterPropertySet::getPropertySetInfo( )
91 : throw(RuntimeException, std::exception)
92 : {
93 492 : return mxInfo;
94 : }
95 :
96 10172 : void MasterPropertySet::registerSlave ( ChainablePropertySet *pNewSet )
97 : throw()
98 : {
99 10172 : maSlaveMap [ ++mnLastId ] = new SlaveData ( pNewSet );
100 10172 : mpInfo->add ( pNewSet->mpInfo->maMap, mnLastId );
101 10172 : }
102 :
103 96918 : void SAL_CALL MasterPropertySet::setPropertyValue( const OUString& rPropertyName, const Any& rValue )
104 : throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException, std::exception)
105 : {
106 : // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
107 96918 : boost::scoped_ptr< osl::Guard< comphelper::SolarMutex > > pMutexGuard;
108 96918 : if (mpMutex)
109 96918 : pMutexGuard.reset( new osl::Guard< comphelper::SolarMutex >(mpMutex) );
110 :
111 96918 : PropertyDataHash::const_iterator aIter = mpInfo->maMap.find ( rPropertyName );
112 :
113 96918 : if( aIter == mpInfo->maMap.end())
114 0 : throw UnknownPropertyException( rPropertyName, static_cast< XPropertySet* >( this ) );
115 :
116 96918 : if ( (*aIter).second->mnMapId == 0 ) // 0 means it's one of ours !
117 : {
118 91366 : _preSetValues();
119 91366 : _setSingleValue( *((*aIter).second->mpInfo), rValue );
120 91366 : _postSetValues();
121 : }
122 : else
123 : {
124 5552 : ChainablePropertySet * pSlave = maSlaveMap [ (*aIter).second->mnMapId ]->mpSlave;
125 :
126 : // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
127 5552 : boost::scoped_ptr< osl::Guard< comphelper::SolarMutex > > pMutexGuard2;
128 5552 : if (pSlave->mpMutex)
129 5552 : pMutexGuard2.reset( new osl::Guard< comphelper::SolarMutex >(pSlave->mpMutex) );
130 :
131 5552 : pSlave->_preSetValues();
132 5552 : pSlave->_setSingleValue( *((*aIter).second->mpInfo), rValue );
133 5552 : pSlave->_postSetValues();
134 96918 : }
135 96918 : }
136 :
137 4822 : Any SAL_CALL MasterPropertySet::getPropertyValue( const OUString& rPropertyName )
138 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
139 : {
140 : // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
141 4822 : boost::scoped_ptr< osl::Guard< comphelper::SolarMutex > > pMutexGuard;
142 4822 : if (mpMutex)
143 4822 : pMutexGuard.reset( new osl::Guard< comphelper::SolarMutex >(mpMutex) );
144 :
145 4822 : PropertyDataHash::const_iterator aIter = mpInfo->maMap.find ( rPropertyName );
146 :
147 4822 : if( aIter == mpInfo->maMap.end())
148 0 : throw UnknownPropertyException( rPropertyName, static_cast< XPropertySet* >( this ) );
149 :
150 4822 : Any aAny;
151 4822 : if ( (*aIter).second->mnMapId == 0 ) // 0 means it's one of ours !
152 : {
153 3726 : _preGetValues();
154 3726 : _getSingleValue( *((*aIter).second->mpInfo), aAny );
155 3726 : _postGetValues();
156 : }
157 : else
158 : {
159 1096 : ChainablePropertySet * pSlave = maSlaveMap [ (*aIter).second->mnMapId ]->mpSlave;
160 :
161 : // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
162 1096 : boost::scoped_ptr< osl::Guard< comphelper::SolarMutex > > pMutexGuard2;
163 1096 : if (pSlave->mpMutex)
164 1096 : pMutexGuard2.reset( new osl::Guard< comphelper::SolarMutex >(pSlave->mpMutex) );
165 :
166 1096 : pSlave->_preGetValues();
167 1096 : pSlave->_getSingleValue( *((*aIter).second->mpInfo), aAny );
168 1096 : pSlave->_postGetValues();
169 : }
170 4822 : return aAny;
171 : }
172 :
173 0 : void SAL_CALL MasterPropertySet::addPropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
174 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
175 : {
176 : // todo
177 0 : }
178 :
179 0 : void SAL_CALL MasterPropertySet::removePropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
180 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
181 : {
182 : // todo
183 0 : }
184 :
185 0 : void SAL_CALL MasterPropertySet::addVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
186 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
187 : {
188 : // todo
189 0 : }
190 :
191 0 : void SAL_CALL MasterPropertySet::removeVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
192 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
193 : {
194 : // todo
195 0 : }
196 :
197 : // XMultiPropertySet
198 0 : void SAL_CALL MasterPropertySet::setPropertyValues( const Sequence< OUString >& aPropertyNames, const Sequence< Any >& aValues )
199 : throw(PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException, std::exception)
200 : {
201 : // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
202 0 : boost::scoped_ptr< osl::Guard< comphelper::SolarMutex > > pMutexGuard;
203 0 : if (mpMutex)
204 0 : pMutexGuard.reset( new osl::Guard< comphelper::SolarMutex >(mpMutex) );
205 :
206 0 : const sal_Int32 nCount = aPropertyNames.getLength();
207 :
208 0 : if( nCount != aValues.getLength() )
209 0 : throw IllegalArgumentException();
210 :
211 0 : if( nCount )
212 : {
213 0 : _preSetValues();
214 :
215 0 : const Any * pAny = aValues.getConstArray();
216 0 : const OUString * pString = aPropertyNames.getConstArray();
217 0 : PropertyDataHash::const_iterator aEnd = mpInfo->maMap.end(), aIter;
218 :
219 : //!! have a scoped_ptr to an array of OGuards in order to have the
220 : //!! allocated memory properly freed (exception safe!).
221 : //!! Since the array itself has scoped_ptrs as members we have to use a
222 : //!! helper class 'AutoOGuardArray' in order to have
223 : //!! the acquired locks properly released.
224 0 : AutoOGuardArray aOGuardArray( nCount );
225 :
226 0 : for ( sal_Int32 i = 0; i < nCount; ++i, ++pString, ++pAny )
227 : {
228 0 : aIter = mpInfo->maMap.find ( *pString );
229 0 : if ( aIter == aEnd )
230 0 : throw RuntimeException( *pString, static_cast< XPropertySet* >( this ) );
231 :
232 0 : if ( (*aIter).second->mnMapId == 0 ) // 0 means it's one of ours !
233 0 : _setSingleValue( *((*aIter).second->mpInfo), *pAny );
234 : else
235 : {
236 0 : SlaveData * pSlave = maSlaveMap [ (*aIter).second->mnMapId ];
237 0 : if (!pSlave->IsInit())
238 : {
239 : // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
240 0 : if (pSlave->mpSlave->mpMutex)
241 0 : aOGuardArray[i].reset( new osl::Guard< comphelper::SolarMutex >(pSlave->mpSlave->mpMutex) );
242 :
243 0 : pSlave->mpSlave->_preSetValues();
244 0 : pSlave->SetInit ( true );
245 : }
246 0 : pSlave->mpSlave->_setSingleValue( *((*aIter).second->mpInfo), *pAny );
247 : }
248 : }
249 :
250 0 : _postSetValues();
251 0 : SlaveMap::const_iterator aSlaveIter = maSlaveMap.begin(), aSlaveEnd = maSlaveMap.end();
252 0 : while (aSlaveIter != aSlaveEnd)
253 : {
254 0 : if ( (*aSlaveIter).second->IsInit())
255 : {
256 0 : (*aSlaveIter).second->mpSlave->_postSetValues();
257 0 : (*aSlaveIter).second->SetInit ( false );
258 : }
259 0 : ++aSlaveIter;
260 0 : }
261 0 : }
262 0 : }
263 :
264 0 : Sequence< Any > SAL_CALL MasterPropertySet::getPropertyValues( const Sequence< OUString >& aPropertyNames )
265 : throw(RuntimeException, std::exception)
266 : {
267 : // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
268 0 : boost::scoped_ptr< osl::Guard< comphelper::SolarMutex > > pMutexGuard;
269 0 : if (mpMutex)
270 0 : pMutexGuard.reset( new osl::Guard< comphelper::SolarMutex >(mpMutex) );
271 :
272 0 : const sal_Int32 nCount = aPropertyNames.getLength();
273 :
274 0 : Sequence < Any > aValues ( nCount );
275 :
276 0 : if( nCount )
277 : {
278 0 : _preGetValues();
279 :
280 0 : Any * pAny = aValues.getArray();
281 0 : const OUString * pString = aPropertyNames.getConstArray();
282 0 : PropertyDataHash::const_iterator aEnd = mpInfo->maMap.end(), aIter;
283 :
284 : //!! have an scoped_ptr to an array of OGuards in order to have the
285 : //!! allocated memory properly freed (exception safe!).
286 : //!! Since the array itself has scoped_ptrs as members we have to use a
287 : //!! helper class 'AutoOGuardArray' in order to have
288 : //!! the acquired locks properly released.
289 0 : AutoOGuardArray aOGuardArray( nCount );
290 :
291 0 : for ( sal_Int32 i = 0; i < nCount; ++i, ++pString, ++pAny )
292 : {
293 0 : aIter = mpInfo->maMap.find ( *pString );
294 0 : if ( aIter == aEnd )
295 0 : throw RuntimeException( *pString, static_cast< XPropertySet* >( this ) );
296 :
297 0 : if ( (*aIter).second->mnMapId == 0 ) // 0 means it's one of ours !
298 0 : _getSingleValue( *((*aIter).second->mpInfo), *pAny );
299 : else
300 : {
301 0 : SlaveData * pSlave = maSlaveMap [ (*aIter).second->mnMapId ];
302 0 : if (!pSlave->IsInit())
303 : {
304 : // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
305 0 : if (pSlave->mpSlave->mpMutex)
306 0 : aOGuardArray[i].reset( new osl::Guard< comphelper::SolarMutex >(pSlave->mpSlave->mpMutex) );
307 :
308 0 : pSlave->mpSlave->_preGetValues();
309 0 : pSlave->SetInit ( true );
310 : }
311 0 : pSlave->mpSlave->_getSingleValue( *((*aIter).second->mpInfo), *pAny );
312 : }
313 : }
314 :
315 0 : _postSetValues();
316 0 : SlaveMap::const_iterator aSlaveIter = maSlaveMap.begin(), aSlaveEnd = maSlaveMap.end();
317 0 : while (aSlaveIter != aSlaveEnd)
318 : {
319 0 : if ( (*aSlaveIter).second->IsInit())
320 : {
321 0 : (*aSlaveIter).second->mpSlave->_postSetValues();
322 0 : (*aSlaveIter).second->SetInit ( false );
323 : }
324 0 : ++aSlaveIter;
325 0 : }
326 : }
327 0 : return aValues;
328 : }
329 :
330 0 : void SAL_CALL MasterPropertySet::addPropertiesChangeListener( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
331 : throw(RuntimeException, std::exception)
332 : {
333 : // todo
334 0 : }
335 :
336 0 : void SAL_CALL MasterPropertySet::removePropertiesChangeListener( const Reference< XPropertiesChangeListener >& )
337 : throw(RuntimeException, std::exception)
338 : {
339 : // todo
340 0 : }
341 :
342 0 : void SAL_CALL MasterPropertySet::firePropertiesChangeEvent( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
343 : throw(RuntimeException, std::exception)
344 : {
345 : // todo
346 0 : }
347 :
348 : // XPropertyState
349 0 : PropertyState SAL_CALL MasterPropertySet::getPropertyState( const OUString& PropertyName )
350 : throw(UnknownPropertyException, RuntimeException, std::exception)
351 : {
352 0 : PropertyDataHash::const_iterator aIter = mpInfo->maMap.find( PropertyName );
353 0 : if( aIter == mpInfo->maMap.end())
354 0 : throw UnknownPropertyException( PropertyName, static_cast< XPropertySet* >( this ) );
355 :
356 0 : PropertyState aState(PropertyState_AMBIGUOUS_VALUE);
357 :
358 0 : if ( (*aIter).second->mnMapId == 0 ) // 0 means it's one of ours !
359 : {
360 0 : _preGetPropertyState();
361 0 : _getPropertyState( *((*aIter).second->mpInfo), aState );
362 0 : _postGetPropertyState();
363 : }
364 : else
365 : {
366 0 : ChainablePropertySet * pSlave = maSlaveMap [ (*aIter).second->mnMapId ]->mpSlave;
367 :
368 : // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
369 0 : boost::scoped_ptr< osl::Guard< comphelper::SolarMutex > > pMutexGuard;
370 0 : if (pSlave->mpMutex)
371 0 : pMutexGuard.reset( new osl::Guard< comphelper::SolarMutex >(pSlave->mpMutex) );
372 :
373 0 : pSlave->_preGetPropertyState();
374 0 : pSlave->_getPropertyState( *((*aIter).second->mpInfo), aState );
375 0 : pSlave->_postGetPropertyState();
376 : }
377 :
378 0 : return aState;
379 : }
380 :
381 0 : Sequence< PropertyState > SAL_CALL MasterPropertySet::getPropertyStates( const Sequence< OUString >& rPropertyNames )
382 : throw(UnknownPropertyException, RuntimeException, std::exception)
383 : {
384 0 : const sal_Int32 nCount = rPropertyNames.getLength();
385 :
386 0 : Sequence< PropertyState > aStates( nCount );
387 0 : if( nCount )
388 : {
389 0 : PropertyState * pState = aStates.getArray();
390 0 : const OUString * pString = rPropertyNames.getConstArray();
391 0 : PropertyDataHash::const_iterator aEnd = mpInfo->maMap.end(), aIter;
392 0 : _preGetPropertyState();
393 :
394 0 : for ( sal_Int32 i = 0; i < nCount; ++i, ++pString, ++pState )
395 : {
396 0 : aIter = mpInfo->maMap.find ( *pString );
397 0 : if ( aIter == aEnd )
398 0 : throw UnknownPropertyException( *pString, static_cast< XPropertySet* >( this ) );
399 :
400 0 : if ( (*aIter).second->mnMapId == 0 ) // 0 means it's one of ours !
401 0 : _getPropertyState( *((*aIter).second->mpInfo), *pState );
402 : else
403 : {
404 0 : SlaveData * pSlave = maSlaveMap [ (*aIter).second->mnMapId ];
405 0 : if (!pSlave->IsInit())
406 : {
407 0 : pSlave->mpSlave->_preGetPropertyState();
408 0 : pSlave->SetInit ( true );
409 : }
410 0 : pSlave->mpSlave->_getPropertyState( *((*aIter).second->mpInfo), *pState );
411 : }
412 : }
413 0 : _postGetPropertyState();
414 0 : SlaveMap::const_iterator aSlaveIter = maSlaveMap.begin(), aSlaveEnd = maSlaveMap.end();
415 0 : while (aSlaveIter != aSlaveEnd)
416 : {
417 0 : if ( (*aSlaveIter).second->IsInit())
418 : {
419 0 : (*aSlaveIter).second->mpSlave->_postGetPropertyState();
420 0 : (*aSlaveIter).second->SetInit ( false );
421 : }
422 0 : ++aSlaveIter;
423 : }
424 : }
425 0 : return aStates;
426 : }
427 :
428 0 : void SAL_CALL MasterPropertySet::setPropertyToDefault( const OUString& rPropertyName )
429 : throw(UnknownPropertyException, RuntimeException, std::exception)
430 : {
431 0 : PropertyDataHash::const_iterator aIter = mpInfo->maMap.find ( rPropertyName );
432 :
433 0 : if( aIter == mpInfo->maMap.end())
434 0 : throw UnknownPropertyException( rPropertyName, static_cast< XPropertySet* >( this ) );
435 0 : _setPropertyToDefault( *((*aIter).second->mpInfo) );
436 0 : }
437 :
438 0 : Any SAL_CALL MasterPropertySet::getPropertyDefault( const OUString& rPropertyName )
439 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
440 : {
441 0 : PropertyDataHash::const_iterator aIter = mpInfo->maMap.find ( rPropertyName );
442 :
443 0 : if( aIter == mpInfo->maMap.end())
444 0 : throw UnknownPropertyException( rPropertyName, static_cast< XPropertySet* >( this ) );
445 0 : return _getPropertyDefault( *((*aIter).second->mpInfo) );
446 : }
447 :
448 0 : void MasterPropertySet::_preGetPropertyState ()
449 : throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException )
450 : {
451 : OSL_FAIL( "you have to implement this yourself!");
452 0 : }
453 :
454 0 : void MasterPropertySet::_getPropertyState( const comphelper::PropertyInfo&, PropertyState& )
455 : throw(UnknownPropertyException )
456 : {
457 : OSL_FAIL( "you have to implement this yourself!");
458 0 : }
459 :
460 0 : void MasterPropertySet::_postGetPropertyState ()
461 : throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException )
462 : {
463 : OSL_FAIL( "you have to implement this yourself!");
464 0 : }
465 :
466 0 : void MasterPropertySet::_setPropertyToDefault( const comphelper::PropertyInfo& )
467 : throw(UnknownPropertyException )
468 : {
469 : OSL_FAIL( "you have to implement this yourself!");
470 0 : }
471 :
472 0 : Any MasterPropertySet::_getPropertyDefault( const comphelper::PropertyInfo& )
473 : throw(UnknownPropertyException, WrappedTargetException )
474 : {
475 : OSL_FAIL( "you have to implement this yourself!");
476 0 : Any aAny;
477 0 : return aAny;
478 : }
479 :
480 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|