Line data Source code
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 :
20 : #include <comphelper/propertybag.hxx>
21 :
22 : #include <com/sun/star/beans/IllegalTypeException.hpp>
23 : #include <com/sun/star/beans/PropertyExistException.hpp>
24 : #include <com/sun/star/container/ElementExistException.hpp>
25 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 : #include <com/sun/star/beans/PropertyAttribute.hpp>
27 : #include <com/sun/star/beans/NotRemoveableException.hpp>
28 :
29 : #include <map>
30 :
31 :
32 : namespace comphelper
33 : {
34 :
35 :
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::container::ElementExistException;
42 : using ::com::sun::star::lang::IllegalArgumentException;
43 : using ::com::sun::star::beans::Property;
44 : using ::com::sun::star::beans::NotRemoveableException;
45 : using ::com::sun::star::beans::UnknownPropertyException;
46 :
47 : namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
48 :
49 : typedef ::std::map< sal_Int32, Any > MapInt2Any;
50 3719 : struct PropertyBag_Impl
51 : {
52 3768 : PropertyBag_Impl() : m_bAllowEmptyPropertyName(false) { }
53 : MapInt2Any aDefaults;
54 : bool m_bAllowEmptyPropertyName;
55 : };
56 :
57 3768 : PropertyBag::PropertyBag()
58 3768 : :m_pImpl( new PropertyBag_Impl )
59 : {
60 3768 : }
61 :
62 3719 : PropertyBag::~PropertyBag()
63 : {
64 3719 : }
65 :
66 :
67 2838 : void PropertyBag::setAllowEmptyPropertyName( bool i_isAllowed )
68 : {
69 2838 : m_pImpl->m_bAllowEmptyPropertyName = i_isAllowed;
70 2838 : }
71 :
72 :
73 : namespace
74 : {
75 19429 : void lcl_checkForEmptyName( const bool _allowEmpty, const OUString& _name )
76 : {
77 19429 : if ( !_allowEmpty && _name.isEmpty() )
78 : throw IllegalArgumentException(
79 : "The property name must not be empty.",
80 : // TODO: resource
81 : NULL,
82 : 1
83 19 : );
84 19410 : }
85 :
86 19077 : void lcl_checkNameAndHandle_PropertyExistException( const OUString& _name, const sal_Int32 _handle, const PropertyBag& _container )
87 : {
88 19077 : if ( _container.hasPropertyByName( _name ) || _container.hasPropertyByHandle( _handle ) )
89 : throw PropertyExistException(
90 : "Property name or handle already used.",
91 116 : NULL );
92 :
93 18961 : }
94 :
95 333 : void lcl_checkNameAndHandle_ElementExistException( const OUString& _name, const sal_Int32 _handle, const PropertyBag& _container )
96 : {
97 333 : if ( _container.hasPropertyByName( _name ) || _container.hasPropertyByHandle( _handle ) )
98 : throw ElementExistException(
99 : "Property name or handle already used.",
100 0 : NULL );
101 :
102 333 : }
103 :
104 : }
105 :
106 :
107 333 : void PropertyBag::addVoidProperty( const OUString& _rName, const Type& _rType, sal_Int32 _nHandle, sal_Int32 _nAttributes )
108 : {
109 333 : if ( _rType.getTypeClass() == TypeClass_VOID )
110 : throw IllegalArgumentException(
111 : "Illegal property type: VOID",
112 : // TODO: resource
113 : NULL,
114 : 1
115 0 : );
116 :
117 : // check name/handle sanity
118 333 : lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
119 333 : lcl_checkNameAndHandle_ElementExistException( _rName, _nHandle, *this );
120 :
121 : // register the property
122 : OSL_ENSURE( _nAttributes & PropertyAttribute::MAYBEVOID, "PropertyBag::addVoidProperty: this is for default-void properties only!" );
123 333 : registerPropertyNoMember( _rName, _nHandle, _nAttributes | PropertyAttribute::MAYBEVOID, _rType, NULL );
124 :
125 : // remember the default
126 333 : m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, Any() ) );
127 333 : }
128 :
129 :
130 19114 : void PropertyBag::addProperty( const OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, const Any& _rInitialValue )
131 : {
132 : // check type sanity
133 19114 : Type aPropertyType = _rInitialValue.getValueType();
134 19114 : if ( aPropertyType.getTypeClass() == TypeClass_VOID )
135 : throw IllegalTypeException(
136 : "The initial value must be non-NULL to determine the property type.",
137 : // TODO: resource
138 18 : NULL );
139 :
140 : // check name/handle sanity
141 19096 : lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
142 19077 : lcl_checkNameAndHandle_PropertyExistException( _rName, _nHandle, *this );
143 :
144 : // register the property
145 : registerPropertyNoMember( _rName, _nHandle, _nAttributes, aPropertyType,
146 18961 : _rInitialValue.hasValue() ? _rInitialValue.getValue() : NULL );
147 :
148 : // remember the default
149 18961 : m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, _rInitialValue ) );
150 18961 : }
151 :
152 :
153 23 : void PropertyBag::removeProperty( const OUString& _rName )
154 : {
155 23 : const Property& rProp = getProperty( _rName );
156 : // will throw an UnknownPropertyException if necessary
157 22 : if ( ( rProp.Attributes & PropertyAttribute::REMOVABLE ) == 0 )
158 1 : throw NotRemoveableException( OUString(), NULL );
159 21 : const sal_Int32 nHandle = rProp.Handle;
160 :
161 21 : revokeProperty( nHandle );
162 :
163 21 : m_pImpl->aDefaults.erase( nHandle );
164 21 : }
165 :
166 :
167 51321 : void PropertyBag::getFastPropertyValue( sal_Int32 _nHandle, Any& _out_rValue ) const
168 : {
169 51321 : if ( !hasPropertyByHandle( _nHandle ) )
170 0 : throw UnknownPropertyException();
171 :
172 51321 : OPropertyContainerHelper::getFastPropertyValue( _out_rValue, _nHandle );
173 51321 : }
174 :
175 :
176 857 : bool PropertyBag::convertFastPropertyValue( sal_Int32 _nHandle, const Any& _rNewValue, Any& _out_rConvertedValue, Any& _out_rCurrentValue ) const
177 : {
178 857 : if ( !hasPropertyByHandle( _nHandle ) )
179 0 : throw UnknownPropertyException();
180 :
181 : return const_cast< PropertyBag* >( this )->OPropertyContainerHelper::convertFastPropertyValue(
182 857 : _out_rConvertedValue, _out_rCurrentValue, _nHandle, _rNewValue );
183 : }
184 :
185 :
186 21 : void PropertyBag::setFastPropertyValue( sal_Int32 _nHandle, const Any& _rValue )
187 : {
188 21 : if ( !hasPropertyByHandle( _nHandle ) )
189 0 : throw UnknownPropertyException();
190 :
191 21 : OPropertyContainerHelper::setFastPropertyValue( _nHandle, _rValue );
192 21 : }
193 :
194 :
195 16531 : void PropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle, Any& _out_rValue ) const
196 : {
197 16531 : if ( !hasPropertyByHandle( _nHandle ) )
198 0 : throw UnknownPropertyException();
199 :
200 16531 : MapInt2Any::const_iterator pos = m_pImpl->aDefaults.find( _nHandle );
201 : OSL_ENSURE( pos != m_pImpl->aDefaults.end(), "PropertyBag::getPropertyDefaultByHandle: inconsistency!" );
202 16531 : if ( pos != m_pImpl->aDefaults.end() )
203 16531 : _out_rValue = pos->second;
204 : else
205 0 : _out_rValue.clear();
206 16531 : }
207 :
208 :
209 :
210 : } // namespace comphelper
211 :
212 :
213 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|