1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: propertysetbase.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_forms.hxx"
34 #include "propertysetbase.hxx"
36 #include <cppuhelper/typeprovider.hxx> // for getImplementationId()
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <com/sun/star/beans/XMultiPropertySet.hpp>
40 #include <com/sun/star/beans/XPropertyState.hpp>
41 #include <com/sun/star/uno/Reference.hxx>
42 #include <tools/debug.hxx>
46 using com::sun::star::uno::Any
;
47 using com::sun::star::uno::Type
;
48 using com::sun::star::uno::Sequence
;
49 using com::sun::star::uno::Reference
;
50 using com::sun::star::uno::Exception
;
51 using com::sun::star::uno::RuntimeException
;
52 using com::sun::star::lang::IllegalArgumentException
;
53 using com::sun::star::beans::Property
;
54 using com::sun::star::beans::XPropertySetInfo
;
56 oslInterlockedCount SAL_CALL
PropertyAccessorBase::acquire()
61 oslInterlockedCount SAL_CALL
PropertyAccessorBase::release()
63 if ( --m_refCount
== 0 )
71 PropertySetBase::PropertySetBase( )
72 :m_pProperties( NULL
)
76 PropertySetBase::~PropertySetBase( )
78 DELETEZ( m_pProperties
);
81 cppu::IPropertyArrayHelper
& SAL_CALL
PropertySetBase::getInfoHelper()
85 DBG_ASSERT( !m_aProperties
.empty(), "PropertySetBase::getInfoHelper: no registered properties!" );
86 m_pProperties
= new cppu::OPropertyArrayHelper( &m_aProperties
[0], m_aProperties
.size(), sal_False
);
88 return *m_pProperties
;
91 Reference
< XPropertySetInfo
> SAL_CALL
PropertySetBase::getPropertySetInfo( ) throw(RuntimeException
)
93 return cppu::OPropertySetHelper::createPropertySetInfo( getInfoHelper() );
96 void PropertySetBase::registerProperty( const Property
& rProperty
,
97 const ::rtl::Reference
< PropertyAccessorBase
>& rAccessor
)
99 DBG_ASSERT( rAccessor
.get(), "PropertySetBase::registerProperty: invalid property accessor, this will crash!" );
100 m_aAccessors
.insert( PropertyAccessors::value_type( rProperty
.Handle
, rAccessor
) );
102 DBG_ASSERT( ( rAccessor
->isWriteable() == true )
103 == ( ( rProperty
.Attributes
& com::sun::star::beans::PropertyAttribute::READONLY
) == 0 ),
104 "PropertySetBase::registerProperty: inconsistence!" );
106 m_aProperties
.push_back( rProperty
);
109 void PropertySetBase::notifyAndCachePropertyValue( sal_Int32 nHandle
)
111 ::osl::ClearableMutexGuard
aGuard( GetMutex() );
113 PropertyValueCache::iterator aPos
= m_aCache
.find( nHandle
);
114 if ( aPos
== m_aCache
.end() )
115 { // method has never before been invoked for this property
118 // determine the type of this property
119 ::cppu::IPropertyArrayHelper
& rPropertyMetaData
= getInfoHelper();
120 ::rtl::OUString sPropName
;
121 OSL_VERIFY( rPropertyMetaData
.fillPropertyMembersByHandle( &sPropName
, NULL
, nHandle
) );
122 Property aProperty
= rPropertyMetaData
.getPropertyByName( sPropName
);
123 // default construct a value of this type
124 Any
aEmptyValue( NULL
, aProperty
.Type
);
125 // insert into the cache
126 aPos
= m_aCache
.insert( PropertyValueCache::value_type( nHandle
, aEmptyValue
) ).first
;
130 DBG_ERROR( "PropertySetBase::notifyAndCachePropertyValue: this is not expected to fail!" );
133 Any aOldValue
= aPos
->second
;
134 // determine the current value
136 getFastPropertyValue( aNewValue
, nHandle
);
137 // remember the old value
138 aPos
->second
= aNewValue
;
141 if ( aNewValue
!= aOldValue
)
142 firePropertyChange( nHandle
, aNewValue
, aOldValue
);
145 void PropertySetBase::initializePropertyValueCache( sal_Int32 nHandle
)
148 getFastPropertyValue( aCurrentValue
, nHandle
);
150 #if OSL_DEBUG_LEVEL > 0
151 ::std::pair
< PropertyValueCache::iterator
, bool > aInsertResult
=
153 m_aCache
.insert( PropertyValueCache::value_type( nHandle
, aCurrentValue
) );
154 DBG_ASSERT( aInsertResult
.second
, "PropertySetBase::initializePropertyValueCache: already cached a value for this property!" );
157 PropertyAccessorBase
& PropertySetBase::locatePropertyHandler( sal_Int32 nHandle
) const
159 PropertyAccessors::const_iterator aPropertyPos
= m_aAccessors
.find( nHandle
);
160 DBG_ASSERT( 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 return *aPropertyPos
->second
;
167 sal_Bool SAL_CALL
PropertySetBase::convertFastPropertyValue( Any
& rConvertedValue
, Any
& rOldValue
, sal_Int32 nHandle
,
169 throw (IllegalArgumentException
)
171 PropertyAccessorBase
& rAccessor
= locatePropertyHandler( nHandle
);
172 if ( !rAccessor
.approveValue( rValue
) )
173 throw IllegalArgumentException( ::rtl::OUString(), *this, 0 );
175 rAccessor
.getValue( rOldValue
);
176 if ( rOldValue
!= rValue
)
178 rConvertedValue
= rValue
; // no conversion at all
184 void SAL_CALL
PropertySetBase::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
, const Any
& rValue
)
187 PropertyAccessorBase
& rAccessor
= locatePropertyHandler( nHandle
);
188 rAccessor
.setValue( rValue
);
191 void SAL_CALL
PropertySetBase::getFastPropertyValue( Any
& rValue
, sal_Int32 nHandle
) const
193 PropertyAccessorBase
& rAccessor
= locatePropertyHandler( nHandle
);
194 rAccessor
.getValue( rValue
);