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 PropertyBag::PropertyBag()
52 : m_bAllowEmptyPropertyName(false)
56 PropertyBag::~PropertyBag()
61 void PropertyBag::setAllowEmptyPropertyName(
bool i_isAllowed )
63 m_bAllowEmptyPropertyName = i_isAllowed;
69 void lcl_checkForEmptyName(
const bool _allowEmpty, std::u16string_view _name )
71 if ( !_allowEmpty && _name.empty() )
72 throw IllegalArgumentException(
73 "The property name must not be empty.",
80 void lcl_checkNameAndHandle_PropertyExistException(
const OUString& _name,
const sal_Int32 _handle,
const PropertyBag& _container )
82 if ( _container.hasPropertyByName( _name ) || _container.hasPropertyByHandle( _handle ) )
83 throw PropertyExistException(
84 "Property name or handle already used.",
89 void lcl_checkNameAndHandle_ElementExistException(
const OUString& _name,
const sal_Int32 _handle,
const PropertyBag& _container )
91 if ( _container.hasPropertyByName( _name ) || _container.hasPropertyByHandle( _handle ) )
92 throw ElementExistException(
93 "Property name or handle already used.",
101 void PropertyBag::addVoidProperty(
const OUString& _rName,
const Type& _rType, sal_Int32 _nHandle, sal_Int32 _nAttributes )
103 if ( _rType.getTypeClass() == TypeClass_VOID )
104 throw IllegalArgumentException(
105 "Illegal property type: VOID",
112 lcl_checkForEmptyName( m_bAllowEmptyPropertyName, _rName );
113 lcl_checkNameAndHandle_ElementExistException( _rName, _nHandle, *
this );
116 OSL_ENSURE( _nAttributes & PropertyAttribute::MAYBEVOID,
"PropertyBag::addVoidProperty: this is for default-void properties only!" );
117 registerPropertyNoMember( _rName, _nHandle, _nAttributes | PropertyAttribute::MAYBEVOID, _rType, css::uno::Any() );
120 aDefaults.emplace( _nHandle,
Any() );
124 void PropertyBag::addProperty(
const OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes,
const Any& _rInitialValue )
127 const Type& aPropertyType = _rInitialValue.getValueType();
128 if ( aPropertyType.getTypeClass() == TypeClass_VOID )
129 throw IllegalTypeException(
130 "The initial value must be non-NULL to determine the property type.",
135 lcl_checkForEmptyName( m_bAllowEmptyPropertyName, _rName );
136 lcl_checkNameAndHandle_PropertyExistException( _rName, _nHandle, *
this );
139 registerPropertyNoMember( _rName, _nHandle, _nAttributes, aPropertyType,
143 aDefaults.emplace( _nHandle, _rInitialValue );
147 void PropertyBag::removeProperty(
const OUString& _rName )
151 if ( ( rProp.Attributes & PropertyAttribute::REMOVABLE ) == 0 )
152 throw NotRemoveableException( OUString(),
nullptr );
153 const sal_Int32
nHandle = rProp.Handle;
155 revokeProperty( nHandle );
157 aDefaults.erase( nHandle );
161 void PropertyBag::getFastPropertyValue( sal_Int32 _nHandle, Any& _out_rValue )
const
163 if ( !hasPropertyByHandle( _nHandle ) )
164 throw UnknownPropertyException(OUString::number(_nHandle));
166 OPropertyContainerHelper::getFastPropertyValue( _out_rValue, _nHandle );
170 bool PropertyBag::convertFastPropertyValue( sal_Int32 _nHandle,
const Any& _rNewValue, Any& _out_rConvertedValue, Any& _out_rCurrentValue )
const
172 if ( !hasPropertyByHandle( _nHandle ) )
173 throw UnknownPropertyException(OUString::number(_nHandle));
175 return const_cast< PropertyBag*
>( this )->OPropertyContainerHelper::convertFastPropertyValue(
176 _out_rConvertedValue, _out_rCurrentValue, _nHandle, _rNewValue );
180 void PropertyBag::setFastPropertyValue( sal_Int32 _nHandle,
const Any& _rValue )
182 if ( !hasPropertyByHandle( _nHandle ) )
183 throw UnknownPropertyException(OUString::number(_nHandle));
185 OPropertyContainerHelper::setFastPropertyValue( _nHandle, _rValue );
189 void PropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle, Any& _out_rValue )
const
191 if ( !hasPropertyByHandle( _nHandle ) )
192 throw UnknownPropertyException(OUString::number(_nHandle));
194 auto pos = aDefaults.find( _nHandle );
195 OSL_ENSURE( pos != aDefaults.end(),
"PropertyBag::getPropertyDefaultByHandle: inconsistency!" );
196 if ( pos != aDefaults.end() )
197 _out_rValue =
pos->second;
std::set< css::beans::Property, PropertyLessByName > PropertyBag
SVX_DLLPUBLIC OUString getProperty(css::uno::Reference< css::beans::XPropertyContainer > const &rxPropertyContainer, OUString const &rName)