Debian Codegraph @

basic/source/sbx/sbxcoll.cxx

raw
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 #include <o3tl/safeint.hxx>
21 #include <tools/stream.hxx>
22
23 #include <basic/sbx.hxx>
24 #include <basic/sberrors.hxx>
25 #include "sbxres.hxx"
26
27
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;
33
34
35 SbxCollection::SbxCollection()
36 : SbxObject( u""_ustr )
37 {
38 if( !nCountHash )
39 {
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 );
48 }
49 Initialize();
50 // For Access on itself
51 StartListening(GetBroadcaster(), DuplicateHandling::Prevent);
52 }
53
54 SbxCollection::SbxCollection( const SbxCollection& rColl )
55 : SvRefBase( rColl ), SbxObject( rColl )
56 {}
57
58 SbxCollection& SbxCollection::operator=( const SbxCollection& r )
59 {
60 if( &r != this )
61 SbxObject::operator=( r );
62 return *this;
63 }
64
65 SbxCollection::~SbxCollection()
66 {}
67
68 void SbxCollection::Clear()
69 {
70 SbxObject::Clear();
71 Initialize();
72 }
73
74 void SbxCollection::Initialize()
75 {
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 );
89 }
90
91 SbxVariable* SbxCollection::Find( const OUString& rName, SbxClassType t )
92 {
93 if( GetParameters() )
94 {
95 SbxObject* pObj = static_cast<SbxObject*>(GetObject());
96 return pObj ? pObj->Find( rName, t ) : nullptr;
97 }
98 else
99 {
100 return SbxObject::Find( rName, t );
101 }
102 }
103
104 void SbxCollection::Notify( SfxBroadcaster& rCst, const SfxHint& rHint )
105 {
106 const SfxHintId nId = rHint.GetId();
107 if( nId == SfxHintId::BasicDataWanted || nId == SfxHintId::BasicDataChanged )
108 {
109 const SbxHint* p = static_cast<const SbxHint*>(&rHint);
110 SbxVariable* pVar = p->GetVar();
111 SbxArray* pArg = pVar->GetParameters();
112 OUString aVarName( pVar->GetName() );
113 if( pVar == this )
114 {
115 CollItem( pArg );
116 }
117 else if( pVar->GetHashCode() == nCountHash
118 && aVarName.equalsIgnoreAsciiCase( pCount ) )
119 {
120 pVar->PutLong(sal::static_int_cast<sal_Int32>(pObjs->Count()));
121 }
122 else if( pVar->GetHashCode() == nAddHash
123 && aVarName.equalsIgnoreAsciiCase( pAdd ) )
124 {
125 CollAdd( pArg );
126 }
127 else if( pVar->GetHashCode() == nItemHash
128 && aVarName.equalsIgnoreAsciiCase( pItem ) )
129 {
130 CollItem( pArg );
131 }
132 else if( pVar->GetHashCode() == nRemoveHash
133 && aVarName.equalsIgnoreAsciiCase( pRemove ) )
134 {
135 CollRemove( pArg );
136 }
137 else
138 {
139 SbxObject::Notify( rCst, rHint );
140 }
141 return;
142 }
143 SbxObject::Notify( rCst, rHint );
144 }
145
146 // Default: argument is object
147
148 void SbxCollection::CollAdd( SbxArray* pPar_ )
149 {
150 if (pPar_->Count() != 2)
151 {
152 SetError( ERRCODE_BASIC_WRONG_ARGS );
153 }
154 else
155 {
156 SbxBase* pObj = pPar_->Get(1)->GetObject();
157 if( !pObj || dynamic_cast<const SbxObject*>(pObj) == nullptr )
158 {
159 SetError( ERRCODE_BASIC_BAD_ARGUMENT );
160 }
161 else
162 {
163 Insert( static_cast<SbxObject*>(pObj) );
164 }
165 }
166 }
167
168 // Default: index from 1 or object name
169
170 void SbxCollection::CollItem( SbxArray* pPar_ )
171 {
172 if (pPar_->Count() != 2)
173 {
174 SetError( ERRCODE_BASIC_WRONG_ARGS );
175 }
176 else
177 {
178 SbxVariable* pRes = nullptr;
179 SbxVariable* p = pPar_->Get(1);
180 if( p->GetType() == SbxSTRING )
181 {
182 pRes = Find( p->GetOUString(), SbxClassType::Object );
183 }
184 else
185 {
186 short n = p->GetInteger();
187 if (n >= 1 && o3tl::make_unsigned(n) <= pObjs->Count())
188 {
189 pRes = pObjs->Get(static_cast<sal_uInt32>(n) - 1);
190 }
191 }
192 if( !pRes )
193 {
194 SetError( ERRCODE_BASIC_BAD_INDEX );
195 }
196 pPar_->Get(0)->PutObject(pRes);
197 }
198 }
199
200 // Default: index from 1
201
202 void SbxCollection::CollRemove( SbxArray* pPar_ )
203 {
204 if (pPar_->Count() != 2)
205 SetError( ERRCODE_BASIC_WRONG_ARGS );
206 else
207 {
208 short n = pPar_->Get(1)->GetInteger();
209 if (n < 1 || o3tl::make_unsigned(n) > pObjs->Count())
210 SetError( ERRCODE_BASIC_BAD_INDEX );
211 else
212 Remove(pObjs->Get(static_cast<sal_uInt32>(n) - 1));
213 }
214 }
215
216 bool SbxCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
217 {
218 bool bRes = SbxObject::LoadData( rStrm, nVer );
219 Initialize();
220 return bRes;
221 }
222
223
224 SbxStdCollection::SbxStdCollection()
225 : bAddRemoveOk( true )
226 {}
227
228 SbxStdCollection::SbxStdCollection( const SbxStdCollection& r )
229 : SvRefBase( r ), SbxCollection( r ),
230 aElemClass( r.aElemClass ), bAddRemoveOk( r.bAddRemoveOk )
231 {}
232
233 SbxStdCollection& SbxStdCollection::operator=( const SbxStdCollection& r )
234 {
235 if( &r != this )
236 {
237 if( !r.aElemClass.equalsIgnoreAsciiCase( aElemClass ) )
238 {
239 SetError( ERRCODE_BASIC_CONVERSION );
240 }
241 else
242 {
243 SbxCollection::operator=( r );
244 }
245 }
246 return *this;
247 }
248
249 SbxStdCollection::~SbxStdCollection()
250 {}
251
252 // Default: Error, if wrong object
253
254 void SbxStdCollection::Insert( SbxVariable* p )
255 {
256 SbxObject* pObj = dynamic_cast<SbxObject*>( p );
257 if( pObj && !pObj->IsClass( aElemClass ) )
258 SetError( ERRCODE_BASIC_BAD_ACTION );
259 else
260 SbxCollection::Insert( p );
261 }
262
263 void SbxStdCollection::CollAdd( SbxArray* pPar_ )
264 {
265 if( !bAddRemoveOk )
266 SetError( ERRCODE_BASIC_BAD_ACTION );
267 else
268 SbxCollection::CollAdd( pPar_ );
269 }
270
271 void SbxStdCollection::CollRemove( SbxArray* pPar_ )
272 {
273 if( !bAddRemoveOk )
274 SetError( ERRCODE_BASIC_BAD_ACTION );
275 else
276 SbxCollection::CollRemove( pPar_ );
277 }
278
279 bool SbxStdCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
280 {
281 bool bRes = SbxCollection::LoadData( rStrm, nVer );
282 if( bRes )
283 {
284 aElemClass = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
285 RTL_TEXTENCODING_ASCII_US);
286 rStrm.ReadCharAsBool( bAddRemoveOk );
287 }
288 return bRes;
289 }
290
291 std::pair<bool, sal_uInt32> SbxStdCollection::StoreData( SvStream& rStrm ) const
292 {
293 const auto [bRes, nVersion] = SbxCollection::StoreData(rStrm);
294 if( bRes )
295 {
296 write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, aElemClass,
297 RTL_TEXTENCODING_ASCII_US);
298 rStrm.WriteBool( bAddRemoveOk );
299 }
300 return { bRes, nVersion };
301 }
302
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */