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 <osl/diagnose.h>
28 :
29 : #include <memory>
30 :
31 : class AutoOGuardArray
32 : {
33 : std::unique_ptr< osl::Guard< comphelper::SolarMutex > > * mpGuardArray;
34 :
35 : public:
36 : explicit AutoOGuardArray( sal_Int32 nNumElements );
37 : ~AutoOGuardArray();
38 :
39 0 : std::unique_ptr< osl::Guard< comphelper::SolarMutex > > & operator[] ( sal_Int32 i ) { return mpGuardArray[i]; }
40 : };
41 :
42 0 : AutoOGuardArray::AutoOGuardArray( sal_Int32 nNumElements ) : mpGuardArray(new std::unique_ptr< osl::Guard< comphelper::SolarMutex > >[nNumElements])
43 : {
44 0 : }
45 :
46 0 : AutoOGuardArray::~AutoOGuardArray()
47 : {
48 : //!! release unique_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 5838 : SlaveData::SlaveData ( ChainablePropertySet *pSlave)
63 : : mpSlave ( pSlave )
64 : , mxSlave ( pSlave )
65 5838 : , mbInit ( false )
66 : {
67 5838 : }
68 :
69 5838 : MasterPropertySet::MasterPropertySet( comphelper::MasterPropertySetInfo* pInfo, comphelper::SolarMutex* pMutex )
70 : throw()
71 : : mpInfo ( pInfo )
72 : , mpMutex ( pMutex )
73 : , mnLastId ( 0 )
74 5838 : , mxInfo ( pInfo )
75 : {
76 5838 : }
77 :
78 5838 : MasterPropertySet::~MasterPropertySet()
79 11676 : throw()
80 : {
81 5838 : SlaveMap::iterator aEnd = maSlaveMap.end(), aIter = maSlaveMap.begin();
82 17514 : while (aIter != aEnd )
83 : {
84 5838 : delete (*aIter).second;
85 5838 : ++aIter;
86 : }
87 5838 : }
88 :
89 : // XPropertySet
90 432 : Reference< XPropertySetInfo > SAL_CALL MasterPropertySet::getPropertySetInfo( )
91 : throw(RuntimeException, std::exception)
92 : {
93 432 : return mxInfo;
94 : }
95 :
96 5838 : void MasterPropertySet::registerSlave ( ChainablePropertySet *pNewSet )
97 : throw()
98 : {
99 5838 : maSlaveMap [ ++mnLastId ] = new SlaveData ( pNewSet );
100 5838 : mpInfo->add ( pNewSet->mpInfo->maMap, mnLastId );
101 5838 : }
102 :
103 62428 : 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 62428 : std::unique_ptr< osl::Guard< comphelper::SolarMutex > > xMutexGuard;
108 62428 : if (mpMutex)
109 62428 : xMutexGuard.reset( new osl::Guard< comphelper::SolarMutex >(mpMutex) );
110 :
111 62428 : PropertyDataHash::const_iterator aIter = mpInfo->maMap.find ( rPropertyName );
112 :
113 62428 : if( aIter == mpInfo->maMap.end())
114 0 : throw UnknownPropertyException( rPropertyName, static_cast< XPropertySet* >( this ) );
115 :
116 62428 : if ( (*aIter).second->mnMapId == 0 ) // 0 means it's one of ours !
117 : {
118 57329 : _preSetValues();
119 57329 : _setSingleValue( *((*aIter).second->mpInfo), rValue );
120 57329 : _postSetValues();
121 : }
122 : else
123 : {
124 5099 : ChainablePropertySet * pSlave = maSlaveMap [ (*aIter).second->mnMapId ]->mpSlave;
125 :
126 : // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
127 5099 : std::unique_ptr< osl::Guard< comphelper::SolarMutex > > xMutexGuard2;
128 5099 : if (pSlave->mpMutex)
129 5099 : xMutexGuard2.reset( new osl::Guard< comphelper::SolarMutex >(pSlave->mpMutex) );
130 :
131 5099 : pSlave->_preSetValues();
132 5099 : pSlave->_setSingleValue( *((*aIter).second->mpInfo), rValue );
133 5099 : pSlave->_postSetValues();
134 62428 : }
135 62428 : }
136 :
137 6051 : 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 6051 : std::unique_ptr< osl::Guard< comphelper::SolarMutex > > xMutexGuard;
142 6051 : if (mpMutex)
143 6051 : xMutexGuard.reset( new osl::Guard< comphelper::SolarMutex >(mpMutex) );
144 :
145 6051 : PropertyDataHash::const_iterator aIter = mpInfo->maMap.find ( rPropertyName );
146 :
147 6051 : if( aIter == mpInfo->maMap.end())
148 0 : throw UnknownPropertyException( rPropertyName, static_cast< XPropertySet* >( this ) );
149 :
150 6051 : Any aAny;
151 6051 : if ( (*aIter).second->mnMapId == 0 ) // 0 means it's one of ours !
152 : {
153 4729 : _preGetValues();
154 4729 : _getSingleValue( *((*aIter).second->mpInfo), aAny );
155 4729 : _postGetValues();
156 : }
157 : else
158 : {
159 1322 : ChainablePropertySet * pSlave = maSlaveMap [ (*aIter).second->mnMapId ]->mpSlave;
160 :
161 : // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
162 1322 : std::unique_ptr< osl::Guard< comphelper::SolarMutex > > xMutexGuard2;
163 1322 : if (pSlave->mpMutex)
164 1322 : xMutexGuard2.reset( new osl::Guard< comphelper::SolarMutex >(pSlave->mpMutex) );
165 :
166 1322 : pSlave->_preGetValues();
167 1322 : pSlave->_getSingleValue( *((*aIter).second->mpInfo), aAny );
168 1322 : pSlave->_postGetValues();
169 : }
170 6051 : 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 : std::unique_ptr< osl::Guard< comphelper::SolarMutex > > xMutexGuard;
203 0 : if (mpMutex)
204 0 : xMutexGuard.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 unique_ptr to an array of OGuards in order to have the
220 : //!! allocated memory properly freed (exception safe!).
221 : //!! Since the array itself has unique_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 : std::unique_ptr< osl::Guard< comphelper::SolarMutex > > xMutexGuard;
269 0 : if (mpMutex)
270 0 : xMutexGuard.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 unique_ptr to an array of OGuards in order to have the
285 : //!! allocated memory properly freed (exception safe!).
286 : //!! Since the array itself has unique_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 : std::unique_ptr< osl::Guard< comphelper::SolarMutex > > xMutexGuard;
370 0 : if (pSlave->mpMutex)
371 0 : xMutexGuard.reset( new osl::Guard< comphelper::SolarMutex >(pSlave->mpMutex) );
372 :
373 : // FIXME: Each of these three methods does OSL_FAIL( "you have
374 : // to implement this yourself!") and nothing else, so this
375 : // can't make much sense. Is this whole else branch in fact
376 : // dead code?
377 0 : ChainablePropertySet::_preGetPropertyState();
378 0 : ChainablePropertySet::_getPropertyState( *((*aIter).second->mpInfo), aState );
379 0 : ChainablePropertySet::_postGetPropertyState();
380 : }
381 :
382 0 : return aState;
383 : }
384 :
385 0 : Sequence< PropertyState > SAL_CALL MasterPropertySet::getPropertyStates( const Sequence< OUString >& rPropertyNames )
386 : throw(UnknownPropertyException, RuntimeException, std::exception)
387 : {
388 0 : const sal_Int32 nCount = rPropertyNames.getLength();
389 :
390 0 : Sequence< PropertyState > aStates( nCount );
391 0 : if( nCount )
392 : {
393 0 : PropertyState * pState = aStates.getArray();
394 0 : const OUString * pString = rPropertyNames.getConstArray();
395 0 : PropertyDataHash::const_iterator aEnd = mpInfo->maMap.end(), aIter;
396 0 : _preGetPropertyState();
397 :
398 0 : for ( sal_Int32 i = 0; i < nCount; ++i, ++pString, ++pState )
399 : {
400 0 : aIter = mpInfo->maMap.find ( *pString );
401 0 : if ( aIter == aEnd )
402 0 : throw UnknownPropertyException( *pString, static_cast< XPropertySet* >( this ) );
403 :
404 0 : if ( (*aIter).second->mnMapId == 0 ) // 0 means it's one of ours !
405 0 : _getPropertyState( *((*aIter).second->mpInfo), *pState );
406 : else
407 : {
408 0 : SlaveData * pSlave = maSlaveMap [ (*aIter).second->mnMapId ];
409 0 : if (!pSlave->IsInit())
410 : {
411 0 : comphelper::ChainablePropertySet::_preGetPropertyState();
412 0 : pSlave->SetInit ( true );
413 : }
414 0 : comphelper::ChainablePropertySet::_getPropertyState( *((*aIter).second->mpInfo), *pState );
415 : }
416 : }
417 0 : _postGetPropertyState();
418 0 : SlaveMap::const_iterator aSlaveIter = maSlaveMap.begin(), aSlaveEnd = maSlaveMap.end();
419 0 : while (aSlaveIter != aSlaveEnd)
420 : {
421 0 : if ( (*aSlaveIter).second->IsInit())
422 : {
423 0 : comphelper::ChainablePropertySet::_postGetPropertyState();
424 0 : (*aSlaveIter).second->SetInit ( false );
425 : }
426 0 : ++aSlaveIter;
427 : }
428 : }
429 0 : return aStates;
430 : }
431 :
432 0 : void SAL_CALL MasterPropertySet::setPropertyToDefault( const OUString& rPropertyName )
433 : throw(UnknownPropertyException, RuntimeException, std::exception)
434 : {
435 0 : PropertyDataHash::const_iterator aIter = mpInfo->maMap.find ( rPropertyName );
436 :
437 0 : if( aIter == mpInfo->maMap.end())
438 0 : throw UnknownPropertyException( rPropertyName, static_cast< XPropertySet* >( this ) );
439 0 : _setPropertyToDefault( *((*aIter).second->mpInfo) );
440 0 : }
441 :
442 0 : Any SAL_CALL MasterPropertySet::getPropertyDefault( const OUString& rPropertyName )
443 : throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
444 : {
445 0 : PropertyDataHash::const_iterator aIter = mpInfo->maMap.find ( rPropertyName );
446 :
447 0 : if( aIter == mpInfo->maMap.end())
448 0 : throw UnknownPropertyException( rPropertyName, static_cast< XPropertySet* >( this ) );
449 0 : return _getPropertyDefault( *((*aIter).second->mpInfo) );
450 : }
451 :
452 0 : void MasterPropertySet::_preGetPropertyState ()
453 : throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException )
454 : {
455 : OSL_FAIL( "you have to implement this yourself!");
456 0 : }
457 :
458 0 : void MasterPropertySet::_getPropertyState( const comphelper::PropertyInfo&, PropertyState& )
459 : throw(UnknownPropertyException )
460 : {
461 : OSL_FAIL( "you have to implement this yourself!");
462 0 : }
463 :
464 0 : void MasterPropertySet::_postGetPropertyState ()
465 : throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException )
466 : {
467 : OSL_FAIL( "you have to implement this yourself!");
468 0 : }
469 :
470 0 : void MasterPropertySet::_setPropertyToDefault( const comphelper::PropertyInfo& )
471 : throw(UnknownPropertyException )
472 : {
473 : OSL_FAIL( "you have to implement this yourself!");
474 0 : }
475 :
476 0 : Any MasterPropertySet::_getPropertyDefault( const comphelper::PropertyInfo& )
477 : throw(UnknownPropertyException, WrappedTargetException )
478 : {
479 : OSL_FAIL( "you have to implement this yourself!");
480 0 : Any aAny;
481 0 : return aAny;
482 : }
483 :
484 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|