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