bump product version to 5.0.4.1
[LibreOffice.git] / basic / source / classes / propacc.cxx
blob20b1fda7ac6cb0d273f9c45ec28be6996d35e1ba
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "propacc.hxx"
23 #include <basic/sbstar.hxx>
24 #include <basic/sbuno.hxx>
25 #include <sbunoobj.hxx>
26 #include <limits.h>
28 using com::sun::star::uno::Reference;
29 using namespace com::sun::star;
30 using namespace com::sun::star::uno;
31 using namespace com::sun::star::lang;
32 using namespace com::sun::star::beans;
33 using namespace cppu;
35 struct SbCompare_UString_PropertyValue_Impl
37 bool operator() (PropertyValue const & lhs, const OUString& rhs)
39 return lhs.Name.compareTo(rhs) < 0;
43 extern "C" int SAL_CALL SbCompare_UString_Property_Impl( const void *arg1, const void *arg2 )
45 const OUString *pArg1 = static_cast<OUString const *>(arg1);
46 const Property *pArg2 = static_cast<Property const *>(arg2);
47 return pArg1->compareTo( pArg2->Name );
52 SbPropertyValues::SbPropertyValues()
58 SbPropertyValues::~SbPropertyValues()
60 m_xInfo.clear();
65 Reference< XPropertySetInfo > SbPropertyValues::getPropertySetInfo() throw( RuntimeException, std::exception )
67 // create on demand?
68 if (!m_xInfo.is())
70 SbPropertySetInfo *pInfo = new SbPropertySetInfo( m_aPropVals );
71 m_xInfo.set(pInfo);
73 return m_xInfo;
78 size_t SbPropertyValues::GetIndex_Impl( const OUString &rPropName ) const
80 SbPropertyValueArr_Impl::const_iterator it = std::lower_bound(
81 m_aPropVals.begin(), m_aPropVals.end(), rPropName,
82 SbCompare_UString_PropertyValue_Impl() );
83 if (it == m_aPropVals.end())
85 throw beans::UnknownPropertyException(
86 "Property not found: " + rPropName,
87 const_cast<SbPropertyValues&>(*this));
89 return it - m_aPropVals.begin();
94 void SbPropertyValues::setPropertyValue(
95 const OUString& aPropertyName,
96 const Any& aValue)
97 throw (::com::sun::star::beans::UnknownPropertyException,
98 ::com::sun::star::beans::PropertyVetoException,
99 ::com::sun::star::lang::IllegalArgumentException,
100 ::com::sun::star::lang::WrappedTargetException,
101 ::com::sun::star::uno::RuntimeException, std::exception)
103 size_t const nIndex = GetIndex_Impl( aPropertyName );
104 PropertyValue & rPropVal = m_aPropVals[nIndex];
105 rPropVal.Value = aValue;
110 Any SbPropertyValues::getPropertyValue(
111 const OUString& aPropertyName)
112 throw(::com::sun::star::beans::UnknownPropertyException,
113 ::com::sun::star::lang::WrappedTargetException,
114 ::com::sun::star::uno::RuntimeException, std::exception)
116 size_t const nIndex = GetIndex_Impl( aPropertyName );
117 return m_aPropVals[nIndex].Value;
122 void SbPropertyValues::addPropertyChangeListener(
123 const OUString& aPropertyName,
124 const Reference< XPropertyChangeListener >& )
125 throw (std::exception)
127 (void)aPropertyName;
132 void SbPropertyValues::removePropertyChangeListener(
133 const OUString& aPropertyName,
134 const Reference< XPropertyChangeListener >& )
135 throw (std::exception)
137 (void)aPropertyName;
142 void SbPropertyValues::addVetoableChangeListener(
143 const OUString& aPropertyName,
144 const Reference< XVetoableChangeListener >& )
145 throw(std::exception)
147 (void)aPropertyName;
152 void SbPropertyValues::removeVetoableChangeListener(
153 const OUString& aPropertyName,
154 const Reference< XVetoableChangeListener >& )
155 throw(std::exception)
157 (void)aPropertyName;
162 Sequence< PropertyValue > SbPropertyValues::getPropertyValues() throw (::com::sun::star::uno::RuntimeException, std::exception)
164 Sequence<PropertyValue> aRet( m_aPropVals.size() );
165 for (size_t n = 0; n < m_aPropVals.size(); ++n)
166 aRet.getArray()[n] = m_aPropVals[n];
167 return aRet;
172 void SbPropertyValues::setPropertyValues(const Sequence< PropertyValue >& rPropertyValues )
173 throw (::com::sun::star::beans::UnknownPropertyException,
174 ::com::sun::star::beans::PropertyVetoException,
175 ::com::sun::star::lang::IllegalArgumentException,
176 ::com::sun::star::lang::WrappedTargetException,
177 ::com::sun::star::uno::RuntimeException, std::exception)
179 if ( !m_aPropVals.empty() )
180 throw IllegalArgumentException();
182 const PropertyValue *pPropVals = rPropertyValues.getConstArray();
183 for (sal_Int32 n = 0; n < rPropertyValues.getLength(); ++n)
185 PropertyValue *pPropVal = new PropertyValue(pPropVals[n]);
186 m_aPropVals.push_back( pPropVal );
191 //PropertySetInfoImpl
193 PropertySetInfoImpl::PropertySetInfoImpl()
197 sal_Int32 PropertySetInfoImpl::GetIndex_Impl( const OUString &rPropName ) const
199 Property *pP;
200 pP = static_cast<Property*>(
201 bsearch( &rPropName, _aProps.getConstArray(), _aProps.getLength(),
202 sizeof( Property ),
203 SbCompare_UString_Property_Impl ));
204 return pP ? sal::static_int_cast<sal_Int32>( pP - _aProps.getConstArray() ) : -1;
208 Property PropertySetInfoImpl::getPropertyByName(const OUString& Name) throw( RuntimeException )
210 sal_Int32 nIndex = GetIndex_Impl( Name );
211 if( USHRT_MAX != nIndex )
212 return _aProps.getConstArray()[ nIndex ];
213 return Property();
216 bool PropertySetInfoImpl::hasPropertyByName(const OUString& Name) throw( RuntimeException )
218 sal_Int32 nIndex = GetIndex_Impl( Name );
219 return USHRT_MAX != nIndex;
225 SbPropertySetInfo::SbPropertySetInfo( const SbPropertyValueArr_Impl &rPropVals )
227 aImpl._aProps.realloc( rPropVals.size() );
228 for ( sal_uInt16 n = 0; n < rPropVals.size(); ++n )
230 Property &rProp = aImpl._aProps.getArray()[n];
231 const PropertyValue &rPropVal = rPropVals[n];
232 rProp.Name = rPropVal.Name;
233 rProp.Handle = rPropVal.Handle;
234 rProp.Type = cppu::UnoType<void>::get();
235 rProp.Attributes = 0;
241 SbPropertySetInfo::~SbPropertySetInfo()
247 Sequence< Property > SbPropertySetInfo::getProperties() throw( RuntimeException, std::exception )
249 return aImpl.getProperties();
252 Property SbPropertySetInfo::getPropertyByName(const OUString& Name)
253 throw( RuntimeException, std::exception )
255 return aImpl.getPropertyByName( Name );
258 sal_Bool SbPropertySetInfo::hasPropertyByName(const OUString& Name)
259 throw( RuntimeException, std::exception )
261 return aImpl.hasPropertyByName( Name );
266 void RTL_Impl_CreatePropertySet( StarBASIC* pBasic, SbxArray& rPar, bool bWrite )
268 (void)pBasic;
269 (void)bWrite;
271 // We need at least one parameter
272 // TODO: In this case < 2 is not correct ;-)
273 if ( rPar.Count() < 2 )
275 StarBASIC::Error( SbERR_BAD_ARGUMENT );
276 return;
279 // Get class names of struct
280 OUString aServiceName( "stardiv.uno.beans.PropertySet");
282 Reference< XInterface > xInterface = (OWeakObject*) new SbPropertyValues();
284 SbxVariableRef refVar = rPar.Get(0);
285 if( xInterface.is() )
287 // Set PropertyValues
288 Any aArgAsAny = sbxToUnoValue( rPar.Get(1),
289 cppu::UnoType<Sequence<PropertyValue>>::get() );
290 Sequence<PropertyValue> const *pArg =
291 static_cast<Sequence<PropertyValue> const *>(aArgAsAny.getValue());
292 Reference< XPropertyAccess > xPropAcc = Reference< XPropertyAccess >::query( xInterface );
293 xPropAcc->setPropertyValues( *pArg );
295 // Build a SbUnoObject and return it
296 Any aAny;
297 aAny <<= xInterface;
298 SbUnoObjectRef xUnoObj = new SbUnoObject( aServiceName, aAny );
299 if( xUnoObj->getUnoAny().getValueType().getTypeClass() != TypeClass_VOID )
301 // Return object
302 refVar->PutObject( (SbUnoObject*)xUnoObj );
303 return;
307 // Object could not be created
308 refVar->PutObject( NULL );
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */