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 .
20 #ifndef INCLUDED_BASIC_SBXVAR_HXX
21 #define INCLUDED_BASIC_SBXVAR_HXX
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>
36 namespace com::sun::star::bridge::oleautomation
{ struct Decimal
; }
79 SbxValues(): pData( nullptr ), eType(SbxEMPTY
) {}
80 SbxValues( SbxDataType e
): pData( nullptr ), eType(e
) {}
81 SbxValues( double _nDouble
): nDouble( _nDouble
), eType(SbxDOUBLE
) {}
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
));
94 class BASIC_DLLPUBLIC SbxValue
: public SbxBase
96 // #55226 Transport additional infos
97 BASIC_DLLPRIVATE SbxValue
* TheRealValue( bool bObjInObjError
) const;
99 SbxValues aData
; // Data
100 OUString aPic
; // Picture-String
101 OUString aToolString
; // tool string copy
103 virtual void Broadcast( SfxHintId
); // Broadcast-Call
104 virtual ~SbxValue() override
;
105 virtual bool LoadData( SvStream
&, sal_uInt16
) override
;
106 virtual bool StoreData( SvStream
& ) const override
;
108 SBX_DECL_PERSIST_NODATA(SBXID_VALUE
,1);
110 SbxValue( SbxDataType
);
111 SbxValue( const SbxValue
& );
112 SbxValue
& operator=( const SbxValue
& );
113 virtual void Clear() override
;
114 virtual bool IsFixed() const override
;
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
130 virtual SbxDataType
GetType() const override
;
131 SbxDataType
GetFullType() const { return aData
.eType
;}
132 bool SetType( SbxDataType
);
134 bool Get( SbxValues
& ) const;
135 const SbxValues
& GetValues_Impl() const { return aData
; }
136 bool Put( const SbxValues
& );
138 SbxValues
* data() { return &aData
; }
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
; }
146 sal_Int64
GetCurrency() const { return Get(SbxCURRENCY
).nInt64
; }
147 SbxDecimal
* GetDecimal() const { return Get(SbxDECIMAL
).pDecimal
; }
149 float GetSingle() const { return Get(SbxSINGLE
).nSingle
; }
150 double GetDouble() const { return Get(SbxDOUBLE
).nDouble
; }
151 double GetDate() const { return Get(SbxDATE
).nDouble
; }
153 bool GetBool() const { return Get(SbxBOOL
).nUShort
!= 0; }
154 const OUString
& GetCoreString() const;
155 OUString
GetOUString() const;
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
; }
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
& );
173 bool PutChar( sal_Unicode
);
174 bool PutByte( sal_uInt8
);
175 bool PutUShort( sal_uInt16
);
176 bool PutULong( sal_uInt32
);
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 );
188 bool PutObject( SbxBase
* );
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;
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
200 inline bool operator <=( const SbxValue
& ) const;
201 inline bool operator >=( const SbxValue
& ) const;
203 inline SbxValue
& operator *=( const SbxValue
& );
204 inline SbxValue
& operator /=( const SbxValue
& );
205 inline SbxValue
& operator +=( const SbxValue
& );
206 inline SbxValue
& operator -=( const SbxValue
& );
209 SbxValues
Get(SbxDataType t
) const;
212 inline bool SbxValue::operator<=( const SbxValue
& r
) const
213 { return Compare( SbxLE
, r
); }
215 inline bool SbxValue::operator>=( const SbxValue
& r
) const
216 { return Compare( SbxGE
, r
); }
218 inline SbxValue
& SbxValue::operator*=( const SbxValue
& r
)
219 { Compute( SbxMUL
, r
); return *this; }
221 inline SbxValue
& SbxValue::operator/=( const SbxValue
& r
)
222 { Compute( SbxDIV
, r
); return *this; }
224 inline SbxValue
& SbxValue::operator+=( const SbxValue
& r
)
225 { Compute( SbxPLUS
, r
); return *this; }
227 inline SbxValue
& SbxValue::operator-=( const SbxValue
& r
)
228 { Compute( SbxMINUS
, r
); return *this; }
233 typedef tools::SvRef
<SbxArray
> SbxArrayRef
;
235 typedef tools::SvRef
<SbxInfo
> SbxInfoRef
;
237 class SfxBroadcaster
;
239 class SbxVariableImpl
;
242 class BASIC_DLLPUBLIC SbxVariable
: public SbxValue
244 friend class SbMethod
;
246 OUString m_aDeclareClassName
;
247 css::uno::Reference
< css::uno::XInterface
> m_xComListener
;
248 StarBASIC
* m_pComListenerParentBasic
= nullptr;
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
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 bool StoreData( SvStream
& ) const override
;
263 SBX_DECL_PERSIST_NODATA(SBXID_VARIABLE
,2);
265 SbxVariable( SbxDataType
);
266 SbxVariable( const SbxVariable
& );
267 SbxVariable
& operator=( const SbxVariable
& );
269 void Dump( SvStream
&, bool bDumpAll
);
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
);
276 virtual void SetModified( bool ) override
;
278 sal_uInt32
GetUserData() const { return nUserData
; }
279 void SetUserData( sal_uInt32 n
) { nUserData
= n
; }
281 virtual SbxDataType
GetType() const override
;
282 virtual SbxClassType
GetClass() const;
284 // Parameter-Interface
285 virtual SbxInfo
* GetInfo();
286 void SetInfo( SbxInfo
* p
);
287 void SetParameters( SbxArray
* p
);
288 SbxArray
* GetParameters() const;
290 // Sfx-Broadcasting-Support:
291 // Due to data reduction and better DLL-hierarchy currently via casting
292 SfxBroadcaster
& GetBroadcaster();
293 bool IsBroadcaster() const { return mpBroadcaster
!= nullptr; }
294 virtual void Broadcast( SfxHintId nHintId
) override
;
296 const SbxObject
* GetParent() const { return pParent
; }
297 SbxObject
* GetParent() { return pParent
;}
298 virtual void SetParent( SbxObject
* );
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();
306 // Create a simple hashcode: the first six characters are evaluated.
307 static constexpr sal_uInt16
MakeHashCode(std::u16string_view aName
)
310 const auto first6
= aName
.substr(0, 6);
311 for (const auto& c
: first6
)
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
));
321 typedef tools::SvRef
<SbxObject
> SbxObjectRef
;
322 typedef tools::SvRef
<SbxVariable
> SbxVariableRef
;
324 //tdf#59222 SbxEnsureParentVariable is a SbxVariable which keeps a reference to
325 //its parent, ensuring it always exists while this SbxVariable exists
326 class SbxEnsureParentVariable final
: public SbxVariable
328 SbxObjectRef xParent
;
330 SbxEnsureParentVariable(const SbxVariable
& r
);
331 virtual void SetParent(SbxObject
* p
) override
;
334 #endif // INCLUDED_BASIC_SBXVAR_HXX
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */