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>
36 std::vector
<std::optional
< osl::Guard
< comphelper::SolarMutex
> >> maGuardArray
;
39 explicit AutoOGuardArray( sal_Int32 nNumElements
);
41 std::optional
< osl::Guard
< comphelper::SolarMutex
> > & operator[] ( sal_Int32 i
) { return maGuardArray
[i
]; }
46 AutoOGuardArray::AutoOGuardArray( sal_Int32 nNumElements
) : maGuardArray(nNumElements
)
51 using namespace ::comphelper
;
52 using namespace ::com::sun::star
;
53 using namespace ::com::sun::star::uno
;
54 using namespace ::com::sun::star::lang
;
55 using namespace ::com::sun::star::beans
;
58 SlaveData::SlaveData ( ChainablePropertySet
*pSlave
)
64 MasterPropertySet::MasterPropertySet( comphelper::MasterPropertySetInfo
* pInfo
, comphelper::SolarMutex
* pMutex
)
72 MasterPropertySet::~MasterPropertySet()
75 for( const auto& rSlave
: maSlaveMap
)
80 Reference
< XPropertySetInfo
> SAL_CALL
MasterPropertySet::getPropertySetInfo( )
85 void MasterPropertySet::registerSlave ( ChainablePropertySet
*pNewSet
)
88 maSlaveMap
[ ++mnLastId
] = new SlaveData ( pNewSet
);
89 mxInfo
->add ( pNewSet
->mxInfo
->maMap
, mnLastId
);
92 void SAL_CALL
MasterPropertySet::setPropertyValue( const OUString
& rPropertyName
, const Any
& rValue
)
94 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
95 std::optional
< osl::Guard
< comphelper::SolarMutex
> > xMutexGuard
;
97 xMutexGuard
.emplace( mpMutex
);
99 PropertyDataHash::const_iterator aIter
= mxInfo
->maMap
.find ( rPropertyName
);
101 if( aIter
== mxInfo
->maMap
.end())
102 throw UnknownPropertyException( rPropertyName
, static_cast< XPropertySet
* >( this ) );
104 if ( (*aIter
).second
->mnMapId
== 0 ) // 0 means it's one of ours !
107 _setSingleValue( *((*aIter
).second
->mpInfo
), rValue
);
112 ChainablePropertySet
* pSlave
= maSlaveMap
[ (*aIter
).second
->mnMapId
]->mxSlave
.get();
114 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
115 std::optional
< osl::Guard
< comphelper::SolarMutex
> > xMutexGuard2
;
117 xMutexGuard2
.emplace( pSlave
->mpMutex
);
119 pSlave
->_preSetValues();
120 pSlave
->_setSingleValue( *((*aIter
).second
->mpInfo
), rValue
);
121 pSlave
->_postSetValues();
125 Any SAL_CALL
MasterPropertySet::getPropertyValue( const OUString
& rPropertyName
)
127 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
128 std::optional
< osl::Guard
< comphelper::SolarMutex
> > xMutexGuard
;
130 xMutexGuard
.emplace( mpMutex
);
132 PropertyDataHash::const_iterator aIter
= mxInfo
->maMap
.find ( rPropertyName
);
134 if( aIter
== mxInfo
->maMap
.end())
135 throw UnknownPropertyException( rPropertyName
, static_cast< XPropertySet
* >( this ) );
138 if ( (*aIter
).second
->mnMapId
== 0 ) // 0 means it's one of ours !
141 _getSingleValue( *((*aIter
).second
->mpInfo
), aAny
);
146 ChainablePropertySet
* pSlave
= maSlaveMap
[ (*aIter
).second
->mnMapId
]->mxSlave
.get();
148 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
149 std::optional
< osl::Guard
< comphelper::SolarMutex
> > xMutexGuard2
;
151 xMutexGuard2
.emplace( pSlave
->mpMutex
);
153 pSlave
->_preGetValues();
154 pSlave
->_getSingleValue( *((*aIter
).second
->mpInfo
), aAny
);
155 pSlave
->_postGetValues();
160 void SAL_CALL
MasterPropertySet::addPropertyChangeListener( const OUString
&, const Reference
< XPropertyChangeListener
>& )
165 void SAL_CALL
MasterPropertySet::removePropertyChangeListener( const OUString
&, const Reference
< XPropertyChangeListener
>& )
170 void SAL_CALL
MasterPropertySet::addVetoableChangeListener( const OUString
&, const Reference
< XVetoableChangeListener
>& )
175 void SAL_CALL
MasterPropertySet::removeVetoableChangeListener( const OUString
&, const Reference
< XVetoableChangeListener
>& )
181 void SAL_CALL
MasterPropertySet::setPropertyValues( const Sequence
< OUString
>& aPropertyNames
, const Sequence
< Any
>& aValues
)
183 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
184 std::optional
< osl::Guard
< comphelper::SolarMutex
> > xMutexGuard
;
186 xMutexGuard
.emplace( mpMutex
);
188 const sal_Int32 nCount
= aPropertyNames
.getLength();
190 if( nCount
!= aValues
.getLength() )
191 throw IllegalArgumentException();
198 const Any
* pAny
= aValues
.getConstArray();
199 const OUString
* pString
= aPropertyNames
.getConstArray();
200 PropertyDataHash::const_iterator aEnd
= mxInfo
->maMap
.end(), aIter
;
202 //!! have a unique_ptr to an array of OGuards in order to have the
203 //!! allocated memory properly freed (exception safe!).
204 //!! Since the array itself has unique_ptrs as members we have to use a
205 //!! helper class 'AutoOGuardArray' in order to have
206 //!! the acquired locks properly released.
207 AutoOGuardArray
aOGuardArray( nCount
);
209 for ( sal_Int32 i
= 0; i
< nCount
; ++i
, ++pString
, ++pAny
)
211 aIter
= mxInfo
->maMap
.find ( *pString
);
213 throw RuntimeException( *pString
, static_cast< XPropertySet
* >( this ) );
215 if ( (*aIter
).second
->mnMapId
== 0 ) // 0 means it's one of ours !
216 _setSingleValue( *((*aIter
).second
->mpInfo
), *pAny
);
219 SlaveData
* pSlave
= maSlaveMap
[ (*aIter
).second
->mnMapId
];
220 if (!pSlave
->IsInit())
222 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
223 if (pSlave
->mxSlave
->mpMutex
)
224 aOGuardArray
[i
].emplace( pSlave
->mxSlave
->mpMutex
);
226 pSlave
->mxSlave
->_preSetValues();
227 pSlave
->SetInit ( true );
229 pSlave
->mxSlave
->_setSingleValue( *((*aIter
).second
->mpInfo
), *pAny
);
234 for( const auto& rSlave
: maSlaveMap
)
236 if( rSlave
.second
->IsInit() )
238 rSlave
.second
->mxSlave
->_postSetValues();
239 rSlave
.second
->SetInit( false );
244 Sequence
< Any
> SAL_CALL
MasterPropertySet::getPropertyValues( const Sequence
< OUString
>& aPropertyNames
)
246 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
247 std::optional
< osl::Guard
< comphelper::SolarMutex
> > xMutexGuard
;
249 xMutexGuard
.emplace( mpMutex
);
251 const sal_Int32 nCount
= aPropertyNames
.getLength();
253 Sequence
< Any
> aValues ( nCount
);
259 Any
* pAny
= aValues
.getArray();
260 const OUString
* pString
= aPropertyNames
.getConstArray();
261 PropertyDataHash::const_iterator aEnd
= mxInfo
->maMap
.end(), aIter
;
263 //!! have a unique_ptr to an array of OGuards in order to have the
264 //!! allocated memory properly freed (exception safe!).
265 //!! Since the array itself has unique_ptrs as members we have to use a
266 //!! helper class 'AutoOGuardArray' in order to have
267 //!! the acquired locks properly released.
268 AutoOGuardArray
aOGuardArray( nCount
);
270 for ( sal_Int32 i
= 0; i
< nCount
; ++i
, ++pString
, ++pAny
)
272 aIter
= mxInfo
->maMap
.find ( *pString
);
274 throw RuntimeException( *pString
, static_cast< XPropertySet
* >( this ) );
276 if ( (*aIter
).second
->mnMapId
== 0 ) // 0 means it's one of ours !
277 _getSingleValue( *((*aIter
).second
->mpInfo
), *pAny
);
280 SlaveData
* pSlave
= maSlaveMap
[ (*aIter
).second
->mnMapId
];
281 if (!pSlave
->IsInit())
283 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
284 if (pSlave
->mxSlave
->mpMutex
)
285 aOGuardArray
[i
].emplace( pSlave
->mxSlave
->mpMutex
);
287 pSlave
->mxSlave
->_preGetValues();
288 pSlave
->SetInit ( true );
290 pSlave
->mxSlave
->_getSingleValue( *((*aIter
).second
->mpInfo
), *pAny
);
295 for( const auto& rSlave
: maSlaveMap
)
297 if( rSlave
.second
->IsInit() )
299 rSlave
.second
->mxSlave
->_postSetValues();
300 rSlave
.second
->SetInit( false );
307 void SAL_CALL
MasterPropertySet::addPropertiesChangeListener( const Sequence
< OUString
>&, const Reference
< XPropertiesChangeListener
>& )
312 void SAL_CALL
MasterPropertySet::removePropertiesChangeListener( const Reference
< XPropertiesChangeListener
>& )
317 void SAL_CALL
MasterPropertySet::firePropertiesChangeEvent( const Sequence
< OUString
>&, const Reference
< XPropertiesChangeListener
>& )
323 PropertyState SAL_CALL
MasterPropertySet::getPropertyState( const OUString
& PropertyName
)
325 PropertyDataHash::const_iterator aIter
= mxInfo
->maMap
.find( PropertyName
);
326 if( aIter
== mxInfo
->maMap
.end())
327 throw UnknownPropertyException( PropertyName
, static_cast< XPropertySet
* >( this ) );
329 // 0 means it's one of ours !
330 if ( (*aIter
).second
->mnMapId
!= 0 )
332 ChainablePropertySet
* pSlave
= maSlaveMap
[ (*aIter
).second
->mnMapId
]->mxSlave
.get();
334 // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
335 std::optional
< osl::Guard
< comphelper::SolarMutex
> > xMutexGuard
;
337 xMutexGuard
.emplace( pSlave
->mpMutex
);
340 return PropertyState_AMBIGUOUS_VALUE
;
343 Sequence
< PropertyState
> SAL_CALL
MasterPropertySet::getPropertyStates( const Sequence
< OUString
>& rPropertyNames
)
345 const sal_Int32 nCount
= rPropertyNames
.getLength();
347 Sequence
< PropertyState
> aStates( nCount
);
350 PropertyState
* pState
= aStates
.getArray();
351 const OUString
* pString
= rPropertyNames
.getConstArray();
352 PropertyDataHash::const_iterator aEnd
= mxInfo
->maMap
.end(), aIter
;
354 for ( sal_Int32 i
= 0; i
< nCount
; ++i
, ++pString
, ++pState
)
356 aIter
= mxInfo
->maMap
.find ( *pString
);
358 throw UnknownPropertyException( *pString
, static_cast< XPropertySet
* >( this ) );
360 // 0 means it's one of ours !
361 if ( (*aIter
).second
->mnMapId
!= 0 )
363 SlaveData
* pSlave
= maSlaveMap
[ (*aIter
).second
->mnMapId
];
364 if (!pSlave
->IsInit())
366 pSlave
->SetInit ( true );
370 for( const auto& rSlave
: maSlaveMap
)
372 if( rSlave
.second
->IsInit() )
374 rSlave
.second
->SetInit( false );
381 void SAL_CALL
MasterPropertySet::setPropertyToDefault( const OUString
& rPropertyName
)
383 PropertyDataHash::const_iterator aIter
= mxInfo
->maMap
.find ( rPropertyName
);
385 if( aIter
== mxInfo
->maMap
.end())
386 throw UnknownPropertyException( rPropertyName
, static_cast< XPropertySet
* >( this ) );
389 Any SAL_CALL
MasterPropertySet::getPropertyDefault( const OUString
& rPropertyName
)
391 PropertyDataHash::const_iterator aIter
= mxInfo
->maMap
.find ( rPropertyName
);
393 if( aIter
== mxInfo
->maMap
.end())
394 throw UnknownPropertyException( rPropertyName
, static_cast< XPropertySet
* >( this ) );
398 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */