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