1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
27 #include <boost/scoped_ptr.hpp>
33 boost::scoped_ptr
< osl::Guard
< comphelper::SolarMutex
> > * mpGuardArray
;
36 AutoOGuardArray( sal_Int32 nNumElements
);
39 boost::scoped_ptr
< osl::Guard
< comphelper::SolarMutex
> > & operator[] ( sal_Int32 i
) { return mpGuardArray
[i
]; }
42 AutoOGuardArray::AutoOGuardArray( sal_Int32 nNumElements
) : mpGuardArray(new boost::scoped_ptr
< osl::Guard
< comphelper::SolarMutex
> >[nNumElements
])
46 AutoOGuardArray::~AutoOGuardArray()
48 //!! release auto_ptr's and thus the mutexes locks
49 delete [] mpGuardArray
;
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
;
62 SlaveData::SlaveData ( ChainablePropertySet
*pSlave
)
69 MasterPropertySet::MasterPropertySet( comphelper::MasterPropertySetInfo
* pInfo
, comphelper::SolarMutex
* pMutex
)
78 MasterPropertySet::~MasterPropertySet()
81 SlaveMap::iterator aEnd
= maSlaveMap
.end(), aIter
= maSlaveMap
.begin();
82 while (aIter
!= aEnd
)
84 delete (*aIter
).second
;
90 Reference
< XPropertySetInfo
> SAL_CALL
MasterPropertySet::getPropertySetInfo( )
91 throw(RuntimeException
, std::exception
)
96 void MasterPropertySet::registerSlave ( ChainablePropertySet
*pNewSet
)
99 maSlaveMap
[ ++mnLastId
] = new SlaveData ( pNewSet
);
100 mpInfo
->add ( pNewSet
->mpInfo
->maMap
, mnLastId
);
103 void SAL_CALL
MasterPropertySet::setPropertyValue( const OUString
& rPropertyName
, const Any
& rValue
)
104 throw(UnknownPropertyException
, PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
, RuntimeException
, std::exception
)
106 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
107 boost::scoped_ptr
< osl::Guard
< comphelper::SolarMutex
> > pMutexGuard
;
109 pMutexGuard
.reset( new osl::Guard
< comphelper::SolarMutex
>(mpMutex
) );
111 PropertyDataHash::const_iterator aIter
= mpInfo
->maMap
.find ( rPropertyName
);
113 if( aIter
== mpInfo
->maMap
.end())
114 throw UnknownPropertyException( rPropertyName
, static_cast< XPropertySet
* >( this ) );
116 if ( (*aIter
).second
->mnMapId
== 0 ) // 0 means it's one of ours !
119 _setSingleValue( *((*aIter
).second
->mpInfo
), rValue
);
124 ChainablePropertySet
* pSlave
= maSlaveMap
[ (*aIter
).second
->mnMapId
]->mpSlave
;
126 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
127 boost::scoped_ptr
< osl::Guard
< comphelper::SolarMutex
> > pMutexGuard2
;
129 pMutexGuard2
.reset( new osl::Guard
< comphelper::SolarMutex
>(pSlave
->mpMutex
) );
131 pSlave
->_preSetValues();
132 pSlave
->_setSingleValue( *((*aIter
).second
->mpInfo
), rValue
);
133 pSlave
->_postSetValues();
137 Any SAL_CALL
MasterPropertySet::getPropertyValue( const OUString
& rPropertyName
)
138 throw(UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
140 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
141 boost::scoped_ptr
< osl::Guard
< comphelper::SolarMutex
> > pMutexGuard
;
143 pMutexGuard
.reset( new osl::Guard
< comphelper::SolarMutex
>(mpMutex
) );
145 PropertyDataHash::const_iterator aIter
= mpInfo
->maMap
.find ( rPropertyName
);
147 if( aIter
== mpInfo
->maMap
.end())
148 throw UnknownPropertyException( rPropertyName
, static_cast< XPropertySet
* >( this ) );
151 if ( (*aIter
).second
->mnMapId
== 0 ) // 0 means it's one of ours !
154 _getSingleValue( *((*aIter
).second
->mpInfo
), aAny
);
159 ChainablePropertySet
* pSlave
= maSlaveMap
[ (*aIter
).second
->mnMapId
]->mpSlave
;
161 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
162 boost::scoped_ptr
< osl::Guard
< comphelper::SolarMutex
> > pMutexGuard2
;
164 pMutexGuard2
.reset( new osl::Guard
< comphelper::SolarMutex
>(pSlave
->mpMutex
) );
166 pSlave
->_preGetValues();
167 pSlave
->_getSingleValue( *((*aIter
).second
->mpInfo
), aAny
);
168 pSlave
->_postGetValues();
173 void SAL_CALL
MasterPropertySet::addPropertyChangeListener( const OUString
&, const Reference
< XPropertyChangeListener
>& )
174 throw(UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
179 void SAL_CALL
MasterPropertySet::removePropertyChangeListener( const OUString
&, const Reference
< XPropertyChangeListener
>& )
180 throw(UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
185 void SAL_CALL
MasterPropertySet::addVetoableChangeListener( const OUString
&, const Reference
< XVetoableChangeListener
>& )
186 throw(UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
191 void SAL_CALL
MasterPropertySet::removeVetoableChangeListener( const OUString
&, const Reference
< XVetoableChangeListener
>& )
192 throw(UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
198 void SAL_CALL
MasterPropertySet::setPropertyValues( const Sequence
< OUString
>& aPropertyNames
, const Sequence
< Any
>& aValues
)
199 throw(PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
, RuntimeException
, std::exception
)
201 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
202 boost::scoped_ptr
< osl::Guard
< comphelper::SolarMutex
> > pMutexGuard
;
204 pMutexGuard
.reset( new osl::Guard
< comphelper::SolarMutex
>(mpMutex
) );
206 const sal_Int32 nCount
= aPropertyNames
.getLength();
208 if( nCount
!= aValues
.getLength() )
209 throw IllegalArgumentException();
215 const Any
* pAny
= aValues
.getConstArray();
216 const OUString
* pString
= aPropertyNames
.getConstArray();
217 PropertyDataHash::const_iterator aEnd
= mpInfo
->maMap
.end(), aIter
;
219 //!! have an auto_ptr to an array of OGuards in order to have the
220 //!! allocated memory properly freed (exception safe!).
221 //!! Since the array itself has auto_ptrs as members we have to use a
222 //!! helper class 'AutoOGuardArray' in order to have
223 //!! the acquired locks properly released.
224 AutoOGuardArray
aOGuardArray( nCount
);
226 for ( sal_Int32 i
= 0; i
< nCount
; ++i
, ++pString
, ++pAny
)
228 aIter
= mpInfo
->maMap
.find ( *pString
);
230 throw UnknownPropertyException( *pString
, static_cast< XPropertySet
* >( this ) );
232 if ( (*aIter
).second
->mnMapId
== 0 ) // 0 means it's one of ours !
233 _setSingleValue( *((*aIter
).second
->mpInfo
), *pAny
);
236 SlaveData
* pSlave
= maSlaveMap
[ (*aIter
).second
->mnMapId
];
237 if (!pSlave
->IsInit())
239 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
240 if (pSlave
->mpSlave
->mpMutex
)
241 aOGuardArray
[i
].reset( new osl::Guard
< comphelper::SolarMutex
>(pSlave
->mpSlave
->mpMutex
) );
243 pSlave
->mpSlave
->_preSetValues();
244 pSlave
->SetInit ( true );
246 pSlave
->mpSlave
->_setSingleValue( *((*aIter
).second
->mpInfo
), *pAny
);
251 SlaveMap::const_iterator aSlaveIter
= maSlaveMap
.begin(), aSlaveEnd
= maSlaveMap
.end();
252 while (aSlaveIter
!= aSlaveEnd
)
254 if ( (*aSlaveIter
).second
->IsInit())
256 (*aSlaveIter
).second
->mpSlave
->_postSetValues();
257 (*aSlaveIter
).second
->SetInit ( false );
264 Sequence
< Any
> SAL_CALL
MasterPropertySet::getPropertyValues( const Sequence
< OUString
>& aPropertyNames
)
265 throw(RuntimeException
, std::exception
)
267 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
268 boost::scoped_ptr
< osl::Guard
< comphelper::SolarMutex
> > pMutexGuard
;
270 pMutexGuard
.reset( new osl::Guard
< comphelper::SolarMutex
>(mpMutex
) );
272 const sal_Int32 nCount
= aPropertyNames
.getLength();
274 Sequence
< Any
> aValues ( nCount
);
280 Any
* pAny
= aValues
.getArray();
281 const OUString
* pString
= aPropertyNames
.getConstArray();
282 PropertyDataHash::const_iterator aEnd
= mpInfo
->maMap
.end(), aIter
;
284 //!! have an auto_ptr to an array of OGuards in order to have the
285 //!! allocated memory properly freed (exception safe!).
286 //!! Since the array itself has auto_ptrs as members we have to use a
287 //!! helper class 'AutoOGuardArray' in order to have
288 //!! the acquired locks properly released.
289 AutoOGuardArray
aOGuardArray( nCount
);
291 for ( sal_Int32 i
= 0; i
< nCount
; ++i
, ++pString
, ++pAny
)
293 aIter
= mpInfo
->maMap
.find ( *pString
);
295 throw UnknownPropertyException( *pString
, static_cast< XPropertySet
* >( this ) );
297 if ( (*aIter
).second
->mnMapId
== 0 ) // 0 means it's one of ours !
298 _getSingleValue( *((*aIter
).second
->mpInfo
), *pAny
);
301 SlaveData
* pSlave
= maSlaveMap
[ (*aIter
).second
->mnMapId
];
302 if (!pSlave
->IsInit())
304 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
305 if (pSlave
->mpSlave
->mpMutex
)
306 aOGuardArray
[i
].reset( new osl::Guard
< comphelper::SolarMutex
>(pSlave
->mpSlave
->mpMutex
) );
308 pSlave
->mpSlave
->_preGetValues();
309 pSlave
->SetInit ( true );
311 pSlave
->mpSlave
->_getSingleValue( *((*aIter
).second
->mpInfo
), *pAny
);
316 SlaveMap::const_iterator aSlaveIter
= maSlaveMap
.begin(), aSlaveEnd
= maSlaveMap
.end();
317 while (aSlaveIter
!= aSlaveEnd
)
319 if ( (*aSlaveIter
).second
->IsInit())
321 (*aSlaveIter
).second
->mpSlave
->_postSetValues();
322 (*aSlaveIter
).second
->SetInit ( false );
330 void SAL_CALL
MasterPropertySet::addPropertiesChangeListener( const Sequence
< OUString
>&, const Reference
< XPropertiesChangeListener
>& )
331 throw(RuntimeException
, std::exception
)
336 void SAL_CALL
MasterPropertySet::removePropertiesChangeListener( const Reference
< XPropertiesChangeListener
>& )
337 throw(RuntimeException
, std::exception
)
342 void SAL_CALL
MasterPropertySet::firePropertiesChangeEvent( const Sequence
< OUString
>&, const Reference
< XPropertiesChangeListener
>& )
343 throw(RuntimeException
, std::exception
)
349 PropertyState SAL_CALL
MasterPropertySet::getPropertyState( const OUString
& PropertyName
)
350 throw(UnknownPropertyException
, RuntimeException
, std::exception
)
352 PropertyDataHash::const_iterator aIter
= mpInfo
->maMap
.find( PropertyName
);
353 if( aIter
== mpInfo
->maMap
.end())
354 throw UnknownPropertyException( PropertyName
, static_cast< XPropertySet
* >( this ) );
356 PropertyState aState
;
358 if ( (*aIter
).second
->mnMapId
== 0 ) // 0 means it's one of ours !
360 _preGetPropertyState();
361 _getPropertyState( *((*aIter
).second
->mpInfo
), aState
);
362 _postGetPropertyState();
366 ChainablePropertySet
* pSlave
= maSlaveMap
[ (*aIter
).second
->mnMapId
]->mpSlave
;
368 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
369 boost::scoped_ptr
< osl::Guard
< comphelper::SolarMutex
> > pMutexGuard
;
371 pMutexGuard
.reset( new osl::Guard
< comphelper::SolarMutex
>(pSlave
->mpMutex
) );
373 pSlave
->_preGetPropertyState();
374 pSlave
->_getPropertyState( *((*aIter
).second
->mpInfo
), aState
);
375 pSlave
->_postGetPropertyState();
381 Sequence
< PropertyState
> SAL_CALL
MasterPropertySet::getPropertyStates( const Sequence
< OUString
>& rPropertyNames
)
382 throw(UnknownPropertyException
, RuntimeException
, std::exception
)
384 const sal_Int32 nCount
= rPropertyNames
.getLength();
386 Sequence
< PropertyState
> aStates( nCount
);
389 PropertyState
* pState
= aStates
.getArray();
390 const OUString
* pString
= rPropertyNames
.getConstArray();
391 PropertyDataHash::const_iterator aEnd
= mpInfo
->maMap
.end(), aIter
;
392 _preGetPropertyState();
394 for ( sal_Int32 i
= 0; i
< nCount
; ++i
, ++pString
, ++pState
)
396 aIter
= mpInfo
->maMap
.find ( *pString
);
398 throw UnknownPropertyException( *pString
, static_cast< XPropertySet
* >( this ) );
400 if ( (*aIter
).second
->mnMapId
== 0 ) // 0 means it's one of ours !
401 _getPropertyState( *((*aIter
).second
->mpInfo
), *pState
);
404 SlaveData
* pSlave
= maSlaveMap
[ (*aIter
).second
->mnMapId
];
405 if (!pSlave
->IsInit())
407 pSlave
->mpSlave
->_preGetPropertyState();
408 pSlave
->SetInit ( true );
410 pSlave
->mpSlave
->_getPropertyState( *((*aIter
).second
->mpInfo
), *pState
);
413 _postGetPropertyState();
414 SlaveMap::const_iterator aSlaveIter
= maSlaveMap
.begin(), aSlaveEnd
= maSlaveMap
.end();
415 while (aSlaveIter
!= aSlaveEnd
)
417 if ( (*aSlaveIter
).second
->IsInit())
419 (*aSlaveIter
).second
->mpSlave
->_postGetPropertyState();
420 (*aSlaveIter
).second
->SetInit ( false );
428 void SAL_CALL
MasterPropertySet::setPropertyToDefault( const OUString
& rPropertyName
)
429 throw(UnknownPropertyException
, RuntimeException
, std::exception
)
431 PropertyDataHash::const_iterator aIter
= mpInfo
->maMap
.find ( rPropertyName
);
433 if( aIter
== mpInfo
->maMap
.end())
434 throw UnknownPropertyException( rPropertyName
, static_cast< XPropertySet
* >( this ) );
435 _setPropertyToDefault( *((*aIter
).second
->mpInfo
) );
438 Any SAL_CALL
MasterPropertySet::getPropertyDefault( const OUString
& rPropertyName
)
439 throw(UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
441 PropertyDataHash::const_iterator aIter
= mpInfo
->maMap
.find ( rPropertyName
);
443 if( aIter
== mpInfo
->maMap
.end())
444 throw UnknownPropertyException( rPropertyName
, static_cast< XPropertySet
* >( this ) );
445 return _getPropertyDefault( *((*aIter
).second
->mpInfo
) );
448 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
)
451 OSL_FAIL( "you have to implement this yourself!");
454 void MasterPropertySet::_getPropertyState( const comphelper::PropertyInfo
&, PropertyState
& )
455 throw(UnknownPropertyException
)
457 OSL_FAIL( "you have to implement this yourself!");
460 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
)
463 OSL_FAIL( "you have to implement this yourself!");
466 void MasterPropertySet::_setPropertyToDefault( const comphelper::PropertyInfo
& )
467 throw(UnknownPropertyException
)
469 OSL_FAIL( "you have to implement this yourself!");
472 Any
MasterPropertySet::_getPropertyDefault( const comphelper::PropertyInfo
& )
473 throw(UnknownPropertyException
, WrappedTargetException
)
475 OSL_FAIL( "you have to implement this yourself!");
480 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */