Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / basic / source / sbx / sbxvar.cxx
blobd18ada85791dea7d359ff4b47a9551954bcbd960
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 #include <config_features.h>
22 #include <tools/stream.hxx>
23 #include <svl/SfxBroadcaster.hxx>
25 #include <basic/sbx.hxx>
26 #include <runtime.hxx>
27 #include "sbxres.hxx"
28 #include "sbxconv.hxx"
29 #include <sbunoobj.hxx>
30 #include <rtl/character.hxx>
31 #include <sal/log.hxx>
33 #include <com/sun/star/uno/XInterface.hpp>
34 using namespace com::sun::star::uno;
36 // SbxVariable
39 // SbxVariableImpl
41 class SbxVariableImpl
43 friend class SbxVariable;
44 OUString m_aDeclareClassName;
45 Reference< XInterface > m_xComListener;
46 StarBASIC* m_pComListenerParentBasic;
48 SbxVariableImpl()
49 : m_pComListenerParentBasic( nullptr )
54 // Constructors
56 SbxVariable::SbxVariable() : SbxValue()
58 pParent = nullptr;
59 nUserData = 0;
60 nHash = 0;
63 SbxVariable::SbxVariable( const SbxVariable& r )
64 : SvRefBase( r ),
65 SbxValue( r ),
66 mpPar( r.mpPar ),
67 pInfo( r.pInfo )
69 if( r.mpImpl != nullptr )
71 mpImpl.reset( new SbxVariableImpl( *r.mpImpl ) );
72 #if HAVE_FEATURE_SCRIPTING
73 if( mpImpl->m_xComListener.is() )
75 registerComListenerVariableForBasic( this, mpImpl->m_pComListenerParentBasic );
77 #endif
79 if( r.CanRead() )
81 pParent = r.pParent;
82 nUserData = r.nUserData;
83 maName = r.maName;
84 nHash = r.nHash;
86 else
88 pParent = nullptr;
89 nUserData = 0;
90 nHash = 0;
94 SbxEnsureParentVariable::SbxEnsureParentVariable(const SbxVariable& r)
95 : SbxVariable(r)
96 , xParent(const_cast<SbxVariable&>(r).GetParent())
98 assert(GetParent() == xParent.get());
101 void SbxEnsureParentVariable::SetParent(SbxObject* p)
103 assert(GetParent() == xParent.get());
104 SbxVariable::SetParent(p);
105 xParent = SbxObjectRef(p);
106 assert(GetParent() == xParent.get());
109 SbxVariable::SbxVariable( SbxDataType t ) : SbxValue( t )
111 pParent = nullptr;
112 nUserData = 0;
113 nHash = 0;
116 SbxVariable::~SbxVariable()
118 #if HAVE_FEATURE_SCRIPTING
119 if( IsSet( SbxFlagBits::DimAsNew ))
121 removeDimAsNewRecoverItem( this );
123 #endif
124 mpBroadcaster.reset();
127 // Broadcasting
129 SfxBroadcaster& SbxVariable::GetBroadcaster()
131 if( !mpBroadcaster )
133 mpBroadcaster.reset( new SfxBroadcaster );
135 return *mpBroadcaster;
138 SbxArray* SbxVariable::GetParameters() const
140 return mpPar.get();
144 // Perhaps some day one could cut the parameter 0.
145 // Then the copying will be dropped...
147 void SbxVariable::Broadcast( SfxHintId nHintId )
149 if( mpBroadcaster && !IsSet( SbxFlagBits::NoBroadcast ) )
151 // Because the method could be called from outside, check the
152 // rights here again
153 if( nHintId == SfxHintId::BasicDataWanted )
155 if( !CanRead() )
157 return;
160 if( nHintId == SfxHintId::BasicDataChanged )
162 if( !CanWrite() )
164 return;
168 //fdo#86843 Add a ref during the following block to guard against
169 //getting deleted before completing this method
170 SbxVariableRef aBroadcastGuard(this);
172 // Avoid further broadcasting
173 std::unique_ptr<SfxBroadcaster> pSave = std::move(mpBroadcaster);
174 SbxFlagBits nSaveFlags = GetFlags();
175 SetFlag( SbxFlagBits::ReadWrite );
176 if( mpPar.is() )
178 // Register this as element 0, but don't change over the parent!
179 mpPar->GetRef( 0 ) = this;
181 pSave->Broadcast( SbxHint( nHintId, this ) );
182 mpBroadcaster = std::move(pSave);
183 SetFlags( nSaveFlags );
187 SbxInfo* SbxVariable::GetInfo()
189 if( !pInfo.is() )
191 Broadcast( SfxHintId::BasicInfoWanted );
192 if( pInfo.is() )
194 SetModified( true );
197 return pInfo.get();
200 void SbxVariable::SetInfo( SbxInfo* p )
202 pInfo = p;
205 void SbxVariable::SetParameters( SbxArray* p )
207 mpPar = p;
211 // Name of the variables
213 void SbxVariable::SetName( const OUString& rName )
215 maName = rName;
216 nHash = MakeHashCode( rName );
219 const OUString& SbxVariable::GetName( SbxNameType t ) const
221 static const OUString cSuffixes { " %&!#@ $" };
222 if( t == SbxNameType::NONE )
224 return maName;
226 // Request parameter-information (not for objects)
227 const_cast<SbxVariable*>(this)->GetInfo();
228 // Append nothing, if it is a simple property (no empty brackets)
229 if (!pInfo.is() || (pInfo->m_Params.empty() && GetClass() == SbxClassType::Property))
231 return maName;
233 sal_Unicode cType = ' ';
234 OUStringBuffer aTmp( maName );
235 // short type? Then fetch it, possible this is 0.
236 SbxDataType et = GetType();
237 if( t == SbxNameType::ShortTypes )
239 if( et <= SbxSTRING )
241 cType = cSuffixes[ et ];
243 if( cType != ' ' )
245 aTmp.append(cType);
248 aTmp.append("(");
250 for (SbxParams::const_iterator iter = pInfo->m_Params.begin(); iter != pInfo->m_Params.end(); ++iter)
252 auto const& i = *iter;
253 int nt = i->eType & 0x0FFF;
254 if (iter != pInfo->m_Params.begin())
256 aTmp.append(",");
258 if( i->nFlags & SbxFlagBits::Optional )
260 aTmp.append( GetSbxRes( StringId::Optional ) );
262 if( i->eType & SbxBYREF )
264 aTmp.append( GetSbxRes( StringId::ByRef ) );
266 aTmp.append( i->aName );
267 cType = ' ';
268 // short type? Then fetch it, possible this is 0.
269 if( t == SbxNameType::ShortTypes )
271 if( nt <= SbxSTRING )
273 cType = cSuffixes[ nt ];
276 if( cType != ' ' )
278 aTmp.append(cType);
279 if( i->eType & SbxARRAY )
281 aTmp.append("()");
284 else
286 if( i->eType & SbxARRAY )
288 aTmp.append("()");
290 // long type?
291 aTmp.append(GetSbxRes( StringId::As ));
292 if( nt < 32 )
294 aTmp.append(GetSbxRes( static_cast<StringId>( static_cast<int>( StringId::Types ) + nt ) ));
296 else
298 aTmp.append(GetSbxRes( StringId::Any ));
302 aTmp.append(")");
303 const_cast<SbxVariable*>(this)->aToolString = aTmp.makeStringAndClear();
304 return aToolString;
307 // Create a simple hashcode: the first six characters are evaluated.
309 sal_uInt16 SbxVariable::MakeHashCode( const OUString& rName )
311 sal_uInt16 n = 0;
312 sal_Int32 nLen = rName.getLength();
313 if( nLen > 6 )
315 nLen = 6;
317 for( sal_Int32 i=0; i<nLen; ++i )
319 sal_uInt8 c = static_cast<sal_uInt8>(rName[i]);
320 // If we have a comment sign break!!
321 if( c >= 0x80 )
323 return 0;
325 n = sal::static_int_cast< sal_uInt16 >( ( n << 3 ) + rtl::toAsciiUpperCase( c ) );
327 return n;
330 // Operators
332 SbxVariable& SbxVariable::operator=( const SbxVariable& r )
334 if (this != &r)
336 SbxValue::operator=( r );
337 mpImpl.reset();
338 if( r.mpImpl != nullptr )
340 mpImpl.reset( new SbxVariableImpl( *r.mpImpl ) );
341 #if HAVE_FEATURE_SCRIPTING
342 if( mpImpl->m_xComListener.is() )
344 registerComListenerVariableForBasic( this, mpImpl->m_pComListenerParentBasic );
346 #endif
349 return *this;
352 // Conversion
354 SbxDataType SbxVariable::GetType() const
356 if( aData.eType == SbxOBJECT )
358 return aData.pObj ? aData.pObj->GetType() : SbxOBJECT;
360 else if( aData.eType == SbxVARIANT )
362 return aData.pObj ? aData.pObj->GetType() : SbxVARIANT;
364 else
366 return aData.eType;
370 SbxClassType SbxVariable::GetClass() const
372 return SbxClassType::Variable;
375 void SbxVariable::SetModified( bool b )
377 if( IsSet( SbxFlagBits::NoModify ) )
379 return;
381 SbxBase::SetModified( b );
382 if( pParent && pParent != this ) //??? HotFix: Recursion out here MM
384 pParent->SetModified( b );
388 void SbxVariable::SetParent( SbxObject* p )
390 #ifdef DBG_UTIL
391 // Will the parent of a SbxObject be set?
392 if (p && dynamic_cast<SbxObject*>(this))
394 // then this had to be a child of the new parent
395 bool bFound = false;
396 SbxArray *pChildren = p->GetObjects();
397 if ( pChildren )
399 for ( sal_uInt16 nIdx = 0; !bFound && nIdx < pChildren->Count(); ++nIdx )
401 bFound = ( this == pChildren->Get(nIdx) );
404 SAL_INFO_IF(
405 !bFound, "basic.sbx",
406 "dangling: [" << GetName() << "].SetParent([" << p->GetName()
407 << "])");
409 #endif
411 pParent = p;
414 SbxVariableImpl* SbxVariable::getImpl()
416 if(!mpImpl)
418 mpImpl.reset(new SbxVariableImpl);
420 return mpImpl.get();
423 const OUString& SbxVariable::GetDeclareClassName()
425 SbxVariableImpl* pImpl = getImpl();
426 return pImpl->m_aDeclareClassName;
429 void SbxVariable::SetDeclareClassName( const OUString& rDeclareClassName )
431 SbxVariableImpl* pImpl = getImpl();
432 pImpl->m_aDeclareClassName = rDeclareClassName;
435 void SbxVariable::SetComListener( const css::uno::Reference< css::uno::XInterface >& xComListener,
436 StarBASIC* pParentBasic )
438 SbxVariableImpl* pImpl = getImpl();
439 pImpl->m_xComListener = xComListener;
440 pImpl->m_pComListenerParentBasic = pParentBasic;
441 #if HAVE_FEATURE_SCRIPTING
442 registerComListenerVariableForBasic( this, pParentBasic );
443 #endif
446 void SbxVariable::ClearComListener()
448 SbxVariableImpl* pImpl = getImpl();
449 pImpl->m_xComListener.clear();
453 // Loading/Saving
455 bool SbxVariable::LoadData( SvStream& rStrm, sal_uInt16 nVer )
457 sal_uInt8 cMark;
458 rStrm.ReadUChar( cMark );
459 if( cMark == 0xFF )
461 if( !SbxValue::LoadData( rStrm, nVer ) )
463 return false;
465 maName = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
466 RTL_TEXTENCODING_ASCII_US);
467 sal_uInt32 nTemp;
468 rStrm.ReadUInt32( nTemp );
469 nUserData = nTemp;
471 else
473 sal_uInt16 nType;
474 rStrm.SeekRel( -1 );
475 rStrm.ReadUInt16( nType );
476 maName = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
477 RTL_TEXTENCODING_ASCII_US);
478 sal_uInt32 nTemp;
479 rStrm.ReadUInt32( nTemp );
480 nUserData = nTemp;
481 // correction: old methods have instead of SbxNULL now SbxEMPTY
482 if( nType == SbxNULL && GetClass() == SbxClassType::Method )
484 nType = SbxEMPTY;
486 SbxValues aTmp;
487 OUString aTmpString;
488 OUString aVal;
489 aTmp.eType = aData.eType = static_cast<SbxDataType>(nType);
490 aTmp.pOUString = &aVal;
491 switch( nType )
493 case SbxBOOL:
494 case SbxERROR:
495 case SbxINTEGER:
496 rStrm.ReadInt16( aTmp.nInteger ); break;
497 case SbxLONG:
498 rStrm.ReadInt32( aTmp.nLong ); break;
499 case SbxSINGLE:
501 // Floats as ASCII
502 aTmpString = read_uInt16_lenPrefixed_uInt8s_ToOUString(
503 rStrm, RTL_TEXTENCODING_ASCII_US);
504 double d;
505 SbxDataType t;
506 if( ImpScan( aTmpString, d, t, nullptr, false ) != ERRCODE_NONE || t == SbxDOUBLE )
508 aTmp.nSingle = 0;
509 return false;
511 aTmp.nSingle = static_cast<float>(d);
512 break;
514 case SbxDATE:
515 case SbxDOUBLE:
517 // Floats as ASCII
518 aTmpString = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
519 RTL_TEXTENCODING_ASCII_US);
520 SbxDataType t;
521 if( ImpScan( aTmpString, aTmp.nDouble, t, nullptr, false ) != ERRCODE_NONE )
523 aTmp.nDouble = 0;
524 return false;
526 break;
528 case SbxSTRING:
529 aVal = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
530 RTL_TEXTENCODING_ASCII_US);
531 break;
532 case SbxEMPTY:
533 case SbxNULL:
534 break;
535 default:
536 aData.eType = SbxNULL;
537 SAL_WARN( "basic.sbx", "Loaded a non-supported data type" );
538 return false;
540 // putt value
541 if( nType != SbxNULL && nType != SbxEMPTY && !Put( aTmp ) )
543 return false;
546 rStrm.ReadUChar( cMark );
547 // cMark is also a version number!
548 // 1: initial version
549 // 2: with nUserData
550 if( cMark )
552 if( cMark > 2 )
554 return false;
556 pInfo = new SbxInfo;
557 pInfo->LoadData( rStrm, static_cast<sal_uInt16>(cMark) );
559 Broadcast( SfxHintId::BasicDataChanged );
560 nHash = MakeHashCode( maName );
561 SetModified( true );
562 return true;
565 bool SbxVariable::StoreData( SvStream& rStrm ) const
567 rStrm.WriteUChar( 0xFF ); // Marker
568 bool bValStore;
569 if( dynamic_cast<const SbxMethod *>(this) != nullptr )
571 // #50200 Avoid that objects , which during the runtime
572 // as return-value are saved in the method as a value were saved
573 SbxVariable* pThis = const_cast<SbxVariable*>(this);
574 SbxFlagBits nSaveFlags = GetFlags();
575 pThis->SetFlag( SbxFlagBits::Write );
576 pThis->SbxValue::Clear();
577 pThis->SetFlags( nSaveFlags );
579 // So that the method will not be executed in any case!
580 // CAST, to avoid const!
581 pThis->SetFlag( SbxFlagBits::NoBroadcast );
582 bValStore = SbxValue::StoreData( rStrm );
583 pThis->ResetFlag( SbxFlagBits::NoBroadcast );
585 else
587 bValStore = SbxValue::StoreData( rStrm );
589 if( !bValStore )
591 return false;
593 write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, maName,
594 RTL_TEXTENCODING_ASCII_US);
595 rStrm.WriteUInt32( nUserData );
596 if( pInfo.is() )
598 rStrm.WriteUChar( 2 ); // Version 2: with UserData!
599 pInfo->StoreData( rStrm );
601 else
603 rStrm.WriteUChar( 0 );
605 return true;
608 // SbxInfo
610 SbxInfo::SbxInfo()
611 : aHelpFile(), nHelpId(0)
614 SbxInfo::SbxInfo( const OUString& r, sal_uInt32 n )
615 : aHelpFile( r ), nHelpId( n )
618 void SbxVariable::Dump( SvStream& rStrm, bool bFill )
620 OString aBNameStr(OUStringToOString(GetName( SbxNameType::ShortTypes ), RTL_TEXTENCODING_ASCII_US));
621 rStrm.WriteCharPtr( "Variable( " )
622 .WriteOString( OString::number(reinterpret_cast<sal_Int64>(this)) ).WriteCharPtr( "==" )
623 .WriteOString( aBNameStr );
624 OString aBParentNameStr(OUStringToOString(GetParent()->GetName(), RTL_TEXTENCODING_ASCII_US));
625 if ( GetParent() )
627 rStrm.WriteCharPtr( " in parent '" ).WriteOString( aBParentNameStr ).WriteCharPtr( "'" );
629 else
631 rStrm.WriteCharPtr( " no parent" );
633 rStrm.WriteCharPtr( " ) " );
635 // output also the object at object-vars
636 if ( GetValues_Impl().eType == SbxOBJECT &&
637 GetValues_Impl().pObj &&
638 GetValues_Impl().pObj != this &&
639 GetValues_Impl().pObj != GetParent() )
641 rStrm.WriteCharPtr( " contains " );
642 static_cast<SbxObject*>(GetValues_Impl().pObj)->Dump( rStrm, bFill );
644 else
646 rStrm << endl;
650 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */