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