LibreOffice Module basic (master) 1
sbxvar.hxx
Go to the documentation of this file.
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
20#ifndef INCLUDED_BASIC_SBXVAR_HXX
21#define INCLUDED_BASIC_SBXVAR_HXX
22
23#include <rtl/character.hxx>
24#include <rtl/ustring.hxx>
25#include <basic/sbxcore.hxx>
26#include <basic/basicdllapi.h>
27#include <com/sun/star/uno/XInterface.hpp>
28#include <com/sun/star/uno/Reference.hxx>
29
30#include <algorithm>
31#include <cstddef>
32#include <cstring>
33#include <memory>
34
35
37
38class SbxDecimal;
39enum class SfxHintId;
40
42{
43 union {
45 sal_uInt16 nUShort;
47 sal_Int16 nInteger;
48 sal_uInt32 nULong;
49 sal_Int32 nLong;
50 unsigned int nUInt;
51 int nInt;
52 sal_uInt64 uInt64;
53 sal_Int64 nInt64;
54
55 float nSingle;
56 double nDouble;
57
58 OUString* pOUString;
60
62
64 sal_uInt16* pUShort;
66 sal_Int16* pInteger;
67 sal_uInt32* pULong;
68 sal_Int32* pLong;
69 sal_uInt64* puInt64;
70 sal_Int64* pnInt64;
71
72 float* pSingle;
73 double* pDouble;
74
75 void* pData;
76 };
78
79 SbxValues(): pData( nullptr ), eType(SbxEMPTY) {}
80 SbxValues( SbxDataType e ): pData( nullptr ), eType(e) {}
81 SbxValues( double _nDouble ): nDouble( _nDouble ), eType(SbxDOUBLE) {}
82
83 void clear(SbxDataType type) {
84 // A hacky way of zeroing the union value corresponding to the given type (even though the
85 // relevant zero value need not be represented by all-zero bits, in general) without evoking
86 // GCC 8 -Wclass-memaccess or loplugin:classmemaccess, and without having to turn the
87 // anonymous union into a non-anonymous one:
88 auto const p = static_cast<void *>(this);
89 std::memset(p, 0, offsetof(SbxValues, eType));
90 eType = type;
91 }
92};
93
95{
96 // #55226 Transport additional infos
97 BASIC_DLLPRIVATE SbxValue* TheRealValue( bool bObjInObjError ) const;
98protected:
100 OUString aPic; // Picture-String
101 OUString aToolString; // tool string copy
102
103 virtual void Broadcast( SfxHintId ); // Broadcast-Call
104 virtual ~SbxValue() override;
105 virtual bool LoadData( SvStream&, sal_uInt16 ) override;
106 virtual std::pair<bool, sal_uInt32> StoreData( SvStream& ) const override;
107public:
109 SbxValue();
111 SbxValue( const SbxValue& );
112 SbxValue& operator=( const SbxValue& );
113 virtual void Clear() override;
114 virtual bool IsFixed() const override;
115
116 bool IsInteger() const { return GetType() == SbxINTEGER ; }
117 bool IsLong() const { return GetType() == SbxLONG ; }
118 bool IsDouble() const { return GetType() == SbxDOUBLE ; }
119 bool IsString() const { return GetType() == SbxSTRING ; }
120 bool IsCurrency() const { return GetType() == SbxCURRENCY ; }
121 bool IsObject() const { return GetType() == SbxOBJECT ; }
122 bool IsBool() const { return GetType() == SbxBOOL ; }
123 bool IsErr() const { return GetType() == SbxERROR ; }
124 bool IsEmpty() const { return GetType() == SbxEMPTY ; }
125 bool IsNull() const { return GetType() == SbxNULL ; }
126 bool IsNumeric() const;
127 bool IsNumericRTL() const; // #41692 Interface for Basic
128 bool ImpIsNumeric( bool bOnlyIntntl ) const; // Implementation
129
130 virtual SbxDataType GetType() const override;
131 SbxDataType GetFullType() const { return aData.eType;}
132 bool SetType( SbxDataType );
133
134 bool Get( SbxValues& ) const;
135 const SbxValues& GetValues_Impl() const { return aData; }
136 bool Put( const SbxValues& );
137
138 SbxValues * data() { return &aData; }
139
140 sal_Unicode GetChar() const { return Get(SbxCHAR).nChar; }
141 sal_Int16 GetInteger() const { return Get(SbxINTEGER).nInteger; }
142 sal_Int32 GetLong() const { return Get(SbxLONG).nLong; }
143 sal_Int64 GetInt64() const { return Get(SbxSALINT64).nInt64; }
144 sal_uInt64 GetUInt64() const { return Get(SbxSALUINT64).uInt64; }
145
146 sal_Int64 GetCurrency() const { return Get(SbxCURRENCY).nInt64; }
147 SbxDecimal* GetDecimal() const { return Get(SbxDECIMAL).pDecimal; }
148
149 float GetSingle() const { return Get(SbxSINGLE).nSingle; }
150 double GetDouble() const { return Get(SbxDOUBLE).nDouble; }
151 double GetDate() const { return Get(SbxDATE).nDouble; }
152
153 bool GetBool() const { return Get(SbxBOOL).nUShort != 0; }
154 const OUString& GetCoreString() const;
155 OUString GetOUString() const;
156
157 SbxBase* GetObject() const { return Get(SbxOBJECT).pObj; }
158 sal_uInt8 GetByte() const { return Get(SbxBYTE).nByte; }
159 sal_uInt16 GetUShort() const { return Get(SbxUSHORT).nUShort; }
160 sal_uInt32 GetULong() const { return Get(SbxULONG).nULong; }
161
162 bool PutInteger( sal_Int16 );
163 bool PutLong( sal_Int32 );
164 bool PutSingle( float );
165 bool PutDouble( double );
166 void PutDate( double );
167 bool PutBool( bool );
168 void PutErr( sal_uInt16 );
169 void PutStringExt( const OUString& ); // with extended analysis (International, "sal_True"/"sal_False")
170 bool PutInt64( sal_Int64 );
171 bool PutUInt64( sal_uInt64 );
172 bool PutString( const OUString& );
175 bool PutUShort( sal_uInt16 );
176 bool PutULong( sal_uInt32 );
177 bool PutEmpty();
178 void PutNull();
179
180 // Special methods
181 void PutDecimal( css::bridge::oleautomation::Decimal const & rAutomationDec );
182 bool PutDecimal( SbxDecimal* pDecimal ); // This function is needed for Windows build, don't remove
183 void fillAutomationDecimal( css::bridge::oleautomation::Decimal& rAutomationDec ) const;
184 bool PutCurrency( sal_Int64 );
185 // Interface for CDbl in Basic
186 static ErrCode ScanNumIntnl( const OUString& rSrc, double& nVal, bool bSingle = false );
187
189
190 bool Convert( SbxDataType );
191 bool Compute( SbxOperator, const SbxValue& );
192 bool Compare( SbxOperator, const SbxValue& ) const;
193 bool Scan( const OUString&, sal_uInt16* );
194 void Format( OUString&, const OUString* = nullptr ) const;
195
196 // The following operators are defined for easier handling.
197 // TODO: Ensure error conditions (overflow, conversions)
198 // are taken into consideration in Compute and Compare
199
200 inline bool operator <=( const SbxValue& ) const;
201 inline bool operator >=( const SbxValue& ) const;
202
203 inline SbxValue& operator *=( const SbxValue& );
204 inline SbxValue& operator /=( const SbxValue& );
205 inline SbxValue& operator +=( const SbxValue& );
206 inline SbxValue& operator -=( const SbxValue& );
207
208private:
209 SbxValues Get(SbxDataType t) const;
210};
211
212inline bool SbxValue::operator<=( const SbxValue& r ) const
213{ return Compare( SbxLE, r ); }
214
215inline bool SbxValue::operator>=( const SbxValue& r ) const
216{ return Compare( SbxGE, r ); }
217
219{ Compute( SbxMUL, r ); return *this; }
220
222{ Compute( SbxDIV, r ); return *this; }
223
225{ Compute( SbxPLUS, r ); return *this; }
226
228{ Compute( SbxMINUS, r ); return *this; }
229
230class SbxArray;
231class SbxInfo;
232
234
236
237class SfxBroadcaster;
238
239class SbxVariableImpl;
240class StarBASIC;
241
243{
244 friend class SbMethod;
245
247 css::uno::Reference< css::uno::XInterface > m_xComListener;
249 std::unique_ptr<SfxBroadcaster> mpBroadcaster; // Broadcaster, if needed
250 OUString maName; // Name, if available
251 mutable OUString maNameCI; // Name, case insensitive - cached for fast comparison
252 SbxArrayRef mpPar; // Parameter-Array, if set
253 sal_uInt16 nHash = 0; // Hash-ID for search
254
255protected:
256 SbxInfoRef pInfo; // Probably called information
257 sal_uInt32 nUserData= 0; // User data for Call()
258 SbxObject* pParent = nullptr; // Currently attached object
259 virtual ~SbxVariable() override;
260 virtual bool LoadData( SvStream&, sal_uInt16 ) override;
261 virtual std::pair<bool, sal_uInt32> StoreData( SvStream& ) const override;
262public:
264 SbxVariable();
266 SbxVariable( const SbxVariable& );
268
269 void Dump( SvStream&, bool bDumpAll );
270
271 void SetName( const OUString& );
272 const OUString& GetName( SbxNameType = SbxNameType::NONE ) const;
273 sal_uInt16 GetHashCode() const { return nHash; }
274 static OUString NameToCaseInsensitiveName(const OUString& rName);
275
276 virtual void SetModified( bool ) override;
277
278 sal_uInt32 GetUserData() const { return nUserData; }
279 void SetUserData( sal_uInt32 n ) { nUserData = n; }
280
281 virtual SbxDataType GetType() const override;
282 virtual SbxClassType GetClass() const;
283
284 // Parameter-Interface
285 virtual SbxInfo* GetInfo();
286 void SetInfo( SbxInfo* p );
287 void SetParameters( SbxArray* p );
288 SbxArray* GetParameters() const;
289
290 // Sfx-Broadcasting-Support:
291 // Due to data reduction and better DLL-hierarchy currently via casting
293 bool IsBroadcaster() const { return mpBroadcaster != nullptr; }
294 virtual void Broadcast( SfxHintId nHintId ) override;
295
296 const SbxObject* GetParent() const { return pParent; }
298 virtual void SetParent( SbxObject* );
299
300 const OUString& GetDeclareClassName() const;
301 void SetDeclareClassName( const OUString& );
302 void SetComListener( const css::uno::Reference< css::uno::XInterface >& xComListener,
303 StarBASIC* pParentBasic );
304 void ClearComListener();
305
306 // Create a simple hashcode: the first six characters are evaluated.
307 static constexpr sal_uInt16 MakeHashCode(std::u16string_view aName)
308 {
309 sal_uInt16 n = 0;
310 const auto first6 = aName.substr(0, 6);
311 for (const auto& c : first6)
312 {
313 if (!rtl::isAscii(c))
314 continue; // Just skip it to let non-ASCII strings have some hash variance
315 n = static_cast<sal_uInt16>((n << 3) + rtl::toAsciiUpperCase(c));
316 }
317 return n;
318 }
319};
320
323
324//tdf#59222 SbxEnsureParentVariable is a SbxVariable which keeps a reference to
325//its parent, ensuring it always exists while this SbxVariable exists
327{
329public:
331 virtual void SetParent(SbxObject* p) override;
332};
333
334#endif // INCLUDED_BASIC_SBXVAR_HXX
335
336/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define BASIC_DLLPRIVATE
Definition: basicdllapi.h:19
#define BASIC_DLLPUBLIC
Definition: basicdllapi.h:17
virtual SbxInfo * GetInfo() override
Definition: sbxmod.cxx:2046
virtual void Broadcast(SfxHintId nHintId) override
Definition: sbxmod.cxx:2091
Definition: sbx.hxx:95
virtual bool IsFixed() const
Definition: sbxbase.cxx:81
SbxBase & operator=(const SbxBase &)
Definition: sbxbase.cxx:70
virtual void Clear()=0
virtual bool LoadData(SvStream &, sal_uInt16)=0
virtual void SetModified(bool)
Definition: sbxbase.cxx:86
virtual std::pair< bool, sal_uInt32 > StoreData(SvStream &) const =0
virtual SbxDataType GetType() const
Definition: sbxbase.cxx:76
virtual void SetParent(SbxObject *p) override
Definition: sbxvar.cxx:77
SbxEnsureParentVariable(const SbxVariable &r)
Definition: sbxvar.cxx:70
SbxObjectRef xParent
Definition: sbxvar.hxx:328
virtual SbxClassType GetClass() const override
Definition: sbxobj.cxx:854
bool IsNull() const
Definition: sbxvar.hxx:125
sal_Unicode GetChar() const
Definition: sbxvar.hxx:140
sal_uInt32 GetULong() const
Definition: sbxvar.hxx:160
bool Compute(SbxOperator, const SbxValue &)
Definition: sbxvalue.cxx:774
bool Compare(SbxOperator, const SbxValue &) const
Definition: sbxvalue.cxx:1145
bool PutInt64(sal_Int64)
bool PutDouble(double)
SbxValue & operator+=(const SbxValue &)
Definition: sbxvar.hxx:224
SbxBase * GetObject() const
Definition: sbxvar.hxx:157
bool IsString() const
Definition: sbxvar.hxx:119
SbxValues aData
Definition: sbxvar.hxx:99
OUString aToolString
Definition: sbxvar.hxx:101
SbxValues * data()
Definition: sbxvar.hxx:138
bool PutByte(sal_uInt8)
SbxDataType GetFullType() const
Definition: sbxvar.hxx:131
bool PutSingle(float)
SBX_DECL_PERSIST_NODATA(SBXID_VALUE, 1)
bool IsEmpty() const
Definition: sbxvar.hxx:124
bool IsBool() const
Definition: sbxvar.hxx:122
bool PutULong(sal_uInt32)
sal_uInt64 GetUInt64() const
Definition: sbxvar.hxx:144
bool IsInteger() const
Definition: sbxvar.hxx:116
bool PutCurrency(sal_Int64)
SbxValue & operator/=(const SbxValue &)
Definition: sbxvar.hxx:221
sal_uInt8 GetByte() const
Definition: sbxvar.hxx:158
bool GetBool() const
Definition: sbxvar.hxx:153
bool PutInteger(sal_Int16)
const SbxValues & GetValues_Impl() const
Definition: sbxvar.hxx:135
SbxValue & operator-=(const SbxValue &)
Definition: sbxvar.hxx:227
SbxDecimal * GetDecimal() const
Definition: sbxvar.hxx:147
bool PutObject(SbxBase *)
bool operator>=(const SbxValue &) const
Definition: sbxvar.hxx:215
bool IsLong() const
Definition: sbxvar.hxx:117
sal_Int64 GetInt64() const
Definition: sbxvar.hxx:143
sal_Int32 GetLong() const
Definition: sbxvar.hxx:142
sal_Int16 GetInteger() const
Definition: sbxvar.hxx:141
double GetDate() const
Definition: sbxvar.hxx:151
bool operator<=(const SbxValue &) const
Definition: sbxvar.hxx:212
bool IsObject() const
Definition: sbxvar.hxx:121
bool PutUShort(sal_uInt16)
bool IsCurrency() const
Definition: sbxvar.hxx:120
float GetSingle() const
Definition: sbxvar.hxx:149
bool IsErr() const
Definition: sbxvar.hxx:123
bool IsDouble() const
Definition: sbxvar.hxx:118
bool PutDecimal(SbxDecimal *pDecimal)
sal_uInt16 GetUShort() const
Definition: sbxvar.hxx:159
SbxValue & operator*=(const SbxValue &)
Definition: sbxvar.hxx:218
bool PutLong(sal_Int32)
bool PutChar(sal_Unicode)
bool PutUInt64(sal_uInt64)
double GetDouble() const
Definition: sbxvar.hxx:150
OUString aPic
Definition: sbxvar.hxx:100
sal_Int64 GetCurrency() const
Definition: sbxvar.hxx:146
const SbxObject * GetParent() const
Definition: sbxvar.hxx:296
void SetUserData(sal_uInt32 n)
Definition: sbxvar.hxx:279
StarBASIC * m_pComListenerParentBasic
Definition: sbxvar.hxx:248
const OUString & GetDeclareClassName() const
Definition: sbxvar.cxx:381
void ClearComListener()
Definition: sbxvar.cxx:401
SfxBroadcaster & GetBroadcaster()
Definition: sbxvar.cxx:102
void SetName(const OUString &)
Definition: sbxvar.cxx:192
OUString maName
Definition: sbxvar.hxx:250
virtual ~SbxVariable() override
Definition: sbxvar.cxx:89
void SetInfo(SbxInfo *p)
Definition: sbxvar.cxx:173
void SetParameters(SbxArray *p)
Definition: sbxvar.cxx:178
SbxArray * GetParameters() const
Definition: sbxvar.cxx:111
SbxArrayRef mpPar
Definition: sbxvar.hxx:252
SbxObject * GetParent()
Definition: sbxvar.hxx:297
sal_uInt32 nUserData
Definition: sbxvar.hxx:257
void Dump(SvStream &, bool bDumpAll)
Definition: sbxvar.cxx:572
const OUString & GetName(SbxNameType=SbxNameType::NONE) const
Definition: sbxvar.cxx:199
css::uno::Reference< css::uno::XInterface > m_xComListener
Definition: sbxvar.hxx:247
sal_uInt32 GetUserData() const
Definition: sbxvar.hxx:278
OUString maNameCI
Definition: sbxvar.hxx:251
SbxVariable()
Definition: sbxvar.cxx:43
std::unique_ptr< SfxBroadcaster > mpBroadcaster
Definition: sbxvar.hxx:249
void SetDeclareClassName(const OUString &)
Definition: sbxvar.cxx:386
static constexpr sal_uInt16 MakeHashCode(std::u16string_view aName)
Definition: sbxvar.hxx:307
void SetComListener(const css::uno::Reference< css::uno::XInterface > &xComListener, StarBASIC *pParentBasic)
Definition: sbxvar.cxx:391
SbxInfoRef pInfo
Definition: sbxvar.hxx:256
bool IsBroadcaster() const
Definition: sbxvar.hxx:293
static OUString NameToCaseInsensitiveName(const OUString &rName)
Definition: sbxvar.cxx:187
virtual void SetParent(SbxObject *)
Definition: sbxvar.cxx:355
sal_uInt16 GetHashCode() const
Definition: sbxvar.hxx:273
OUString m_aDeclareClassName
Definition: sbxvar.hxx:246
SBX_DECL_PERSIST_NODATA(SBXID_VARIABLE, 2)
SbxObject * pParent
Definition: sbxvar.hxx:258
sal_uInt16 nHash
Definition: sbxvar.hxx:253
SfxHintId
OUString aName
void * p
sal_Int64 n
SVXCORE_DLLPUBLIC MSO_SPT Get(const OUString &)
constexpr OUStringLiteral aData
bool IsNumeric(std::u16string_view rText)
Put
SbxNameType
Definition: sbxdef.hxx:123
constexpr auto SBXID_VARIABLE
Definition: sbxdef.hxx:170
SbxBOOL
Definition: sbxdef.hxx:215
constexpr auto SBXID_VALUE
Definition: sbxdef.hxx:169
SbxOperator
Definition: sbxdef.hxx:92
@ SbxGE
Definition: sbxdef.hxx:120
@ SbxMUL
Definition: sbxdef.hxx:95
@ SbxPLUS
Definition: sbxdef.hxx:98
@ SbxMINUS
Definition: sbxdef.hxx:99
@ SbxDIV
Definition: sbxdef.hxx:96
@ SbxLE
Definition: sbxdef.hxx:119
SbxDataType
Definition: sbxdef.hxx:37
@ SbxOBJECT
Definition: sbxdef.hxx:47
@ SbxSALINT64
Definition: sbxdef.hxx:75
@ SbxLONG
Definition: sbxdef.hxx:41
@ SbxSALUINT64
Definition: sbxdef.hxx:76
@ SbxNULL
Definition: sbxdef.hxx:39
@ SbxBYTE
Definition: sbxdef.hxx:55
@ SbxEMPTY
Definition: sbxdef.hxx:38
@ SbxDECIMAL
Definition: sbxdef.hxx:77
@ SbxULONG
Definition: sbxdef.hxx:57
@ SbxUSHORT
Definition: sbxdef.hxx:56
@ SbxERROR
Definition: sbxdef.hxx:48
@ SbxDATE
Definition: sbxdef.hxx:45
@ SbxCURRENCY
Definition: sbxdef.hxx:44
@ SbxSINGLE
Definition: sbxdef.hxx:42
@ SbxCHAR
Definition: sbxdef.hxx:54
@ SbxSTRING
Definition: sbxdef.hxx:46
@ SbxINTEGER
Definition: sbxdef.hxx:40
@ SbxDOUBLE
Definition: sbxdef.hxx:43
SbxClassType
Definition: sbxdef.hxx:27
tools::SvRef< SbxArray > SbxArrayRef
Definition: sbxvar.hxx:231
tools::SvRef< SbxVariable > SbxVariableRef
Definition: sbxvar.hxx:322
tools::SvRef< SbxInfo > SbxInfoRef
Definition: sbxvar.hxx:235
tools::SvRef< SbxObject > SbxObjectRef
Definition: sbxvar.hxx:321
float nSingle
Definition: sbxvar.hxx:55
sal_uInt64 * puInt64
Definition: sbxvar.hxx:69
sal_Int32 nLong
Definition: sbxvar.hxx:49
float * pSingle
Definition: sbxvar.hxx:72
OUString * pOUString
Definition: sbxvar.hxx:58
SbxBase * pObj
Definition: sbxvar.hxx:61
SbxValues()
Definition: sbxvar.hxx:79
sal_uInt8 * pByte
Definition: sbxvar.hxx:63
sal_uInt32 * pULong
Definition: sbxvar.hxx:67
sal_uInt16 * pUShort
Definition: sbxvar.hxx:64
double * pDouble
Definition: sbxvar.hxx:73
SbxDecimal * pDecimal
Definition: sbxvar.hxx:59
sal_Int16 nInteger
Definition: sbxvar.hxx:47
SbxValues(SbxDataType e)
Definition: sbxvar.hxx:80
void * pData
Definition: sbxvar.hxx:75
sal_Unicode nChar
Definition: sbxvar.hxx:46
sal_Int64 nInt64
Definition: sbxvar.hxx:53
double nDouble
Definition: sbxvar.hxx:56
sal_uInt16 nUShort
Definition: sbxvar.hxx:45
int nInt
Definition: sbxvar.hxx:51
void clear(SbxDataType type)
Definition: sbxvar.hxx:83
sal_uInt64 uInt64
Definition: sbxvar.hxx:52
sal_Int16 * pInteger
Definition: sbxvar.hxx:66
sal_Int32 * pLong
Definition: sbxvar.hxx:68
SbxDataType eType
Definition: sbxvar.hxx:77
sal_uInt8 nByte
Definition: sbxvar.hxx:44
SbxValues(double _nDouble)
Definition: sbxvar.hxx:81
unsigned int nUInt
Definition: sbxvar.hxx:50
sal_uInt32 nULong
Definition: sbxvar.hxx:48
sal_Int64 * pnInt64
Definition: sbxvar.hxx:70
sal_Unicode * pChar
Definition: sbxvar.hxx:65
unsigned char sal_uInt8
sal_uInt16 sal_Unicode
ResultType type
OUString GetOUString(CFStringRef)