Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "propertysetbase.hxx"
31 : :
32 : : #include <cppuhelper/typeprovider.hxx> // for getImplementationId()
33 : :
34 : : #include <com/sun/star/beans/XPropertySet.hpp>
35 : : #include <com/sun/star/beans/XMultiPropertySet.hpp>
36 : : #include <com/sun/star/beans/XPropertyState.hpp>
37 : : #include <com/sun/star/uno/Reference.hxx>
38 : : #include <tools/solar.h>
39 : :
40 : : #include <vector>
41 : :
42 : : using com::sun::star::uno::Any;
43 : : using com::sun::star::uno::Type;
44 : : using com::sun::star::uno::Sequence;
45 : : using com::sun::star::uno::Reference;
46 : : using com::sun::star::uno::Exception;
47 : : using com::sun::star::uno::RuntimeException;
48 : : using com::sun::star::lang::IllegalArgumentException;
49 : : using com::sun::star::beans::Property;
50 : : using com::sun::star::beans::XPropertySetInfo;
51 : :
52 : 0 : PropertyAccessorBase::~PropertyAccessorBase()
53 : : {
54 [ # # ]: 0 : }
55 : :
56 : 0 : oslInterlockedCount SAL_CALL PropertyAccessorBase::acquire()
57 : : {
58 : 0 : return ++m_refCount;
59 : : }
60 : :
61 : 0 : oslInterlockedCount SAL_CALL PropertyAccessorBase::release()
62 : : {
63 [ # # ]: 0 : if ( --m_refCount == 0 )
64 : : {
65 [ # # ]: 0 : delete this;
66 : 0 : return 0;
67 : : }
68 : 0 : return m_refCount;
69 : : }
70 : :
71 : 0 : PropertySetBase::PropertySetBase( )
72 [ # # ][ # # ]: 0 : :m_pProperties( NULL )
[ # # ]
73 : : {
74 : 0 : }
75 : :
76 : 0 : PropertySetBase::~PropertySetBase( )
77 : : {
78 [ # # ][ # # ]: 0 : DELETEZ( m_pProperties );
79 [ # # ]: 0 : }
80 : :
81 : 0 : cppu::IPropertyArrayHelper& SAL_CALL PropertySetBase::getInfoHelper()
82 : : {
83 [ # # ]: 0 : if ( !m_pProperties )
84 : : {
85 : : OSL_ENSURE( !m_aProperties.empty(), "PropertySetBase::getInfoHelper: no registered properties!" );
86 [ # # ]: 0 : m_pProperties = new cppu::OPropertyArrayHelper( &m_aProperties[0], m_aProperties.size(), sal_False );
87 : : }
88 : 0 : return *m_pProperties;
89 : : }
90 : :
91 : 0 : Reference< XPropertySetInfo > SAL_CALL PropertySetBase::getPropertySetInfo( ) throw(RuntimeException)
92 : : {
93 : 0 : return cppu::OPropertySetHelper::createPropertySetInfo( getInfoHelper() );
94 : : }
95 : :
96 : 0 : void PropertySetBase::registerProperty( const Property& rProperty,
97 : : const ::rtl::Reference< PropertyAccessorBase >& rAccessor )
98 : : {
99 : : OSL_ENSURE( rAccessor.get(), "PropertySetBase::registerProperty: invalid property accessor, this will crash!" );
100 [ # # ]: 0 : m_aAccessors.insert( PropertyAccessors::value_type( rProperty.Handle, rAccessor ) );
101 : :
102 : : OSL_ENSURE( ( rAccessor->isWriteable() == true )
103 : : == ( ( rProperty.Attributes & com::sun::star::beans::PropertyAttribute::READONLY ) == 0 ),
104 : : "PropertySetBase::registerProperty: inconsistence!" );
105 : :
106 : 0 : m_aProperties.push_back( rProperty );
107 : 0 : }
108 : :
109 : 0 : void PropertySetBase::notifyAndCachePropertyValue( sal_Int32 nHandle )
110 : : {
111 [ # # ]: 0 : ::osl::ClearableMutexGuard aGuard( GetMutex() );
112 : :
113 [ # # ]: 0 : PropertyValueCache::iterator aPos = m_aCache.find( nHandle );
114 [ # # ]: 0 : if ( aPos == m_aCache.end() )
115 : : { // method has never before been invoked for this property
116 : : try
117 : : {
118 : : // determine the type of this property
119 [ # # ]: 0 : ::cppu::IPropertyArrayHelper& rPropertyMetaData = getInfoHelper();
120 : 0 : ::rtl::OUString sPropName;
121 [ # # ]: 0 : OSL_VERIFY( rPropertyMetaData.fillPropertyMembersByHandle( &sPropName, NULL, nHandle ) );
122 [ # # ]: 0 : Property aProperty = rPropertyMetaData.getPropertyByName( sPropName );
123 : : // default construct a value of this type
124 : 0 : Any aEmptyValue( NULL, aProperty.Type );
125 : : // insert into the cache
126 [ # # ][ # # ]: 0 : aPos = m_aCache.insert( PropertyValueCache::value_type( nHandle, aEmptyValue ) ).first;
127 : : }
128 [ # # ]: 0 : catch( const Exception& )
129 : : {
130 : : OSL_FAIL( "PropertySetBase::notifyAndCachePropertyValue: this is not expected to fail!" );
131 : : }
132 : : }
133 : 0 : Any aOldValue = aPos->second;
134 : : // determine the current value
135 : 0 : Any aNewValue;
136 [ # # ]: 0 : getFastPropertyValue( aNewValue, nHandle );
137 : : // remember the old value
138 : 0 : aPos->second = aNewValue;
139 : :
140 [ # # ]: 0 : aGuard.clear();
141 [ # # ]: 0 : if ( aNewValue != aOldValue )
142 [ # # ][ # # ]: 0 : firePropertyChange( nHandle, aNewValue, aOldValue );
143 : 0 : }
144 : :
145 : 0 : void PropertySetBase::initializePropertyValueCache( sal_Int32 nHandle )
146 : : {
147 : 0 : Any aCurrentValue;
148 [ # # ]: 0 : getFastPropertyValue( aCurrentValue, nHandle );
149 : :
150 : : #if OSL_DEBUG_LEVEL > 0
151 : : ::std::pair< PropertyValueCache::iterator, bool > aInsertResult =
152 : : #endif
153 [ # # ]: 0 : m_aCache.insert( PropertyValueCache::value_type( nHandle, aCurrentValue ) );
154 : 0 : OSL_ENSURE( aInsertResult.second, "PropertySetBase::initializePropertyValueCache: already cached a value for this property!" );
155 : 0 : }
156 : :
157 : 0 : PropertyAccessorBase& PropertySetBase::locatePropertyHandler( sal_Int32 nHandle ) const
158 : : {
159 [ # # ]: 0 : PropertyAccessors::const_iterator aPropertyPos = m_aAccessors.find( nHandle );
160 : : OSL_ENSURE( aPropertyPos != m_aAccessors.end() && aPropertyPos->second.get(),
161 : : "PropertySetBase::locatePropertyHandler: accessor map is corrupted!" );
162 : : // neither should this be called for handles where there is no accessor, nor should a
163 : : // NULL accssor be in the map
164 : 0 : return *aPropertyPos->second;
165 : : }
166 : :
167 : 0 : sal_Bool SAL_CALL PropertySetBase::convertFastPropertyValue( Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle,
168 : : const Any& rValue )
169 : : throw (IllegalArgumentException)
170 : : {
171 : 0 : PropertyAccessorBase& rAccessor = locatePropertyHandler( nHandle );
172 [ # # ]: 0 : if ( !rAccessor.approveValue( rValue ) )
173 [ # # ][ # # ]: 0 : throw IllegalArgumentException( ::rtl::OUString(), *this, 0 );
174 : :
175 : 0 : rAccessor.getValue( rOldValue );
176 [ # # ]: 0 : if ( rOldValue != rValue )
177 : : {
178 : 0 : rConvertedValue = rValue; // no conversion at all
179 : 0 : return sal_True;
180 : : }
181 : 0 : return sal_False;
182 : : }
183 : :
184 : 0 : void SAL_CALL PropertySetBase::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue )
185 : : throw (Exception)
186 : : {
187 : 0 : PropertyAccessorBase& rAccessor = locatePropertyHandler( nHandle );
188 : 0 : rAccessor.setValue( rValue );
189 : 0 : }
190 : :
191 : 0 : void SAL_CALL PropertySetBase::getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const
192 : : {
193 : 0 : PropertyAccessorBase& rAccessor = locatePropertyHandler( nHandle );
194 : 0 : rAccessor.getValue( rValue );
195 : 0 : }
196 : :
197 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|