Coverage Report

Created: 2026-03-31 11:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/basic/source/inc/sbunoobj.hxx
Line
Count
Source
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
#pragma once
20
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>
34
#include <o3tl/string_view.hxx>
35
36
#include <string_view>
37
#include <unordered_map>
38
#include <utility>
39
#include <vector>
40
#include <map>
41
42
void registerComponentToBeDisposedForBasic( const css::uno::Reference< css::lang::XComponent >& xComponent, StarBASIC* pBasic );
43
44
class StructRefInfo
45
{
46
    css::uno::Any& maAny;
47
    css::uno::Type maType;
48
    sal_Int32 mnPos;
49
public:
50
0
    StructRefInfo( css::uno::Any& aAny, css::uno::Type const & rType, sal_Int32 nPos ) : maAny( aAny ), maType( rType ), mnPos( nPos ) {}
51
52
0
    sal_Int32 getPos() const { return mnPos; }
53
0
    const css::uno::Type& getType() const { return maType; }
54
    OUString getTypeName() const;
55
0
    css::uno::Any& getRootAnyRef() { return maAny; };
56
57
    css::uno::TypeClass getTypeClass() const;
58
59
    void* getInst();
60
0
    bool isEmpty() const { return (mnPos == -1); }
61
62
    css::uno::Any getValue();
63
    void setValue( const css::uno::Any& );
64
};
65
66
class SbUnoStructRefObject final : public SbxObject
67
{
68
    struct caseLessComp
69
    {
70
        bool operator() (std::u16string_view rProp, std::u16string_view rOtherProp ) const
71
0
        {
72
0
            return o3tl::compareToIgnoreAsciiCase( rProp, rOtherProp ) < 0;
73
0
        }
74
    };
75
    typedef std::map< OUString, std::unique_ptr<StructRefInfo>, caseLessComp > StructFieldInfo;
76
    StructFieldInfo maFields;
77
    StructRefInfo maMemberInfo;
78
    bool mbMemberCacheInit;
79
    void implCreateAll();
80
    void implCreateDbgProperties();
81
    void initMemberCache();
82
    OUString Impl_DumpProperties();
83
    OUString getDbgObjectName() const;
84
public:
85
    StructRefInfo getStructMember( const OUString& rMember );
86
0
    const StructRefInfo& getStructInfo() const { return maMemberInfo; }
87
    SbUnoStructRefObject( const OUString& aName_, StructRefInfo aMemberInfo );
88
    virtual ~SbUnoStructRefObject() override;
89
90
    // override Find to support e. g. NameAccess
91
    virtual SbxVariable* Find( const OUString&, SbxClassType ) override;
92
93
    // Force creation of all properties for debugging
94
    void createAllProperties()
95
0
        { implCreateAll(); }
96
97
    // give out value
98
    css::uno::Any getUnoAny();
99
    void Notify( SfxBroadcaster&, const SfxHint& rHint ) override;
100
};
101
102
class SbUnoObject: public SbxObject
103
{
104
    css::uno::Reference< css::beans::XIntrospectionAccess > mxUnoAccess;
105
    css::uno::Reference< css::beans::XMaterialHolder > mxMaterialHolder;
106
    css::uno::Reference< css::script::XInvocation > mxInvocation;
107
    css::uno::Reference< css::beans::XExactName > mxExactName;
108
    css::uno::Reference< css::beans::XExactName > mxExactNameInvocation;
109
    bool bNeedIntrospection;
110
    bool bNativeCOMObject;
111
    css::uno::Any maTmpUnoObj; // Only to save obj for doIntrospection!
112
    std::shared_ptr< SbUnoStructRefObject > maStructInfo;
113
    // help method to establish the dbg_-properties
114
    void implCreateDbgProperties();
115
116
    // help method to establish all properties and methods
117
    // (on the on-demand-mechanism required for the dbg_-properties)
118
    void implCreateAll();
119
120
public:
121
    static bool getDefaultPropName( SbUnoObject const * pUnoObj, OUString& sDfltProp );
122
    SbUnoObject( const OUString& aName_, const css::uno::Any& aUnoObj_ );
123
    virtual ~SbUnoObject() override;
124
125
    // #76470 do introspection on demand
126
    void doIntrospection();
127
128
    // override Find to support e. g. NameAccess
129
    virtual SbxVariable* Find( const OUString&, SbxClassType ) override;
130
131
    // Force creation of all properties for debugging
132
    void createAllProperties()
133
0
        { implCreateAll(); }
134
135
    // give out value
136
    css::uno::Any getUnoAny();
137
0
    const css::uno::Reference< css::beans::XIntrospectionAccess >& getIntrospectionAccess() const { return mxUnoAccess; }
138
0
    const css::uno::Reference< css::script::XInvocation >& getInvocation() const { return mxInvocation; }
139
140
    void Notify( SfxBroadcaster&, const SfxHint& rHint ) override;
141
142
    bool isNativeCOMObject() const
143
0
        { return bNativeCOMObject; }
144
145
    virtual double GetDate() const override;
146
};
147
typedef tools::SvRef<SbUnoObject> SbUnoObjectRef;
148
149
// #67781 delete return values of the uno-methods
150
void clearUnoMethods();
151
void clearUnoMethodsForBasic( StarBASIC const * pBasic );
152
153
class SbUnoMethod final : public SbxMethod
154
{
155
    friend class SbUnoObject;
156
    friend void clearUnoMethods();
157
    friend void clearUnoMethodsForBasic( StarBASIC const * pBasic );
158
159
    css::uno::Reference< css::reflection::XIdlMethod > m_xUnoMethod;
160
    std::unique_ptr<css::uno::Sequence< css::reflection::ParamInfo >> pParamInfoSeq;
161
162
    // #67781 reference to the previous and the next method in the method list
163
    SbUnoMethod* pPrev;
164
    SbUnoMethod* pNext;
165
166
    bool mbInvocation;       // Method is based on invocation
167
168
public:
169
170
    SbUnoMethod( const OUString& aName_, SbxDataType eSbxType, css::uno::Reference< css::reflection::XIdlMethod > const & xUnoMethod_,
171
        bool bInvocation );
172
    virtual ~SbUnoMethod() override;
173
    virtual SbxInfo* GetInfo() override;
174
175
    const css::uno::Sequence< css::reflection::ParamInfo >& getParamInfos();
176
177
    bool isInvocationBased() const
178
0
        { return mbInvocation; }
179
};
180
181
182
class SbUnoProperty final : public SbxProperty
183
{
184
    friend class SbUnoObject;
185
    friend class SbUnoStructRefObject;
186
187
    css::beans::Property aUnoProp;
188
    sal_Int32 nId;
189
190
    bool mbInvocation;      // Property is based on invocation
191
    SbxDataType mRealType;
192
    virtual ~SbUnoProperty() override;
193
    bool mbUnoStruct;
194
    SbUnoProperty( const SbUnoProperty&) = delete;
195
    SbUnoProperty& operator = ( const SbUnoProperty&) = delete;
196
public:
197
198
    SbUnoProperty( const OUString& aName_, SbxDataType eSbxType, SbxDataType eRealSbxType,
199
        css::beans::Property aUnoProp_, sal_Int32 nId_, bool bInvocation, bool bUnoStruct );
200
201
0
    bool isUnoStruct() const { return mbUnoStruct; }
202
    bool isInvocationBased() const
203
0
        { return mbInvocation; }
204
0
    SbxDataType getRealType() const { return mRealType; }
205
0
    const OUString& getUnoName() const { return aUnoProp.Name; }
206
};
207
208
// factory class to create uno-structs per DIM AS NEW
209
class SbUnoFactory final : public SbxFactory
210
{
211
public:
212
    virtual SbxBaseRef Create( sal_uInt16 nSbxId, sal_uInt32 ) override;
213
    virtual SbxObjectRef CreateObject( const OUString& ) override;
214
};
215
216
// wrapper for a uno-class
217
class SbUnoClass final : public SbxObject
218
{
219
    const css::uno::Reference< css::reflection::XIdlClass >   m_xClass;
220
221
public:
222
    SbUnoClass( const OUString& aName_ )
223
        : SbxObject( aName_ )
224
0
    {}
225
    SbUnoClass( const OUString& aName_, css::uno::Reference< css::reflection::XIdlClass > xClass_ )
226
        : SbxObject( aName_ )
227
        , m_xClass(std::move( xClass_ ))
228
0
    {}
229
230
231
    virtual SbxVariable* Find( const OUString&, SbxClassType ) override;
232
233
234
0
    const css::uno::Reference< css::reflection::XIdlClass >& getUnoClass() const { return m_xClass; }
235
236
};
237
238
239
// function to find a global identifier in
240
// the UnoScope and to wrap it for Sbx
241
SbUnoClass* findUnoClass( const OUString& rName );
242
243
244
// Wrapper for UNO Service
245
class SbUnoService final : public SbxObject
246
{
247
    const css::uno::Reference< css::reflection::XServiceTypeDescription2 > m_xServiceTypeDesc;
248
    bool m_bNeedsInit;
249
250
public:
251
    SbUnoService( const OUString& aName_,
252
        css::uno::Reference< css::reflection::XServiceTypeDescription2 >  xServiceTypeDesc )
253
            : SbxObject( aName_ )
254
            , m_xServiceTypeDesc(std::move( xServiceTypeDesc ))
255
            , m_bNeedsInit( true )
256
0
    {}
257
258
    virtual SbxVariable* Find( const OUString&, SbxClassType ) override;
259
260
    void Notify( SfxBroadcaster&, const SfxHint& rHint ) override;
261
};
262
263
SbUnoService* findUnoService( const OUString& rName );
264
265
266
class SbUnoServiceCtor final : public SbxMethod
267
{
268
    friend class SbUnoService;
269
270
    css::uno::Reference< css::reflection::XServiceConstructorDescription > m_xServiceCtorDesc;
271
272
public:
273
274
    SbUnoServiceCtor( const OUString& aName_, css::uno::Reference< css::reflection::XServiceConstructorDescription > const & xServiceCtorDesc );
275
    virtual ~SbUnoServiceCtor() override;
276
    virtual SbxInfo* GetInfo() override;
277
278
    const css::uno::Reference< css::reflection::XServiceConstructorDescription >& getServiceCtorDesc() const
279
0
        { return m_xServiceCtorDesc; }
280
};
281
282
283
// Wrapper for UNO Singleton
284
class SbUnoSingleton final : public SbxObject
285
{
286
public:
287
    SbUnoSingleton( const OUString& aName_ );
288
289
    void Notify( SfxBroadcaster&, const SfxHint& rHint ) override;
290
};
291
292
SbUnoSingleton* findUnoSingleton( const OUString& rName );
293
294
295
// #105565 Special Object to wrap a strongly typed Uno Any
296
class SbUnoAnyObject final : public SbxObject
297
{
298
    css::uno::Any     mVal;
299
300
public:
301
    SbUnoAnyObject( css::uno::Any  rVal )
302
        : SbxObject( OUString() )
303
        , mVal(std::move( rVal ))
304
0
    {}
305
306
    const css::uno::Any& getValue() const
307
0
        { return mVal; }
308
309
};
310
311
312
// #112509 Special SbxArray to transport named parameters for calls
313
// to OLEAutomation objects through the UNO OLE automation bridge
314
315
class AutomationNamedArgsSbxArray final : public SbxArray
316
{
317
    css::uno::Sequence< OUString >      maNameSeq;
318
public:
319
    AutomationNamedArgsSbxArray( sal_Int32 nSeqSize )
320
        : maNameSeq( nSeqSize )
321
0
    {}
322
323
    css::uno::Sequence< OUString >& getNames()
324
0
        { return maNameSeq; }
325
};
326
327
328
class StarBASIC;
329
330
// Impl-methods for RTL
331
void RTL_Impl_CreateUnoStruct( SbxArray& rPar );
332
void RTL_Impl_CreateUnoService( SbxArray& rPar );
333
void RTL_Impl_CreateUnoServiceWithArguments( SbxArray& rPar );
334
void RTL_Impl_CreateUnoValue( SbxArray& rPar );
335
void RTL_Impl_GetProcessServiceManager( SbxArray& rPar );
336
void RTL_Impl_HasInterfaces( SbxArray& rPar );
337
void RTL_Impl_IsUnoStruct( SbxArray& rPar );
338
void RTL_Impl_EqualUnoObjects( SbxArray& rPar );
339
void RTL_Impl_GetDefaultContext( SbxArray& rPar );
340
341
void disposeComVariablesForBasic( StarBASIC const * pBasic );
342
void clearNativeObjectWrapperVector();
343
344
345
// #118116 Collection object
346
347
class BasicCollection final : public SbxObject
348
{
349
    friend class SbiRuntime;
350
    SbxArrayRef xItemArray;
351
    static SbxInfoRef xAddInfo;
352
    static SbxInfoRef xItemInfo;
353
354
    void Initialize();
355
    virtual ~BasicCollection() override;
356
    virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) override;
357
    sal_Int32 implGetIndex( SbxVariable const * pIndexVar );
358
    sal_Int32 implGetIndexForName(const OUString& rName);
359
    void CollAdd( SbxArray* pPar_ );
360
    void CollItem( SbxArray* pPar_ );
361
    void CollRemove( SbxArray* pPar_ );
362
363
public:
364
    BasicCollection( const OUString& rClassname );
365
    virtual void Clear() override;
366
};
367
368
class VBAConstantHelper
369
{
370
private:
371
    std::vector< OUString > aConstCache;
372
    std::unordered_map< OUString, css::uno::Any > aConstHash;
373
    bool isInited;
374
0
    VBAConstantHelper():isInited( false ) {}
375
    VBAConstantHelper(const VBAConstantHelper&) = delete;
376
    void init();
377
public:
378
    static VBAConstantHelper& instance();
379
    SbxVariable* getVBAConstant( const OUString& rName );
380
    bool isVBAConstantType( std::u16string_view rName );
381
};
382
383
SbxVariable* getDefaultProp( SbxVariable* pRef );
384
385
css::uno::Reference< css::uno::XInterface > createComListener( const css::uno::Any& aControlAny,
386
                                                               const OUString& aVBAType,
387
                                                               std::u16string_view aPrefix,
388
                                                               const SbxObjectRef& xScopeObj );
389
390
bool checkUnoObjectType(SbUnoObject& refVal, const OUString& aClass);
391
392
SbUnoObject* createOLEObject_Impl( const OUString& aType );
393
394
// #55226 ship additional information
395
bool handleToStringForCOMObjects( SbxObject* pObj, SbxValue* pVal );
396
397
void registerComListenerVariableForBasic( SbxVariable* pVar, StarBASIC* pBasic );
398
399
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */