1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
30 //........................................................................
33 //........................................................................
35 /** === begin UNO using === **/
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::lang::IllegalArgumentException
;
42 using ::com::sun::star::beans::Property
;
43 using ::com::sun::star::beans::NotRemoveableException
;
44 using ::com::sun::star::beans::UnknownPropertyException
;
45 /** === end UNO using === **/
46 namespace PropertyAttribute
= ::com::sun::star::beans::PropertyAttribute
;
48 //====================================================================
50 //====================================================================
51 typedef ::std::map
< sal_Int32
, Any
> MapInt2Any
;
52 struct PropertyBag_Impl
54 PropertyBag_Impl() : m_bAllowEmptyPropertyName(false) { }
56 bool m_bAllowEmptyPropertyName
;
59 //====================================================================
61 //====================================================================
62 //--------------------------------------------------------------------
63 PropertyBag::PropertyBag()
64 :m_pImpl( new PropertyBag_Impl
)
68 PropertyBag::~PropertyBag()
72 //--------------------------------------------------------------------
73 void PropertyBag::setAllowEmptyPropertyName( bool i_isAllowed
)
75 m_pImpl
->m_bAllowEmptyPropertyName
= i_isAllowed
;
78 //--------------------------------------------------------------------
81 void lcl_checkForEmptyName( const bool _allowEmpty
, const ::rtl::OUString
& _name
)
83 if ( !_allowEmpty
&& _name
.isEmpty() )
84 throw IllegalArgumentException(
85 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "The property name must not be empty." ) ),
92 void lcl_checkNameAndHandle( const ::rtl::OUString
& _name
, const sal_Int32 _handle
, const PropertyBag
& _container
)
94 if ( _container
.hasPropertyByName( _name
) || _container
.hasPropertyByHandle( _handle
) )
95 throw PropertyExistException(
96 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Property name or handle already used." ) ),
103 //--------------------------------------------------------------------
104 void PropertyBag::addVoidProperty( const ::rtl::OUString
& _rName
, const Type
& _rType
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
)
106 if ( _rType
.getTypeClass() == TypeClass_VOID
)
107 throw IllegalArgumentException(
108 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Illegal property type: VOID" ) ),
114 // check name/handle sanity
115 lcl_checkForEmptyName( m_pImpl
->m_bAllowEmptyPropertyName
, _rName
);
116 lcl_checkNameAndHandle( _rName
, _nHandle
, *this );
118 // register the property
119 OSL_ENSURE( _nAttributes
& PropertyAttribute::MAYBEVOID
, "PropertyBag::addVoidProperty: this is for default-void properties only!" );
120 registerPropertyNoMember( _rName
, _nHandle
, _nAttributes
| PropertyAttribute::MAYBEVOID
, _rType
, NULL
);
122 // remember the default
123 m_pImpl
->aDefaults
.insert( MapInt2Any::value_type( _nHandle
, Any() ) );
126 //--------------------------------------------------------------------
127 void PropertyBag::addProperty( const ::rtl::OUString
& _rName
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
, const Any
& _rInitialValue
)
130 Type aPropertyType
= _rInitialValue
.getValueType();
131 if ( aPropertyType
.getTypeClass() == TypeClass_VOID
)
132 throw IllegalTypeException(
133 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "The initial value must be non-NULL to determine the property type." ) ),
137 // check name/handle sanity
138 lcl_checkForEmptyName( m_pImpl
->m_bAllowEmptyPropertyName
, _rName
);
139 lcl_checkNameAndHandle( _rName
, _nHandle
, *this );
141 // register the property
142 registerPropertyNoMember( _rName
, _nHandle
, _nAttributes
, aPropertyType
,
143 _rInitialValue
.hasValue() ? _rInitialValue
.getValue() : NULL
);
145 // remember the default
146 m_pImpl
->aDefaults
.insert( MapInt2Any::value_type( _nHandle
, _rInitialValue
) );
149 //--------------------------------------------------------------------
150 void PropertyBag::removeProperty( const ::rtl::OUString
& _rName
)
152 const Property
& rProp
= getProperty( _rName
);
153 // will throw an UnknownPropertyException if necessary
154 if ( ( rProp
.Attributes
& PropertyAttribute::REMOVEABLE
) == 0 )
155 throw NotRemoveableException( ::rtl::OUString(), NULL
);
156 const sal_Int32 nHandle
= rProp
.Handle
;
158 revokeProperty( nHandle
);
160 m_pImpl
->aDefaults
.erase( nHandle
);
163 //--------------------------------------------------------------------
164 void PropertyBag::getFastPropertyValue( sal_Int32 _nHandle
, Any
& _out_rValue
) const
166 if ( !hasPropertyByHandle( _nHandle
) )
167 throw UnknownPropertyException();
169 OPropertyContainerHelper::getFastPropertyValue( _out_rValue
, _nHandle
);
172 //--------------------------------------------------------------------
173 bool PropertyBag::convertFastPropertyValue( sal_Int32 _nHandle
, const Any
& _rNewValue
, Any
& _out_rConvertedValue
, Any
& _out_rCurrentValue
) const
175 if ( !hasPropertyByHandle( _nHandle
) )
176 throw UnknownPropertyException();
178 return const_cast< PropertyBag
* >( this )->OPropertyContainerHelper::convertFastPropertyValue(
179 _out_rConvertedValue
, _out_rCurrentValue
, _nHandle
, _rNewValue
);
182 //--------------------------------------------------------------------
183 void PropertyBag::setFastPropertyValue( sal_Int32 _nHandle
, const Any
& _rValue
)
185 if ( !hasPropertyByHandle( _nHandle
) )
186 throw UnknownPropertyException();
188 OPropertyContainerHelper::setFastPropertyValue( _nHandle
, _rValue
);
191 //--------------------------------------------------------------------
192 void PropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle
, Any
& _out_rValue
) const
194 if ( !hasPropertyByHandle( _nHandle
) )
195 throw UnknownPropertyException();
197 MapInt2Any::const_iterator pos
= m_pImpl
->aDefaults
.find( _nHandle
);
198 OSL_ENSURE( pos
!= m_pImpl
->aDefaults
.end(), "PropertyBag::getPropertyDefaultByHandle: inconsistency!" );
199 if ( pos
!= m_pImpl
->aDefaults
.end() )
200 _out_rValue
= pos
->second
;
206 //........................................................................
207 } // namespace comphelper
208 //........................................................................
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */