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>
26 #include <cppuhelper/weakagg.hxx>
30 #include <osl/mutex.hxx>
31 #include <rtl/ref.hxx>
34 
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;
42 
43 namespace comphelper
44 {
46  {
47  Mutex maMutex;
48  };
49 
50  class GenericPropertySet : public OWeakAggObject,
51  public XServiceInfo,
52  public XTypeProvider,
53  public PropertySetHelper,
55  {
56  private:
57  std::map<OUString, Any> maAnyMap;
59 
60  protected:
61  virtual void _setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues ) override;
62  virtual void _getPropertyValues( const PropertyMapEntry** ppEntries, Any* pValue ) override;
63 
64  public:
65  explicit GenericPropertySet( PropertySetInfo* pInfo ) throw();
66 
67  // XInterface
68  virtual Any SAL_CALL queryAggregation( const Type & rType ) override;
69  virtual Any SAL_CALL queryInterface( const Type & rType ) override;
70  virtual void SAL_CALL acquire() throw() override;
71  virtual void SAL_CALL release() throw() override;
72 
73  // XTypeProvider
74  virtual Sequence< Type > SAL_CALL getTypes( ) override;
75  virtual Sequence< sal_Int8 > SAL_CALL getImplementationId( ) override;
76 
77  // XServiceInfo
78  virtual OUString SAL_CALL getImplementationName() override;
79  virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
80  virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
81 
82  // XPropertySet
83  virtual void SAL_CALL addPropertyChangeListener( const OUString& aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener >& xListener ) override;
84  virtual void SAL_CALL removePropertyChangeListener( const OUString& aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener >& aListener ) override;
85  };
86 
87 }
88 
89 
91 : PropertySetHelper( pInfo )
92 ,m_aListener(maMutex)
93 {
94 }
95 
96 void SAL_CALL GenericPropertySet::addPropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener )
97 {
98  Reference < XPropertySetInfo > xInfo = getPropertySetInfo( );
99  if ( xInfo.is() )
100  {
101  if ( aPropertyName.isEmpty() )
102  {
103  Sequence< Property> aSeq = xInfo->getProperties();
104  const Property* pIter = aSeq.getConstArray();
105  const Property* pEnd = pIter + aSeq.getLength();
106  for( ; pIter != pEnd ; ++pIter)
107  {
108  m_aListener.addInterface(pIter->Name,xListener);
109  }
110  }
111  else if ( xInfo->hasPropertyByName(aPropertyName) )
112  m_aListener.addInterface(aPropertyName,xListener);
113  else
114  throw UnknownPropertyException( aPropertyName, *this );
115  }
116 }
117 
118 void SAL_CALL GenericPropertySet::removePropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener )
119 {
120  ClearableMutexGuard aGuard( maMutex );
121  Reference < XPropertySetInfo > xInfo = getPropertySetInfo( );
122  aGuard.clear();
123  if ( xInfo.is() )
124  {
125  if ( aPropertyName.isEmpty() )
126  {
127  Sequence< Property> aSeq = xInfo->getProperties();
128  const Property* pIter = aSeq.getConstArray();
129  const Property* pEnd = pIter + aSeq.getLength();
130  for( ; pIter != pEnd ; ++pIter)
131  {
132  m_aListener.removeInterface(pIter->Name,xListener);
133  }
134  }
135  else if ( xInfo->hasPropertyByName(aPropertyName) )
136  m_aListener.removeInterface(aPropertyName,xListener);
137  else
138  throw UnknownPropertyException( aPropertyName, *this );
139  }
140 }
141 
142 void GenericPropertySet::_setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues )
143 {
144  ResettableMutexGuard aGuard( maMutex );
145 
146  while( *ppEntries )
147  {
148  OInterfaceContainerHelper * pHelper = m_aListener.getContainer((*ppEntries)->maName);
149 
150  maAnyMap[ (*ppEntries)->maName ] = *pValues;
151 
152  if ( pHelper )
153  {
154  PropertyChangeEvent aEvt;
155  aEvt.PropertyName = (*ppEntries)->maName;
156  aEvt.NewValue = *pValues;
157  aGuard.clear();
158  pHelper->notifyEach( &XPropertyChangeListener::propertyChange, aEvt );
159  aGuard.reset();
160  }
161 
162  ppEntries++;
163  pValues++;
164  }
165 }
166 
168 {
169  MutexGuard aGuard( maMutex );
170 
171  while( *ppEntries )
172  {
173  *pValue = maAnyMap[ (*ppEntries)->maName ];
174 
175  ppEntries++;
176  pValue++;
177  }
178 }
179 
180 // XInterface
181 
182 Any SAL_CALL GenericPropertySet::queryInterface( const Type & rType )
183 {
184  return OWeakAggObject::queryInterface( rType );
185 }
186 
188 {
189  Any aAny;
190 
191  if( rType == cppu::UnoType<XServiceInfo>::get())
192  aAny <<= Reference< XServiceInfo >(this);
193  else if( rType == cppu::UnoType<XTypeProvider>::get())
194  aAny <<= Reference< XTypeProvider >(this);
195  else if( rType == cppu::UnoType<XPropertySet>::get())
196  aAny <<= Reference< XPropertySet >(this);
197  else if( rType == cppu::UnoType<XMultiPropertySet>::get())
198  aAny <<= Reference< XMultiPropertySet >(this);
199  else
200  aAny = OWeakAggObject::queryAggregation( rType );
201 
202  return aAny;
203 }
204 
205 void SAL_CALL GenericPropertySet::acquire() throw()
206 {
207  OWeakAggObject::acquire();
208 }
209 
210 void SAL_CALL GenericPropertySet::release() throw()
211 {
212  OWeakAggObject::release();
213 }
214 
215 uno::Sequence< uno::Type > SAL_CALL GenericPropertySet::getTypes()
216 {
217  return uno::Sequence {
223 }
224 
225 uno::Sequence< sal_Int8 > SAL_CALL GenericPropertySet::getImplementationId()
226 {
227  return css::uno::Sequence<sal_Int8>();
228 }
229 
230 // XServiceInfo
231 sal_Bool SAL_CALL GenericPropertySet::supportsService( const OUString& ServiceName )
232 {
233  return cppu::supportsService(this, ServiceName);
234 }
235 
237 {
238  return "com.sun.star.comp.comphelper.GenericPropertySet";
239 }
240 
242 {
243  Sequence<OUString> aSNS { "com.sun.star.beans.XPropertySet" };
244  return aSNS;
245 }
246 
247 css::uno::Reference< css::beans::XPropertySet > comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo* pInfo )
248 {
249  return static_cast<XPropertySet*>(new GenericPropertySet( pInfo ));
250 }
251 
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
virtual Any SAL_CALL queryInterface(const Type &rType) override
virtual void _getPropertyValues(const PropertyMapEntry **ppEntries, Any *pValue) override
virtual void _setPropertyValues(const PropertyMapEntry **ppEntries, const Any *pValues) override
signed char sal_Int8
virtual Sequence< Type > SAL_CALL getTypes() override
virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() override
virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
sal_Int32 SAL_CALL removeInterface(const key &rKey, const css::uno::Reference< css::uno::XInterface > &rxIFace)
virtual void SAL_CALL removePropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &aListener) override
this class implements a XPropertySetInfo that is initialized with arrays of PropertyMapEntry.
class SAL_NO_VTABLE XPropertySet
COMPHELPER_DLLPUBLIC css::uno::Reference< css::beans::XPropertySet > GenericPropertySet_CreateInstance(PropertySetInfo *pInfo)
virtual OUString SAL_CALL getImplementationName() override
unsigned char sal_Bool
virtual void SAL_CALL addPropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &xListener) override
css::uno::Type const & get()
GenericPropertySet(PropertySetInfo *pInfo)
virtual void SAL_CALL release() override
sal_Int32 SAL_CALL addInterface(const key &rKey, const css::uno::Reference< css::uno::XInterface > &r)
virtual void SAL_CALL acquire() override
cppu::OMultiTypeInterfaceContainerHelperVar< OUString > m_aListener
std::map< OUString, Any > maAnyMap
OInterfaceContainerHelper *SAL_CALL getContainer(const key &) const
virtual Any SAL_CALL queryAggregation(const Type &rType) override
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override