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