1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
;
41 static bool SbCompare_UString_PropertyValue_Impl(PropertyValue
const & lhs
, const OUString
& rhs
)
43 return lhs
.Name
.compareTo(rhs
) < 0;
47 SbPropertyValues::SbPropertyValues() = default;
50 SbPropertyValues::~SbPropertyValues() = default;
52 Reference
< XPropertySetInfo
> SbPropertyValues::getPropertySetInfo()
57 uno::Sequence
<beans::Property
> props(m_aPropVals
.size());
58 for (size_t n
= 0; n
< m_aPropVals
.size(); ++n
)
60 Property
&rProp
= props
.getArray()[n
];
61 const PropertyValue
&rPropVal
= m_aPropVals
[n
];
62 rProp
.Name
= rPropVal
.Name
;
63 rProp
.Handle
= rPropVal
.Handle
;
64 rProp
.Type
= cppu::UnoType
<void>::get();
67 m_xInfo
.set(new ::comphelper::PropertySetInfo(props
));
73 size_t SbPropertyValues::GetIndex_Impl( const OUString
&rPropName
) const
75 SbPropertyValueArr_Impl::const_iterator it
= std::lower_bound(
76 m_aPropVals
.begin(), m_aPropVals
.end(), rPropName
,
77 SbCompare_UString_PropertyValue_Impl
);
78 if (it
== m_aPropVals
.end() || it
->Name
!= rPropName
)
80 throw beans::UnknownPropertyException(
81 "Property not found: " + rPropName
,
82 const_cast<SbPropertyValues
&>(*this));
84 return it
- m_aPropVals
.begin();
88 void SbPropertyValues::setPropertyValue(
89 const OUString
& aPropertyName
,
92 size_t const nIndex
= GetIndex_Impl( aPropertyName
);
93 PropertyValue
& rPropVal
= m_aPropVals
[nIndex
];
94 rPropVal
.Value
= aValue
;
98 Any
SbPropertyValues::getPropertyValue(
99 const OUString
& aPropertyName
)
101 size_t const nIndex
= GetIndex_Impl( aPropertyName
);
102 return m_aPropVals
[nIndex
].Value
;
106 void SbPropertyValues::addPropertyChangeListener(
108 const Reference
< XPropertyChangeListener
>& )
112 void SbPropertyValues::removePropertyChangeListener(
114 const Reference
< XPropertyChangeListener
>& )
118 void SbPropertyValues::addVetoableChangeListener(
120 const Reference
< XVetoableChangeListener
>& )
124 void SbPropertyValues::removeVetoableChangeListener(
126 const Reference
< XVetoableChangeListener
>& )
130 Sequence
< PropertyValue
> SbPropertyValues::getPropertyValues()
132 return comphelper::containerToSequence(m_aPropVals
);
136 void SbPropertyValues::setPropertyValues(const Sequence
< PropertyValue
>& rPropertyValues
)
138 if (!m_aPropVals
.empty())
139 throw IllegalArgumentException("m_aPropVals not empty", static_cast<cppu::OWeakObject
*>(this), -1);
141 for (const PropertyValue
& i
: rPropertyValues
)
143 m_aPropVals
.push_back(i
);
148 void RTL_Impl_CreatePropertySet( SbxArray
& rPar
)
150 // We need at least one parameter
151 // TODO: In this case < 2 is not correct ;-)
152 if (rPar
.Count() < 2)
154 StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT
);
158 // Get class names of struct
160 Reference
< XInterface
> xInterface
= static_cast<OWeakObject
*>(new SbPropertyValues());
162 SbxVariableRef refVar
= rPar
.Get(0);
163 if( xInterface
.is() )
165 // Set PropertyValues
166 Any aArgAsAny
= sbxToUnoValue(rPar
.Get(1),
167 cppu::UnoType
<Sequence
<PropertyValue
>>::get() );
168 auto pArg
= o3tl::doAccess
<Sequence
<PropertyValue
>>(aArgAsAny
);
169 Reference
< XPropertyAccess
> xPropAcc( xInterface
, UNO_QUERY
);
170 xPropAcc
->setPropertyValues( *pArg
);
172 // Build a SbUnoObject and return it
173 auto xUnoObj
= tools::make_ref
<SbUnoObject
>( "stardiv.uno.beans.PropertySet", Any(xInterface
) );
174 if( xUnoObj
->getUnoAny().hasValue() )
177 refVar
->PutObject( xUnoObj
.get() );
182 // Object could not be created
183 refVar
->PutObject( nullptr );
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */