1dde7d3faSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3dde7d3faSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4dde7d3faSAndrew Rist * or more contributor license agreements. See the NOTICE file 5dde7d3faSAndrew Rist * distributed with this work for additional information 6dde7d3faSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7dde7d3faSAndrew Rist * to you under the Apache License, Version 2.0 (the 8dde7d3faSAndrew Rist * "License"); you may not use this file except in compliance 9dde7d3faSAndrew Rist * with the License. You may obtain a copy of the License at 10dde7d3faSAndrew Rist * 11dde7d3faSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12dde7d3faSAndrew Rist * 13dde7d3faSAndrew Rist * Unless required by applicable law or agreed to in writing, 14dde7d3faSAndrew Rist * software distributed under the License is distributed on an 15dde7d3faSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16dde7d3faSAndrew Rist * KIND, either express or implied. See the License for the 17dde7d3faSAndrew Rist * specific language governing permissions and limitations 18dde7d3faSAndrew Rist * under the License. 19dde7d3faSAndrew Rist * 20dde7d3faSAndrew Rist *************************************************************/ 21dde7d3faSAndrew Rist 22dde7d3faSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_comphelper.hxx" 26cdf0e10cSrcweir #include "comphelper/propertybag.hxx" 27cdf0e10cSrcweir 28cdf0e10cSrcweir /** === begin UNO includes === **/ 29cdf0e10cSrcweir #include <com/sun/star/beans/IllegalTypeException.hpp> 30cdf0e10cSrcweir #include <com/sun/star/beans/PropertyExistException.hpp> 31cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp> 32cdf0e10cSrcweir #include <com/sun/star/beans/PropertyAttribute.hpp> 33cdf0e10cSrcweir #include <com/sun/star/beans/NotRemoveableException.hpp> 34cdf0e10cSrcweir /** === end UNO includes === **/ 35cdf0e10cSrcweir 36cdf0e10cSrcweir #include <map> 37cdf0e10cSrcweir 38cdf0e10cSrcweir //........................................................................ 39cdf0e10cSrcweir namespace comphelper 40cdf0e10cSrcweir { 41cdf0e10cSrcweir //........................................................................ 42cdf0e10cSrcweir 43cdf0e10cSrcweir /** === begin UNO using === **/ 44cdf0e10cSrcweir using ::com::sun::star::uno::Any; 45cdf0e10cSrcweir using ::com::sun::star::uno::Type; 46cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_VOID; 47cdf0e10cSrcweir using ::com::sun::star::beans::IllegalTypeException; 48cdf0e10cSrcweir using ::com::sun::star::beans::PropertyExistException; 49cdf0e10cSrcweir using ::com::sun::star::lang::IllegalArgumentException; 50cdf0e10cSrcweir using ::com::sun::star::beans::Property; 51cdf0e10cSrcweir using ::com::sun::star::beans::NotRemoveableException; 52cdf0e10cSrcweir using ::com::sun::star::beans::UnknownPropertyException; 53cdf0e10cSrcweir /** === end UNO using === **/ 54cdf0e10cSrcweir namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute; 55cdf0e10cSrcweir 56cdf0e10cSrcweir //==================================================================== 57cdf0e10cSrcweir //= PropertyBag_Impl 58cdf0e10cSrcweir //==================================================================== 59cdf0e10cSrcweir typedef ::std::map< sal_Int32, Any > MapInt2Any; 60cdf0e10cSrcweir struct PropertyBag_Impl 61cdf0e10cSrcweir { PropertyBag_Implcomphelper::PropertyBag_Impl62cdf0e10cSrcweir PropertyBag_Impl() : m_bAllowEmptyPropertyName(false) { } 63cdf0e10cSrcweir MapInt2Any aDefaults; 64cdf0e10cSrcweir bool m_bAllowEmptyPropertyName; 65cdf0e10cSrcweir }; 66cdf0e10cSrcweir 67cdf0e10cSrcweir //==================================================================== 68cdf0e10cSrcweir //= PropertyBag 69cdf0e10cSrcweir //==================================================================== 70cdf0e10cSrcweir //-------------------------------------------------------------------- PropertyBag()71cdf0e10cSrcweir PropertyBag::PropertyBag() 72cdf0e10cSrcweir :m_pImpl( new PropertyBag_Impl ) 73cdf0e10cSrcweir { 74cdf0e10cSrcweir } 75cdf0e10cSrcweir ~PropertyBag()76cdf0e10cSrcweir PropertyBag::~PropertyBag() 77cdf0e10cSrcweir { 78cdf0e10cSrcweir } 79cdf0e10cSrcweir 80cdf0e10cSrcweir //-------------------------------------------------------------------- setAllowEmptyPropertyName(bool i_isAllowed)81cdf0e10cSrcweir void PropertyBag::setAllowEmptyPropertyName( bool i_isAllowed ) 82cdf0e10cSrcweir { 83cdf0e10cSrcweir m_pImpl->m_bAllowEmptyPropertyName = i_isAllowed; 84cdf0e10cSrcweir } 85cdf0e10cSrcweir 86cdf0e10cSrcweir //-------------------------------------------------------------------- 87cdf0e10cSrcweir namespace 88cdf0e10cSrcweir { lcl_checkForEmptyName(const bool _allowEmpty,const::rtl::OUString & _name)89cdf0e10cSrcweir void lcl_checkForEmptyName( const bool _allowEmpty, const ::rtl::OUString& _name ) 90cdf0e10cSrcweir { 91*49b34792SHerbert Dürr if ( !_allowEmpty && _name.isEmpty() ) 92cdf0e10cSrcweir throw IllegalArgumentException( 93cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "The property name must not be empty." ) ), 94cdf0e10cSrcweir // TODO: resource 95cdf0e10cSrcweir NULL, 96cdf0e10cSrcweir 1 97cdf0e10cSrcweir ); 98cdf0e10cSrcweir } 99cdf0e10cSrcweir lcl_checkNameAndHandle(const::rtl::OUString & _name,const sal_Int32 _handle,const PropertyBag & _container)100cdf0e10cSrcweir void lcl_checkNameAndHandle( const ::rtl::OUString& _name, const sal_Int32 _handle, const PropertyBag& _container ) 101cdf0e10cSrcweir { 102cdf0e10cSrcweir if ( _container.hasPropertyByName( _name ) || _container.hasPropertyByHandle( _handle ) ) 103cdf0e10cSrcweir throw PropertyExistException( 104cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Property name or handle already used." ) ), 105cdf0e10cSrcweir // TODO: resource 106cdf0e10cSrcweir NULL ); 107cdf0e10cSrcweir 108cdf0e10cSrcweir } 109cdf0e10cSrcweir } 110cdf0e10cSrcweir 111cdf0e10cSrcweir //-------------------------------------------------------------------- addVoidProperty(const::rtl::OUString & _rName,const Type & _rType,sal_Int32 _nHandle,sal_Int32 _nAttributes)112cdf0e10cSrcweir void PropertyBag::addVoidProperty( const ::rtl::OUString& _rName, const Type& _rType, sal_Int32 _nHandle, sal_Int32 _nAttributes ) 113cdf0e10cSrcweir { 114cdf0e10cSrcweir if ( _rType.getTypeClass() == TypeClass_VOID ) 115cdf0e10cSrcweir throw IllegalArgumentException( 116cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Illegal property type: VOID" ) ), 117cdf0e10cSrcweir // TODO: resource 118cdf0e10cSrcweir NULL, 119cdf0e10cSrcweir 1 120cdf0e10cSrcweir ); 121cdf0e10cSrcweir 122cdf0e10cSrcweir // check name/handle sanity 123cdf0e10cSrcweir lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName ); 124cdf0e10cSrcweir lcl_checkNameAndHandle( _rName, _nHandle, *this ); 125cdf0e10cSrcweir 126cdf0e10cSrcweir // register the property 127cdf0e10cSrcweir OSL_ENSURE( _nAttributes & PropertyAttribute::MAYBEVOID, "PropertyBag::addVoidProperty: this is for default-void properties only!" ); 128cdf0e10cSrcweir registerPropertyNoMember( _rName, _nHandle, _nAttributes | PropertyAttribute::MAYBEVOID, _rType, NULL ); 129cdf0e10cSrcweir 130cdf0e10cSrcweir // remember the default 131cdf0e10cSrcweir m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, Any() ) ); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir //-------------------------------------------------------------------- addProperty(const::rtl::OUString & _rName,sal_Int32 _nHandle,sal_Int32 _nAttributes,const Any & _rInitialValue)135cdf0e10cSrcweir void PropertyBag::addProperty( const ::rtl::OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, const Any& _rInitialValue ) 136cdf0e10cSrcweir { 137cdf0e10cSrcweir // check type sanity 138cdf0e10cSrcweir Type aPropertyType = _rInitialValue.getValueType(); 139cdf0e10cSrcweir if ( aPropertyType.getTypeClass() == TypeClass_VOID ) 140cdf0e10cSrcweir throw IllegalTypeException( 141cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "The initial value must be non-NULL to determine the property type." ) ), 142cdf0e10cSrcweir // TODO: resource 143cdf0e10cSrcweir NULL ); 144cdf0e10cSrcweir 145cdf0e10cSrcweir // check name/handle sanity 146cdf0e10cSrcweir lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName ); 147cdf0e10cSrcweir lcl_checkNameAndHandle( _rName, _nHandle, *this ); 148cdf0e10cSrcweir 149cdf0e10cSrcweir // register the property 150cdf0e10cSrcweir registerPropertyNoMember( _rName, _nHandle, _nAttributes, aPropertyType, 151cdf0e10cSrcweir _rInitialValue.hasValue() ? _rInitialValue.getValue() : NULL ); 152cdf0e10cSrcweir 153cdf0e10cSrcweir // remember the default 154cdf0e10cSrcweir m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, _rInitialValue ) ); 155cdf0e10cSrcweir } 156cdf0e10cSrcweir 157cdf0e10cSrcweir //-------------------------------------------------------------------- removeProperty(const::rtl::OUString & _rName)158cdf0e10cSrcweir void PropertyBag::removeProperty( const ::rtl::OUString& _rName ) 159cdf0e10cSrcweir { 160cdf0e10cSrcweir const Property& rProp = getProperty( _rName ); 161cdf0e10cSrcweir // will throw an UnknownPropertyException if necessary 162cdf0e10cSrcweir if ( ( rProp.Attributes & PropertyAttribute::REMOVEABLE ) == 0 ) 163cdf0e10cSrcweir throw NotRemoveableException( ::rtl::OUString(), NULL ); 164cdf0e10cSrcweir const sal_Int32 nHandle = rProp.Handle; 165cdf0e10cSrcweir 166cdf0e10cSrcweir revokeProperty( nHandle ); 167cdf0e10cSrcweir 168cdf0e10cSrcweir m_pImpl->aDefaults.erase( nHandle ); 169cdf0e10cSrcweir } 170cdf0e10cSrcweir 171cdf0e10cSrcweir //-------------------------------------------------------------------- getFastPropertyValue(sal_Int32 _nHandle,Any & _out_rValue) const172cdf0e10cSrcweir void PropertyBag::getFastPropertyValue( sal_Int32 _nHandle, Any& _out_rValue ) const 173cdf0e10cSrcweir { 174cdf0e10cSrcweir if ( !hasPropertyByHandle( _nHandle ) ) 175cdf0e10cSrcweir throw UnknownPropertyException(); 176cdf0e10cSrcweir 177cdf0e10cSrcweir OPropertyContainerHelper::getFastPropertyValue( _out_rValue, _nHandle ); 178cdf0e10cSrcweir } 179cdf0e10cSrcweir 180cdf0e10cSrcweir //-------------------------------------------------------------------- convertFastPropertyValue(sal_Int32 _nHandle,const Any & _rNewValue,Any & _out_rConvertedValue,Any & _out_rCurrentValue) const181cdf0e10cSrcweir bool PropertyBag::convertFastPropertyValue( sal_Int32 _nHandle, const Any& _rNewValue, Any& _out_rConvertedValue, Any& _out_rCurrentValue ) const 182cdf0e10cSrcweir { 183cdf0e10cSrcweir if ( !hasPropertyByHandle( _nHandle ) ) 184cdf0e10cSrcweir throw UnknownPropertyException(); 185cdf0e10cSrcweir 186cdf0e10cSrcweir return const_cast< PropertyBag* >( this )->OPropertyContainerHelper::convertFastPropertyValue( 187cdf0e10cSrcweir _out_rConvertedValue, _out_rCurrentValue, _nHandle, _rNewValue ); 188cdf0e10cSrcweir } 189cdf0e10cSrcweir 190cdf0e10cSrcweir //-------------------------------------------------------------------- setFastPropertyValue(sal_Int32 _nHandle,const Any & _rValue)191cdf0e10cSrcweir void PropertyBag::setFastPropertyValue( sal_Int32 _nHandle, const Any& _rValue ) 192cdf0e10cSrcweir { 193cdf0e10cSrcweir if ( !hasPropertyByHandle( _nHandle ) ) 194cdf0e10cSrcweir throw UnknownPropertyException(); 195cdf0e10cSrcweir 196cdf0e10cSrcweir OPropertyContainerHelper::setFastPropertyValue( _nHandle, _rValue ); 197cdf0e10cSrcweir } 198cdf0e10cSrcweir 199cdf0e10cSrcweir //-------------------------------------------------------------------- getPropertyDefaultByHandle(sal_Int32 _nHandle,Any & _out_rValue) const200cdf0e10cSrcweir void PropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle, Any& _out_rValue ) const 201cdf0e10cSrcweir { 202cdf0e10cSrcweir if ( !hasPropertyByHandle( _nHandle ) ) 203cdf0e10cSrcweir throw UnknownPropertyException(); 204cdf0e10cSrcweir 205cdf0e10cSrcweir MapInt2Any::const_iterator pos = m_pImpl->aDefaults.find( _nHandle ); 206cdf0e10cSrcweir OSL_ENSURE( pos != m_pImpl->aDefaults.end(), "PropertyBag::getPropertyDefaultByHandle: inconsistency!" ); 207cdf0e10cSrcweir if ( pos != m_pImpl->aDefaults.end() ) 208cdf0e10cSrcweir _out_rValue = pos->second; 209cdf0e10cSrcweir else 210cdf0e10cSrcweir _out_rValue.clear(); 211cdf0e10cSrcweir } 212cdf0e10cSrcweir 213cdf0e10cSrcweir 214cdf0e10cSrcweir //........................................................................ 215cdf0e10cSrcweir } // namespace comphelper 216cdf0e10cSrcweir //........................................................................ 217cdf0e10cSrcweir 218