LibreOffice Module forms (master) 1
propertysetbase.hxx
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#pragma once
20
22#include <rtl/ref.hxx>
24
25// include for inlined helper function below
26#include <com/sun/star/beans/PropertyAttribute.hpp>
27
28#include <map>
29#include <memory>
30
31namespace com::sun::star::uno {
32 class Any;
33 class RuntimeException;
34 template<class T> class Sequence;
35}
36
40{
41protected:
43 virtual ~PropertyAccessorBase() override;
44
45public:
46
47 virtual bool approveValue( const css::uno::Any& rValue ) const = 0;
48 virtual void setValue( const css::uno::Any& rValue ) = 0;
49 virtual void getValue( css::uno::Any& rValue ) const = 0;
50 virtual bool isWriteable() const = 0;
51};
52
53
56template< typename CLASS, typename VALUE, class WRITER, class READER >
58{
59public:
60 typedef WRITER Writer;
61 typedef READER Reader;
62
63private:
67
68public:
69 GenericPropertyAccessor( CLASS* pInstance, Writer pWriter, Reader pReader )
70 :m_pInstance( pInstance )
71 ,m_pWriter( pWriter )
72 ,m_pReader( pReader )
73 {
74 }
75
76 virtual bool approveValue( const css::uno::Any& rValue ) const override
77 {
78 VALUE aVal;
79 return ( rValue >>= aVal );
80 }
81
82 virtual void setValue( const css::uno::Any& rValue ) override
83 {
84 VALUE aTypedVal = VALUE();
85 OSL_VERIFY( rValue >>= aTypedVal );
86 (m_pInstance->*m_pWriter)( aTypedVal );
87 }
88
89 virtual void getValue( css::uno::Any& rValue ) const override
90 {
91 rValue = css::uno::Any( (m_pInstance->*m_pReader)() );
92 }
93
94 virtual bool isWriteable() const override
95 {
96 return m_pWriter != nullptr;
97 }
98};
99
102template< typename CLASS, typename VALUE >
104 :public GenericPropertyAccessor < CLASS
105 , VALUE
106 , void (CLASS::*)( const VALUE& )
107 , VALUE (CLASS::*)() const
108 >
109{
110protected:
111 typedef void (CLASS::*Writer)( const VALUE& );
112 typedef VALUE (CLASS::*Reader)() const;
113public:
114 DirectPropertyAccessor( CLASS* pInstance, Writer pWriter, Reader pReader )
115 :GenericPropertyAccessor< CLASS, VALUE, Writer, Reader >( pInstance, pWriter, pReader )
116 {
117 }
118};
119
122template< typename CLASS >
124 :public GenericPropertyAccessor < CLASS
125 , bool
126 , void (CLASS::*)( bool )
127 , bool (CLASS::*)() const
128 >
129{
130protected:
131 typedef void (CLASS::*Writer)( bool );
132 typedef bool (CLASS::*Reader)() const;
133public:
134 BooleanPropertyAccessor( CLASS* pInstance, Writer pWriter, Reader pReader )
135 :GenericPropertyAccessor< CLASS, bool, Writer, Reader >( pInstance, pWriter, pReader )
136 {
137 }
138};
139
142template< typename CLASS, typename VALUE >
144 :public GenericPropertyAccessor < CLASS
145 , VALUE
146 , void (SAL_CALL CLASS::*)( const VALUE& )
147 , VALUE (SAL_CALL CLASS::*)()
148 >
149{
150protected:
151 typedef void (SAL_CALL CLASS::*Writer)( const VALUE& );
152 typedef VALUE (SAL_CALL CLASS::*Reader)();
153public:
154 APIPropertyAccessor( CLASS* pInstance, Writer pWriter, Reader pReader )
155 :GenericPropertyAccessor< CLASS, VALUE, Writer, Reader >( pInstance, pWriter, pReader )
156 {
157 }
158};
159
172class PropertySetBase : public ::comphelper::OStatefulPropertySet
173{
174private:
175 typedef ::std::map< const sal_Int32, ::rtl::Reference< PropertyAccessorBase > > PropertyAccessors;
176 typedef ::std::vector< css::beans::Property > PropertyArray;
177 typedef ::std::map< const sal_Int32, css::uno::Any > PropertyValueCache;
178
180 std::unique_ptr<cppu::IPropertyArrayHelper> m_pProperties;
183
184protected:
186 virtual ~PropertySetBase() override;
187
199 void registerProperty(
200 const css::beans::Property& rProperty,
201 const ::rtl::Reference< PropertyAccessorBase >& rAccessor
202 );
203
228 void notifyAndCachePropertyValue( sal_Int32 nHandle );
229
248 void initializePropertyValueCache( sal_Int32 nHandle );
249
251 virtual sal_Bool SAL_CALL convertFastPropertyValue( css::uno::Any& rConvertedValue, css::uno::Any& rOldValue, sal_Int32 nHandle, const css::uno::Any& rValue ) override;
252 virtual void SAL_CALL setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const css::uno::Any& rValue ) override;
253 virtual void SAL_CALL getFastPropertyValue( css::uno::Any& rValue, sal_Int32 nHandle ) const override;
254
255 virtual cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper() override;
256 virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo( ) override;
257
258public:
260 struct NotifierAccess { friend struct PropertyChangeNotifier; private: NotifierAccess() { } };
267 void getCurrentPropertyValueByHandle( sal_Int32 nHandle, css::uno::Any& /* [out] */ rValue, const NotifierAccess& ) const
268 {
269 getFastPropertyValue( rValue, nHandle );
270 }
271
274 void notifyPropertyChange( sal_Int32 nHandle, const css::uno::Any& rOldValue, const css::uno::Any& rNewValue, const NotifierAccess& ) const
275 {
276 const_cast< PropertySetBase* >( this )->firePropertyChange( nHandle, rNewValue, rOldValue );
277 }
278
279 using ::comphelper::OStatefulPropertySet::getFastPropertyValue;
280
281private:
288 PropertyAccessorBase& locatePropertyHandler( sal_Int32 nHandle ) const;
289};
290
307{
308private:
310 sal_Int32 m_nHandle;
311 css::uno::Any m_aOldValue;
312
313public:
323 PropertyChangeNotifier( const PropertySetBase& rPropertySet, sal_Int32 nHandle )
324 :m_rPropertySet( rPropertySet )
326 {
328 }
330 {
331 css::uno::Any aNewValue;
333 if ( aNewValue != m_aOldValue )
334 {
336 }
337 }
338};
339
340/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
helper class for implementing property accessors via UNO methods
APIPropertyAccessor(CLASS *pInstance, Writer pWriter, Reader pReader)
typedef void(SAL_CALL CLASS::*Writer)(const VALUE &)
typedef VALUE(SAL_CALL CLASS::*Reader)()
helper class for implementing non-UNO accessors to a boolean property
BooleanPropertyAccessor(CLASS *pInstance, Writer pWriter, Reader pReader)
helper class for implementing property accessors via non-UNO methods
DirectPropertyAccessor(CLASS *pInstance, Writer pWriter, Reader pReader)
helper class for implementing property accessors through public member functions
virtual void setValue(const css::uno::Any &rValue) override
virtual void getValue(css::uno::Any &rValue) const override
virtual bool isWriteable() const override
GenericPropertyAccessor(CLASS *pInstance, Writer pWriter, Reader pReader)
virtual bool approveValue(const css::uno::Any &rValue) const override
base class which encapsulates accessing (reading/writing) concrete property values
virtual bool approveValue(const css::uno::Any &rValue) const =0
virtual bool isWriteable() const =0
virtual ~PropertyAccessorBase() override
virtual void getValue(css::uno::Any &rValue) const =0
virtual void setValue(const css::uno::Any &rValue)=0
bridges two XPropertySet helper implementations
void registerProperty(const css::beans::Property &rProperty, const ::rtl::Reference< PropertyAccessorBase > &rAccessor)
registers a new property to be supported by this instance
virtual cppu::IPropertyArrayHelper &SAL_CALL getInfoHelper() override
virtual void SAL_CALL getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const override
::std::map< const sal_Int32, css::uno::Any > PropertyValueCache
::std::vector< css::beans::Property > PropertyArray
PropertyAccessorBase & locatePropertyHandler(sal_Int32 nHandle) const
locates a property given by handle
void notifyAndCachePropertyValue(sal_Int32 nHandle)
notifies a change in a given property value, if necessary
PropertyValueCache m_aCache
::std::map< const sal_Int32, ::rtl::Reference< PropertyAccessorBase > > PropertyAccessors
virtual sal_Bool SAL_CALL convertFastPropertyValue(css::uno::Any &rConvertedValue, css::uno::Any &rOldValue, sal_Int32 nHandle, const css::uno::Any &rValue) override
OPropertysetHelper methods.
void notifyPropertyChange(sal_Int32 nHandle, const css::uno::Any &rOldValue, const css::uno::Any &rNewValue, const NotifierAccess &) const
notifies a change in a given property to all interested listeners
void initializePropertyValueCache(sal_Int32 nHandle)
initializes the property value cache for the given property, with its current value
virtual ~PropertySetBase() override
PropertyArray m_aProperties
std::unique_ptr< cppu::IPropertyArrayHelper > m_pProperties
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
void getCurrentPropertyValueByHandle(sal_Int32 nHandle, css::uno::Any &rValue, const NotifierAccess &) const
retrieves the current property value for the given handle
PropertyAccessors m_aAccessors
virtual void SAL_CALL setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const css::uno::Any &rValue) override
WRITER
sal_Int32 nHandle
a helper class for notifying property changes in a <type>PropertySetBase</type> instance.
const PropertySetBase & m_rPropertySet
PropertyChangeNotifier(const PropertySetBase &rPropertySet, sal_Int32 nHandle)
constructs a PropertyChangeNotifier
helper struct for granting selective access to some notification-related methods
unsigned char sal_Bool