1*24acc546SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*24acc546SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*24acc546SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*24acc546SAndrew Rist  * distributed with this work for additional information
6*24acc546SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*24acc546SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*24acc546SAndrew Rist  * "License"); you may not use this file except in compliance
9*24acc546SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*24acc546SAndrew Rist  *
11*24acc546SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*24acc546SAndrew Rist  *
13*24acc546SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*24acc546SAndrew Rist  * software distributed under the License is distributed on an
15*24acc546SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*24acc546SAndrew Rist  * KIND, either express or implied.  See the License for the
17*24acc546SAndrew Rist  * specific language governing permissions and limitations
18*24acc546SAndrew Rist  * under the License.
19*24acc546SAndrew Rist  *
20*24acc546SAndrew Rist  *************************************************************/
21*24acc546SAndrew Rist 
22*24acc546SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_forms.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "propertysetbase.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>  // for getImplementationId()
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
32cdf0e10cSrcweir #include <com/sun/star/beans/XMultiPropertySet.hpp>
33cdf0e10cSrcweir #include <com/sun/star/beans/XPropertyState.hpp>
34cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
35cdf0e10cSrcweir #include <tools/debug.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <vector>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using com::sun::star::uno::Any;
40cdf0e10cSrcweir using com::sun::star::uno::Type;
41cdf0e10cSrcweir using com::sun::star::uno::Sequence;
42cdf0e10cSrcweir using com::sun::star::uno::Reference;
43cdf0e10cSrcweir using com::sun::star::uno::Exception;
44cdf0e10cSrcweir using com::sun::star::uno::RuntimeException;
45cdf0e10cSrcweir using com::sun::star::lang::IllegalArgumentException;
46cdf0e10cSrcweir using com::sun::star::beans::Property;
47cdf0e10cSrcweir using com::sun::star::beans::XPropertySetInfo;
48cdf0e10cSrcweir 
acquire()49cdf0e10cSrcweir oslInterlockedCount SAL_CALL PropertyAccessorBase::acquire()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir     return ++m_refCount;
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
release()54cdf0e10cSrcweir oslInterlockedCount SAL_CALL PropertyAccessorBase::release()
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     if ( --m_refCount == 0 )
57cdf0e10cSrcweir     {
58cdf0e10cSrcweir         delete this;
59cdf0e10cSrcweir         return 0;
60cdf0e10cSrcweir     }
61cdf0e10cSrcweir     return m_refCount;
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
PropertySetBase()64cdf0e10cSrcweir PropertySetBase::PropertySetBase( )
65cdf0e10cSrcweir     :m_pProperties( NULL )
66cdf0e10cSrcweir {
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
~PropertySetBase()69cdf0e10cSrcweir PropertySetBase::~PropertySetBase( )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir     DELETEZ( m_pProperties );
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
getInfoHelper()74cdf0e10cSrcweir cppu::IPropertyArrayHelper& SAL_CALL PropertySetBase::getInfoHelper()
75cdf0e10cSrcweir {
76cdf0e10cSrcweir     if ( !m_pProperties )
77cdf0e10cSrcweir     {
78cdf0e10cSrcweir         DBG_ASSERT( !m_aProperties.empty(), "PropertySetBase::getInfoHelper: no registered properties!" );
79cdf0e10cSrcweir         m_pProperties = new cppu::OPropertyArrayHelper( &m_aProperties[0], m_aProperties.size(), sal_False );
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir     return *m_pProperties;
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
getPropertySetInfo()84cdf0e10cSrcweir Reference< XPropertySetInfo > SAL_CALL PropertySetBase::getPropertySetInfo(  ) throw(RuntimeException)
85cdf0e10cSrcweir {
86cdf0e10cSrcweir 	return cppu::OPropertySetHelper::createPropertySetInfo( getInfoHelper() );
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
registerProperty(const Property & rProperty,const::rtl::Reference<PropertyAccessorBase> & rAccessor)89cdf0e10cSrcweir void PropertySetBase::registerProperty( const Property& rProperty,
90cdf0e10cSrcweir     const ::rtl::Reference< PropertyAccessorBase >& rAccessor )
91cdf0e10cSrcweir {
92cdf0e10cSrcweir     DBG_ASSERT( rAccessor.get(), "PropertySetBase::registerProperty: invalid property accessor, this will crash!" );
93cdf0e10cSrcweir     m_aAccessors.insert( PropertyAccessors::value_type( rProperty.Handle, rAccessor ) );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     DBG_ASSERT( ( rAccessor->isWriteable() == true )
96cdf0e10cSrcweir                 == ( ( rProperty.Attributes & com::sun::star::beans::PropertyAttribute::READONLY ) == 0 ),
97cdf0e10cSrcweir         "PropertySetBase::registerProperty: inconsistence!" );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     m_aProperties.push_back( rProperty );
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
notifyAndCachePropertyValue(sal_Int32 nHandle)102cdf0e10cSrcweir void PropertySetBase::notifyAndCachePropertyValue( sal_Int32 nHandle )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir     ::osl::ClearableMutexGuard aGuard( GetMutex() );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     PropertyValueCache::iterator aPos = m_aCache.find( nHandle );
107cdf0e10cSrcweir     if ( aPos == m_aCache.end() )
108cdf0e10cSrcweir     {   // method has never before been invoked for this property
109cdf0e10cSrcweir         try
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir             // determine the type of this property
112cdf0e10cSrcweir             ::cppu::IPropertyArrayHelper& rPropertyMetaData = getInfoHelper();
113cdf0e10cSrcweir             ::rtl::OUString sPropName;
114cdf0e10cSrcweir             OSL_VERIFY( rPropertyMetaData.fillPropertyMembersByHandle( &sPropName, NULL, nHandle ) );
115cdf0e10cSrcweir             Property aProperty = rPropertyMetaData.getPropertyByName( sPropName );
116cdf0e10cSrcweir             // default construct a value of this type
117cdf0e10cSrcweir             Any aEmptyValue( NULL, aProperty.Type );
118cdf0e10cSrcweir             // insert into the cache
119cdf0e10cSrcweir             aPos = m_aCache.insert( PropertyValueCache::value_type( nHandle, aEmptyValue ) ).first;
120cdf0e10cSrcweir         }
121cdf0e10cSrcweir         catch( Exception& )
122cdf0e10cSrcweir         {
123cdf0e10cSrcweir             DBG_ERROR( "PropertySetBase::notifyAndCachePropertyValue: this is not expected to fail!" );
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir     Any aOldValue = aPos->second;
127cdf0e10cSrcweir     // determine the current value
128cdf0e10cSrcweir     Any aNewValue;
129cdf0e10cSrcweir     getFastPropertyValue( aNewValue, nHandle );
130cdf0e10cSrcweir     // remember the old value
131cdf0e10cSrcweir     aPos->second = aNewValue;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     aGuard.clear();
134cdf0e10cSrcweir     if ( aNewValue != aOldValue )
135cdf0e10cSrcweir         firePropertyChange( nHandle, aNewValue, aOldValue );
136cdf0e10cSrcweir }
137cdf0e10cSrcweir 
initializePropertyValueCache(sal_Int32 nHandle)138cdf0e10cSrcweir void PropertySetBase::initializePropertyValueCache( sal_Int32 nHandle )
139cdf0e10cSrcweir {
140cdf0e10cSrcweir     Any aCurrentValue;
141cdf0e10cSrcweir     getFastPropertyValue( aCurrentValue, nHandle );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
144cdf0e10cSrcweir     ::std::pair< PropertyValueCache::iterator, bool > aInsertResult =
145cdf0e10cSrcweir #endif
146cdf0e10cSrcweir     m_aCache.insert( PropertyValueCache::value_type( nHandle, aCurrentValue ) );
147cdf0e10cSrcweir     DBG_ASSERT( aInsertResult.second, "PropertySetBase::initializePropertyValueCache: already cached a value for this property!" );
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
locatePropertyHandler(sal_Int32 nHandle) const150cdf0e10cSrcweir PropertyAccessorBase& PropertySetBase::locatePropertyHandler( sal_Int32 nHandle ) const
151cdf0e10cSrcweir {
152cdf0e10cSrcweir     PropertyAccessors::const_iterator aPropertyPos = m_aAccessors.find( nHandle );
153cdf0e10cSrcweir     DBG_ASSERT( aPropertyPos != m_aAccessors.end() && aPropertyPos->second.get(),
154cdf0e10cSrcweir         "PropertySetBase::locatePropertyHandler: accessor map is corrupted!" );
155cdf0e10cSrcweir         // neither should this be called for handles where there is no accessor, nor should a
156cdf0e10cSrcweir         // NULL accssor be in the map
157cdf0e10cSrcweir     return *aPropertyPos->second;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
convertFastPropertyValue(Any & rConvertedValue,Any & rOldValue,sal_Int32 nHandle,const Any & rValue)160cdf0e10cSrcweir sal_Bool SAL_CALL PropertySetBase::convertFastPropertyValue( Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle,
161cdf0e10cSrcweir 	const Any& rValue )
162cdf0e10cSrcweir     throw (IllegalArgumentException)
163cdf0e10cSrcweir {
164cdf0e10cSrcweir     PropertyAccessorBase& rAccessor = locatePropertyHandler( nHandle );
165cdf0e10cSrcweir     if ( !rAccessor.approveValue( rValue ) )
166cdf0e10cSrcweir         throw IllegalArgumentException( ::rtl::OUString(), *this, 0 );
167cdf0e10cSrcweir 
168cdf0e10cSrcweir     rAccessor.getValue( rOldValue );
169cdf0e10cSrcweir     if ( rOldValue != rValue )
170cdf0e10cSrcweir     {
171cdf0e10cSrcweir         rConvertedValue = rValue;   // no conversion at all
172cdf0e10cSrcweir         return sal_True;
173cdf0e10cSrcweir     }
174cdf0e10cSrcweir     return sal_False;
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any & rValue)177cdf0e10cSrcweir void SAL_CALL PropertySetBase::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue )
178cdf0e10cSrcweir     throw (Exception)
179cdf0e10cSrcweir {
180cdf0e10cSrcweir     PropertyAccessorBase& rAccessor = locatePropertyHandler( nHandle );
181cdf0e10cSrcweir     rAccessor.setValue( rValue );
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
getFastPropertyValue(Any & rValue,sal_Int32 nHandle) const184cdf0e10cSrcweir void SAL_CALL PropertySetBase::getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const
185cdf0e10cSrcweir {
186cdf0e10cSrcweir     PropertyAccessorBase& rAccessor = locatePropertyHandler( nHandle );
187cdf0e10cSrcweir     rAccessor.getValue( rValue );
188cdf0e10cSrcweir }
189