LibreOffice Module basic (master) 1
sbxcoll.cxx
Go to the documentation of this file.
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
28static OUString pCount;
29static OUString pAdd;
30static OUString pItem;
31static OUString pRemove;
32static sal_uInt16 nCountHash = 0, nAddHash, nItemHash, nRemoveHash;
33
34
36 : SbxObject( "" )
37{
38 if( !nCountHash )
39 {
48 }
49 Initialize();
50 // For Access on itself
51 StartListening(GetBroadcaster(), DuplicateHandling::Prevent);
52}
53
55 : SvRefBase( rColl ), SbxObject( rColl )
56{}
57
59{
60 if( &r != this )
62 return *this;
63}
64
66{}
67
69{
71 Initialize();
72}
73
75{
81 p->ResetFlag( SbxFlagBits::Write );
82 p->SetFlag( SbxFlagBits::DontStore );
84 p->SetFlag( SbxFlagBits::DontStore );
86 p->SetFlag( SbxFlagBits::DontStore );
88 p->SetFlag( SbxFlagBits::DontStore );
89}
90
91SbxVariable* 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
105{
106 const SbxHint* p = dynamic_cast<const SbxHint*>(&rHint);
107 if( p )
108 {
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 )
115 {
116 OUString aVarName( pVar->GetName() );
117 if( pVar == this )
118 {
119 CollItem( pArg );
120 }
121 else if( pVar->GetHashCode() == nCountHash
122 && aVarName.equalsIgnoreAsciiCase( pCount ) )
123 {
124 pVar->PutLong(sal::static_int_cast<sal_Int32>(pObjs->Count()));
125 }
126 else if( pVar->GetHashCode() == nAddHash
127 && aVarName.equalsIgnoreAsciiCase( pAdd ) )
128 {
129 CollAdd( pArg );
130 }
131 else if( pVar->GetHashCode() == nItemHash
132 && aVarName.equalsIgnoreAsciiCase( pItem ) )
133 {
134 CollItem( pArg );
135 }
136 else if( pVar->GetHashCode() == nRemoveHash
137 && aVarName.equalsIgnoreAsciiCase( pRemove ) )
138 {
139 CollRemove( pArg );
140 }
141 else
142 {
143 SbxObject::Notify( rCst, rHint );
144 }
145 return;
146 }
147 }
148 SbxObject::Notify( rCst, rHint );
149}
150
151// Default: argument is object
152
154{
155 if (pPar_->Count() != 2)
156 {
158 }
159 else
160 {
161 SbxBase* pObj = pPar_->Get(1)->GetObject();
162 if( !pObj || dynamic_cast<const SbxObject*>(pObj) == nullptr )
163 {
165 }
166 else
167 {
168 Insert( static_cast<SbxObject*>(pObj) );
169 }
170 }
171}
172
173// Default: index from 1 or object name
174
176{
177 if (pPar_->Count() != 2)
178 {
180 }
181 else
182 {
183 SbxVariable* pRes = nullptr;
184 SbxVariable* p = pPar_->Get(1);
185 if( p->GetType() == SbxSTRING )
186 {
187 pRes = Find( p->GetOUString(), SbxClassType::Object );
188 }
189 else
190 {
191 short n = p->GetInteger();
192 if (n >= 1 && o3tl::make_unsigned(n) <= pObjs->Count())
193 {
194 pRes = pObjs->Get(static_cast<sal_uInt32>(n) - 1);
195 }
196 }
197 if( !pRes )
198 {
200 }
201 pPar_->Get(0)->PutObject(pRes);
202 }
203}
204
205// Default: index from 1
206
208{
209 if (pPar_->Count() != 2)
211 else
212 {
213 short n = pPar_->Get(1)->GetInteger();
214 if (n < 1 || o3tl::make_unsigned(n) > pObjs->Count())
216 else
217 Remove(pObjs->Get(static_cast<sal_uInt32>(n) - 1));
218 }
219}
220
221bool SbxCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
222{
223 bool bRes = SbxObject::LoadData( rStrm, nVer );
224 Initialize();
225 return bRes;
226}
227
228
230 : bAddRemoveOk( true )
231{}
232
234 : SvRefBase( r ), SbxCollection( r ),
235 aElemClass( r.aElemClass ), bAddRemoveOk( r.bAddRemoveOk )
236{}
237
239{
240 if( &r != this )
241 {
242 if( !r.aElemClass.equalsIgnoreAsciiCase( aElemClass ) )
243 {
245 }
246 else
247 {
249 }
250 }
251 return *this;
252}
253
255{}
256
257// Default: Error, if wrong object
258
260{
261 SbxObject* pObj = dynamic_cast<SbxObject*>( p );
262 if( pObj && !pObj->IsClass( aElemClass ) )
264 else
266}
267
269{
270 if( !bAddRemoveOk )
272 else
273 SbxCollection::CollAdd( pPar_ );
274}
275
277{
278 if( !bAddRemoveOk )
280 else
282}
283
284bool SbxStdCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
285{
286 bool bRes = SbxCollection::LoadData( rStrm, nVer );
287 if( bRes )
288 {
290 RTL_TEXTENCODING_ASCII_US);
292 }
293 return bRes;
294}
295
296std::pair<bool, sal_uInt32> SbxStdCollection::StoreData( SvStream& rStrm ) const
297{
298 const auto& [bRes, nVersion] = SbxCollection::StoreData(rStrm);
299 if( bRes )
300 {
302 RTL_TEXTENCODING_ASCII_US);
304 }
305 return { bRes, nVersion };
306}
307
308/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
Definition: sbx.hxx:95
sal_uInt32 Count() const
Definition: sbxarray.cxx:87
SbxVariable * Get(sal_uInt32)
Definition: sbxarray.cxx:108
static void SetError(ErrCode)
Definition: sbxbase.cxx:116
void SetFlag(SbxFlagBits n)
Definition: sbxcore.hxx:108
void ResetFlag(SbxFlagBits n)
Definition: sbxcore.hxx:111
virtual void CollAdd(SbxArray *pPar)
Definition: sbxcoll.cxx:153
SbxCollection & operator=(const SbxCollection &)
Definition: sbxcoll.cxx:58
void CollItem(SbxArray *pPar)
Definition: sbxcoll.cxx:175
virtual bool LoadData(SvStream &, sal_uInt16) override
Definition: sbxcoll.cxx:221
virtual void CollRemove(SbxArray *pPar)
Definition: sbxcoll.cxx:207
virtual ~SbxCollection() override
Definition: sbxcoll.cxx:65
virtual SbxVariable * Find(const OUString &, SbxClassType) override
Definition: sbxcoll.cxx:91
virtual void Clear() override
Definition: sbxcoll.cxx:68
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: sbxcoll.cxx:104
void Initialize()
Definition: sbxcoll.cxx:74
Definition: sbx.hxx:81
virtual void Clear() override
Definition: sbxobj.cxx:125
virtual void Insert(SbxVariable *)
Definition: sbxobj.cxx:396
virtual SbxVariable * Find(const OUString &, SbxClassType)
Definition: sbxobj.cxx:182
SbxArrayRef pObjs
Definition: sbxobj.hxx:36
void Remove(const OUString &, SbxClassType)
Definition: sbxobj.cxx:499
virtual bool LoadData(SvStream &, sal_uInt16) override
Definition: sbxobj.cxx:561
SbxVariable * Make(const OUString &, SbxClassType, SbxDataType, bool bIsRuntimeFunction=false)
Definition: sbxobj.cxx:347
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: sbxobj.cxx:140
virtual bool IsClass(const OUString &) const
Definition: sbxobj.cxx:177
SbxObject & operator=(const SbxObject &)
Definition: sbxobj.cxx:65
virtual std::pair< bool, sal_uInt32 > StoreData(SvStream &) const override
Definition: sbxobj.cxx:607
SbxStdCollection & operator=(const SbxStdCollection &)
Definition: sbxcoll.cxx:238
virtual void CollRemove(SbxArray *pPar) override
Definition: sbxcoll.cxx:276
OUString aElemClass
Definition: sbx.hxx:191
virtual bool LoadData(SvStream &, sal_uInt16) override
Definition: sbxcoll.cxx:284
virtual void Insert(SbxVariable *) override
Definition: sbxcoll.cxx:259
virtual void CollAdd(SbxArray *pPar) override
Definition: sbxcoll.cxx:268
virtual std::pair< bool, sal_uInt32 > StoreData(SvStream &) const override
Definition: sbxcoll.cxx:296
bool bAddRemoveOk
Definition: sbx.hxx:192
virtual ~SbxStdCollection() override
Definition: sbxcoll.cxx:254
SbxBase * GetObject() const
Definition: sbxvar.hxx:157
bool PutObject(SbxBase *)
sal_Int16 GetInteger() const
Definition: sbxvar.hxx:141
bool SetType(SbxDataType)
Definition: sbxvalue.cxx:674
bool PutLong(sal_Int32)
SfxBroadcaster & GetBroadcaster()
Definition: sbxvar.cxx:102
SbxArray * GetParameters() const
Definition: sbxvar.cxx:111
const OUString & GetName(SbxNameType=SbxNameType::NONE) const
Definition: sbxvar.cxx:199
static constexpr sal_uInt16 MakeHashCode(std::u16string_view aName)
Definition: sbxvar.hxx:307
sal_uInt16 GetHashCode() const
Definition: sbxvar.hxx:273
void StartListening(SfxBroadcaster &rBroadcaster, DuplicateHandling eDuplicateHanding=DuplicateHandling::Unexpected)
SvStream & ReadCharAsBool(bool &rBool)
SvStream & WriteBool(bool b)
sal_Int16 nVersion
SfxHintId
void * p
sal_Int64 n
void SvStream & rStrm
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
sal_Int16 nId
#define ERRCODE_BASIC_BAD_ACTION
Definition: sberrors.hxx:47
#define ERRCODE_BASIC_BAD_INDEX
Definition: sberrors.hxx:36
#define ERRCODE_BASIC_CONVERSION
Definition: sberrors.hxx:30
#define ERRCODE_BASIC_BAD_ARGUMENT
Definition: sberrors.hxx:26
#define ERRCODE_BASIC_WRONG_ARGS
Definition: sberrors.hxx:52
static sal_uInt16 nRemoveHash
Definition: sbxcoll.cxx:32
static sal_uInt16 nItemHash
Definition: sbxcoll.cxx:32
static OUString pAdd
Definition: sbxcoll.cxx:29
static OUString pRemove
Definition: sbxcoll.cxx:31
static OUString pItem
Definition: sbxcoll.cxx:30
static sal_uInt16 nAddHash
Definition: sbxcoll.cxx:32
static sal_uInt16 nCountHash
Definition: sbxcoll.cxx:32
static OUString pCount
Definition: sbxcoll.cxx:28
@ SbxOBJECT
Definition: sbxdef.hxx:47
@ SbxEMPTY
Definition: sbxdef.hxx:38
@ SbxSTRING
Definition: sbxdef.hxx:46
@ SbxINTEGER
Definition: sbxdef.hxx:40
SbxClassType
Definition: sbxdef.hxx:27
OUString GetSbxRes(StringId nId)
Definition: sbxres.cxx:75
OUString read_uInt16_lenPrefixed_uInt8s_ToOUString(SvStream &rStrm, rtl_TextEncoding eEnc)
std::size_t write_uInt16_lenPrefixed_uInt8s_FromOUString(SvStream &rStrm, std::u16string_view rStr, rtl_TextEncoding eEnc)