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 .
20 #include <sal/config.h>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/lang/XTypeProvider.hpp>
26 #include <cppuhelper/weakagg.hxx>
27 #include <cppuhelper/interfacecontainer.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <comphelper/propertysethelper.hxx>
30 #include <osl/mutex.hxx>
31 #include <rtl/ref.hxx>
32 #include <comphelper/genericpropertyset.hxx>
33 #include <comphelper/propertysetinfo.hxx>
35 using namespace ::osl
;
36 using namespace ::cppu
;
37 using namespace ::comphelper
;
38 using namespace ::com::sun::star
;
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::beans
;
41 using namespace ::com::sun::star::lang
;
45 struct IMPL_GenericPropertySet_MutexContainer
50 class GenericPropertySet
: public OWeakAggObject
,
53 public PropertySetHelper
,
54 private IMPL_GenericPropertySet_MutexContainer
57 std::map
<OUString
, Any
> maAnyMap
;
58 cppu::OMultiTypeInterfaceContainerHelperVar
<OUString
> m_aListener
;
61 virtual void _setPropertyValues( const PropertyMapEntry
** ppEntries
, const Any
* pValues
) override
;
62 virtual void _getPropertyValues( const PropertyMapEntry
** ppEntries
, Any
* pValue
) override
;
65 explicit GenericPropertySet( PropertySetInfo
* pInfo
) throw();
68 virtual Any SAL_CALL
queryAggregation( const Type
& rType
) override
;
69 virtual Any SAL_CALL
queryInterface( const Type
& rType
) override
;
70 virtual void SAL_CALL
acquire() throw() override
;
71 virtual void SAL_CALL
release() throw() override
;
74 virtual Sequence
< Type
> SAL_CALL
getTypes( ) override
;
75 virtual Sequence
< sal_Int8
> SAL_CALL
getImplementationId( ) override
;
78 virtual OUString SAL_CALL
getImplementationName() override
;
79 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
80 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
83 virtual void SAL_CALL
addPropertyChangeListener( const OUString
& aPropertyName
, const css::uno::Reference
< css::beans::XPropertyChangeListener
>& xListener
) override
;
84 virtual void SAL_CALL
removePropertyChangeListener( const OUString
& aPropertyName
, const css::uno::Reference
< css::beans::XPropertyChangeListener
>& aListener
) override
;
90 GenericPropertySet::GenericPropertySet( PropertySetInfo
* pInfo
) throw()
91 : PropertySetHelper( pInfo
)
96 void SAL_CALL
GenericPropertySet::addPropertyChangeListener( const OUString
& aPropertyName
, const Reference
< XPropertyChangeListener
>& xListener
)
98 Reference
< XPropertySetInfo
> xInfo
= getPropertySetInfo( );
101 if ( aPropertyName
.isEmpty() )
103 Sequence
< Property
> aSeq
= xInfo
->getProperties();
104 const Property
* pIter
= aSeq
.getConstArray();
105 const Property
* pEnd
= pIter
+ aSeq
.getLength();
106 for( ; pIter
!= pEnd
; ++pIter
)
108 m_aListener
.addInterface(pIter
->Name
,xListener
);
111 else if ( xInfo
->hasPropertyByName(aPropertyName
) )
112 m_aListener
.addInterface(aPropertyName
,xListener
);
114 throw UnknownPropertyException( aPropertyName
, *this );
118 void SAL_CALL
GenericPropertySet::removePropertyChangeListener( const OUString
& aPropertyName
, const Reference
< XPropertyChangeListener
>& xListener
)
120 ClearableMutexGuard
aGuard( maMutex
);
121 Reference
< XPropertySetInfo
> xInfo
= getPropertySetInfo( );
125 if ( aPropertyName
.isEmpty() )
127 Sequence
< Property
> aSeq
= xInfo
->getProperties();
128 const Property
* pIter
= aSeq
.getConstArray();
129 const Property
* pEnd
= pIter
+ aSeq
.getLength();
130 for( ; pIter
!= pEnd
; ++pIter
)
132 m_aListener
.removeInterface(pIter
->Name
,xListener
);
135 else if ( xInfo
->hasPropertyByName(aPropertyName
) )
136 m_aListener
.removeInterface(aPropertyName
,xListener
);
138 throw UnknownPropertyException( aPropertyName
, *this );
142 void GenericPropertySet::_setPropertyValues( const PropertyMapEntry
** ppEntries
, const Any
* pValues
)
144 ResettableMutexGuard
aGuard( maMutex
);
148 OInterfaceContainerHelper
* pHelper
= m_aListener
.getContainer((*ppEntries
)->maName
);
150 maAnyMap
[ (*ppEntries
)->maName
] = *pValues
;
154 PropertyChangeEvent aEvt
;
155 aEvt
.PropertyName
= (*ppEntries
)->maName
;
156 aEvt
.NewValue
= *pValues
;
158 pHelper
->notifyEach( &XPropertyChangeListener::propertyChange
, aEvt
);
167 void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry
** ppEntries
, Any
* pValue
)
169 MutexGuard
aGuard( maMutex
);
173 *pValue
= maAnyMap
[ (*ppEntries
)->maName
];
182 Any SAL_CALL
GenericPropertySet::queryInterface( const Type
& rType
)
184 return OWeakAggObject::queryInterface( rType
);
187 Any SAL_CALL
GenericPropertySet::queryAggregation( const Type
& rType
)
191 if( rType
== cppu::UnoType
<XServiceInfo
>::get())
192 aAny
<<= Reference
< XServiceInfo
>(this);
193 else if( rType
== cppu::UnoType
<XTypeProvider
>::get())
194 aAny
<<= Reference
< XTypeProvider
>(this);
195 else if( rType
== cppu::UnoType
<XPropertySet
>::get())
196 aAny
<<= Reference
< XPropertySet
>(this);
197 else if( rType
== cppu::UnoType
<XMultiPropertySet
>::get())
198 aAny
<<= Reference
< XMultiPropertySet
>(this);
200 aAny
= OWeakAggObject::queryAggregation( rType
);
205 void SAL_CALL
GenericPropertySet::acquire() throw()
207 OWeakAggObject::acquire();
210 void SAL_CALL
GenericPropertySet::release() throw()
212 OWeakAggObject::release();
215 uno::Sequence
< uno::Type
> SAL_CALL
GenericPropertySet::getTypes()
217 return uno::Sequence
{
218 cppu::UnoType
<XAggregation
>::get(),
219 cppu::UnoType
<XServiceInfo
>::get(),
220 cppu::UnoType
<XTypeProvider
>::get(),
221 cppu::UnoType
<XPropertySet
>::get(),
222 cppu::UnoType
<XMultiPropertySet
>::get() };
225 uno::Sequence
< sal_Int8
> SAL_CALL
GenericPropertySet::getImplementationId()
227 return css::uno::Sequence
<sal_Int8
>();
231 sal_Bool SAL_CALL
GenericPropertySet::supportsService( const OUString
& ServiceName
)
233 return cppu::supportsService(this, ServiceName
);
236 OUString SAL_CALL
GenericPropertySet::getImplementationName()
238 return OUString( "com.sun.star.comp.comphelper.GenericPropertySet" );
241 Sequence
< OUString
> SAL_CALL
GenericPropertySet::getSupportedServiceNames( )
243 Sequence
<OUString
> aSNS
{ "com.sun.star.beans.XPropertySet" };
247 css::uno::Reference
< css::beans::XPropertySet
> comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo
* pInfo
)
249 return static_cast<XPropertySet
*>(new GenericPropertySet( pInfo
));
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */