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