LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / basic / source / inc / sbunoobj.hxx
blob1f3042951e51f6f67abca74e0af8f4425ce7fdb5
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 .
19 #pragma once
21 #include <basic/sbxobj.hxx>
22 #include <basic/sbxmeth.hxx>
23 #include <sbxprop.hxx>
24 #include <sbxfac.hxx>
25 #include <basic/sbx.hxx>
26 #include <com/sun/star/beans/XMaterialHolder.hpp>
27 #include <com/sun/star/beans/XExactName.hpp>
28 #include <com/sun/star/beans/XIntrospectionAccess.hpp>
29 #include <com/sun/star/lang/XComponent.hpp>
30 #include <com/sun/star/script/XInvocation.hpp>
31 #include <com/sun/star/reflection/XIdlClass.hpp>
32 #include <com/sun/star/reflection/XServiceTypeDescription2.hpp>
33 #include <rtl/ustring.hxx>
35 #include <string_view>
36 #include <unordered_map>
37 #include <vector>
38 #include <map>
40 void registerComponentToBeDisposedForBasic( const css::uno::Reference< css::lang::XComponent >& xComponent, StarBASIC* pBasic );
42 class StructRefInfo
44 css::uno::Any& maAny;
45 css::uno::Type maType;
46 sal_Int32 mnPos;
47 public:
48 StructRefInfo( css::uno::Any& aAny, css::uno::Type const & rType, sal_Int32 nPos ) : maAny( aAny ), maType( rType ), mnPos( nPos ) {}
50 sal_Int32 getPos() const { return mnPos; }
51 const css::uno::Type& getType() const { return maType; }
52 OUString getTypeName() const;
53 css::uno::Any& getRootAnyRef() { return maAny; };
55 css::uno::TypeClass getTypeClass() const;
57 void* getInst();
58 bool isEmpty() const { return (mnPos == -1); }
60 css::uno::Any getValue();
61 void setValue( const css::uno::Any& );
64 class SbUnoStructRefObject final : public SbxObject
66 struct caseLessComp
68 bool operator() (const OUString& rProp, std::u16string_view rOtherProp ) const
70 return rProp.compareToIgnoreAsciiCase( rOtherProp ) < 0;
73 typedef std::map< OUString, std::unique_ptr<StructRefInfo>, caseLessComp > StructFieldInfo;
74 StructFieldInfo maFields;
75 StructRefInfo maMemberInfo;
76 bool mbMemberCacheInit;
77 void implCreateAll();
78 void implCreateDbgProperties();
79 void initMemberCache();
80 OUString Impl_DumpProperties();
81 OUString getDbgObjectName() const;
82 public:
83 StructRefInfo getStructMember( const OUString& rMember );
84 const StructRefInfo& getStructInfo() const { return maMemberInfo; }
85 SbUnoStructRefObject( const OUString& aName_, const StructRefInfo& rMemberInfo );
86 virtual ~SbUnoStructRefObject() override;
88 // override Find to support e. g. NameAccess
89 virtual SbxVariable* Find( const OUString&, SbxClassType ) override;
91 // Force creation of all properties for debugging
92 void createAllProperties()
93 { implCreateAll(); }
95 // give out value
96 css::uno::Any getUnoAny();
97 void Notify( SfxBroadcaster&, const SfxHint& rHint ) override;
100 class SbUnoObject: public SbxObject
102 css::uno::Reference< css::beans::XIntrospectionAccess > mxUnoAccess;
103 css::uno::Reference< css::beans::XMaterialHolder > mxMaterialHolder;
104 css::uno::Reference< css::script::XInvocation > mxInvocation;
105 css::uno::Reference< css::beans::XExactName > mxExactName;
106 css::uno::Reference< css::beans::XExactName > mxExactNameInvocation;
107 bool bNeedIntrospection;
108 bool bNativeCOMObject;
109 css::uno::Any maTmpUnoObj; // Only to save obj for doIntrospection!
110 std::shared_ptr< SbUnoStructRefObject > maStructInfo;
111 // help method to establish the dbg_-properties
112 void implCreateDbgProperties();
114 // help method to establish all properties and methods
115 // (on the on-demand-mechanism required for the dbg_-properties)
116 void implCreateAll();
118 public:
119 static bool getDefaultPropName( SbUnoObject const * pUnoObj, OUString& sDfltProp );
120 SbUnoObject( const OUString& aName_, const css::uno::Any& aUnoObj_ );
121 virtual ~SbUnoObject() override;
123 // #76470 do introspection on demand
124 void doIntrospection();
126 // override Find to support e. g. NameAccess
127 virtual SbxVariable* Find( const OUString&, SbxClassType ) override;
129 // Force creation of all properties for debugging
130 void createAllProperties()
131 { implCreateAll(); }
133 // give out value
134 css::uno::Any getUnoAny();
135 const css::uno::Reference< css::beans::XIntrospectionAccess >& getIntrospectionAccess() const { return mxUnoAccess; }
136 const css::uno::Reference< css::script::XInvocation >& getInvocation() const { return mxInvocation; }
138 void Notify( SfxBroadcaster&, const SfxHint& rHint ) override;
140 bool isNativeCOMObject() const
141 { return bNativeCOMObject; }
143 typedef tools::SvRef<SbUnoObject> SbUnoObjectRef;
145 // #67781 delete return values of the uno-methods
146 void clearUnoMethods();
147 void clearUnoMethodsForBasic( StarBASIC const * pBasic );
149 class SbUnoMethod final : public SbxMethod
151 friend class SbUnoObject;
152 friend void clearUnoMethods();
153 friend void clearUnoMethodsForBasic( StarBASIC const * pBasic );
155 css::uno::Reference< css::reflection::XIdlMethod > m_xUnoMethod;
156 std::unique_ptr<css::uno::Sequence< css::reflection::ParamInfo >> pParamInfoSeq;
158 // #67781 reference to the previous and the next method in the method list
159 SbUnoMethod* pPrev;
160 SbUnoMethod* pNext;
162 bool mbInvocation; // Method is based on invocation
164 public:
166 SbUnoMethod( const OUString& aName_, SbxDataType eSbxType, css::uno::Reference< css::reflection::XIdlMethod > const & xUnoMethod_,
167 bool bInvocation );
168 virtual ~SbUnoMethod() override;
169 virtual SbxInfo* GetInfo() override;
171 const css::uno::Sequence< css::reflection::ParamInfo >& getParamInfos();
173 bool isInvocationBased() const
174 { return mbInvocation; }
178 class SbUnoProperty final : public SbxProperty
180 friend class SbUnoObject;
181 friend class SbUnoStructRefObject;
183 css::beans::Property aUnoProp;
184 sal_Int32 nId;
186 bool mbInvocation; // Property is based on invocation
187 SbxDataType mRealType;
188 virtual ~SbUnoProperty() override;
189 bool mbUnoStruct;
190 SbUnoProperty( const SbUnoProperty&) = delete;
191 SbUnoProperty& operator = ( const SbUnoProperty&) = delete;
192 public:
194 SbUnoProperty( const OUString& aName_, SbxDataType eSbxType, SbxDataType eRealSbxType,
195 const css::beans::Property& aUnoProp_, sal_Int32 nId_, bool bInvocation, bool bUnoStruct );
197 bool isUnoStruct() const { return mbUnoStruct; }
198 bool isInvocationBased() const
199 { return mbInvocation; }
200 SbxDataType getRealType() const { return mRealType; }
203 // factory class to create uno-structs per DIM AS NEW
204 class SbUnoFactory final : public SbxFactory
206 public:
207 virtual SbxBaseRef Create( sal_uInt16 nSbxId, sal_uInt32 ) override;
208 virtual SbxObjectRef CreateObject( const OUString& ) override;
211 // wrapper for a uno-class
212 class SbUnoClass final : public SbxObject
214 const css::uno::Reference< css::reflection::XIdlClass > m_xClass;
216 public:
217 SbUnoClass( const OUString& aName_ )
218 : SbxObject( aName_ )
220 SbUnoClass( const OUString& aName_, const css::uno::Reference< css::reflection::XIdlClass >& xClass_ )
221 : SbxObject( aName_ )
222 , m_xClass( xClass_ )
226 virtual SbxVariable* Find( const OUString&, SbxClassType ) override;
229 const css::uno::Reference< css::reflection::XIdlClass >& getUnoClass() const { return m_xClass; }
234 // function to find a global identifier in
235 // the UnoScope and to wrap it for Sbx
236 SbUnoClass* findUnoClass( const OUString& rName );
239 // Wrapper for UNO Service
240 class SbUnoService final : public SbxObject
242 const css::uno::Reference< css::reflection::XServiceTypeDescription2 > m_xServiceTypeDesc;
243 bool m_bNeedsInit;
245 public:
246 SbUnoService( const OUString& aName_,
247 const css::uno::Reference< css::reflection::XServiceTypeDescription2 >& xServiceTypeDesc )
248 : SbxObject( aName_ )
249 , m_xServiceTypeDesc( xServiceTypeDesc )
250 , m_bNeedsInit( true )
253 virtual SbxVariable* Find( const OUString&, SbxClassType ) override;
255 void Notify( SfxBroadcaster&, const SfxHint& rHint ) override;
258 SbUnoService* findUnoService( const OUString& rName );
261 class SbUnoServiceCtor final : public SbxMethod
263 friend class SbUnoService;
265 css::uno::Reference< css::reflection::XServiceConstructorDescription > m_xServiceCtorDesc;
267 public:
269 SbUnoServiceCtor( const OUString& aName_, css::uno::Reference< css::reflection::XServiceConstructorDescription > const & xServiceCtorDesc );
270 virtual ~SbUnoServiceCtor() override;
271 virtual SbxInfo* GetInfo() override;
273 const css::uno::Reference< css::reflection::XServiceConstructorDescription >& getServiceCtorDesc() const
274 { return m_xServiceCtorDesc; }
278 // Wrapper for UNO Singleton
279 class SbUnoSingleton final : public SbxObject
281 public:
282 SbUnoSingleton( const OUString& aName_ );
284 void Notify( SfxBroadcaster&, const SfxHint& rHint ) override;
287 SbUnoSingleton* findUnoSingleton( const OUString& rName );
290 // #105565 Special Object to wrap a strongly typed Uno Any
291 class SbUnoAnyObject final : public SbxObject
293 css::uno::Any mVal;
295 public:
296 SbUnoAnyObject( const css::uno::Any& rVal )
297 : SbxObject( OUString() )
298 , mVal( rVal )
301 const css::uno::Any& getValue() const
302 { return mVal; }
307 // #112509 Special SbxArray to transport named parameters for calls
308 // to OLEAutomation objects through the UNO OLE automation bridge
310 class AutomationNamedArgsSbxArray final : public SbxArray
312 css::uno::Sequence< OUString > maNameSeq;
313 public:
314 AutomationNamedArgsSbxArray( sal_Int32 nSeqSize )
315 : maNameSeq( nSeqSize )
318 css::uno::Sequence< OUString >& getNames()
319 { return maNameSeq; }
323 class StarBASIC;
325 // Impl-methods for RTL
326 void RTL_Impl_CreateUnoStruct( SbxArray& rPar );
327 void RTL_Impl_CreateUnoService( SbxArray& rPar );
328 void RTL_Impl_CreateUnoServiceWithArguments( SbxArray& rPar );
329 void RTL_Impl_CreateUnoValue( SbxArray& rPar );
330 void RTL_Impl_GetProcessServiceManager( SbxArray& rPar );
331 void RTL_Impl_HasInterfaces( SbxArray& rPar );
332 void RTL_Impl_IsUnoStruct( SbxArray& rPar );
333 void RTL_Impl_EqualUnoObjects( SbxArray& rPar );
334 void RTL_Impl_GetDefaultContext( SbxArray& rPar );
336 void disposeComVariablesForBasic( StarBASIC const * pBasic );
337 void clearNativeObjectWrapperVector();
340 // #118116 Collection object
342 class BasicCollection final : public SbxObject
344 friend class SbiRuntime;
345 SbxArrayRef xItemArray;
346 static SbxInfoRef xAddInfo;
347 static SbxInfoRef xItemInfo;
349 void Initialize();
350 virtual ~BasicCollection() override;
351 virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) override;
352 sal_Int32 implGetIndex( SbxVariable const * pIndexVar );
353 sal_Int32 implGetIndexForName(std::u16string_view rName);
354 void CollAdd( SbxArray* pPar_ );
355 void CollItem( SbxArray* pPar_ );
356 void CollRemove( SbxArray* pPar_ );
358 public:
359 BasicCollection( const OUString& rClassname );
360 virtual void Clear() override;
363 class VBAConstantHelper
365 private:
366 std::vector< OUString > aConstCache;
367 std::unordered_map< OUString, css::uno::Any > aConstHash;
368 bool isInited;
369 VBAConstantHelper():isInited( false ) {}
370 VBAConstantHelper(const VBAConstantHelper&) = delete;
371 void init();
372 public:
373 static VBAConstantHelper& instance();
374 SbxVariable* getVBAConstant( const OUString& rName );
375 bool isVBAConstantType( const OUString& rName );
378 SbxVariable* getDefaultProp( SbxVariable* pRef );
380 css::uno::Reference< css::uno::XInterface > createComListener( const css::uno::Any& aControlAny,
381 const OUString& aVBAType,
382 std::u16string_view aPrefix,
383 const SbxObjectRef& xScopeObj );
385 bool checkUnoObjectType(SbUnoObject& refVal, const OUString& aClass);
387 SbUnoObject* createOLEObject_Impl( const OUString& aType );
389 // #55226 ship additional information
390 bool handleToStringForCOMObjects( SbxObject* pObj, SbxValue* pVal );
392 void registerComListenerVariableForBasic( SbxVariable* pVar, StarBASIC* pBasic );
394 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */