Line data Source code
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 : #ifndef INCLUDED_FORMS_SOURCE_XFORMS_PROPERTYSETBASE_HXX
20 : #define INCLUDED_FORMS_SOURCE_XFORMS_PROPERTYSETBASE_HXX
21 :
22 : #include <cppuhelper/weak.hxx>
23 : #include <com/sun/star/lang/XTypeProvider.hpp>
24 : #include <comphelper/propstate.hxx>
25 : #include <comphelper/propertysetinfo.hxx>
26 : #include <comphelper/proparrhlp.hxx>
27 : #include <rtl/ref.hxx>
28 : #include <salhelper/simplereferenceobject.hxx>
29 :
30 : // include for inlined helper function below
31 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 : #include <com/sun/star/beans/PropertyAttribute.hpp>
33 :
34 : #include <map>
35 :
36 : // forward declarations for method arguments
37 : namespace com { namespace sun { namespace star { namespace uno {
38 : class Any;
39 : class RuntimeException;
40 : template<class T> class Sequence;
41 : } } } }
42 :
43 : /** base class which encapsulates accessing (reading/writing) concrete property values
44 : */
45 : class PropertyAccessorBase : public salhelper::SimpleReferenceObject
46 : {
47 : protected:
48 10 : PropertyAccessorBase() { }
49 : virtual ~PropertyAccessorBase();
50 :
51 : public:
52 :
53 : virtual bool approveValue( const com::sun::star::uno::Any& rValue ) const = 0;
54 : virtual void setValue( const com::sun::star::uno::Any& rValue ) = 0;
55 : virtual void getValue( com::sun::star::uno::Any& rValue ) const = 0;
56 : virtual bool isWriteable() const = 0;
57 : };
58 :
59 :
60 : /** helper class for implementing property accessors through public member functions
61 : */
62 : template< typename CLASS, typename VALUE, class WRITER, class READER >
63 10 : class GenericPropertyAccessor : public PropertyAccessorBase
64 : {
65 : public:
66 : typedef WRITER Writer;
67 : typedef READER Reader;
68 :
69 : private:
70 : CLASS* m_pInstance;
71 : Writer m_pWriter;
72 : Reader m_pReader;
73 :
74 : public:
75 10 : GenericPropertyAccessor( CLASS* pInstance, Writer pWriter, Reader pReader )
76 : :m_pInstance( pInstance )
77 : ,m_pWriter( pWriter )
78 10 : ,m_pReader( pReader )
79 : {
80 10 : }
81 :
82 0 : virtual bool approveValue( const com::sun::star::uno::Any& rValue ) const SAL_OVERRIDE
83 : {
84 0 : VALUE aVal;
85 0 : return ( rValue >>= aVal );
86 : }
87 :
88 0 : virtual void setValue( const com::sun::star::uno::Any& rValue ) SAL_OVERRIDE
89 : {
90 0 : VALUE aTypedVal = VALUE();
91 0 : OSL_VERIFY( rValue >>= aTypedVal );
92 0 : (m_pInstance->*m_pWriter)( aTypedVal );
93 0 : }
94 :
95 0 : virtual void getValue( com::sun::star::uno::Any& rValue ) const SAL_OVERRIDE
96 : {
97 0 : rValue = com::sun::star::uno::makeAny( (m_pInstance->*m_pReader)() );
98 0 : }
99 :
100 0 : virtual bool isWriteable() const SAL_OVERRIDE
101 : {
102 0 : return m_pWriter != 0;
103 : }
104 : };
105 :
106 : /** helper class for implementing property accessors via non-UNO methods
107 : */
108 : template< typename CLASS, typename VALUE >
109 12 : class DirectPropertyAccessor
110 : :public GenericPropertyAccessor < CLASS
111 : , VALUE
112 : , void (CLASS::*)( const VALUE& )
113 : , VALUE (CLASS::*)() const
114 : >
115 : {
116 : protected:
117 : typedef void (CLASS::*Writer)( const VALUE& );
118 : typedef VALUE (CLASS::*Reader)() const;
119 : public:
120 6 : DirectPropertyAccessor( CLASS* pInstance, Writer pWriter, Reader pReader )
121 6 : :GenericPropertyAccessor< CLASS, VALUE, Writer, Reader >( pInstance, pWriter, pReader )
122 : {
123 6 : }
124 : };
125 :
126 : /** helper class for implementing non-UNO accessors to a boolean property
127 : */
128 : template< typename CLASS, typename DUMMY >
129 4 : class BooleanPropertyAccessor
130 : :public GenericPropertyAccessor < CLASS
131 : , bool
132 : , void (CLASS::*)( bool )
133 : , bool (CLASS::*)() const
134 : >
135 : {
136 : protected:
137 : typedef void (CLASS::*Writer)( bool );
138 : typedef bool (CLASS::*Reader)() const;
139 : public:
140 2 : BooleanPropertyAccessor( CLASS* pInstance, Writer pWriter, Reader pReader )
141 2 : :GenericPropertyAccessor< CLASS, bool, Writer, Reader >( pInstance, pWriter, pReader )
142 : {
143 2 : }
144 : };
145 :
146 : /** helper class for implementing property accessors via UNO methods
147 : */
148 : template< typename CLASS, typename VALUE >
149 4 : class APIPropertyAccessor
150 : :public GenericPropertyAccessor < CLASS
151 : , VALUE
152 : , void (SAL_CALL CLASS::*)( const VALUE& )
153 : , VALUE (SAL_CALL CLASS::*)()
154 : >
155 : {
156 : protected:
157 : typedef void (SAL_CALL CLASS::*Writer)( const VALUE& );
158 : typedef VALUE (SAL_CALL CLASS::*Reader)();
159 : public:
160 2 : APIPropertyAccessor( CLASS* pInstance, Writer pWriter, Reader pReader )
161 2 : :GenericPropertyAccessor< CLASS, VALUE, Writer, Reader >( pInstance, pWriter, pReader )
162 : {
163 2 : }
164 : };
165 :
166 : /** bridges two XPropertySet helper implementations
167 :
168 : The <type scope="comphelper">OStatefulPropertySet</type> (basically, the
169 : <type scope="cppu">OPropertySetHelper</type>) implements a comprehensive framework
170 : for property sets, including property change notifications.
171 : However, it lacks some easy possibilities to declare the supported properties.
172 : Other helper structs and classes allow for this, but are lacking needed features
173 : such as property change notifications.
174 :
175 : The <type>PropertySetBase</type> bridges various implementations,
176 : so you have the best of both worlds.
177 : */
178 : class PropertySetBase : public ::comphelper::OStatefulPropertySet
179 : {
180 : private:
181 : typedef com::sun::star::uno::Any Any_t;
182 :
183 : typedef ::std::map< const sal_Int32, ::rtl::Reference< PropertyAccessorBase > > PropertyAccessors;
184 : typedef ::std::vector< ::com::sun::star::beans::Property > PropertyArray;
185 : typedef ::std::map< const sal_Int32, Any_t > PropertyValueCache;
186 :
187 : PropertyArray m_aProperties;
188 : cppu::IPropertyArrayHelper* m_pProperties;
189 : PropertyAccessors m_aAccessors;
190 : PropertyValueCache m_aCache;
191 :
192 : protected:
193 : PropertySetBase();
194 : virtual ~PropertySetBase();
195 :
196 : /** registers a new property to be supported by this instance
197 : @param rProperty
198 : the descriptor for the to-be-supported property
199 : @param rAccessor
200 : an instance which is able to provide read and possibly write access to
201 : the property.
202 : @precond
203 : Must not be called after any of the property set related UNO interfaces
204 : has been used. Usually, you will do a number of <member>registerProperty</member>
205 : calls in the constructor of your class.
206 : */
207 : void registerProperty(
208 : const com::sun::star::beans::Property& rProperty,
209 : const ::rtl::Reference< PropertyAccessorBase >& rAccessor
210 : );
211 :
212 : /** notifies a change in a given property value, if necessary
213 :
214 : The necessity of the notification is determined by a cached value for the given
215 : property. Caching happens after notification.
216 :
217 : That is, when you call <member>notifyAndCachePropertyValue</member> for the first time,
218 : a value for the given property is default constructed, and considered to be the "old value".
219 : If this value differs from the current value, then this change is notified to all interested
220 : listeners. Finally, the current value is remembered.
221 :
222 : Subsequent calls to <member>notifyAndCachePropertyValue</member> use the remembered value as
223 : "old value", and from then on behave as the first call.
224 :
225 : @param nHandle
226 : the handle of the property. Must denote a property supported by this instance, i.e.
227 : one previously registered via <member>registerProperty</member>.
228 :
229 : @precond
230 : our ref count must not be 0. The reason is that during this method's execution,
231 : the instance might be acquired and released, which would immediately destroy
232 : the instance if it has a ref count of 0.
233 :
234 : @seealso initializePropertyValueCache
235 : */
236 : void notifyAndCachePropertyValue( sal_Int32 nHandle );
237 :
238 : /** initializes the property value cache for the given property, with its current value
239 :
240 : Usually used to initialize the cache with values which are different from default
241 : constructed values. Say you have a boolean property whose initial state
242 : is <TRUE/>. Say you call <member>notifyAndCachePropertyValue</member> the first time: it will
243 : default construct the "old value" for this property as <FALSE/>, and thus <b>not</b> do
244 : any notifications if the "current value" is also <FALSE/> - which might be wrong, since
245 : the guessing of the "old value" differed from the real initial value which was <TRUE/>.
246 :
247 : Too confusing? Okay, than just call this method for every property you have.
248 :
249 : @param nHandle
250 : the handle of the property. Must denote a property supported by this instance, i.e.
251 : one previously registered via <member>registerProperty</member>.
252 : @param rValue
253 : the value to cache
254 : @seealso notifyAndCachePropertyValue
255 : */
256 : void initializePropertyValueCache( sal_Int32 nHandle );
257 :
258 : /// OPropertysetHelper methods
259 : virtual sal_Bool SAL_CALL convertFastPropertyValue( Any_t& rConvertedValue, Any_t& rOldValue, sal_Int32 nHandle, const Any_t& rValue )
260 : throw (::com::sun::star::lang::IllegalArgumentException) SAL_OVERRIDE;
261 : virtual void SAL_CALL setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any_t& rValue )
262 : throw (::com::sun::star::uno::Exception, std::exception) SAL_OVERRIDE;
263 : virtual void SAL_CALL getFastPropertyValue( Any_t& rValue, sal_Int32 nHandle ) const SAL_OVERRIDE;
264 :
265 : virtual cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper() SAL_OVERRIDE;
266 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo( ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
267 :
268 : public:
269 : /// helper struct for granting selective access to some notification-related methods
270 0 : struct NotifierAccess { friend struct PropertyChangeNotifier; private: NotifierAccess() { } };
271 : /** retrieves the current property value for the given handle
272 : @param nHandle
273 : the handle of the property. Must denote a property supported by this instance, i.e.
274 : one previously registered via <member>registerProperty</member>.
275 : @see registerProperty
276 : */
277 0 : inline void getCurrentPropertyValueByHandle( sal_Int32 nHandle, Any_t& /* [out] */ rValue, const NotifierAccess& ) const
278 : {
279 0 : getFastPropertyValue( rValue, nHandle );
280 0 : }
281 :
282 : /** notifies a change in a given property to all interested listeners
283 : */
284 0 : inline void notifyPropertyChange( sal_Int32 nHandle, const Any_t& rOldValue, const Any_t& rNewValue, const NotifierAccess& ) const
285 : {
286 0 : const_cast< PropertySetBase* >( this )->firePropertyChange( nHandle, rNewValue, rOldValue );
287 0 : }
288 :
289 : using ::comphelper::OStatefulPropertySet::getFastPropertyValue;
290 :
291 : private:
292 : /** locates a property given by handle
293 : @param nHandle
294 : the handle of the property. Must denote a property supported by this instance, i.e.
295 : one previously registered via <member>registerProperty</member>.
296 : @see registerProperty
297 : */
298 : PropertyAccessorBase& locatePropertyHandler( sal_Int32 nHandle ) const;
299 : };
300 :
301 : /** a helper class for notifying property changes in a <type>PropertySetBase</type> instance.
302 :
303 : You can create an instance of this class on the stack of a method which is to programmatically
304 : change the value of a property. In its constructor, the instance will acquire the current property
305 : value, and in its destructor, it will notify the change of this property's value (if necessary).
306 :
307 : You do not need this class if you are modifying property values by using the X(Fast|Multi)PropertSet
308 : methods, since those already care for property notifications. You only need it if you're changing
309 : the internal representation of your property directly.
310 :
311 : Also note that usually, notifications in the UNO world should be done without a locked mutex. So
312 : if you use this class in conjunction with a <type>MutexGuard</type>, ensure that you <b>first</b>
313 : instantiate the <type>PropertyChangeNotifier</type>, and <b>then</b> the <type>MutexGuard</type>,
314 : so your mutex is released before the notification happens.
315 : */
316 : struct PropertyChangeNotifier
317 : {
318 : private:
319 : const PropertySetBase& m_rPropertySet;
320 : sal_Int32 m_nHandle;
321 : com::sun::star::uno::Any m_aOldValue;
322 :
323 : public:
324 : /** constructs a PropertyChangeNotifier
325 : @param rPropertySet
326 : the property set implementation whose property is going to be changed. Note
327 : that this property set implementation must live at least as long as the
328 : PropertyChangeNotifier instance does.
329 : @param nHandle
330 : the handle of the property which is going to be changed. Must be a valid property
331 : handle for the given <arg>rPropertySet</arg>
332 : */
333 0 : inline PropertyChangeNotifier( const PropertySetBase& rPropertySet, sal_Int32 nHandle )
334 : :m_rPropertySet( rPropertySet )
335 0 : ,m_nHandle( nHandle )
336 : {
337 0 : m_rPropertySet.getCurrentPropertyValueByHandle( m_nHandle, m_aOldValue, PropertySetBase::NotifierAccess() );
338 0 : }
339 0 : inline ~PropertyChangeNotifier()
340 0 : {
341 0 : com::sun::star::uno::Any aNewValue;
342 0 : m_rPropertySet.getCurrentPropertyValueByHandle( m_nHandle, aNewValue, PropertySetBase::NotifierAccess() );
343 0 : if ( aNewValue != m_aOldValue )
344 : {
345 0 : m_rPropertySet.notifyPropertyChange( m_nHandle, m_aOldValue, aNewValue, PropertySetBase::NotifierAccess() );
346 0 : }
347 0 : }
348 : };
349 :
350 :
351 : #define PROPERTY_FLAGS( NAME, TYPE, FLAG ) com::sun::star::beans::Property( \
352 : OUString( #NAME, sizeof( #NAME ) - 1, RTL_TEXTENCODING_ASCII_US ), \
353 : HANDLE_##NAME, getCppuType( static_cast< TYPE* >( NULL ) ), FLAG )
354 : #define PROPERTY( NAME, TYPE ) PROPERTY_FLAGS( NAME, TYPE, com::sun::star::beans::PropertyAttribute::BOUND )
355 : #define PROPERTY_RO( NAME, TYPE ) PROPERTY_FLAGS( NAME, TYPE, com::sun::star::beans::PropertyAttribute::BOUND | com::sun::star::beans::PropertyAttribute::READONLY )
356 :
357 : #endif
358 :
359 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|