bump product version to 4.1.6.2
[LibreOffice.git] / comphelper / source / property / propertybag.cxx
blob79225fc492e6c4bdf2b1dbb22f5be590e90708ac
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/lang/IllegalArgumentException.hpp>
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <com/sun/star/beans/NotRemoveableException.hpp>
28 #include <map>
30 //........................................................................
31 namespace comphelper
33 //........................................................................
35 using ::com::sun::star::uno::Any;
36 using ::com::sun::star::uno::Type;
37 using ::com::sun::star::uno::TypeClass_VOID;
38 using ::com::sun::star::beans::IllegalTypeException;
39 using ::com::sun::star::beans::PropertyExistException;
40 using ::com::sun::star::lang::IllegalArgumentException;
41 using ::com::sun::star::beans::Property;
42 using ::com::sun::star::beans::NotRemoveableException;
43 using ::com::sun::star::beans::UnknownPropertyException;
45 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
47 //====================================================================
48 //= PropertyBag_Impl
49 //====================================================================
50 typedef ::std::map< sal_Int32, Any > MapInt2Any;
51 struct PropertyBag_Impl
53 PropertyBag_Impl() : m_bAllowEmptyPropertyName(false) { }
54 MapInt2Any aDefaults;
55 bool m_bAllowEmptyPropertyName;
58 //====================================================================
59 //= PropertyBag
60 //====================================================================
61 //--------------------------------------------------------------------
62 PropertyBag::PropertyBag()
63 :m_pImpl( new PropertyBag_Impl )
67 PropertyBag::~PropertyBag()
71 //--------------------------------------------------------------------
72 void PropertyBag::setAllowEmptyPropertyName( bool i_isAllowed )
74 m_pImpl->m_bAllowEmptyPropertyName = i_isAllowed;
77 //--------------------------------------------------------------------
78 namespace
80 void lcl_checkForEmptyName( const bool _allowEmpty, const OUString& _name )
82 if ( !_allowEmpty && _name.isEmpty() )
83 throw IllegalArgumentException(
84 OUString( "The property name must not be empty." ),
85 // TODO: resource
86 NULL,
91 void lcl_checkNameAndHandle( const OUString& _name, const sal_Int32 _handle, const PropertyBag& _container )
93 if ( _container.hasPropertyByName( _name ) || _container.hasPropertyByHandle( _handle ) )
94 throw PropertyExistException(
95 OUString( "Property name or handle already used." ),
96 // TODO: resource
97 NULL );
102 //--------------------------------------------------------------------
103 void PropertyBag::addVoidProperty( const OUString& _rName, const Type& _rType, sal_Int32 _nHandle, sal_Int32 _nAttributes )
105 if ( _rType.getTypeClass() == TypeClass_VOID )
106 throw IllegalArgumentException(
107 OUString( "Illegal property type: VOID" ),
108 // TODO: resource
109 NULL,
113 // check name/handle sanity
114 lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
115 lcl_checkNameAndHandle( _rName, _nHandle, *this );
117 // register the property
118 OSL_ENSURE( _nAttributes & PropertyAttribute::MAYBEVOID, "PropertyBag::addVoidProperty: this is for default-void properties only!" );
119 registerPropertyNoMember( _rName, _nHandle, _nAttributes | PropertyAttribute::MAYBEVOID, _rType, NULL );
121 // remember the default
122 m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, Any() ) );
125 //--------------------------------------------------------------------
126 void PropertyBag::addProperty( const OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, const Any& _rInitialValue )
128 // check type sanity
129 Type aPropertyType = _rInitialValue.getValueType();
130 if ( aPropertyType.getTypeClass() == TypeClass_VOID )
131 throw IllegalTypeException(
132 OUString( "The initial value must be non-NULL to determine the property type." ),
133 // TODO: resource
134 NULL );
136 // check name/handle sanity
137 lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
138 lcl_checkNameAndHandle( _rName, _nHandle, *this );
140 // register the property
141 registerPropertyNoMember( _rName, _nHandle, _nAttributes, aPropertyType,
142 _rInitialValue.hasValue() ? _rInitialValue.getValue() : NULL );
144 // remember the default
145 m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, _rInitialValue ) );
148 //--------------------------------------------------------------------
149 void PropertyBag::removeProperty( const OUString& _rName )
151 const Property& rProp = getProperty( _rName );
152 // will throw an UnknownPropertyException if necessary
153 if ( ( rProp.Attributes & PropertyAttribute::REMOVABLE ) == 0 )
154 throw NotRemoveableException( OUString(), NULL );
155 const sal_Int32 nHandle = rProp.Handle;
157 revokeProperty( nHandle );
159 m_pImpl->aDefaults.erase( nHandle );
162 //--------------------------------------------------------------------
163 void PropertyBag::getFastPropertyValue( sal_Int32 _nHandle, Any& _out_rValue ) const
165 if ( !hasPropertyByHandle( _nHandle ) )
166 throw UnknownPropertyException();
168 OPropertyContainerHelper::getFastPropertyValue( _out_rValue, _nHandle );
171 //--------------------------------------------------------------------
172 bool PropertyBag::convertFastPropertyValue( sal_Int32 _nHandle, const Any& _rNewValue, Any& _out_rConvertedValue, Any& _out_rCurrentValue ) const
174 if ( !hasPropertyByHandle( _nHandle ) )
175 throw UnknownPropertyException();
177 return const_cast< PropertyBag* >( this )->OPropertyContainerHelper::convertFastPropertyValue(
178 _out_rConvertedValue, _out_rCurrentValue, _nHandle, _rNewValue );
181 //--------------------------------------------------------------------
182 void PropertyBag::setFastPropertyValue( sal_Int32 _nHandle, const Any& _rValue )
184 if ( !hasPropertyByHandle( _nHandle ) )
185 throw UnknownPropertyException();
187 OPropertyContainerHelper::setFastPropertyValue( _nHandle, _rValue );
190 //--------------------------------------------------------------------
191 void PropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle, Any& _out_rValue ) const
193 if ( !hasPropertyByHandle( _nHandle ) )
194 throw UnknownPropertyException();
196 MapInt2Any::const_iterator pos = m_pImpl->aDefaults.find( _nHandle );
197 OSL_ENSURE( pos != m_pImpl->aDefaults.end(), "PropertyBag::getPropertyDefaultByHandle: inconsistency!" );
198 if ( pos != m_pImpl->aDefaults.end() )
199 _out_rValue = pos->second;
200 else
201 _out_rValue.clear();
205 //........................................................................
206 } // namespace comphelper
207 //........................................................................
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */