update dev300-m57
[ooovba.git] / comphelper / source / property / propertybag.cxx
blob365797652bc418db7f0aec1c8aa6ea9e9cb0bbba
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: propertybag.cxx,v $
10 * $Revision: 1.4.60.1 $
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_comphelper.hxx"
33 #include "comphelper/propertybag.hxx"
35 /** === begin UNO includes === **/
36 #include <com/sun/star/beans/IllegalTypeException.hpp>
37 #include <com/sun/star/beans/PropertyExistException.hpp>
38 #include <com/sun/star/lang/IllegalArgumentException.hpp>
39 #include <com/sun/star/beans/PropertyAttribute.hpp>
40 #include <com/sun/star/beans/NotRemoveableException.hpp>
41 /** === end UNO includes === **/
43 #include <map>
45 //........................................................................
46 namespace comphelper
48 //........................................................................
50 /** === begin UNO using === **/
51 using ::com::sun::star::uno::Any;
52 using ::com::sun::star::uno::Type;
53 using ::com::sun::star::uno::TypeClass_VOID;
54 using ::com::sun::star::beans::IllegalTypeException;
55 using ::com::sun::star::beans::PropertyExistException;
56 using ::com::sun::star::lang::IllegalArgumentException;
57 using ::com::sun::star::beans::Property;
58 using ::com::sun::star::beans::NotRemoveableException;
59 using ::com::sun::star::beans::UnknownPropertyException;
60 /** === end UNO using === **/
61 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
63 //====================================================================
64 //= PropertyBag_Impl
65 //====================================================================
66 typedef ::std::map< sal_Int32, Any > MapInt2Any;
67 struct PropertyBag_Impl
69 PropertyBag_Impl() : m_bAllowEmptyPropertyName(false) { }
70 MapInt2Any aDefaults;
71 bool m_bAllowEmptyPropertyName;
74 //====================================================================
75 //= PropertyBag
76 //====================================================================
77 //--------------------------------------------------------------------
78 PropertyBag::PropertyBag()
79 :m_pImpl( new PropertyBag_Impl )
83 PropertyBag::~PropertyBag()
87 //--------------------------------------------------------------------
88 void PropertyBag::setAllowEmptyPropertyName( bool i_isAllowed )
90 m_pImpl->m_bAllowEmptyPropertyName = i_isAllowed;
93 //--------------------------------------------------------------------
94 namespace
96 void lcl_checkForEmptyName( const bool _allowEmpty, const ::rtl::OUString& _name )
98 if ( !_allowEmpty && !_name.getLength() )
99 throw IllegalArgumentException(
100 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "The property name must not be empty." ) ),
101 // TODO: resource
102 NULL,
107 void lcl_checkNameAndHandle( const ::rtl::OUString& _name, const sal_Int32 _handle, const PropertyBag& _container )
109 if ( _container.hasPropertyByName( _name ) || _container.hasPropertyByHandle( _handle ) )
110 throw PropertyExistException(
111 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Property name or handle already used." ) ),
112 // TODO: resource
113 NULL );
118 //--------------------------------------------------------------------
119 void PropertyBag::addVoidProperty( const ::rtl::OUString& _rName, const Type& _rType, sal_Int32 _nHandle, sal_Int32 _nAttributes )
121 if ( _rType.getTypeClass() == TypeClass_VOID )
122 throw IllegalArgumentException(
123 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Illegal property type: VOID" ) ),
124 // TODO: resource
125 NULL,
129 // check name/handle sanity
130 lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
131 lcl_checkNameAndHandle( _rName, _nHandle, *this );
133 // register the property
134 OSL_ENSURE( _nAttributes & PropertyAttribute::MAYBEVOID, "PropertyBag::addVoidProperty: this is for default-void properties only!" );
135 registerPropertyNoMember( _rName, _nHandle, _nAttributes | PropertyAttribute::MAYBEVOID, _rType, NULL );
137 // remember the default
138 m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, Any() ) );
141 //--------------------------------------------------------------------
142 void PropertyBag::addProperty( const ::rtl::OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, const Any& _rInitialValue )
144 // check type sanity
145 Type aPropertyType = _rInitialValue.getValueType();
146 if ( aPropertyType.getTypeClass() == TypeClass_VOID )
147 throw IllegalTypeException(
148 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "The initial value must be non-NULL to determine the property type." ) ),
149 // TODO: resource
150 NULL );
152 // check name/handle sanity
153 lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
154 lcl_checkNameAndHandle( _rName, _nHandle, *this );
156 // register the property
157 registerPropertyNoMember( _rName, _nHandle, _nAttributes, aPropertyType,
158 _rInitialValue.hasValue() ? _rInitialValue.getValue() : NULL );
160 // remember the default
161 m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, _rInitialValue ) );
164 //--------------------------------------------------------------------
165 void PropertyBag::removeProperty( const ::rtl::OUString& _rName )
167 const Property& rProp = getProperty( _rName );
168 // will throw an UnknownPropertyException if necessary
169 if ( ( rProp.Attributes & PropertyAttribute::REMOVEABLE ) == 0 )
170 throw NotRemoveableException( ::rtl::OUString(), NULL );
171 const sal_Int32 nHandle = rProp.Handle;
173 revokeProperty( nHandle );
175 m_pImpl->aDefaults.erase( nHandle );
178 //--------------------------------------------------------------------
179 void PropertyBag::getFastPropertyValue( sal_Int32 _nHandle, Any& _out_rValue ) const
181 if ( !hasPropertyByHandle( _nHandle ) )
182 throw UnknownPropertyException();
184 OPropertyContainerHelper::getFastPropertyValue( _out_rValue, _nHandle );
187 //--------------------------------------------------------------------
188 bool PropertyBag::convertFastPropertyValue( sal_Int32 _nHandle, const Any& _rNewValue, Any& _out_rConvertedValue, Any& _out_rCurrentValue ) const
190 if ( !hasPropertyByHandle( _nHandle ) )
191 throw UnknownPropertyException();
193 return const_cast< PropertyBag* >( this )->OPropertyContainerHelper::convertFastPropertyValue(
194 _out_rConvertedValue, _out_rCurrentValue, _nHandle, _rNewValue );
197 //--------------------------------------------------------------------
198 void PropertyBag::setFastPropertyValue( sal_Int32 _nHandle, const Any& _rValue )
200 if ( !hasPropertyByHandle( _nHandle ) )
201 throw UnknownPropertyException();
203 OPropertyContainerHelper::setFastPropertyValue( _nHandle, _rValue );
206 //--------------------------------------------------------------------
207 void PropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle, Any& _out_rValue ) const
209 if ( !hasPropertyByHandle( _nHandle ) )
210 throw UnknownPropertyException();
212 MapInt2Any::const_iterator pos = m_pImpl->aDefaults.find( _nHandle );
213 OSL_ENSURE( pos != m_pImpl->aDefaults.end(), "PropertyBag::getPropertyDefaultByHandle: inconsistency!" );
214 if ( pos != m_pImpl->aDefaults.end() )
215 _out_rValue = pos->second;
216 else
217 _out_rValue.clear();
221 //........................................................................
222 } // namespace comphelper
223 //........................................................................