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>
32 #include <string_view>
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 u
"The property name must not be empty."_ustr
,
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 u
"Property name or handle already used."_ustr
,
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 u
"Property name or handle already used."_ustr
,
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 u
"Illegal property type: VOID"_ustr
,
111 // check name/handle sanity
112 lcl_checkForEmptyName( m_bAllowEmptyPropertyName
, _rName
);
113 lcl_checkNameAndHandle_ElementExistException( _rName
, _nHandle
, *this );
115 // register the property
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() );
119 // remember the default
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 u
"The initial value must be non-NULL to determine the property type."_ustr
,
134 // check name/handle sanity
135 lcl_checkForEmptyName( m_bAllowEmptyPropertyName
, _rName
);
136 lcl_checkNameAndHandle_PropertyExistException( _rName
, _nHandle
, *this );
138 // register the property
139 registerPropertyNoMember( _rName
, _nHandle
, _nAttributes
, aPropertyType
,
142 // remember the default
143 aDefaults
.emplace( _nHandle
, _rInitialValue
);
147 void PropertyBag::removeProperty( const OUString
& _rName
)
149 const Property
& rProp
= getProperty( _rName
);
150 // will throw an UnknownPropertyException if necessary
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
;
203 } // namespace comphelper
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */