1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: genericpropertyset.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <com/sun/star/lang/XServiceInfo.hpp>
34 #include <com/sun/star/lang/XTypeProvider.hpp>
35 #include <cppuhelper/weakagg.hxx>
36 #include <cppuhelper/interfacecontainer.hxx>
37 #include <comphelper/propertysethelper.hxx>
38 #include <osl/mutex.hxx>
39 #include <comphelper/genericpropertyset.hxx>
40 #include <comphelper/propertysetinfo.hxx>
41 #include <comphelper/stl_types.hxx>
42 #include <vos/mutex.hxx>
44 #include <boost/mem_fn.hpp>
45 #include <boost/bind.hpp>
46 #include <boost/utility.hpp>
48 ///////////////////////////////////////////////////////////////////////
50 using namespace ::rtl
;
51 using namespace ::osl
;
52 using namespace ::cppu
;
53 using namespace ::comphelper
;
54 using namespace ::com::sun::star
;
55 using namespace ::com::sun::star::uno
;
56 using namespace ::com::sun::star::beans
;
57 using namespace ::com::sun::star::lang
;
59 DECLARE_STL_USTRINGACCESS_MAP( Any
, GenericAnyMapImpl
);
63 struct IMPL_GenericPropertySet_MutexContainer
68 class GenericPropertySet
: public OWeakAggObject
,
71 public PropertySetHelper
,
72 private IMPL_GenericPropertySet_MutexContainer
75 GenericAnyMapImpl maAnyMap
;
76 ::cppu::OMultiTypeInterfaceContainerHelperVar
< ::rtl::OUString
,UStringHash
,UStringEqual
> m_aListener
;
79 virtual void _setPropertyValues( const PropertyMapEntry
** ppEntries
, const Any
* pValues
) throw( UnknownPropertyException
, PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
);
80 virtual void _getPropertyValues( const PropertyMapEntry
** ppEntries
, Any
* pValue
) throw( UnknownPropertyException
, WrappedTargetException
);
83 GenericPropertySet( PropertySetInfo
* pInfo
) throw();
84 virtual ~GenericPropertySet() throw();
87 virtual Any SAL_CALL
queryAggregation( const Type
& rType
) throw( RuntimeException
);
88 virtual Any SAL_CALL
queryInterface( const Type
& rType
) throw( RuntimeException
);
89 virtual void SAL_CALL
acquire() throw();
90 virtual void SAL_CALL
release() throw();
93 virtual Sequence
< Type
> SAL_CALL
getTypes( ) throw( RuntimeException
);
94 virtual Sequence
< sal_Int8
> SAL_CALL
getImplementationId( ) throw( RuntimeException
);
97 virtual rtl::OUString SAL_CALL
getImplementationName() throw( RuntimeException
);
98 virtual sal_Bool SAL_CALL
supportsService( const rtl::OUString
& ServiceName
) throw( RuntimeException
);
99 virtual Sequence
< rtl::OUString
> SAL_CALL
getSupportedServiceNames() throw( RuntimeException
);
102 virtual void SAL_CALL
addPropertyChangeListener( const ::rtl::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
);
103 virtual void SAL_CALL
removePropertyChangeListener( const ::rtl::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
);
108 ///////////////////////////////////////////////////////////////////////
110 GenericPropertySet::GenericPropertySet( PropertySetInfo
* pInfo
) throw()
111 : PropertySetHelper( pInfo
)
112 ,m_aListener(maMutex
)
116 GenericPropertySet::~GenericPropertySet() throw()
119 void SAL_CALL
GenericPropertySet::addPropertyChangeListener( const ::rtl::OUString
& aPropertyName
, const Reference
< XPropertyChangeListener
>& xListener
) throw(UnknownPropertyException
, WrappedTargetException
, RuntimeException
)
121 Reference
< XPropertySetInfo
> xInfo
= getPropertySetInfo( );
124 if ( !aPropertyName
.getLength() )
126 Sequence
< Property
> aSeq
= xInfo
->getProperties();
127 const Property
* pIter
= aSeq
.getConstArray();
128 const Property
* pEnd
= pIter
+ aSeq
.getLength();
129 for( ; pIter
!= pEnd
; ++pIter
)
131 m_aListener
.addInterface(pIter
->Name
,xListener
);
134 else if ( xInfo
->hasPropertyByName(aPropertyName
) )
135 m_aListener
.addInterface(aPropertyName
,xListener
);
137 throw UnknownPropertyException( aPropertyName
, *this );
141 void SAL_CALL
GenericPropertySet::removePropertyChangeListener( const ::rtl::OUString
& aPropertyName
, const Reference
< XPropertyChangeListener
>& xListener
) throw(UnknownPropertyException
, WrappedTargetException
, RuntimeException
)
143 ResettableMutexGuard
aGuard( maMutex
);
144 Reference
< XPropertySetInfo
> xInfo
= getPropertySetInfo( );
148 if ( !aPropertyName
.getLength() )
150 Sequence
< Property
> aSeq
= xInfo
->getProperties();
151 const Property
* pIter
= aSeq
.getConstArray();
152 const Property
* pEnd
= pIter
+ aSeq
.getLength();
153 for( ; pIter
!= pEnd
; ++pIter
)
155 m_aListener
.removeInterface(pIter
->Name
,xListener
);
158 else if ( xInfo
->hasPropertyByName(aPropertyName
) )
159 m_aListener
.removeInterface(aPropertyName
,xListener
);
161 throw UnknownPropertyException( aPropertyName
, *this );
165 void GenericPropertySet::_setPropertyValues( const PropertyMapEntry
** ppEntries
, const Any
* pValues
)
166 throw(UnknownPropertyException
, PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
)
168 ResettableMutexGuard
aGuard( maMutex
);
172 const OUString
aPropertyName( (*ppEntries
)->mpName
, (*ppEntries
)->mnNameLen
, RTL_TEXTENCODING_ASCII_US
);
173 OInterfaceContainerHelper
* pHelper
= m_aListener
.getContainer(aPropertyName
);
175 maAnyMap
[ aPropertyName
] = *pValues
;
179 PropertyChangeEvent aEvt
;
180 aEvt
.PropertyName
= aPropertyName
;
181 aEvt
.NewValue
= *pValues
;
183 pHelper
->notifyEach( &XPropertyChangeListener::propertyChange
, aEvt
);
192 void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry
** ppEntries
, Any
* pValue
)
193 throw( UnknownPropertyException
, WrappedTargetException
)
195 MutexGuard
aGuard( maMutex
);
199 const OUString
aPropertyName( (*ppEntries
)->mpName
, (*ppEntries
)->mnNameLen
, RTL_TEXTENCODING_ASCII_US
);
200 *pValue
= maAnyMap
[ aPropertyName
];
209 Any SAL_CALL
GenericPropertySet::queryInterface( const Type
& rType
)
210 throw( RuntimeException
)
212 return OWeakAggObject::queryInterface( rType
);
215 Any SAL_CALL
GenericPropertySet::queryAggregation( const Type
& rType
)
216 throw(RuntimeException
)
220 if( rType
== ::getCppuType((const Reference
< XServiceInfo
>*)0) )
221 aAny
<<= Reference
< XServiceInfo
>(this);
222 else if( rType
== ::getCppuType((const Reference
< XTypeProvider
>*)0) )
223 aAny
<<= Reference
< XTypeProvider
>(this);
224 else if( rType
== ::getCppuType((const Reference
< XPropertySet
>*)0) )
225 aAny
<<= Reference
< XPropertySet
>(this);
226 else if( rType
== ::getCppuType((const Reference
< XMultiPropertySet
>*)0) )
227 aAny
<<= Reference
< XMultiPropertySet
>(this);
229 aAny
<<= OWeakAggObject::queryAggregation( rType
);
234 void SAL_CALL
GenericPropertySet::acquire() throw()
236 OWeakAggObject::acquire();
239 void SAL_CALL
GenericPropertySet::release() throw()
241 OWeakAggObject::release();
244 uno::Sequence
< uno::Type
> SAL_CALL
GenericPropertySet::getTypes()
245 throw (uno::RuntimeException
)
247 uno::Sequence
< uno::Type
> aTypes( 5 );
248 uno::Type
* pTypes
= aTypes
.getArray();
250 *pTypes
++ = ::getCppuType((const uno::Reference
< XAggregation
>*)0);
251 *pTypes
++ = ::getCppuType((const uno::Reference
< XServiceInfo
>*)0);
252 *pTypes
++ = ::getCppuType((const uno::Reference
< XTypeProvider
>*)0);
253 *pTypes
++ = ::getCppuType((const uno::Reference
< XPropertySet
>*)0);
254 *pTypes
++ = ::getCppuType((const uno::Reference
< XMultiPropertySet
>*)0);
259 uno::Sequence
< sal_Int8
> SAL_CALL
GenericPropertySet::getImplementationId()
260 throw (uno::RuntimeException
)
262 MutexGuard
aGuard( maMutex
);
264 static uno::Sequence
< sal_Int8
> aId
;
265 if( aId
.getLength() == 0 )
268 rtl_createUuid( (sal_uInt8
*)aId
.getArray(), 0, sal_True
);
275 sal_Bool SAL_CALL
GenericPropertySet::supportsService( const OUString
& ServiceName
) throw(RuntimeException
)
277 Sequence
< OUString
> aSNL( getSupportedServiceNames() );
278 const OUString
* pArray
= aSNL
.getConstArray();
280 for( sal_Int32 i
= 0; i
< aSNL
.getLength(); ++i
)
281 if( pArray
[i
] == ServiceName
)
287 OUString SAL_CALL
GenericPropertySet::getImplementationName() throw( RuntimeException
)
289 return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.comphelper.GenericPropertySet") );
292 Sequence
< OUString
> SAL_CALL
GenericPropertySet::getSupportedServiceNames( )
293 throw( RuntimeException
)
295 Sequence
< OUString
> aSNS( 1 );
296 aSNS
.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.beans.XPropertySet" ));
300 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo
* pInfo
)
302 return (XPropertySet
*)new GenericPropertySet( pInfo
);