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 <com/sun/star/lang/XServiceInfo.hpp>
21 #include <com/sun/star/lang/XTypeProvider.hpp>
22 #include <cppuhelper/weakagg.hxx>
23 #include <cppuhelper/interfacecontainer.hxx>
24 #include <comphelper/propertysethelper.hxx>
25 #include <osl/mutex.hxx>
26 #include <comphelper/genericpropertyset.hxx>
27 #include <comphelper/propertysetinfo.hxx>
28 #include <comphelper/stl_types.hxx>
29 #include <comphelper/servicehelper.hxx>
32 ///////////////////////////////////////////////////////////////////////
34 using namespace ::rtl
;
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 DECLARE_STL_USTRINGACCESS_MAP( 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
, OUStringHash
,UStringEqual
> m_aListener
;
63 virtual void _setPropertyValues( const PropertyMapEntry
** ppEntries
, const Any
* pValues
) throw( UnknownPropertyException
, PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
);
64 virtual void _getPropertyValues( const PropertyMapEntry
** ppEntries
, Any
* pValue
) throw( UnknownPropertyException
, WrappedTargetException
);
67 GenericPropertySet( PropertySetInfo
* pInfo
) throw();
68 virtual ~GenericPropertySet() throw();
71 virtual Any SAL_CALL
queryAggregation( const Type
& rType
) throw( RuntimeException
);
72 virtual Any SAL_CALL
queryInterface( const Type
& rType
) throw( RuntimeException
);
73 virtual void SAL_CALL
acquire() throw();
74 virtual void SAL_CALL
release() throw();
77 virtual Sequence
< Type
> SAL_CALL
getTypes( ) throw( RuntimeException
);
78 virtual Sequence
< sal_Int8
> SAL_CALL
getImplementationId( ) throw( RuntimeException
);
81 virtual OUString SAL_CALL
getImplementationName() throw( RuntimeException
);
82 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw( RuntimeException
);
83 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw( RuntimeException
);
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
);
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
);
92 ///////////////////////////////////////////////////////////////////////
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
)
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
)
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 const OUString
aPropertyName( (*ppEntries
)->mpName
, (*ppEntries
)->mnNameLen
, RTL_TEXTENCODING_ASCII_US
);
157 OInterfaceContainerHelper
* pHelper
= m_aListener
.getContainer(aPropertyName
);
159 maAnyMap
[ aPropertyName
] = *pValues
;
163 PropertyChangeEvent aEvt
;
164 aEvt
.PropertyName
= aPropertyName
;
165 aEvt
.NewValue
= *pValues
;
167 pHelper
->notifyEach( &XPropertyChangeListener::propertyChange
, aEvt
);
176 void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry
** ppEntries
, Any
* pValue
)
177 throw( UnknownPropertyException
, WrappedTargetException
)
179 MutexGuard
aGuard( maMutex
);
183 const OUString
aPropertyName( (*ppEntries
)->mpName
, (*ppEntries
)->mnNameLen
, RTL_TEXTENCODING_ASCII_US
);
184 *pValue
= maAnyMap
[ aPropertyName
];
193 Any SAL_CALL
GenericPropertySet::queryInterface( const Type
& rType
)
194 throw( RuntimeException
)
196 return OWeakAggObject::queryInterface( rType
);
199 Any SAL_CALL
GenericPropertySet::queryAggregation( const Type
& rType
)
200 throw(RuntimeException
)
204 if( rType
== ::getCppuType((const Reference
< XServiceInfo
>*)0) )
205 aAny
<<= Reference
< XServiceInfo
>(this);
206 else if( rType
== ::getCppuType((const Reference
< XTypeProvider
>*)0) )
207 aAny
<<= Reference
< XTypeProvider
>(this);
208 else if( rType
== ::getCppuType((const Reference
< XPropertySet
>*)0) )
209 aAny
<<= Reference
< XPropertySet
>(this);
210 else if( rType
== ::getCppuType((const Reference
< XMultiPropertySet
>*)0) )
211 aAny
<<= Reference
< XMultiPropertySet
>(this);
213 aAny
<<= OWeakAggObject::queryAggregation( rType
);
218 void SAL_CALL
GenericPropertySet::acquire() throw()
220 OWeakAggObject::acquire();
223 void SAL_CALL
GenericPropertySet::release() throw()
225 OWeakAggObject::release();
228 uno::Sequence
< uno::Type
> SAL_CALL
GenericPropertySet::getTypes()
229 throw (uno::RuntimeException
)
231 uno::Sequence
< uno::Type
> aTypes( 5 );
232 uno::Type
* pTypes
= aTypes
.getArray();
234 *pTypes
++ = ::getCppuType((const uno::Reference
< XAggregation
>*)0);
235 *pTypes
++ = ::getCppuType((const uno::Reference
< XServiceInfo
>*)0);
236 *pTypes
++ = ::getCppuType((const uno::Reference
< XTypeProvider
>*)0);
237 *pTypes
++ = ::getCppuType((const uno::Reference
< XPropertySet
>*)0);
238 *pTypes
++ = ::getCppuType((const uno::Reference
< XMultiPropertySet
>*)0);
245 class theGenericPropertySetImplmentationId
: public rtl::Static
< UnoTunnelIdInit
, theGenericPropertySetImplmentationId
> {};
248 uno::Sequence
< sal_Int8
> SAL_CALL
GenericPropertySet::getImplementationId()
249 throw (uno::RuntimeException
)
251 return theGenericPropertySetImplmentationId::get().getSeq();
256 sal_Bool SAL_CALL
GenericPropertySet::supportsService( const OUString
& ServiceName
) throw(RuntimeException
)
258 Sequence
< OUString
> aSNL( getSupportedServiceNames() );
259 const OUString
* pArray
= aSNL
.getConstArray();
261 for( sal_Int32 i
= 0; i
< aSNL
.getLength(); ++i
)
262 if( pArray
[i
] == ServiceName
)
268 OUString SAL_CALL
GenericPropertySet::getImplementationName() throw( RuntimeException
)
270 return OUString( "com.sun.star.comp.comphelper.GenericPropertySet" );
273 Sequence
< OUString
> SAL_CALL
GenericPropertySet::getSupportedServiceNames( )
274 throw( RuntimeException
)
276 Sequence
< OUString
> aSNS( 1 );
277 aSNS
.getArray()[0] = "com.sun.star.beans.XPropertySet";
281 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo
* pInfo
)
283 return (XPropertySet
*)new GenericPropertySet( pInfo
);
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */