LibreOffice Module comphelper (master) 1
genericpropertyset.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <map>
23
24#include <com/sun/star/lang/XServiceInfo.hpp>
25#include <com/sun/star/lang/XTypeProvider.hpp>
30#include <mutex>
31#include <rtl/ref.hxx>
34
35using namespace ::osl;
36using namespace ::cppu;
37using namespace ::comphelper;
38using namespace ::com::sun::star;
39using namespace ::com::sun::star::uno;
40using namespace ::com::sun::star::beans;
41using namespace ::com::sun::star::lang;
42
43namespace comphelper
44{
45 namespace {
46
47 class GenericPropertySet : public OWeakAggObject,
48 public XServiceInfo,
49 public XTypeProvider,
50 public PropertySetHelper
51 {
52 private:
53 std::map<OUString, Any> maAnyMap;
56
57 protected:
58 virtual void _setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues ) override;
59 virtual void _getPropertyValues( const PropertyMapEntry** ppEntries, Any* pValue ) override;
60
61 public:
62 explicit GenericPropertySet( PropertySetInfo* pInfo ) noexcept;
63
64 // XInterface
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;
69
70 // XTypeProvider
71 virtual Sequence< Type > SAL_CALL getTypes( ) override;
72 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId( ) override;
73
74 // XServiceInfo
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;
78
79 // XPropertySet
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;
82 };
83
84 }
85}
86
87
88GenericPropertySet::GenericPropertySet( PropertySetInfo* pInfo ) noexcept
89: PropertySetHelper( pInfo )
90{
91}
92
93void SAL_CALL GenericPropertySet::addPropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener )
94{
95 Reference < XPropertySetInfo > xInfo = getPropertySetInfo( );
96 if ( !xInfo.is() )
97 return;
98
99 std::unique_lock aGuard(maMutex);
100 if ( aPropertyName.isEmpty() )
101 {
102 Sequence< Property> aSeq = xInfo->getProperties();
103 const Property* pIter = aSeq.getConstArray();
104 const Property* pEnd = pIter + aSeq.getLength();
105 for( ; pIter != pEnd ; ++pIter)
106 {
107 m_aListener.addInterface(aGuard, pIter->Name,xListener);
108 }
109 }
110 else if ( xInfo->hasPropertyByName(aPropertyName) )
111 m_aListener.addInterface(aGuard, aPropertyName,xListener);
112 else
113 throw UnknownPropertyException( aPropertyName, *this );
114}
115
116void SAL_CALL GenericPropertySet::removePropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener )
117{
118 Reference < XPropertySetInfo > xInfo = getPropertySetInfo( );
119 if ( !xInfo.is() )
120 return;
121
122 std::unique_lock aGuard(maMutex);
123 if ( aPropertyName.isEmpty() )
124 {
125 Sequence< Property> aSeq = xInfo->getProperties();
126 const Property* pIter = aSeq.getConstArray();
127 const Property* pEnd = pIter + aSeq.getLength();
128 for( ; pIter != pEnd ; ++pIter)
129 {
130 m_aListener.removeInterface(aGuard, pIter->Name,xListener);
131 }
132 }
133 else if ( xInfo->hasPropertyByName(aPropertyName) )
134 m_aListener.removeInterface(aGuard, aPropertyName,xListener);
135 else
136 throw UnknownPropertyException( aPropertyName, *this );
137}
138
139void GenericPropertySet::_setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues )
140{
141 std::unique_lock aGuard(maMutex);
142
143 while( *ppEntries )
144 {
145 OInterfaceContainerHelper4<XPropertyChangeListener> * pHelper = m_aListener.getContainer(aGuard, (*ppEntries)->maName);
146
147 maAnyMap[ (*ppEntries)->maName ] = *pValues;
148
149 if ( pHelper )
150 {
151 PropertyChangeEvent aEvt;
152 aEvt.PropertyName = (*ppEntries)->maName;
153 aEvt.NewValue = *pValues;
154 pHelper->notifyEach( aGuard, &XPropertyChangeListener::propertyChange, aEvt );
155 }
156
157 ppEntries++;
158 pValues++;
159 }
160}
161
162void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, Any* pValue )
163{
164 std::unique_lock aGuard(maMutex);
165
166 while( *ppEntries )
167 {
168 *pValue = maAnyMap[ (*ppEntries)->maName ];
169
170 ppEntries++;
171 pValue++;
172 }
173}
174
175// XInterface
176
177Any SAL_CALL GenericPropertySet::queryInterface( const Type & rType )
178{
179 return OWeakAggObject::queryInterface( rType );
180}
181
182Any SAL_CALL GenericPropertySet::queryAggregation( const Type & rType )
183{
184 Any aAny;
185
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);
194 else
195 aAny = OWeakAggObject::queryAggregation( rType );
196
197 return aAny;
198}
199
200void SAL_CALL GenericPropertySet::acquire() noexcept
201{
202 OWeakAggObject::acquire();
203}
204
205void SAL_CALL GenericPropertySet::release() noexcept
206{
207 OWeakAggObject::release();
208}
209
210uno::Sequence< uno::Type > SAL_CALL GenericPropertySet::getTypes()
211{
212 return uno::Sequence {
218}
219
220uno::Sequence< sal_Int8 > SAL_CALL GenericPropertySet::getImplementationId()
221{
222 return css::uno::Sequence<sal_Int8>();
223}
224
225// XServiceInfo
226sal_Bool SAL_CALL GenericPropertySet::supportsService( const OUString& ServiceName )
227{
228 return cppu::supportsService(this, ServiceName);
229}
230
231OUString SAL_CALL GenericPropertySet::getImplementationName()
232{
233 return "com.sun.star.comp.comphelper.GenericPropertySet";
234}
235
236Sequence< OUString > SAL_CALL GenericPropertySet::getSupportedServiceNames( )
237{
238 return { "com.sun.star.beans.XPropertySet" };
239}
240
241css::uno::Reference< css::beans::XPropertySet > comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo* pInfo )
242{
243 return static_cast<XPropertySet*>(new GenericPropertySet( pInfo ));
244}
245
246/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const PropertyValue * pValues
A helper class to store interface references of different types.
this class implements a XPropertySetInfo that is initialized with arrays of PropertyMapEntry.
css::uno::Type const & get()
std::mutex maMutex
std::map< OUString, Any > maAnyMap
comphelper::OMultiTypeInterfaceContainerHelperVar4< OUString, XPropertyChangeListener > m_aListener
Sequence< sal_Int8 > aSeq
class SAL_NO_VTABLE XPropertySet
COMPHELPER_DLLPUBLIC css::uno::Reference< css::beans::XPropertySet > GenericPropertySet_CreateInstance(PropertySetInfo *pInfo)
Type
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
std::pair< OUString const, DataAccessDescriptorProperty > PropertyMapEntry
std::mutex mutex
Definition: random.cxx:41
unsigned char sal_Bool
signed char sal_Int8