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 83 : struct PropertyBag_Impl
53 : {
54 144 : PropertyBag_Impl() : m_bAllowEmptyPropertyName(false) { }
55 : MapInt2Any aDefaults;
56 : bool m_bAllowEmptyPropertyName;
57 : };
58 :
59 : //====================================================================
60 : //= PropertyBag
61 : //====================================================================
62 : //--------------------------------------------------------------------
63 144 : PropertyBag::PropertyBag()
64 144 : :m_pImpl( new PropertyBag_Impl )
65 : {
66 144 : }
67 :
68 83 : PropertyBag::~PropertyBag()
69 : {
70 83 : }
71 :
72 : //--------------------------------------------------------------------
73 133 : void PropertyBag::setAllowEmptyPropertyName( bool i_isAllowed )
74 : {
75 133 : m_pImpl->m_bAllowEmptyPropertyName = i_isAllowed;
76 133 : }
77 :
78 : //--------------------------------------------------------------------
79 : namespace
80 : {
81 415 : void lcl_checkForEmptyName( const bool _allowEmpty, const ::rtl::OUString& _name )
82 : {
83 415 : 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 0 : );
90 415 : }
91 :
92 415 : void lcl_checkNameAndHandle( const ::rtl::OUString& _name, const sal_Int32 _handle, const PropertyBag& _container )
93 : {
94 415 : 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 0 : NULL );
99 :
100 415 : }
101 : }
102 :
103 : //--------------------------------------------------------------------
104 0 : void PropertyBag::addVoidProperty( const ::rtl::OUString& _rName, const Type& _rType, sal_Int32 _nHandle, sal_Int32 _nAttributes )
105 : {
106 0 : 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 0 : lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
116 0 : 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 0 : registerPropertyNoMember( _rName, _nHandle, _nAttributes | PropertyAttribute::MAYBEVOID, _rType, NULL );
121 :
122 : // remember the default
123 0 : m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, Any() ) );
124 0 : }
125 :
126 : //--------------------------------------------------------------------
127 415 : void PropertyBag::addProperty( const ::rtl::OUString& _rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, const Any& _rInitialValue )
128 : {
129 : // check type sanity
130 415 : Type aPropertyType = _rInitialValue.getValueType();
131 415 : 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 0 : NULL );
136 :
137 : // check name/handle sanity
138 415 : lcl_checkForEmptyName( m_pImpl->m_bAllowEmptyPropertyName, _rName );
139 415 : lcl_checkNameAndHandle( _rName, _nHandle, *this );
140 :
141 : // register the property
142 : registerPropertyNoMember( _rName, _nHandle, _nAttributes, aPropertyType,
143 415 : _rInitialValue.hasValue() ? _rInitialValue.getValue() : NULL );
144 :
145 : // remember the default
146 415 : m_pImpl->aDefaults.insert( MapInt2Any::value_type( _nHandle, _rInitialValue ) );
147 415 : }
148 :
149 : //--------------------------------------------------------------------
150 0 : void PropertyBag::removeProperty( const ::rtl::OUString& _rName )
151 : {
152 0 : const Property& rProp = getProperty( _rName );
153 : // will throw an UnknownPropertyException if necessary
154 0 : if ( ( rProp.Attributes & PropertyAttribute::REMOVEABLE ) == 0 )
155 0 : throw NotRemoveableException( ::rtl::OUString(), NULL );
156 0 : const sal_Int32 nHandle = rProp.Handle;
157 :
158 0 : revokeProperty( nHandle );
159 :
160 0 : m_pImpl->aDefaults.erase( nHandle );
161 0 : }
162 :
163 : //--------------------------------------------------------------------
164 48 : void PropertyBag::getFastPropertyValue( sal_Int32 _nHandle, Any& _out_rValue ) const
165 : {
166 48 : if ( !hasPropertyByHandle( _nHandle ) )
167 0 : throw UnknownPropertyException();
168 :
169 48 : OPropertyContainerHelper::getFastPropertyValue( _out_rValue, _nHandle );
170 48 : }
171 :
172 : //--------------------------------------------------------------------
173 0 : bool PropertyBag::convertFastPropertyValue( sal_Int32 _nHandle, const Any& _rNewValue, Any& _out_rConvertedValue, Any& _out_rCurrentValue ) const
174 : {
175 0 : if ( !hasPropertyByHandle( _nHandle ) )
176 0 : throw UnknownPropertyException();
177 :
178 : return const_cast< PropertyBag* >( this )->OPropertyContainerHelper::convertFastPropertyValue(
179 0 : _out_rConvertedValue, _out_rCurrentValue, _nHandle, _rNewValue );
180 : }
181 :
182 : //--------------------------------------------------------------------
183 0 : void PropertyBag::setFastPropertyValue( sal_Int32 _nHandle, const Any& _rValue )
184 : {
185 0 : if ( !hasPropertyByHandle( _nHandle ) )
186 0 : throw UnknownPropertyException();
187 :
188 0 : OPropertyContainerHelper::setFastPropertyValue( _nHandle, _rValue );
189 0 : }
190 :
191 : //--------------------------------------------------------------------
192 0 : void PropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle, Any& _out_rValue ) const
193 : {
194 0 : if ( !hasPropertyByHandle( _nHandle ) )
195 0 : throw UnknownPropertyException();
196 :
197 0 : MapInt2Any::const_iterator pos = m_pImpl->aDefaults.find( _nHandle );
198 : OSL_ENSURE( pos != m_pImpl->aDefaults.end(), "PropertyBag::getPropertyDefaultByHandle: inconsistency!" );
199 0 : if ( pos != m_pImpl->aDefaults.end() )
200 0 : _out_rValue = pos->second;
201 : else
202 0 : _out_rValue.clear();
203 0 : }
204 :
205 :
206 : //........................................................................
207 : } // namespace comphelper
208 : //........................................................................
209 :
210 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|