tdf#132752: docx import: improvements for first line indent in lists
[LibreOffice.git] / include / basic / sbxvar.hxx
blob6454e445629f9b3e87d960b99a5a8c8e25ecc795
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 .
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>
30 #include <algorithm>
31 #include <cstddef>
32 #include <cstring>
33 #include <memory>
36 namespace com::sun::star::bridge::oleautomation { struct Decimal; }
38 class SbxDecimal;
39 enum class SfxHintId;
41 struct SbxValues
43 union {
44 sal_uInt8 nByte;
45 sal_uInt16 nUShort;
46 sal_Unicode nChar;
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;
55 float nSingle;
56 double nDouble;
58 OUString* pOUString;
59 SbxDecimal* pDecimal;
61 SbxBase* pObj;
63 sal_uInt8* pByte;
64 sal_uInt16* pUShort;
65 sal_Unicode* pChar;
66 sal_Int16* pInteger;
67 sal_uInt32* pULong;
68 sal_Int32* pLong;
69 sal_uInt64* puInt64;
70 sal_Int64* pnInt64;
72 float* pSingle;
73 double* pDouble;
75 void* pData;
77 SbxDataType eType;
79 SbxValues(): pData( nullptr ), eType(SbxEMPTY) {}
80 SbxValues( SbxDataType e ): 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));
90 eType = type;
94 class BASIC_DLLPUBLIC SbxValue : public SbxBase
96 // #55226 Transport additional infos
97 BASIC_DLLPRIVATE SbxValue* TheRealValue( bool bObjInObjError ) const;
98 protected:
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;
107 public:
108 SBX_DECL_PERSIST_NODATA(SBXID_VALUE,1);
109 SbxValue();
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 );
177 bool PutEmpty();
178 void PutNull();
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 );
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& );
208 private:
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; }
230 class SbxArray;
231 class SbxInfo;
233 typedef tools::SvRef<SbxArray> SbxArrayRef;
235 typedef tools::SvRef<SbxInfo> SbxInfoRef;
237 class SfxBroadcaster;
239 class SbxVariableImpl;
240 class StarBASIC;
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 SbxArrayRef mpPar; // Parameter-Array, if set
252 sal_uInt16 nHash = 0; // Hash-ID for search
254 protected:
255 SbxInfoRef pInfo; // Probably called information
256 sal_uInt32 nUserData= 0; // User data for Call()
257 SbxObject* pParent = nullptr; // Currently attached object
258 virtual ~SbxVariable() override;
259 virtual bool LoadData( SvStream&, sal_uInt16 ) override;
260 virtual bool StoreData( SvStream& ) const override;
261 public:
262 SBX_DECL_PERSIST_NODATA(SBXID_VARIABLE,2);
263 SbxVariable();
264 SbxVariable( SbxDataType );
265 SbxVariable( const SbxVariable& );
266 SbxVariable& operator=( const SbxVariable& );
268 void Dump( SvStream&, bool bDumpAll );
270 void SetName( const OUString& );
271 const OUString& GetName( SbxNameType = SbxNameType::NONE ) const;
272 sal_uInt16 GetHashCode() const { return nHash; }
274 virtual void SetModified( bool ) override;
276 sal_uInt32 GetUserData() const { return nUserData; }
277 void SetUserData( sal_uInt32 n ) { nUserData = n; }
279 virtual SbxDataType GetType() const override;
280 virtual SbxClassType GetClass() const;
282 // Parameter-Interface
283 virtual SbxInfo* GetInfo();
284 void SetInfo( SbxInfo* p );
285 void SetParameters( SbxArray* p );
286 SbxArray* GetParameters() const;
288 // Sfx-Broadcasting-Support:
289 // Due to data reduction and better DLL-hierarchy currently via casting
290 SfxBroadcaster& GetBroadcaster();
291 bool IsBroadcaster() const { return mpBroadcaster != nullptr; }
292 virtual void Broadcast( SfxHintId nHintId ) override;
294 const SbxObject* GetParent() const { return pParent; }
295 SbxObject* GetParent() { return pParent;}
296 virtual void SetParent( SbxObject* );
298 const OUString& GetDeclareClassName() const;
299 void SetDeclareClassName( const OUString& );
300 void SetComListener( const css::uno::Reference< css::uno::XInterface >& xComListener,
301 StarBASIC* pParentBasic );
302 void ClearComListener();
304 // Create a simple hashcode: the first six characters are evaluated.
305 static constexpr sal_uInt16 MakeHashCode(std::u16string_view aName)
307 sal_uInt16 n = 0;
308 const auto first6 = aName.substr(0, 6);
309 for (const auto& c : first6)
311 // If we have a comment sign break!!
312 if (c >= 0x80)
313 return 0;
314 n = static_cast<sal_uInt16>((n << 3) + rtl::toAsciiUpperCase(c));
316 return n;
320 typedef tools::SvRef<SbxObject> SbxObjectRef;
321 typedef tools::SvRef<SbxVariable> SbxVariableRef;
323 //tdf#59222 SbxEnsureParentVariable is a SbxVariable which keeps a reference to
324 //its parent, ensuring it always exists while this SbxVariable exists
325 class SbxEnsureParentVariable final : public SbxVariable
327 SbxObjectRef xParent;
328 public:
329 SbxEnsureParentVariable(const SbxVariable& r);
330 virtual void SetParent(SbxObject* p) override;
333 #endif // INCLUDED_BASIC_SBXVAR_HXX
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */