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>
21 #include <osl/diagnose.h>
23 #include <com/sun/star/beans/IllegalTypeException.hpp>
24 #include <com/sun/star/beans/PropertyExistException.hpp>
25 #include <com/sun/star/container/ElementExistException.hpp>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <com/sun/star/beans/NotRemoveableException.hpp>
29 #include <com/sun/star/beans/UnknownPropertyException.hpp>
38 using ::com::sun::star::uno::Any
;
39 using ::com::sun::star::uno::Type
;
40 using ::com::sun::star::uno::TypeClass_VOID
;
41 using ::com::sun::star::beans::IllegalTypeException
;
42 using ::com::sun::star::beans::PropertyExistException
;
43 using ::com::sun::star::container::ElementExistException
;
44 using ::com::sun::star::lang::IllegalArgumentException
;
45 using ::com::sun::star::beans::Property
;
46 using ::com::sun::star::beans::NotRemoveableException
;
47 using ::com::sun::star::beans::UnknownPropertyException
;
49 namespace PropertyAttribute
= ::com::sun::star::beans::PropertyAttribute
;
51 typedef std::map
< sal_Int32
, Any
> MapInt2Any
;
52 struct PropertyBag_Impl
54 PropertyBag_Impl() : m_bAllowEmptyPropertyName(false) { }
56 bool m_bAllowEmptyPropertyName
;
59 PropertyBag::PropertyBag()
60 :m_pImpl( new PropertyBag_Impl
)
64 PropertyBag::~PropertyBag()
69 void PropertyBag::setAllowEmptyPropertyName( bool i_isAllowed
)
71 m_pImpl
->m_bAllowEmptyPropertyName
= i_isAllowed
;
77 void lcl_checkForEmptyName( const bool _allowEmpty
, const OUString
& _name
)
79 if ( !_allowEmpty
&& _name
.isEmpty() )
80 throw IllegalArgumentException(
81 "The property name must not be empty.",
88 void lcl_checkNameAndHandle_PropertyExistException( const OUString
& _name
, const sal_Int32 _handle
, const PropertyBag
& _container
)
90 if ( _container
.hasPropertyByName( _name
) || _container
.hasPropertyByHandle( _handle
) )
91 throw PropertyExistException(
92 "Property name or handle already used.",
97 void lcl_checkNameAndHandle_ElementExistException( const OUString
& _name
, const sal_Int32 _handle
, const PropertyBag
& _container
)
99 if ( _container
.hasPropertyByName( _name
) || _container
.hasPropertyByHandle( _handle
) )
100 throw ElementExistException(
101 "Property name or handle already used.",
109 void PropertyBag::addVoidProperty( const OUString
& _rName
, const Type
& _rType
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
)
111 if ( _rType
.getTypeClass() == TypeClass_VOID
)
112 throw IllegalArgumentException(
113 "Illegal property type: VOID",
119 // check name/handle sanity
120 lcl_checkForEmptyName( m_pImpl
->m_bAllowEmptyPropertyName
, _rName
);
121 lcl_checkNameAndHandle_ElementExistException( _rName
, _nHandle
, *this );
123 // register the property
124 OSL_ENSURE( _nAttributes
& PropertyAttribute::MAYBEVOID
, "PropertyBag::addVoidProperty: this is for default-void properties only!" );
125 registerPropertyNoMember( _rName
, _nHandle
, _nAttributes
| PropertyAttribute::MAYBEVOID
, _rType
, css::uno::Any() );
127 // remember the default
128 m_pImpl
->aDefaults
.emplace( _nHandle
, Any() );
132 void PropertyBag::addProperty( const OUString
& _rName
, sal_Int32 _nHandle
, sal_Int32 _nAttributes
, const Any
& _rInitialValue
)
135 const Type
& aPropertyType
= _rInitialValue
.getValueType();
136 if ( aPropertyType
.getTypeClass() == TypeClass_VOID
)
137 throw IllegalTypeException(
138 "The initial value must be non-NULL to determine the property type.",
142 // check name/handle sanity
143 lcl_checkForEmptyName( m_pImpl
->m_bAllowEmptyPropertyName
, _rName
);
144 lcl_checkNameAndHandle_PropertyExistException( _rName
, _nHandle
, *this );
146 // register the property
147 registerPropertyNoMember( _rName
, _nHandle
, _nAttributes
, aPropertyType
,
150 // remember the default
151 m_pImpl
->aDefaults
.emplace( _nHandle
, _rInitialValue
);
155 void PropertyBag::removeProperty( const OUString
& _rName
)
157 const Property
& rProp
= getProperty( _rName
);
158 // will throw an UnknownPropertyException if necessary
159 if ( ( rProp
.Attributes
& PropertyAttribute::REMOVABLE
) == 0 )
160 throw NotRemoveableException( OUString(), nullptr );
161 const sal_Int32 nHandle
= rProp
.Handle
;
163 revokeProperty( nHandle
);
165 m_pImpl
->aDefaults
.erase( nHandle
);
169 void PropertyBag::getFastPropertyValue( sal_Int32 _nHandle
, Any
& _out_rValue
) const
171 if ( !hasPropertyByHandle( _nHandle
) )
172 throw UnknownPropertyException();
174 OPropertyContainerHelper::getFastPropertyValue( _out_rValue
, _nHandle
);
178 bool PropertyBag::convertFastPropertyValue( sal_Int32 _nHandle
, const Any
& _rNewValue
, Any
& _out_rConvertedValue
, Any
& _out_rCurrentValue
) const
180 if ( !hasPropertyByHandle( _nHandle
) )
181 throw UnknownPropertyException();
183 return const_cast< PropertyBag
* >( this )->OPropertyContainerHelper::convertFastPropertyValue(
184 _out_rConvertedValue
, _out_rCurrentValue
, _nHandle
, _rNewValue
);
188 void PropertyBag::setFastPropertyValue( sal_Int32 _nHandle
, const Any
& _rValue
)
190 if ( !hasPropertyByHandle( _nHandle
) )
191 throw UnknownPropertyException();
193 OPropertyContainerHelper::setFastPropertyValue( _nHandle
, _rValue
);
197 void PropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle
, Any
& _out_rValue
) const
199 if ( !hasPropertyByHandle( _nHandle
) )
200 throw UnknownPropertyException();
202 MapInt2Any::const_iterator pos
= m_pImpl
->aDefaults
.find( _nHandle
);
203 OSL_ENSURE( pos
!= m_pImpl
->aDefaults
.end(), "PropertyBag::getPropertyDefaultByHandle: inconsistency!" );
204 if ( pos
!= m_pImpl
->aDefaults
.end() )
205 _out_rValue
= pos
->second
;
211 } // namespace comphelper
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */