cid#1509221 silence Dereference null return value
[LibreOffice.git] / basic / source / sbx / sbxcoll.cxx
blob328115d4f6fa01c5ae9fa19c167cb1099c34d21e
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 <o3tl/safeint.hxx>
21 #include <tools/stream.hxx>
23 #include <basic/sbx.hxx>
24 #include <basic/sberrors.hxx>
25 #include "sbxres.hxx"
28 static OUString pCount;
29 static OUString pAdd;
30 static OUString pItem;
31 static OUString pRemove;
32 static sal_uInt16 nCountHash = 0, nAddHash, nItemHash, nRemoveHash;
35 SbxCollection::SbxCollection()
36 : SbxObject( "" )
38 if( !nCountHash )
40 pCount = GetSbxRes( StringId::CountProp );
41 pAdd = GetSbxRes( StringId::AddMeth );
42 pItem = GetSbxRes( StringId::ItemMeth );
43 pRemove = GetSbxRes( StringId::RemoveMeth );
44 nCountHash = MakeHashCode( pCount );
45 nAddHash = MakeHashCode( pAdd );
46 nItemHash = MakeHashCode( pItem );
47 nRemoveHash = MakeHashCode( pRemove );
49 Initialize();
50 // For Access on itself
51 StartListening(GetBroadcaster(), DuplicateHandling::Prevent);
54 SbxCollection::SbxCollection( const SbxCollection& rColl )
55 : SvRefBase( rColl ), SbxObject( rColl )
58 SbxCollection& SbxCollection::operator=( const SbxCollection& r )
60 if( &r != this )
61 SbxObject::operator=( r );
62 return *this;
65 SbxCollection::~SbxCollection()
68 void SbxCollection::Clear()
70 SbxObject::Clear();
71 Initialize();
74 void SbxCollection::Initialize()
76 SetType( SbxOBJECT );
77 SetFlag( SbxFlagBits::Fixed );
78 ResetFlag( SbxFlagBits::Write );
79 SbxVariable* p;
80 p = Make( pCount , SbxClassType::Property, SbxINTEGER );
81 p->ResetFlag( SbxFlagBits::Write );
82 p->SetFlag( SbxFlagBits::DontStore );
83 p = Make( pAdd, SbxClassType::Method, SbxEMPTY );
84 p->SetFlag( SbxFlagBits::DontStore );
85 p = Make( pItem , SbxClassType::Method, SbxOBJECT );
86 p->SetFlag( SbxFlagBits::DontStore );
87 p = Make( pRemove, SbxClassType::Method, SbxEMPTY );
88 p->SetFlag( SbxFlagBits::DontStore );
91 SbxVariable* SbxCollection::Find( const OUString& rName, SbxClassType t )
93 if( GetParameters() )
95 SbxObject* pObj = static_cast<SbxObject*>(GetObject());
96 return pObj ? pObj->Find( rName, t ) : nullptr;
98 else
100 return SbxObject::Find( rName, t );
104 void SbxCollection::Notify( SfxBroadcaster& rCst, const SfxHint& rHint )
106 const SbxHint* p = dynamic_cast<const SbxHint*>(&rHint);
107 if( p )
109 const SfxHintId nId = p->GetId();
110 bool bRead = ( nId == SfxHintId::BasicDataWanted );
111 bool bWrite = ( nId == SfxHintId::BasicDataChanged );
112 SbxVariable* pVar = p->GetVar();
113 SbxArray* pArg = pVar->GetParameters();
114 if( bRead || bWrite )
116 OUString aVarName( pVar->GetName() );
117 if( pVar == this )
119 CollItem( pArg );
121 else if( pVar->GetHashCode() == nCountHash
122 && aVarName.equalsIgnoreAsciiCase( pCount ) )
124 pVar->PutLong(sal::static_int_cast<sal_Int32>(pObjs->Count()));
126 else if( pVar->GetHashCode() == nAddHash
127 && aVarName.equalsIgnoreAsciiCase( pAdd ) )
129 CollAdd( pArg );
131 else if( pVar->GetHashCode() == nItemHash
132 && aVarName.equalsIgnoreAsciiCase( pItem ) )
134 CollItem( pArg );
136 else if( pVar->GetHashCode() == nRemoveHash
137 && aVarName.equalsIgnoreAsciiCase( pRemove ) )
139 CollRemove( pArg );
141 else
143 SbxObject::Notify( rCst, rHint );
145 return;
148 SbxObject::Notify( rCst, rHint );
151 // Default: argument is object
153 void SbxCollection::CollAdd( SbxArray* pPar_ )
155 if (pPar_->Count() != 2)
157 SetError( ERRCODE_BASIC_WRONG_ARGS );
159 else
161 SbxBase* pObj = pPar_->Get(1)->GetObject();
162 if( !pObj || dynamic_cast<const SbxObject*>(pObj) == nullptr )
164 SetError( ERRCODE_BASIC_BAD_ARGUMENT );
166 else
168 Insert( static_cast<SbxObject*>(pObj) );
173 // Default: index from 1 or object name
175 void SbxCollection::CollItem( SbxArray* pPar_ )
177 if (pPar_->Count() != 2)
179 SetError( ERRCODE_BASIC_WRONG_ARGS );
181 else
183 SbxVariable* pRes = nullptr;
184 SbxVariable* p = pPar_->Get(1);
185 if( p->GetType() == SbxSTRING )
187 pRes = Find( p->GetOUString(), SbxClassType::Object );
189 else
191 short n = p->GetInteger();
192 if (n >= 1 && o3tl::make_unsigned(n) <= pObjs->Count())
194 pRes = pObjs->Get(static_cast<sal_uInt32>(n) - 1);
197 if( !pRes )
199 SetError( ERRCODE_BASIC_BAD_INDEX );
201 pPar_->Get(0)->PutObject(pRes);
205 // Default: index from 1
207 void SbxCollection::CollRemove( SbxArray* pPar_ )
209 if (pPar_->Count() != 2)
210 SetError( ERRCODE_BASIC_WRONG_ARGS );
211 else
213 short n = pPar_->Get(1)->GetInteger();
214 if (n < 1 || o3tl::make_unsigned(n) > pObjs->Count())
215 SetError( ERRCODE_BASIC_BAD_INDEX );
216 else
217 Remove(pObjs->Get(static_cast<sal_uInt32>(n) - 1));
221 bool SbxCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
223 bool bRes = SbxObject::LoadData( rStrm, nVer );
224 Initialize();
225 return bRes;
229 SbxStdCollection::SbxStdCollection()
230 : bAddRemoveOk( true )
233 SbxStdCollection::SbxStdCollection( const SbxStdCollection& r )
234 : SvRefBase( r ), SbxCollection( r ),
235 aElemClass( r.aElemClass ), bAddRemoveOk( r.bAddRemoveOk )
238 SbxStdCollection& SbxStdCollection::operator=( const SbxStdCollection& r )
240 if( &r != this )
242 if( !r.aElemClass.equalsIgnoreAsciiCase( aElemClass ) )
244 SetError( ERRCODE_BASIC_CONVERSION );
246 else
248 SbxCollection::operator=( r );
251 return *this;
254 SbxStdCollection::~SbxStdCollection()
257 // Default: Error, if wrong object
259 void SbxStdCollection::Insert( SbxVariable* p )
261 SbxObject* pObj = dynamic_cast<SbxObject*>( p );
262 if( pObj && !pObj->IsClass( aElemClass ) )
263 SetError( ERRCODE_BASIC_BAD_ACTION );
264 else
265 SbxCollection::Insert( p );
268 void SbxStdCollection::CollAdd( SbxArray* pPar_ )
270 if( !bAddRemoveOk )
271 SetError( ERRCODE_BASIC_BAD_ACTION );
272 else
273 SbxCollection::CollAdd( pPar_ );
276 void SbxStdCollection::CollRemove( SbxArray* pPar_ )
278 if( !bAddRemoveOk )
279 SetError( ERRCODE_BASIC_BAD_ACTION );
280 else
281 SbxCollection::CollRemove( pPar_ );
284 bool SbxStdCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
286 bool bRes = SbxCollection::LoadData( rStrm, nVer );
287 if( bRes )
289 aElemClass = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
290 RTL_TEXTENCODING_ASCII_US);
291 rStrm.ReadCharAsBool( bAddRemoveOk );
293 return bRes;
296 bool SbxStdCollection::StoreData( SvStream& rStrm ) const
298 bool bRes = SbxCollection::StoreData( rStrm );
299 if( bRes )
301 write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, aElemClass,
302 RTL_TEXTENCODING_ASCII_US);
303 rStrm.WriteBool( bAddRemoveOk );
305 return bRes;
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */