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