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 :
20 :
21 : #include "propertysetbase.hxx"
22 :
23 : #include <cppuhelper/typeprovider.hxx> // for getImplementationId()
24 :
25 : #include <com/sun/star/beans/XPropertySet.hpp>
26 : #include <com/sun/star/beans/XMultiPropertySet.hpp>
27 : #include <com/sun/star/beans/XPropertyState.hpp>
28 : #include <com/sun/star/uno/Reference.hxx>
29 : #include <tools/solar.h>
30 :
31 : #include <vector>
32 :
33 : using com::sun::star::uno::Any;
34 : using com::sun::star::uno::Type;
35 : using com::sun::star::uno::Sequence;
36 : using com::sun::star::uno::Reference;
37 : using com::sun::star::uno::Exception;
38 : using com::sun::star::uno::RuntimeException;
39 : using com::sun::star::lang::IllegalArgumentException;
40 : using com::sun::star::beans::Property;
41 : using com::sun::star::beans::XPropertySetInfo;
42 :
43 0 : PropertyAccessorBase::~PropertyAccessorBase()
44 : {
45 0 : }
46 :
47 0 : oslInterlockedCount SAL_CALL PropertyAccessorBase::acquire()
48 : {
49 0 : return ++m_refCount;
50 : }
51 :
52 0 : oslInterlockedCount SAL_CALL PropertyAccessorBase::release()
53 : {
54 0 : if ( --m_refCount == 0 )
55 : {
56 0 : delete this;
57 0 : return 0;
58 : }
59 0 : return m_refCount;
60 : }
61 :
62 0 : PropertySetBase::PropertySetBase( )
63 0 : :m_pProperties( NULL )
64 : {
65 0 : }
66 :
67 0 : PropertySetBase::~PropertySetBase( )
68 : {
69 0 : DELETEZ( m_pProperties );
70 0 : }
71 :
72 0 : cppu::IPropertyArrayHelper& SAL_CALL PropertySetBase::getInfoHelper()
73 : {
74 0 : if ( !m_pProperties )
75 : {
76 : OSL_ENSURE( !m_aProperties.empty(), "PropertySetBase::getInfoHelper: no registered properties!" );
77 0 : m_pProperties = new cppu::OPropertyArrayHelper( &m_aProperties[0], m_aProperties.size(), sal_False );
78 : }
79 0 : return *m_pProperties;
80 : }
81 :
82 0 : Reference< XPropertySetInfo > SAL_CALL PropertySetBase::getPropertySetInfo( ) throw(RuntimeException)
83 : {
84 0 : return cppu::OPropertySetHelper::createPropertySetInfo( getInfoHelper() );
85 : }
86 :
87 0 : void PropertySetBase::registerProperty( const Property& rProperty,
88 : const ::rtl::Reference< PropertyAccessorBase >& rAccessor )
89 : {
90 : OSL_ENSURE( rAccessor.get(), "PropertySetBase::registerProperty: invalid property accessor, this will crash!" );
91 0 : m_aAccessors.insert( PropertyAccessors::value_type( rProperty.Handle, rAccessor ) );
92 :
93 : OSL_ENSURE( ( rAccessor->isWriteable() == true )
94 : == ( ( rProperty.Attributes & com::sun::star::beans::PropertyAttribute::READONLY ) == 0 ),
95 : "PropertySetBase::registerProperty: inconsistence!" );
96 :
97 0 : m_aProperties.push_back( rProperty );
98 0 : }
99 :
100 0 : void PropertySetBase::notifyAndCachePropertyValue( sal_Int32 nHandle )
101 : {
102 0 : ::osl::ClearableMutexGuard aGuard( GetMutex() );
103 :
104 0 : PropertyValueCache::iterator aPos = m_aCache.find( nHandle );
105 0 : if ( aPos == m_aCache.end() )
106 : { // method has never before been invoked for this property
107 : try
108 : {
109 : // determine the type of this property
110 0 : ::cppu::IPropertyArrayHelper& rPropertyMetaData = getInfoHelper();
111 0 : ::rtl::OUString sPropName;
112 0 : OSL_VERIFY( rPropertyMetaData.fillPropertyMembersByHandle( &sPropName, NULL, nHandle ) );
113 0 : Property aProperty = rPropertyMetaData.getPropertyByName( sPropName );
114 : // default construct a value of this type
115 0 : Any aEmptyValue( NULL, aProperty.Type );
116 : // insert into the cache
117 0 : aPos = m_aCache.insert( PropertyValueCache::value_type( nHandle, aEmptyValue ) ).first;
118 : }
119 0 : catch( const Exception& )
120 : {
121 : OSL_FAIL( "PropertySetBase::notifyAndCachePropertyValue: this is not expected to fail!" );
122 : }
123 : }
124 0 : Any aOldValue = aPos->second;
125 : // determine the current value
126 0 : Any aNewValue;
127 0 : getFastPropertyValue( aNewValue, nHandle );
128 : // remember the old value
129 0 : aPos->second = aNewValue;
130 :
131 0 : aGuard.clear();
132 0 : if ( aNewValue != aOldValue )
133 0 : firePropertyChange( nHandle, aNewValue, aOldValue );
134 0 : }
135 :
136 0 : void PropertySetBase::initializePropertyValueCache( sal_Int32 nHandle )
137 : {
138 0 : Any aCurrentValue;
139 0 : getFastPropertyValue( aCurrentValue, nHandle );
140 :
141 : #if OSL_DEBUG_LEVEL > 0
142 : ::std::pair< PropertyValueCache::iterator, bool > aInsertResult =
143 : #endif
144 0 : m_aCache.insert( PropertyValueCache::value_type( nHandle, aCurrentValue ) );
145 0 : OSL_ENSURE( aInsertResult.second, "PropertySetBase::initializePropertyValueCache: already cached a value for this property!" );
146 0 : }
147 :
148 0 : PropertyAccessorBase& PropertySetBase::locatePropertyHandler( sal_Int32 nHandle ) const
149 : {
150 0 : PropertyAccessors::const_iterator aPropertyPos = m_aAccessors.find( nHandle );
151 : OSL_ENSURE( aPropertyPos != m_aAccessors.end() && aPropertyPos->second.get(),
152 : "PropertySetBase::locatePropertyHandler: accessor map is corrupted!" );
153 : // neither should this be called for handles where there is no accessor, nor should a
154 : // NULL accssor be in the map
155 0 : return *aPropertyPos->second;
156 : }
157 :
158 0 : sal_Bool SAL_CALL PropertySetBase::convertFastPropertyValue( Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle,
159 : const Any& rValue )
160 : throw (IllegalArgumentException)
161 : {
162 0 : PropertyAccessorBase& rAccessor = locatePropertyHandler( nHandle );
163 0 : if ( !rAccessor.approveValue( rValue ) )
164 0 : throw IllegalArgumentException( ::rtl::OUString(), *this, 0 );
165 :
166 0 : rAccessor.getValue( rOldValue );
167 0 : if ( rOldValue != rValue )
168 : {
169 0 : rConvertedValue = rValue; // no conversion at all
170 0 : return sal_True;
171 : }
172 0 : return sal_False;
173 : }
174 :
175 0 : void SAL_CALL PropertySetBase::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue )
176 : throw (Exception)
177 : {
178 0 : PropertyAccessorBase& rAccessor = locatePropertyHandler( nHandle );
179 0 : rAccessor.setValue( rValue );
180 0 : }
181 :
182 0 : void SAL_CALL PropertySetBase::getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const
183 : {
184 0 : PropertyAccessorBase& rAccessor = locatePropertyHandler( nHandle );
185 0 : rAccessor.getValue( rValue );
186 0 : }
187 :
188 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|