/src/libreoffice/comphelper/source/property/genericpropertyset.cxx
Line | Count | Source |
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> |
26 | | #include <com/sun/star/uno/XAggregation.hpp> |
27 | | #include <cppuhelper/supportsservice.hxx> |
28 | | #include <comphelper/multiinterfacecontainer4.hxx> |
29 | | #include <comphelper/propertysethelper.hxx> |
30 | | #include <mutex> |
31 | | #include <rtl/ref.hxx> |
32 | | #include <comphelper/genericpropertyset.hxx> |
33 | | #include <comphelper/propertysetinfo.hxx> |
34 | | |
35 | | using namespace ::cppu; |
36 | | using namespace ::comphelper; |
37 | | using namespace ::com::sun::star; |
38 | | using namespace ::com::sun::star::uno; |
39 | | using namespace ::com::sun::star::beans; |
40 | | using namespace ::com::sun::star::lang; |
41 | | |
42 | | namespace comphelper |
43 | | { |
44 | | namespace { |
45 | | |
46 | | class GenericPropertySet : public OWeakObject, |
47 | | public XServiceInfo, |
48 | | public XTypeProvider, |
49 | | public PropertySetHelper |
50 | | { |
51 | | private: |
52 | | std::map<OUString, Any> maAnyMap; |
53 | | std::mutex maMutex; |
54 | | comphelper::OMultiTypeInterfaceContainerHelperVar4<OUString, XPropertyChangeListener> m_aListener; |
55 | | |
56 | | protected: |
57 | | virtual void _setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues ) override; |
58 | | virtual void _getPropertyValues( const PropertyMapEntry** ppEntries, Any* pValue ) override; |
59 | | |
60 | | public: |
61 | | explicit GenericPropertySet( PropertySetInfo* pInfo ) noexcept; |
62 | | |
63 | | // XInterface |
64 | | virtual Any SAL_CALL queryInterface( const Type & rType ) override; |
65 | | virtual void SAL_CALL acquire() noexcept override; |
66 | | virtual void SAL_CALL release() noexcept override; |
67 | | |
68 | | // XTypeProvider |
69 | | virtual Sequence< Type > SAL_CALL getTypes( ) override; |
70 | | virtual Sequence< sal_Int8 > SAL_CALL getImplementationId( ) override; |
71 | | |
72 | | // XServiceInfo |
73 | | virtual OUString SAL_CALL getImplementationName() override; |
74 | | virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override; |
75 | | virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override; |
76 | | |
77 | | // XPropertySet |
78 | | virtual void SAL_CALL addPropertyChangeListener( const OUString& aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener >& xListener ) override; |
79 | | virtual void SAL_CALL removePropertyChangeListener( const OUString& aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener >& aListener ) override; |
80 | | }; |
81 | | |
82 | | } |
83 | | } |
84 | | |
85 | | |
86 | | GenericPropertySet::GenericPropertySet( PropertySetInfo* pInfo ) noexcept |
87 | 77.9k | : PropertySetHelper( pInfo ) |
88 | 77.9k | { |
89 | 77.9k | } |
90 | | |
91 | | void SAL_CALL GenericPropertySet::addPropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener ) |
92 | 0 | { |
93 | 0 | Reference < XPropertySetInfo > xInfo = getPropertySetInfo( ); |
94 | 0 | if ( !xInfo.is() ) |
95 | 0 | return; |
96 | | |
97 | 0 | std::unique_lock aGuard(maMutex); |
98 | 0 | if ( aPropertyName.isEmpty() ) |
99 | 0 | { |
100 | 0 | for (auto& prop : xInfo->getProperties()) |
101 | 0 | { |
102 | 0 | m_aListener.addInterface(aGuard, prop.Name, xListener); |
103 | 0 | } |
104 | 0 | } |
105 | 0 | else if ( xInfo->hasPropertyByName(aPropertyName) ) |
106 | 0 | m_aListener.addInterface(aGuard, aPropertyName,xListener); |
107 | 0 | else |
108 | 0 | throw UnknownPropertyException( aPropertyName, *this ); |
109 | 0 | } |
110 | | |
111 | | void SAL_CALL GenericPropertySet::removePropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener ) |
112 | 0 | { |
113 | 0 | Reference < XPropertySetInfo > xInfo = getPropertySetInfo( ); |
114 | 0 | if ( !xInfo.is() ) |
115 | 0 | return; |
116 | | |
117 | 0 | std::unique_lock aGuard(maMutex); |
118 | 0 | if ( aPropertyName.isEmpty() ) |
119 | 0 | { |
120 | 0 | for (auto& prop : xInfo->getProperties()) |
121 | 0 | { |
122 | 0 | m_aListener.removeInterface(aGuard, prop.Name, xListener); |
123 | 0 | } |
124 | 0 | } |
125 | 0 | else if ( xInfo->hasPropertyByName(aPropertyName) ) |
126 | 0 | m_aListener.removeInterface(aGuard, aPropertyName,xListener); |
127 | 0 | else |
128 | 0 | throw UnknownPropertyException( aPropertyName, *this ); |
129 | 0 | } |
130 | | |
131 | | void GenericPropertySet::_setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues ) |
132 | 81.7k | { |
133 | 81.7k | std::unique_lock aGuard(maMutex); |
134 | | |
135 | 163k | while( *ppEntries ) |
136 | 81.7k | { |
137 | 81.7k | OInterfaceContainerHelper4<XPropertyChangeListener> * pHelper = m_aListener.getContainer(aGuard, (*ppEntries)->maName); |
138 | | |
139 | 81.7k | maAnyMap[ (*ppEntries)->maName ] = *pValues; |
140 | | |
141 | 81.7k | if ( pHelper ) |
142 | 0 | { |
143 | 0 | PropertyChangeEvent aEvt; |
144 | 0 | aEvt.PropertyName = (*ppEntries)->maName; |
145 | 0 | aEvt.NewValue = *pValues; |
146 | 0 | pHelper->notifyEach( aGuard, &XPropertyChangeListener::propertyChange, aEvt ); |
147 | 0 | } |
148 | | |
149 | 81.7k | ppEntries++; |
150 | 81.7k | pValues++; |
151 | 81.7k | } |
152 | 81.7k | } |
153 | | |
154 | | void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, Any* pValue ) |
155 | 585k | { |
156 | 585k | std::unique_lock aGuard(maMutex); |
157 | | |
158 | 1.17M | while( *ppEntries ) |
159 | 585k | { |
160 | 585k | *pValue = maAnyMap[ (*ppEntries)->maName ]; |
161 | | |
162 | 585k | ppEntries++; |
163 | 585k | pValue++; |
164 | 585k | } |
165 | 585k | } |
166 | | |
167 | | // XInterface |
168 | | |
169 | | Any SAL_CALL GenericPropertySet::queryInterface( const Type & rType ) |
170 | 389k | { |
171 | 389k | Any aAny; |
172 | | |
173 | 389k | if( rType == cppu::UnoType<XServiceInfo>::get()) |
174 | 0 | aAny <<= Reference< XServiceInfo >(this); |
175 | 389k | else if( rType == cppu::UnoType<XTypeProvider>::get()) |
176 | 0 | aAny <<= Reference< XTypeProvider >(this); |
177 | 389k | else if( rType == cppu::UnoType<XPropertySet>::get()) |
178 | 77.9k | aAny <<= Reference< XPropertySet >(this); |
179 | 311k | else if( rType == cppu::UnoType<XMultiPropertySet>::get()) |
180 | 0 | aAny <<= Reference< XMultiPropertySet >(this); |
181 | 311k | else |
182 | 311k | aAny = OWeakObject::queryInterface( rType ); |
183 | | |
184 | 389k | return aAny; |
185 | 389k | } |
186 | | |
187 | | void SAL_CALL GenericPropertySet::acquire() noexcept |
188 | 612k | { |
189 | 612k | OWeakObject::acquire(); |
190 | 612k | } |
191 | | |
192 | | void SAL_CALL GenericPropertySet::release() noexcept |
193 | 612k | { |
194 | 612k | OWeakObject::release(); |
195 | 612k | } |
196 | | |
197 | | uno::Sequence< uno::Type > SAL_CALL GenericPropertySet::getTypes() |
198 | 0 | { |
199 | 0 | return uno::Sequence { |
200 | 0 | cppu::UnoType<XAggregation>::get(), |
201 | 0 | cppu::UnoType<XServiceInfo>::get(), |
202 | 0 | cppu::UnoType<XTypeProvider>::get(), |
203 | 0 | cppu::UnoType<XPropertySet>::get(), |
204 | 0 | cppu::UnoType<XMultiPropertySet>::get() }; |
205 | 0 | } |
206 | | |
207 | | uno::Sequence< sal_Int8 > SAL_CALL GenericPropertySet::getImplementationId() |
208 | 0 | { |
209 | 0 | return css::uno::Sequence<sal_Int8>(); |
210 | 0 | } |
211 | | |
212 | | // XServiceInfo |
213 | | sal_Bool SAL_CALL GenericPropertySet::supportsService( const OUString& ServiceName ) |
214 | 0 | { |
215 | 0 | return cppu::supportsService(this, ServiceName); |
216 | 0 | } |
217 | | |
218 | | OUString SAL_CALL GenericPropertySet::getImplementationName() |
219 | 0 | { |
220 | 0 | return u"com.sun.star.comp.comphelper.GenericPropertySet"_ustr; |
221 | 0 | } |
222 | | |
223 | | Sequence< OUString > SAL_CALL GenericPropertySet::getSupportedServiceNames( ) |
224 | 0 | { |
225 | 0 | return { u"com.sun.star.beans.XPropertySet"_ustr }; |
226 | 0 | } |
227 | | |
228 | | css::uno::Reference< css::beans::XPropertySet > comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo* pInfo ) |
229 | 77.9k | { |
230 | 77.9k | return static_cast<XPropertySet*>(new GenericPropertySet( pInfo )); |
231 | 77.9k | } |
232 | | |
233 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |