LibreOffice Module comphelper (master)  1
propertybag.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
21 #include <osl/diagnose.h>
22 
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>
30 
31 #include <map>
32 
33 
34 namespace comphelper
35 {
36 
37 
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;
48 
49  namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
50 
51  typedef std::map< sal_Int32, Any > MapInt2Any;
53  {
55  MapInt2Any aDefaults;
57  };
58 
61  {
62  }
63 
65  {
66  }
67 
68 
69  void PropertyBag::setAllowEmptyPropertyName( bool i_isAllowed )
70  {
71  m_pImpl->m_bAllowEmptyPropertyName = i_isAllowed;
72  }
73 
74 
75  namespace
76  {
77  void lcl_checkForEmptyName( const bool _allowEmpty, const OUString& _name )
78  {
79  if ( !_allowEmpty && _name.isEmpty() )
80  throw IllegalArgumentException(
81  "The property name must not be empty.",
82  // TODO: resource
83  nullptr,
84  1
85  );
86  }
87 
88  void lcl_checkNameAndHandle_PropertyExistException( const OUString& _name, const sal_Int32 _handle, const PropertyBag& _container )
89  {
90  if ( _container.hasPropertyByName( _name ) || _container.hasPropertyByHandle( _handle ) )
91  throw PropertyExistException(
92  "Property name or handle already used.",
93  nullptr );
94 
95  }
96 
97  void lcl_checkNameAndHandle_ElementExistException( const OUString& _name, const sal_Int32 _handle, const PropertyBag& _container )
98  {
99  if ( _container.hasPropertyByName( _name ) || _container.hasPropertyByHandle( _handle ) )
100  throw ElementExistException(
101  "Property name or handle already used.",
102  nullptr );
103 
104  }
105 
106  }
107 
108 
109  void PropertyBag::addVoidProperty( const OUString& _rName, const Type& _rType, sal_Int32 _nHandle, sal_Int32 _nAttributes )
110  {
111  if ( _rType.getTypeClass() == TypeClass_VOID )
112  throw IllegalArgumentException(
113  "Illegal property type: VOID",
114  // TODO: resource
115  nullptr,
116  1
117  );
118 
119  // check name/handle sanity
120  lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
121  lcl_checkNameAndHandle_ElementExistException( _rName, _nHandle, *this );
122 
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() );
126 
127  // remember the default
128  m_pImpl->aDefaults.emplace( _nHandle, Any() );
129  }
130 
131 
132  void PropertyBag::addProperty( const OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, const Any& _rInitialValue )
133  {
134  // check type sanity
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.",
139  // TODO: resource
140  nullptr );
141 
142  // check name/handle sanity
143  lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
144  lcl_checkNameAndHandle_PropertyExistException( _rName, _nHandle, *this );
145 
146  // register the property
147  registerPropertyNoMember( _rName, _nHandle, _nAttributes, aPropertyType,
148  _rInitialValue );
149 
150  // remember the default
151  m_pImpl->aDefaults.emplace( _nHandle, _rInitialValue );
152  }
153 
154 
155  void PropertyBag::removeProperty( const OUString& _rName )
156  {
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;
162 
163  revokeProperty( nHandle );
164 
165  m_pImpl->aDefaults.erase( nHandle );
166  }
167 
168 
169  void PropertyBag::getFastPropertyValue( sal_Int32 _nHandle, Any& _out_rValue ) const
170  {
171  if ( !hasPropertyByHandle( _nHandle ) )
172  throw UnknownPropertyException(OUString::number(_nHandle));
173 
174  OPropertyContainerHelper::getFastPropertyValue( _out_rValue, _nHandle );
175  }
176 
177 
178  bool PropertyBag::convertFastPropertyValue( sal_Int32 _nHandle, const Any& _rNewValue, Any& _out_rConvertedValue, Any& _out_rCurrentValue ) const
179  {
180  if ( !hasPropertyByHandle( _nHandle ) )
181  throw UnknownPropertyException(OUString::number(_nHandle));
182 
184  _out_rConvertedValue, _out_rCurrentValue, _nHandle, _rNewValue );
185  }
186 
187 
188  void PropertyBag::setFastPropertyValue( sal_Int32 _nHandle, const Any& _rValue )
189  {
190  if ( !hasPropertyByHandle( _nHandle ) )
191  throw UnknownPropertyException(OUString::number(_nHandle));
192 
194  }
195 
196 
197  void PropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle, Any& _out_rValue ) const
198  {
199  if ( !hasPropertyByHandle( _nHandle ) )
200  throw UnknownPropertyException(OUString::number(_nHandle));
201 
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;
206  else
207  _out_rValue.clear();
208  }
209 
210 
211 } // namespace comphelper
212 
213 
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
std::set< css::beans::Property, PropertyLessByName > PropertyBag
void getPropertyDefaultByHandle(sal_Int32 _nHandle, css::uno::Any &_out_rValue) const
returns the default value for a property given by handle
size_t pos
provides a bag of properties associated with their values
Definition: propertybag.hxx:42
const css::beans::Property & getProperty(const OUString &_rName) const
retrieves the description for a registered property
void setFastPropertyValue(sal_Int32 _nHandle, const css::uno::Any &_rValue)
sets a new value for a property given by handle
void removeProperty(const OUString &_rName)
removes a property from the bag
void getFastPropertyValue(sal_Int32 _nHandle, css::uno::Any &_out_rValue) const
retrieves the value of a property given by handle
bool hasPropertyByHandle(sal_Int32 _nHandle) const
determines whether a property with a given handle is part of the bag
::std::unique_ptr< PropertyBag_Impl > m_pImpl
Definition: propertybag.hxx:45
sal_Int32 const nHandle
bool convertFastPropertyValue(sal_Int32 _nHandle, const css::uno::Any &_rNewValue, css::uno::Any &_out_rConvertedValue, css::uno::Any &_out_rCurrentValue) const
converts a to-be-set value of a property (given by handle) so that it can be used in subsequent calls...
void revokeProperty(sal_Int32 _nHandle)
revokes a previously registered property
void getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const
void registerPropertyNoMember(const OUString &_rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, const css::uno::Type &_rType, css::uno::Any const &_pInitialValue)
register a property.
void setAllowEmptyPropertyName(bool i_isAllowed)
allow adding property with empty string as name (by default, such names are rejected with IllegalActi...
Definition: propertybag.cxx:69
bool convertFastPropertyValue(css::uno::Any &rConvertedValue, css::uno::Any &rOldValue, sal_Int32 nHandle, const css::uno::Any &rValue)
void addProperty(const OUString &_rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, const css::uno::Any &_rInitialValue)
adds a property to the bag
void setFastPropertyValue(sal_Int32 nHandle, const css::uno::Any &rValue)
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
std::map< sal_Int32, css::uno::Any > MapInt2Any
void addVoidProperty(const OUString &_rName, const css::uno::Type &_rType, sal_Int32 _nHandle, sal_Int32 _nAttributes)
adds a property to the bag