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/supportsservice.hxx>
28 #include <comphelper/multiinterfacecontainer4.hxx>
29 #include <comphelper/propertysethelper.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
;
47 class GenericPropertySet
: public OWeakAggObject
,
50 public PropertySetHelper
53 std::map
<OUString
, Any
> maAnyMap
;
55 comphelper::OMultiTypeInterfaceContainerHelperVar4
<OUString
, XPropertyChangeListener
> m_aListener
;
58 virtual void _setPropertyValues( const PropertyMapEntry
** ppEntries
, const Any
* pValues
) override
;
59 virtual void _getPropertyValues( const PropertyMapEntry
** ppEntries
, Any
* pValue
) override
;
62 explicit GenericPropertySet( PropertySetInfo
* pInfo
) noexcept
;
65 virtual Any SAL_CALL
queryAggregation( const Type
& rType
) override
;
66 virtual Any SAL_CALL
queryInterface( const Type
& rType
) override
;
67 virtual void SAL_CALL
acquire() noexcept override
;
68 virtual void SAL_CALL
release() noexcept override
;
71 virtual Sequence
< Type
> SAL_CALL
getTypes( ) override
;
72 virtual Sequence
< sal_Int8
> SAL_CALL
getImplementationId( ) override
;
75 virtual OUString SAL_CALL
getImplementationName() override
;
76 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
77 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
80 virtual void SAL_CALL
addPropertyChangeListener( const OUString
& aPropertyName
, const css::uno::Reference
< css::beans::XPropertyChangeListener
>& xListener
) override
;
81 virtual void SAL_CALL
removePropertyChangeListener( const OUString
& aPropertyName
, const css::uno::Reference
< css::beans::XPropertyChangeListener
>& aListener
) override
;
88 GenericPropertySet::GenericPropertySet( PropertySetInfo
* pInfo
) noexcept
89 : PropertySetHelper( pInfo
)
93 void SAL_CALL
GenericPropertySet::addPropertyChangeListener( const OUString
& aPropertyName
, const Reference
< XPropertyChangeListener
>& xListener
)
95 Reference
< XPropertySetInfo
> xInfo
= getPropertySetInfo( );
99 std::unique_lock
aGuard(maMutex
);
100 if ( aPropertyName
.isEmpty() )
102 Sequence
< Property
> aSeq
= xInfo
->getProperties();
103 const Property
* pIter
= aSeq
.getConstArray();
104 const Property
* pEnd
= pIter
+ aSeq
.getLength();
105 for( ; pIter
!= pEnd
; ++pIter
)
107 m_aListener
.addInterface(aGuard
, pIter
->Name
,xListener
);
110 else if ( xInfo
->hasPropertyByName(aPropertyName
) )
111 m_aListener
.addInterface(aGuard
, aPropertyName
,xListener
);
113 throw UnknownPropertyException( aPropertyName
, *this );
116 void SAL_CALL
GenericPropertySet::removePropertyChangeListener( const OUString
& aPropertyName
, const Reference
< XPropertyChangeListener
>& xListener
)
118 Reference
< XPropertySetInfo
> xInfo
= getPropertySetInfo( );
122 std::unique_lock
aGuard(maMutex
);
123 if ( aPropertyName
.isEmpty() )
125 Sequence
< Property
> aSeq
= xInfo
->getProperties();
126 const Property
* pIter
= aSeq
.getConstArray();
127 const Property
* pEnd
= pIter
+ aSeq
.getLength();
128 for( ; pIter
!= pEnd
; ++pIter
)
130 m_aListener
.removeInterface(aGuard
, pIter
->Name
,xListener
);
133 else if ( xInfo
->hasPropertyByName(aPropertyName
) )
134 m_aListener
.removeInterface(aGuard
, aPropertyName
,xListener
);
136 throw UnknownPropertyException( aPropertyName
, *this );
139 void GenericPropertySet::_setPropertyValues( const PropertyMapEntry
** ppEntries
, const Any
* pValues
)
141 std::unique_lock
aGuard(maMutex
);
145 OInterfaceContainerHelper4
<XPropertyChangeListener
> * pHelper
= m_aListener
.getContainer(aGuard
, (*ppEntries
)->maName
);
147 maAnyMap
[ (*ppEntries
)->maName
] = *pValues
;
151 PropertyChangeEvent aEvt
;
152 aEvt
.PropertyName
= (*ppEntries
)->maName
;
153 aEvt
.NewValue
= *pValues
;
154 pHelper
->notifyEach( aGuard
, &XPropertyChangeListener::propertyChange
, aEvt
);
162 void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry
** ppEntries
, Any
* pValue
)
164 std::unique_lock
aGuard(maMutex
);
168 *pValue
= maAnyMap
[ (*ppEntries
)->maName
];
177 Any SAL_CALL
GenericPropertySet::queryInterface( const Type
& rType
)
179 return OWeakAggObject::queryInterface( rType
);
182 Any SAL_CALL
GenericPropertySet::queryAggregation( const Type
& rType
)
186 if( rType
== cppu::UnoType
<XServiceInfo
>::get())
187 aAny
<<= Reference
< XServiceInfo
>(this);
188 else if( rType
== cppu::UnoType
<XTypeProvider
>::get())
189 aAny
<<= Reference
< XTypeProvider
>(this);
190 else if( rType
== cppu::UnoType
<XPropertySet
>::get())
191 aAny
<<= Reference
< XPropertySet
>(this);
192 else if( rType
== cppu::UnoType
<XMultiPropertySet
>::get())
193 aAny
<<= Reference
< XMultiPropertySet
>(this);
195 aAny
= OWeakAggObject::queryAggregation( rType
);
200 void SAL_CALL
GenericPropertySet::acquire() noexcept
202 OWeakAggObject::acquire();
205 void SAL_CALL
GenericPropertySet::release() noexcept
207 OWeakAggObject::release();
210 uno::Sequence
< uno::Type
> SAL_CALL
GenericPropertySet::getTypes()
212 return uno::Sequence
{
213 cppu::UnoType
<XAggregation
>::get(),
214 cppu::UnoType
<XServiceInfo
>::get(),
215 cppu::UnoType
<XTypeProvider
>::get(),
216 cppu::UnoType
<XPropertySet
>::get(),
217 cppu::UnoType
<XMultiPropertySet
>::get() };
220 uno::Sequence
< sal_Int8
> SAL_CALL
GenericPropertySet::getImplementationId()
222 return css::uno::Sequence
<sal_Int8
>();
226 sal_Bool SAL_CALL
GenericPropertySet::supportsService( const OUString
& ServiceName
)
228 return cppu::supportsService(this, ServiceName
);
231 OUString SAL_CALL
GenericPropertySet::getImplementationName()
233 return "com.sun.star.comp.comphelper.GenericPropertySet";
236 Sequence
< OUString
> SAL_CALL
GenericPropertySet::getSupportedServiceNames( )
238 return { "com.sun.star.beans.XPropertySet" };
241 css::uno::Reference
< css::beans::XPropertySet
> comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo
* pInfo
)
243 return static_cast<XPropertySet
*>(new GenericPropertySet( pInfo
));
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */