LibreOffice Module comphelper (master) 1
propertysethelper.cxx
Go to the documentation of this file.
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
22#include <osl/diagnose.h>
23#include <rtl/ref.hxx>
24
25#include <memory>
26#include <utility>
27
28using namespace ::comphelper;
29using namespace ::com::sun::star;
30using namespace ::com::sun::star::uno;
31using namespace ::com::sun::star::beans;
32using namespace ::com::sun::star::lang;
33
34static PropertyMapEntry const * find( const rtl::Reference<PropertySetInfo>& mxInfo, const OUString& aName ) noexcept
35{
36 PropertyMap::const_iterator aIter = mxInfo->getPropertyMap().find( aName );
37
38 if( mxInfo->getPropertyMap().end() != aIter )
39 return (*aIter).second;
40 else
41 return nullptr;
42}
43
44
45PropertySetHelper::PropertySetHelper( rtl::Reference<comphelper::PropertySetInfo> xInfo ) noexcept
46 : mxInfo(std::move(xInfo))
47{
48}
49
50PropertySetHelper::~PropertySetHelper() noexcept
51{
52}
53
54// XPropertySet
55Reference< XPropertySetInfo > SAL_CALL PropertySetHelper::getPropertySetInfo( )
56{
57 return mxInfo;
58}
59
60void SAL_CALL PropertySetHelper::setPropertyValue( const OUString& aPropertyName, const Any& aValue )
61{
62 PropertyMapEntry const * aEntries[2];
63 aEntries[0] = find( mxInfo, aPropertyName );
64
65 if( nullptr == aEntries[0] )
66 throw UnknownPropertyException( aPropertyName, static_cast< XPropertySet* >( this ) );
67
68 aEntries[1] = nullptr;
69
70 _setPropertyValues( aEntries, &aValue );
71}
72
73Any SAL_CALL PropertySetHelper::getPropertyValue( const OUString& PropertyName )
74{
75 PropertyMapEntry const * aEntries[2];
76 aEntries[0] = find( mxInfo, PropertyName );
77
78 if( nullptr == aEntries[0] )
79 throw UnknownPropertyException( PropertyName, static_cast< XPropertySet* >( this ) );
80
81 aEntries[1] = nullptr;
82
83 Any aAny;
84 _getPropertyValues( aEntries, &aAny );
85
86 return aAny;
87}
88
89void SAL_CALL PropertySetHelper::addPropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
90{
91 // todo
92}
93
94void SAL_CALL PropertySetHelper::removePropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
95{
96 // todo
97}
98
99void SAL_CALL PropertySetHelper::addVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
100{
101 // todo
102}
103
104void SAL_CALL PropertySetHelper::removeVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
105{
106 // todo
107}
108
109// XMultiPropertySet
110void SAL_CALL PropertySetHelper::setPropertyValues( const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rValues )
111{
112 const sal_Int32 nCount = rPropertyNames.getLength();
113
114 if( nCount != rValues.getLength() )
115 throw IllegalArgumentException("lengths do not match", uno::Reference<uno::XInterface>(), -1);
116
117 if( !nCount )
118 return;
119
120 std::unique_ptr<PropertyMapEntry const *[]> pEntries(new PropertyMapEntry const *[nCount+1]);
121 pEntries[nCount] = nullptr;
122 const OUString* pNames = rPropertyNames.getConstArray();
123
124 bool bUnknown = false;
125 sal_Int32 n;
126 for( n = 0; !bUnknown && ( n < nCount ); n++, pNames++ )
127 {
128 pEntries[n] = find( mxInfo, *pNames );
129 bUnknown = nullptr == pEntries[n];
130 }
131
132 if( !bUnknown )
133 _setPropertyValues( pEntries.get(), rValues.getConstArray() );
134
135 if( bUnknown )
136 throw RuntimeException( *pNames, static_cast< XPropertySet* >( this ) );
137}
138
139Sequence< Any > SAL_CALL PropertySetHelper::getPropertyValues(const Sequence< OUString >& rPropertyNames)
140{
141 const sal_Int32 nCount = rPropertyNames.getLength();
142
143 if( !nCount )
144 return Sequence< Any >();
145
146 std::unique_ptr<PropertyMapEntry const *[]> pEntries(new PropertyMapEntry const *[nCount+1]);
147 const OUString* pNames = rPropertyNames.getConstArray();
148
149 bool bUnknown = false;
150 sal_Int32 n;
151 for( n = 0; !bUnknown && ( n < nCount ); n++, pNames++ )
152 {
153 pEntries[n] = find( mxInfo, *pNames );
154 bUnknown = nullptr == pEntries[n];
155 }
156
157 if( bUnknown )
158 throw RuntimeException( *pNames, static_cast< XPropertySet* >( this ) );
159
160 pEntries[nCount] = nullptr;
161 Sequence< Any > aValues(nCount);
162 aValues.realloc(nCount);
163 _getPropertyValues( pEntries.get(), aValues.getArray() );
164 return aValues;
165
166}
167
168void SAL_CALL PropertySetHelper::addPropertiesChangeListener( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
169{
170 // todo
171}
172
173void SAL_CALL PropertySetHelper::removePropertiesChangeListener( const Reference< XPropertiesChangeListener >& )
174{
175 // todo
176}
177
178void SAL_CALL PropertySetHelper::firePropertiesChangeEvent( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
179{
180 // todo
181}
182
183// XPropertyState
184PropertyState SAL_CALL PropertySetHelper::getPropertyState( const OUString& PropertyName )
185{
186 PropertyMapEntry const * aEntries[2];
187
188 aEntries[0] = find( mxInfo, PropertyName );
189 if( aEntries[0] == nullptr )
190 throw UnknownPropertyException( PropertyName, static_cast< XPropertySet* >( this ) );
191
192 aEntries[1] = nullptr;
193
194 PropertyState aState(PropertyState_AMBIGUOUS_VALUE);
195 _getPropertyStates( aEntries, &aState );
196
197 return aState;
198}
199
200Sequence< PropertyState > SAL_CALL PropertySetHelper::getPropertyStates( const Sequence< OUString >& aPropertyName )
201{
202 const sal_Int32 nCount = aPropertyName.getLength();
203
204 Sequence< PropertyState > aStates( nCount );
205
206 if( nCount )
207 {
208 const OUString* pNames = aPropertyName.getConstArray();
209
210 bool bUnknown = false;
211
212 std::unique_ptr<PropertyMapEntry const *[]> pEntries(new PropertyMapEntry const *[nCount+1]);
213
214 sal_Int32 n;
215 for( n = 0; !bUnknown && (n < nCount); n++, pNames++ )
216 {
217 pEntries[n] = find( mxInfo, *pNames );
218 bUnknown = nullptr == pEntries[n];
219 }
220
221 pEntries[nCount] = nullptr;
222
223 if( !bUnknown )
224 _getPropertyStates( pEntries.get(), aStates.getArray() );
225
226 if( bUnknown )
227 throw UnknownPropertyException( *pNames, static_cast< XPropertySet* >( this ) );
228 }
229
230 return aStates;
231}
232
233void SAL_CALL PropertySetHelper::setPropertyToDefault( const OUString& PropertyName )
234{
235 PropertyMapEntry const *pEntry = find(mxInfo, PropertyName );
236 if( nullptr == pEntry )
237 throw UnknownPropertyException( PropertyName, static_cast< XPropertySet* >( this ) );
238
239 _setPropertyToDefault( pEntry );
240}
241
242Any SAL_CALL PropertySetHelper::getPropertyDefault( const OUString& aPropertyName )
243{
244 PropertyMapEntry const * pEntry = find(mxInfo, aPropertyName );
245 if( nullptr == pEntry )
246 throw UnknownPropertyException( aPropertyName, static_cast< XPropertySet* >( this ) );
247
248 return _getPropertyDefault( pEntry );
249}
250
251void PropertySetHelper::_getPropertyStates(
252 const comphelper::PropertyMapEntry**, PropertyState*)
253{
254 OSL_FAIL( "you have to implement this yourself!");
255}
256
257void
258PropertySetHelper::_setPropertyToDefault(const comphelper::PropertyMapEntry*)
259{
260 OSL_FAIL( "you have to implement this yourself!");
261}
262
263Any PropertySetHelper::_getPropertyDefault(const comphelper::PropertyMapEntry*)
264{
265 OSL_FAIL( "you have to implement this yourself!");
266
267 Any aAny;
268 return aAny;
269}
270
271/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
int nCount
ScXMLEditAttributeMap::Entry const aEntries[]
OUString aName
sal_Int64 n
class SAL_NO_VTABLE XPropertySet
std::pair< OUString const, DataAccessDescriptorProperty > PropertyMapEntry
static PropertyMapEntry const * find(const rtl::Reference< PropertySetInfo > &mxInfo, const OUString &aName) noexcept