1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include "comphelper/propertybag.hxx"
32 /** === begin UNO includes === **/
33 #include <com/sun/star/beans/IllegalTypeException.hpp>
34 #include <com/sun/star/beans/PropertyExistException.hpp>
35 #include <com/sun/star/lang/IllegalArgumentException.hpp>
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 #include <com/sun/star/beans/NotRemoveableException.hpp>
38 /** === end UNO includes === **/
42 //........................................................................
45 //........................................................................
47 /** === begin UNO using === **/
48 using ::com::sun::star::uno::Any
;
49 using ::com::sun::star::uno::Type
;
50 using ::com::sun::star::uno::TypeClass_VOID
;
51 using ::com::sun::star::beans::IllegalTypeException
;
52 using ::com::sun::star::beans::PropertyExistException
;
53 using ::com::sun::star::lang::IllegalArgumentException
;
54 using ::com::sun::star::beans::Property
;
55 using ::com::sun::star::beans::NotRemoveableException
;
56 using ::com::sun::star::beans::UnknownPropertyException
;
57 /** === end UNO using === **/
58 namespace PropertyAttribute
= ::com::sun::star::beans::PropertyAttribute
;
60 //====================================================================
62 //====================================================================
63 typedef ::std::map
< sal_Int32
, Any
> MapInt2Any
;
64 struct PropertyBag_Impl
66 PropertyBag_Impl() : m_bAllowEmptyPropertyName(false) { }
68 bool m_bAllowEmptyPropertyName
;
71 //====================================================================
73 //====================================================================
74 //--------------------------------------------------------------------
75 PropertyBag::PropertyBag()
76 :m_pImpl( new PropertyBag_Impl
)
80 PropertyBag::~PropertyBag()
84 //--------------------------------------------------------------------
85 void PropertyBag::setAllowEmptyPropertyName( bool i_isAllowed
)
87 m_pImpl
->m_bAllowEmptyPropertyName
= i_isAllowed
;
90 //--------------------------------------------------------------------
93 void lcl_checkForEmptyName( const bool _allowEmpty
, const ::rtl::OUString
& _name
)
95 if ( !_allowEmpty
&& !_name
.getLength() )
96 throw IllegalArgumentException(
97 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "The property name must not be empty." ) ),
104 void lcl_checkNameAndHandle( const ::rtl::OUString
& _name
, const sal_Int32 _handle
, const PropertyBag
& _container
)
106 if ( _container
.hasPropertyByName( _name
) || _container
.hasPropertyByHandle( _handle
) )
107 throw PropertyExistException(
108 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Property name or handle already used." ) ),
115 //--------------------------------------------------------------------
116 void PropertyBag::addVoidProperty( const ::rtl::OUString
& _rName
, const Type
& _rType
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
)
118 if ( _rType
.getTypeClass() == TypeClass_VOID
)
119 throw IllegalArgumentException(
120 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Illegal property type: VOID" ) ),
126 // check name/handle sanity
127 lcl_checkForEmptyName( m_pImpl
->m_bAllowEmptyPropertyName
, _rName
);
128 lcl_checkNameAndHandle( _rName
, _nHandle
, *this );
130 // register the property
131 OSL_ENSURE( _nAttributes
& PropertyAttribute::MAYBEVOID
, "PropertyBag::addVoidProperty: this is for default-void properties only!" );
132 registerPropertyNoMember( _rName
, _nHandle
, _nAttributes
| PropertyAttribute::MAYBEVOID
, _rType
, NULL
);
134 // remember the default
135 m_pImpl
->aDefaults
.insert( MapInt2Any::value_type( _nHandle
, Any() ) );
138 //--------------------------------------------------------------------
139 void PropertyBag::addProperty( const ::rtl::OUString
& _rName
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
, const Any
& _rInitialValue
)
142 Type aPropertyType
= _rInitialValue
.getValueType();
143 if ( aPropertyType
.getTypeClass() == TypeClass_VOID
)
144 throw IllegalTypeException(
145 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "The initial value must be non-NULL to determine the property type." ) ),
149 // check name/handle sanity
150 lcl_checkForEmptyName( m_pImpl
->m_bAllowEmptyPropertyName
, _rName
);
151 lcl_checkNameAndHandle( _rName
, _nHandle
, *this );
153 // register the property
154 registerPropertyNoMember( _rName
, _nHandle
, _nAttributes
, aPropertyType
,
155 _rInitialValue
.hasValue() ? _rInitialValue
.getValue() : NULL
);
157 // remember the default
158 m_pImpl
->aDefaults
.insert( MapInt2Any::value_type( _nHandle
, _rInitialValue
) );
161 //--------------------------------------------------------------------
162 void PropertyBag::removeProperty( const ::rtl::OUString
& _rName
)
164 const Property
& rProp
= getProperty( _rName
);
165 // will throw an UnknownPropertyException if necessary
166 if ( ( rProp
.Attributes
& PropertyAttribute::REMOVEABLE
) == 0 )
167 throw NotRemoveableException( ::rtl::OUString(), NULL
);
168 const sal_Int32 nHandle
= rProp
.Handle
;
170 revokeProperty( nHandle
);
172 m_pImpl
->aDefaults
.erase( nHandle
);
175 //--------------------------------------------------------------------
176 void PropertyBag::getFastPropertyValue( sal_Int32 _nHandle
, Any
& _out_rValue
) const
178 if ( !hasPropertyByHandle( _nHandle
) )
179 throw UnknownPropertyException();
181 OPropertyContainerHelper::getFastPropertyValue( _out_rValue
, _nHandle
);
184 //--------------------------------------------------------------------
185 bool PropertyBag::convertFastPropertyValue( sal_Int32 _nHandle
, const Any
& _rNewValue
, Any
& _out_rConvertedValue
, Any
& _out_rCurrentValue
) const
187 if ( !hasPropertyByHandle( _nHandle
) )
188 throw UnknownPropertyException();
190 return const_cast< PropertyBag
* >( this )->OPropertyContainerHelper::convertFastPropertyValue(
191 _out_rConvertedValue
, _out_rCurrentValue
, _nHandle
, _rNewValue
);
194 //--------------------------------------------------------------------
195 void PropertyBag::setFastPropertyValue( sal_Int32 _nHandle
, const Any
& _rValue
)
197 if ( !hasPropertyByHandle( _nHandle
) )
198 throw UnknownPropertyException();
200 OPropertyContainerHelper::setFastPropertyValue( _nHandle
, _rValue
);
203 //--------------------------------------------------------------------
204 void PropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle
, Any
& _out_rValue
) const
206 if ( !hasPropertyByHandle( _nHandle
) )
207 throw UnknownPropertyException();
209 MapInt2Any::const_iterator pos
= m_pImpl
->aDefaults
.find( _nHandle
);
210 OSL_ENSURE( pos
!= m_pImpl
->aDefaults
.end(), "PropertyBag::getPropertyDefaultByHandle: inconsistency!" );
211 if ( pos
!= m_pImpl
->aDefaults
.end() )
212 _out_rValue
= pos
->second
;
218 //........................................................................
219 } // namespace comphelper
220 //........................................................................