Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / basic / source / classes / propacc.cxx
blobfbceb81ad7b1772b7a7b92c6ee73813bd5ac890b
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/sberrors.hxx>
24 #include <basic/sbstar.hxx>
25 #include <basic/sbuno.hxx>
26 #include <sbunoobj.hxx>
28 #include <comphelper/propertysetinfo.hxx>
29 #include <comphelper/sequence.hxx>
30 #include <o3tl/any.hxx>
32 #include <algorithm>
34 using com::sun::star::uno::Reference;
35 using namespace com::sun::star;
36 using namespace com::sun::star::uno;
37 using namespace com::sun::star::lang;
38 using namespace com::sun::star::beans;
39 using namespace cppu;
41 static bool SbCompare_UString_PropertyValue_Impl(PropertyValue const & lhs, const OUString& rhs)
43 return lhs.Name.compareTo(rhs) < 0;
47 SbPropertyValues::SbPropertyValues()
52 SbPropertyValues::~SbPropertyValues()
54 m_xInfo.clear();
57 Reference< XPropertySetInfo > SbPropertyValues::getPropertySetInfo()
59 // create on demand?
60 if (!m_xInfo.is())
62 uno::Sequence<beans::Property> props(m_aPropVals.size());
63 for (size_t n = 0; n < m_aPropVals.size(); ++n)
65 Property &rProp = props.getArray()[n];
66 const PropertyValue &rPropVal = m_aPropVals[n];
67 rProp.Name = rPropVal.Name;
68 rProp.Handle = rPropVal.Handle;
69 rProp.Type = cppu::UnoType<void>::get();
70 rProp.Attributes = 0;
72 m_xInfo.set(new ::comphelper::PropertySetInfo(props));
74 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() || it->Name != rPropName)
85 throw beans::UnknownPropertyException(
86 "Property not found: " + rPropName,
87 const_cast<SbPropertyValues&>(*this));
89 return it - m_aPropVals.begin();
93 void SbPropertyValues::setPropertyValue(
94 const OUString& aPropertyName,
95 const Any& aValue)
97 size_t const nIndex = GetIndex_Impl( aPropertyName );
98 PropertyValue & rPropVal = m_aPropVals[nIndex];
99 rPropVal.Value = aValue;
103 Any SbPropertyValues::getPropertyValue(
104 const OUString& aPropertyName)
106 size_t const nIndex = GetIndex_Impl( aPropertyName );
107 return m_aPropVals[nIndex].Value;
111 void SbPropertyValues::addPropertyChangeListener(
112 const OUString&,
113 const Reference< XPropertyChangeListener >& )
117 void SbPropertyValues::removePropertyChangeListener(
118 const OUString&,
119 const Reference< XPropertyChangeListener >& )
123 void SbPropertyValues::addVetoableChangeListener(
124 const OUString&,
125 const Reference< XVetoableChangeListener >& )
129 void SbPropertyValues::removeVetoableChangeListener(
130 const OUString&,
131 const Reference< XVetoableChangeListener >& )
135 Sequence< PropertyValue > SbPropertyValues::getPropertyValues()
137 return comphelper::containerToSequence(m_aPropVals);
141 void SbPropertyValues::setPropertyValues(const Sequence< PropertyValue >& rPropertyValues )
143 if (!m_aPropVals.empty())
144 throw IllegalArgumentException();
146 const PropertyValue *pPropVals = rPropertyValues.getConstArray();
147 for (sal_Int32 n = 0; n < rPropertyValues.getLength(); ++n)
149 m_aPropVals.push_back(pPropVals[n]);
154 void RTL_Impl_CreatePropertySet( SbxArray& rPar )
156 // We need at least one parameter
157 // TODO: In this case < 2 is not correct ;-)
158 if ( rPar.Count() < 2 )
160 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT );
161 return;
164 // Get class names of struct
166 Reference< XInterface > xInterface = static_cast<OWeakObject*>(new SbPropertyValues());
168 SbxVariableRef refVar = rPar.Get(0);
169 if( xInterface.is() )
171 // Set PropertyValues
172 Any aArgAsAny = sbxToUnoValue( rPar.Get(1),
173 cppu::UnoType<Sequence<PropertyValue>>::get() );
174 auto pArg = o3tl::doAccess<Sequence<PropertyValue>>(aArgAsAny);
175 Reference< XPropertyAccess > xPropAcc( xInterface, UNO_QUERY );
176 xPropAcc->setPropertyValues( *pArg );
178 // Build a SbUnoObject and return it
179 auto xUnoObj = tools::make_ref<SbUnoObject>( "stardiv.uno.beans.PropertySet", Any(xInterface) );
180 if( xUnoObj->getUnoAny().hasValue() )
182 // Return object
183 refVar->PutObject( xUnoObj.get() );
184 return;
188 // Object could not be created
189 refVar->PutObject( nullptr );
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */