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