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 <comphelper/genericpropertyset.hxx>
32 #include <comphelper/propertysetinfo.hxx>
33 #include <comphelper/servicehelper.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
;
43 typedef std::map
<OUString
, Any
> GenericAnyMapImpl
;
47 struct IMPL_GenericPropertySet_MutexContainer
52 class GenericPropertySet
: public OWeakAggObject
,
55 public PropertySetHelper
,
56 private IMPL_GenericPropertySet_MutexContainer
59 GenericAnyMapImpl maAnyMap
;
60 cppu::OMultiTypeInterfaceContainerHelperVar
<OUString
> m_aListener
;
63 virtual void _setPropertyValues( const PropertyMapEntry
** ppEntries
, const Any
* pValues
) throw( UnknownPropertyException
, PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
) SAL_OVERRIDE
;
64 virtual void _getPropertyValues( const PropertyMapEntry
** ppEntries
, Any
* pValue
) throw( UnknownPropertyException
, WrappedTargetException
) SAL_OVERRIDE
;
67 GenericPropertySet( PropertySetInfo
* pInfo
) throw();
68 virtual ~GenericPropertySet() throw();
71 virtual Any SAL_CALL
queryAggregation( const Type
& rType
) throw( RuntimeException
, std::exception
) SAL_OVERRIDE
;
72 virtual Any SAL_CALL
queryInterface( const Type
& rType
) throw( RuntimeException
, std::exception
) SAL_OVERRIDE
;
73 virtual void SAL_CALL
acquire() throw() SAL_OVERRIDE
;
74 virtual void SAL_CALL
release() throw() SAL_OVERRIDE
;
77 virtual Sequence
< Type
> SAL_CALL
getTypes( ) throw( RuntimeException
, std::exception
) SAL_OVERRIDE
;
78 virtual Sequence
< sal_Int8
> SAL_CALL
getImplementationId( ) throw( RuntimeException
, std::exception
) SAL_OVERRIDE
;
81 virtual OUString SAL_CALL
getImplementationName() throw( RuntimeException
, std::exception
) SAL_OVERRIDE
;
82 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw( RuntimeException
, std::exception
) SAL_OVERRIDE
;
83 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw( RuntimeException
, std::exception
) SAL_OVERRIDE
;
86 virtual void SAL_CALL
addPropertyChangeListener( const OUString
& aPropertyName
, const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertyChangeListener
>& xListener
) throw(::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
87 virtual void SAL_CALL
removePropertyChangeListener( const OUString
& aPropertyName
, const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertyChangeListener
>& aListener
) throw(::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
94 GenericPropertySet::GenericPropertySet( PropertySetInfo
* pInfo
) throw()
95 : PropertySetHelper( pInfo
)
100 GenericPropertySet::~GenericPropertySet() throw()
103 void SAL_CALL
GenericPropertySet::addPropertyChangeListener( const OUString
& aPropertyName
, const Reference
< XPropertyChangeListener
>& xListener
) throw(UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
105 Reference
< XPropertySetInfo
> xInfo
= getPropertySetInfo( );
108 if ( aPropertyName
.isEmpty() )
110 Sequence
< Property
> aSeq
= xInfo
->getProperties();
111 const Property
* pIter
= aSeq
.getConstArray();
112 const Property
* pEnd
= pIter
+ aSeq
.getLength();
113 for( ; pIter
!= pEnd
; ++pIter
)
115 m_aListener
.addInterface(pIter
->Name
,xListener
);
118 else if ( xInfo
->hasPropertyByName(aPropertyName
) )
119 m_aListener
.addInterface(aPropertyName
,xListener
);
121 throw UnknownPropertyException( aPropertyName
, *this );
125 void SAL_CALL
GenericPropertySet::removePropertyChangeListener( const OUString
& aPropertyName
, const Reference
< XPropertyChangeListener
>& xListener
) throw(UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
127 ResettableMutexGuard
aGuard( maMutex
);
128 Reference
< XPropertySetInfo
> xInfo
= getPropertySetInfo( );
132 if ( aPropertyName
.isEmpty() )
134 Sequence
< Property
> aSeq
= xInfo
->getProperties();
135 const Property
* pIter
= aSeq
.getConstArray();
136 const Property
* pEnd
= pIter
+ aSeq
.getLength();
137 for( ; pIter
!= pEnd
; ++pIter
)
139 m_aListener
.removeInterface(pIter
->Name
,xListener
);
142 else if ( xInfo
->hasPropertyByName(aPropertyName
) )
143 m_aListener
.removeInterface(aPropertyName
,xListener
);
145 throw UnknownPropertyException( aPropertyName
, *this );
149 void GenericPropertySet::_setPropertyValues( const PropertyMapEntry
** ppEntries
, const Any
* pValues
)
150 throw(UnknownPropertyException
, PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
)
152 ResettableMutexGuard
aGuard( maMutex
);
156 OInterfaceContainerHelper
* pHelper
= m_aListener
.getContainer((*ppEntries
)->maName
);
158 maAnyMap
[ (*ppEntries
)->maName
] = *pValues
;
162 PropertyChangeEvent aEvt
;
163 aEvt
.PropertyName
= (*ppEntries
)->maName
;
164 aEvt
.NewValue
= *pValues
;
166 pHelper
->notifyEach( &XPropertyChangeListener::propertyChange
, aEvt
);
175 void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry
** ppEntries
, Any
* pValue
)
176 throw( UnknownPropertyException
, WrappedTargetException
)
178 MutexGuard
aGuard( maMutex
);
182 *pValue
= maAnyMap
[ (*ppEntries
)->maName
];
191 Any SAL_CALL
GenericPropertySet::queryInterface( const Type
& rType
)
192 throw( RuntimeException
, std::exception
)
194 return OWeakAggObject::queryInterface( rType
);
197 Any SAL_CALL
GenericPropertySet::queryAggregation( const Type
& rType
)
198 throw(RuntimeException
, std::exception
)
202 if( rType
== cppu::UnoType
<XServiceInfo
>::get())
203 aAny
<<= Reference
< XServiceInfo
>(this);
204 else if( rType
== cppu::UnoType
<XTypeProvider
>::get())
205 aAny
<<= Reference
< XTypeProvider
>(this);
206 else if( rType
== cppu::UnoType
<XPropertySet
>::get())
207 aAny
<<= Reference
< XPropertySet
>(this);
208 else if( rType
== cppu::UnoType
<XMultiPropertySet
>::get())
209 aAny
<<= Reference
< XMultiPropertySet
>(this);
211 aAny
<<= OWeakAggObject::queryAggregation( rType
);
216 void SAL_CALL
GenericPropertySet::acquire() throw()
218 OWeakAggObject::acquire();
221 void SAL_CALL
GenericPropertySet::release() throw()
223 OWeakAggObject::release();
226 uno::Sequence
< uno::Type
> SAL_CALL
GenericPropertySet::getTypes()
227 throw (uno::RuntimeException
, std::exception
)
229 uno::Sequence
< uno::Type
> aTypes( 5 );
230 uno::Type
* pTypes
= aTypes
.getArray();
232 *pTypes
++ = cppu::UnoType
<XAggregation
>::get();
233 *pTypes
++ = cppu::UnoType
<XServiceInfo
>::get();
234 *pTypes
++ = cppu::UnoType
<XTypeProvider
>::get();
235 *pTypes
++ = cppu::UnoType
<XPropertySet
>::get();
236 *pTypes
++ = cppu::UnoType
<XMultiPropertySet
>::get();
241 uno::Sequence
< sal_Int8
> SAL_CALL
GenericPropertySet::getImplementationId()
242 throw (uno::RuntimeException
, std::exception
)
244 return css::uno::Sequence
<sal_Int8
>();
248 sal_Bool SAL_CALL
GenericPropertySet::supportsService( const OUString
& ServiceName
) throw(RuntimeException
, std::exception
)
250 return cppu::supportsService(this, ServiceName
);
253 OUString SAL_CALL
GenericPropertySet::getImplementationName() throw( RuntimeException
, std::exception
)
255 return OUString( "com.sun.star.comp.comphelper.GenericPropertySet" );
258 Sequence
< OUString
> SAL_CALL
GenericPropertySet::getSupportedServiceNames( )
259 throw( RuntimeException
, std::exception
)
261 Sequence
< OUString
> aSNS( 1 );
262 aSNS
.getArray()[0] = "com.sun.star.beans.XPropertySet";
266 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo
* pInfo
)
268 return (XPropertySet
*)new GenericPropertySet( pInfo
);
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */