LibreOffice Module forms (master) 1
propertysetbase.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
21#include "propertysetbase.hxx"
22
23#include <com/sun/star/beans/XMultiPropertySet.hpp>
24#include <com/sun/star/uno/Reference.hxx>
25
26using com::sun::star::uno::Any;
28using com::sun::star::uno::Exception;
29using com::sun::star::lang::IllegalArgumentException;
30using com::sun::star::beans::Property;
31using com::sun::star::beans::XPropertySetInfo;
32
34{
35}
36
38{
39}
40
42{
43}
44
46{
47 if ( !m_pProperties )
48 {
49 OSL_ENSURE( !m_aProperties.empty(), "PropertySetBase::getInfoHelper: no registered properties!" );
50 m_pProperties.reset(new cppu::OPropertyArrayHelper( m_aProperties.data(), m_aProperties.size(), false ));
51 }
52 return *m_pProperties;
53}
54
56{
58}
59
60void PropertySetBase::registerProperty( const Property& rProperty,
61 const ::rtl::Reference< PropertyAccessorBase >& rAccessor )
62{
63 OSL_ENSURE(rAccessor,
64 "PropertySetBase::registerProperty: invalid property accessor, this will crash!");
65 m_aAccessors.emplace( rProperty.Handle, rAccessor );
66
67 OSL_ENSURE( rAccessor->isWriteable()
68 == ( ( rProperty.Attributes & css::beans::PropertyAttribute::READONLY ) == 0 ),
69 "PropertySetBase::registerProperty: inconsistence!" );
70
71 m_aProperties.push_back( rProperty );
72}
73
75{
76 ::osl::ClearableMutexGuard aGuard( GetMutex() );
77
78 PropertyValueCache::iterator aPos = m_aCache.find( nHandle );
79 if ( aPos == m_aCache.end() )
80 { // method has never before been invoked for this property
81 try
82 {
83 // determine the type of this property
84 ::cppu::IPropertyArrayHelper& rPropertyMetaData = getInfoHelper();
85 OUString sPropName;
86 OSL_VERIFY( rPropertyMetaData.fillPropertyMembersByHandle( &sPropName, nullptr, nHandle ) );
87 Property aProperty = rPropertyMetaData.getPropertyByName( sPropName );
88 // default construct a value of this type
89 Any aEmptyValue( nullptr, aProperty.Type );
90 // insert into the cache
91 aPos = m_aCache.emplace( nHandle, aEmptyValue ).first;
92 }
93 catch( const Exception& )
94 {
95 OSL_FAIL( "PropertySetBase::notifyAndCachePropertyValue: this is not expected to fail!" );
96 }
97 }
98 Any aOldValue = aPos->second;
99 // determine the current value
100 Any aNewValue;
101 getFastPropertyValue( aNewValue, nHandle );
102 // remember the old value
103 aPos->second = aNewValue;
104
105 aGuard.clear();
106 if ( aNewValue != aOldValue )
107 firePropertyChange( nHandle, aNewValue, aOldValue );
108}
109
111{
112 Any aCurrentValue;
113 getFastPropertyValue( aCurrentValue, nHandle );
114
115 ::std::pair< PropertyValueCache::iterator, bool > aInsertResult =
116 m_aCache.emplace( nHandle, aCurrentValue );
117 OSL_ENSURE( aInsertResult.second, "PropertySetBase::initializePropertyValueCache: already cached a value for this property!" );
118}
119
121{
122 PropertyAccessors::const_iterator aPropertyPos = m_aAccessors.find( nHandle );
123 OSL_ENSURE( aPropertyPos != m_aAccessors.end() && aPropertyPos->second,
124 "PropertySetBase::locatePropertyHandler: accessor map is corrupted!" );
125 // neither should this be called for handles where there is no accessor, nor should a
126 // NULL accessor be in the map
127 return *aPropertyPos->second;
128}
129
130sal_Bool SAL_CALL PropertySetBase::convertFastPropertyValue( Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle,
131 const Any& rValue )
132{
134 if ( !rAccessor.approveValue( rValue ) )
135 throw IllegalArgumentException( OUString(), *this, 0 );
136
137 rAccessor.getValue( rOldValue );
138 if ( rOldValue != rValue )
139 {
140 rConvertedValue = rValue; // no conversion at all
141 return true;
142 }
143 return false;
144}
145
146void SAL_CALL PropertySetBase::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue )
147{
149 rAccessor.setValue( rValue );
150}
151
152void SAL_CALL PropertySetBase::getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const
153{
155 rAccessor.getValue( rValue );
156}
157
158/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
base class which encapsulates accessing (reading/writing) concrete property values
virtual bool approveValue(const css::uno::Any &rValue) const =0
virtual ~PropertyAccessorBase() override
virtual void getValue(css::uno::Any &rValue) const =0
virtual void setValue(const css::uno::Any &rValue)=0
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
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
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 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
PropertyAccessors m_aAccessors
virtual void SAL_CALL setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const css::uno::Any &rValue) override
virtual sal_Bool SAL_CALL fillPropertyMembersByHandle(::rtl::OUString *pPropName, sal_Int16 *pAttributes, sal_Int32 nHandle)=0
virtual css::beans::Property SAL_CALL getPropertyByName(const ::rtl::OUString &rPropertyName)=0
static css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL createPropertySetInfo(IPropertyArrayHelper &rProperties)
@ Exception
sal_Int32 nHandle
unsigned char sal_Bool