bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / property / propertybag.cxx
blobdaa05cd5893536666281818b580df873825b82d7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/propertybag.hxx>
22 #include <com/sun/star/beans/IllegalTypeException.hpp>
23 #include <com/sun/star/beans/PropertyExistException.hpp>
24 #include <com/sun/star/container/ElementExistException.hpp>
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <com/sun/star/beans/NotRemoveableException.hpp>
29 #include <map>
32 namespace comphelper
36 using ::com::sun::star::uno::Any;
37 using ::com::sun::star::uno::Type;
38 using ::com::sun::star::uno::TypeClass_VOID;
39 using ::com::sun::star::beans::IllegalTypeException;
40 using ::com::sun::star::beans::PropertyExistException;
41 using ::com::sun::star::container::ElementExistException;
42 using ::com::sun::star::lang::IllegalArgumentException;
43 using ::com::sun::star::beans::Property;
44 using ::com::sun::star::beans::NotRemoveableException;
45 using ::com::sun::star::beans::UnknownPropertyException;
47 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
49 typedef ::std::map< sal_Int32, Any > MapInt2Any;
50 struct PropertyBag_Impl
52 PropertyBag_Impl() : m_bAllowEmptyPropertyName(false) { }
53 MapInt2Any aDefaults;
54 bool m_bAllowEmptyPropertyName;
57 PropertyBag::PropertyBag()
58 :m_pImpl( new PropertyBag_Impl )
62 PropertyBag::~PropertyBag()
67 void PropertyBag::setAllowEmptyPropertyName( bool i_isAllowed )
69 m_pImpl->m_bAllowEmptyPropertyName = i_isAllowed;
73 namespace
75 void lcl_checkForEmptyName( const bool _allowEmpty, const OUString& _name )
77 if ( !_allowEmpty && _name.isEmpty() )
78 throw IllegalArgumentException(
79 "The property name must not be empty.",
80 // TODO: resource
81 NULL,
86 void lcl_checkNameAndHandle_PropertyExistException( const OUString& _name, const sal_Int32 _handle, const PropertyBag& _container )
88 if ( _container.hasPropertyByName( _name ) || _container.hasPropertyByHandle( _handle ) )
89 throw PropertyExistException(
90 "Property name or handle already used.",
91 NULL );
95 void lcl_checkNameAndHandle_ElementExistException( const OUString& _name, const sal_Int32 _handle, const PropertyBag& _container )
97 if ( _container.hasPropertyByName( _name ) || _container.hasPropertyByHandle( _handle ) )
98 throw ElementExistException(
99 "Property name or handle already used.",
100 NULL );
107 void PropertyBag::addVoidProperty( const OUString& _rName, const Type& _rType, sal_Int32 _nHandle, sal_Int32 _nAttributes )
109 if ( _rType.getTypeClass() == TypeClass_VOID )
110 throw IllegalArgumentException(
111 "Illegal property type: VOID",
112 // TODO: resource
113 NULL,
117 // check name/handle sanity
118 lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
119 lcl_checkNameAndHandle_ElementExistException( _rName, _nHandle, *this );
121 // register the property
122 OSL_ENSURE( _nAttributes & PropertyAttribute::MAYBEVOID, "PropertyBag::addVoidProperty: this is for default-void properties only!" );
123 registerPropertyNoMember( _rName, _nHandle, _nAttributes | PropertyAttribute::MAYBEVOID, _rType, NULL );
125 // remember the default
126 m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, Any() ) );
130 void PropertyBag::addProperty( const OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, const Any& _rInitialValue )
132 // check type sanity
133 Type aPropertyType = _rInitialValue.getValueType();
134 if ( aPropertyType.getTypeClass() == TypeClass_VOID )
135 throw IllegalTypeException(
136 "The initial value must be non-NULL to determine the property type.",
137 // TODO: resource
138 NULL );
140 // check name/handle sanity
141 lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
142 lcl_checkNameAndHandle_PropertyExistException( _rName, _nHandle, *this );
144 // register the property
145 registerPropertyNoMember( _rName, _nHandle, _nAttributes, aPropertyType,
146 _rInitialValue.hasValue() ? _rInitialValue.getValue() : NULL );
148 // remember the default
149 m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, _rInitialValue ) );
153 void PropertyBag::removeProperty( const OUString& _rName )
155 const Property& rProp = getProperty( _rName );
156 // will throw an UnknownPropertyException if necessary
157 if ( ( rProp.Attributes & PropertyAttribute::REMOVABLE ) == 0 )
158 throw NotRemoveableException( OUString(), NULL );
159 const sal_Int32 nHandle = rProp.Handle;
161 revokeProperty( nHandle );
163 m_pImpl->aDefaults.erase( nHandle );
167 void PropertyBag::getFastPropertyValue( sal_Int32 _nHandle, Any& _out_rValue ) const
169 if ( !hasPropertyByHandle( _nHandle ) )
170 throw UnknownPropertyException();
172 OPropertyContainerHelper::getFastPropertyValue( _out_rValue, _nHandle );
176 bool PropertyBag::convertFastPropertyValue( sal_Int32 _nHandle, const Any& _rNewValue, Any& _out_rConvertedValue, Any& _out_rCurrentValue ) const
178 if ( !hasPropertyByHandle( _nHandle ) )
179 throw UnknownPropertyException();
181 return const_cast< PropertyBag* >( this )->OPropertyContainerHelper::convertFastPropertyValue(
182 _out_rConvertedValue, _out_rCurrentValue, _nHandle, _rNewValue );
186 void PropertyBag::setFastPropertyValue( sal_Int32 _nHandle, const Any& _rValue )
188 if ( !hasPropertyByHandle( _nHandle ) )
189 throw UnknownPropertyException();
191 OPropertyContainerHelper::setFastPropertyValue( _nHandle, _rValue );
195 void PropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle, Any& _out_rValue ) const
197 if ( !hasPropertyByHandle( _nHandle ) )
198 throw UnknownPropertyException();
200 MapInt2Any::const_iterator pos = m_pImpl->aDefaults.find( _nHandle );
201 OSL_ENSURE( pos != m_pImpl->aDefaults.end(), "PropertyBag::getPropertyDefaultByHandle: inconsistency!" );
202 if ( pos != m_pImpl->aDefaults.end() )
203 _out_rValue = pos->second;
204 else
205 _out_rValue.clear();
210 } // namespace comphelper
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */