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((*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 aGuard.lock();
156 }
157
158 ppEntries++;
159 pValues++;
160 }
161}
162
163void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, Any* pValue )
164{
165 std::unique_lock aGuard(maMutex);
166
167 while( *ppEntries )
168 {
169 *pValue = maAnyMap[ (*ppEntries)->maName ];
170
171 ppEntries++;
172 pValue++;
173 }
174}
175
176// XInterface
177
178Any SAL_CALL GenericPropertySet::queryInterface( const Type & rType )
179{
180 return OWeakAggObject::queryInterface( rType );
181}
182
183Any SAL_CALL GenericPropertySet::queryAggregation( const Type & rType )
184{
185 Any aAny;
186
188 aAny <<= Reference< XServiceInfo >(this);
189 else if( rType == cppu::UnoType<XTypeProvider>::get())
190 aAny <<= Reference< XTypeProvider >(this);
191 else if( rType == cppu::UnoType<XPropertySet>::get())
192 aAny <<= Reference< XPropertySet >(this);
193 else if( rType == cppu::UnoType<XMultiPropertySet>::get())
194 aAny <<= Reference< XMultiPropertySet >(this);
195 else
196 aAny = OWeakAggObject::queryAggregation( rType );
197
198 return aAny;
199}
200
201void SAL_CALL GenericPropertySet::acquire() noexcept
202{
203 OWeakAggObject::acquire();
204}
205
206void SAL_CALL GenericPropertySet::release() noexcept
207{
208 OWeakAggObject::release();
209}
210
211uno::Sequence< uno::Type > SAL_CALL GenericPropertySet::getTypes()
212{
213 return uno::Sequence {
219}
220
221uno::Sequence< sal_Int8 > SAL_CALL GenericPropertySet::getImplementationId()
222{
223 return css::uno::Sequence<sal_Int8>();
224}
225
226// XServiceInfo
227sal_Bool SAL_CALL GenericPropertySet::supportsService( const OUString& ServiceName )
228{
229 return cppu::supportsService(this, ServiceName);
230}
231
232OUString SAL_CALL GenericPropertySet::getImplementationName()
233{
234 return "com.sun.star.comp.comphelper.GenericPropertySet";
235}
236
237Sequence< OUString > SAL_CALL GenericPropertySet::getSupportedServiceNames( )
238{
239 return { "com.sun.star.beans.XPropertySet" };
240}
241
242css::uno::Reference< css::beans::XPropertySet > comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo* pInfo )
243{
244 return static_cast<XPropertySet*>(new GenericPropertySet( pInfo ));
245}
246
247/* 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