Debian Codegraph @

basic/source/sbx/sbxint.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 <sal/config.h>
21
22 #include <config_features.h>
23
24 #include <comphelper/errcode.hxx>
25 #include <basic/sberrors.hxx>
26 #include "sbxconv.hxx"
27 #include <runtime.hxx>
28
29 namespace
30 {
31 template <class T>
32 concept number = std::is_arithmetic_v<T>;
33
34 template <SbxDataType MySbxType, number N> N ImpGet(const SbxValues* p)
35 {
36 switch (+p->eType)
37 {
38 case SbxEMPTY:
39 return 0;
40 case SbxCHAR:
41 return ConvertWithOverflowTo<N>(p->nChar);
42 case SbxBYREF | SbxCHAR:
43 return ConvertWithOverflowTo<N>(*p->pChar);
44 case SbxBYTE:
45 return ConvertWithOverflowTo<N>(p->nByte);
46 case SbxBYREF | SbxBYTE:
47 return ConvertWithOverflowTo<N>(*p->pByte);
48 case SbxINTEGER:
49 case SbxBOOL:
50 return ConvertWithOverflowTo<N>(p->nInteger);
51 case SbxBYREF | SbxINTEGER:
52 case SbxBYREF | SbxBOOL:
53 return ConvertWithOverflowTo<N>(*p->pInteger);
54 case SbxERROR:
55 case SbxUSHORT:
56 return ConvertWithOverflowTo<N>(p->nUShort);
57 case SbxBYREF | SbxERROR:
58 case SbxBYREF | SbxUSHORT:
59 return ConvertWithOverflowTo<N>(*p->pUShort);
60 case SbxLONG:
61 return ConvertWithOverflowTo<N>(p->nLong);
62 case SbxBYREF | SbxLONG:
63 return ConvertWithOverflowTo<N>(*p->pLong);
64 case SbxULONG:
65 return ConvertWithOverflowTo<N>(p->nULong);
66 case SbxBYREF | SbxULONG:
67 return ConvertWithOverflowTo<N>(*p->pULong);
68 case SbxCURRENCY:
69 return CurTo<N>(p->nInt64);
70 case SbxBYREF | SbxCURRENCY:
71 return CurTo<N>(*p->pnInt64);
72 case SbxSALINT64:
73 return ConvertWithOverflowTo<N>(p->nInt64);
74 case SbxBYREF | SbxSALINT64:
75 return ConvertWithOverflowTo<N>(*p->pnInt64);
76 case SbxSALUINT64:
77 return ConvertWithOverflowTo<N>(p->uInt64);
78 case SbxBYREF | SbxSALUINT64:
79 return ConvertWithOverflowTo<N>(*p->puInt64);
80 case SbxSINGLE:
81 return ConvertWithOverflowTo<N>(p->nSingle);
82 case SbxBYREF | SbxSINGLE:
83 return ConvertWithOverflowTo<N>(*p->pSingle);
84 case SbxDATE:
85 case SbxDOUBLE:
86 return ConvertWithOverflowTo<N>(p->nDouble);
87 case SbxBYREF | SbxDATE:
88 case SbxBYREF | SbxDOUBLE:
89 return ConvertWithOverflowTo<N>(*p->pDouble);
90 case SbxDECIMAL:
91 case SbxBYREF | SbxDECIMAL:
92 if (p->pDecimal)
93 return p->pDecimal->getWithOverflow<N>();
94 return 0;
95 case SbxBYREF | SbxSTRING:
96 case SbxSTRING:
97 case SbxLPSTR:
98 if (p->pOUString)
99 {
100 double d;
101 SbxDataType t;
102 if (ImpScan(*p->pOUString, d, t, nullptr) == ERRCODE_NONE)
103 return ConvertWithOverflowTo<N>(d);
104 }
105 #if HAVE_FEATURE_SCRIPTING
106 if (SbiRuntime::isVBAEnabled()) // VBA only behaviour
107 SbxBase::SetError(ERRCODE_BASIC_CONVERSION);
108 #endif
109 return 0;
110 case SbxOBJECT:
111 if (SbxValue* pVal = dynamic_cast<SbxValue*>(p->pObj))
112 {
113 SbxValues aObjVal(MySbxType);
114 pVal->Get(aObjVal);
115 return ImpGet<MySbxType, N>(&aObjVal);
116 }
117 SbxBase::SetError(ERRCODE_BASIC_NO_OBJECT);
118 return 0;
119
120 default:
121 SbxBase::SetError(ERRCODE_BASIC_CONVERSION);
122 return 0;
123 }
124 }
125
126 template <typename T> void assignTo(SbxDecimal& dest, T n); // not defined
127 template <> void assignTo<sal_uInt8>(SbxDecimal& dest, sal_uInt8 n) { dest.setByte(n); }
128 template <> void assignTo<sal_Unicode>(SbxDecimal& dest, sal_Unicode n) { dest.setChar(n); }
129 template <> void assignTo<sal_Int16>(SbxDecimal& dest, sal_Int16 n) { dest.setShort(n); }
130 template <> void assignTo<sal_uInt16>(SbxDecimal& dest, sal_uInt16 n) { dest.setUShort(n); }
131 template <> void assignTo<sal_Int32>(SbxDecimal& dest, sal_Int32 n) { dest.setLong(n); }
132 template <> void assignTo<sal_uInt32>(SbxDecimal& dest, sal_uInt32 n) { dest.setULong(n); }
133 template <> void assignTo<sal_Int64>(SbxDecimal& dest, sal_Int64 n) { dest.setWithOverflow(n); }
134 template <> void assignTo<sal_uInt64>(SbxDecimal& dest, sal_uInt64 n) { dest.setWithOverflow(n); }
135 template <> void assignTo<double>(SbxDecimal& dest, double n) { dest.setWithOverflow(n); }
136 template <> void assignTo<float>(SbxDecimal& dest, float n) { dest.setWithOverflow(n); }
137
138 template <std::integral I> void assignTo(OUString& dest, I n, bool) { dest = OUString::number(n); }
139 void assignTo(OUString& dest, double n, bool bCoreString)
140 {
141 // tdf#107953 - show 17 significant digits
142 ImpCvtNum(n, 17, dest, bCoreString);
143 }
144 void assignTo(OUString& dest, float n, bool)
145 {
146 // tdf#107953 - show 9 significant digits
147 ImpCvtNum(n, 9, dest);
148 }
149
150 template <SbxDataType MySbxType, number N> void ImpPut(SbxValues* p, N n, bool bCoreString)
151 {
152 switch (+p->eType)
153 {
154 case SbxCHAR:
155 assignWithOverflowTo(p->nChar, n);
156 break;
157 case SbxBYREF | SbxCHAR:
158 assignWithOverflowTo(*p->pChar, n);
159 break;
160 case SbxBYTE:
161 assignWithOverflowTo(p->nByte, n);
162 break;
163 case SbxBYREF | SbxBYTE:
164 assignWithOverflowTo(*p->pByte, n);
165 break;
166 case SbxINTEGER:
167 case SbxBOOL:
168 assignWithOverflowTo(p->nInteger, n);
169 break;
170 case SbxBYREF | SbxINTEGER:
171 case SbxBYREF | SbxBOOL:
172 assignWithOverflowTo(*p->pInteger, n);
173 break;
174 case SbxERROR:
175 case SbxUSHORT:
176 assignWithOverflowTo(p->nUShort, n);
177 break;
178 case SbxBYREF | SbxERROR:
179 case SbxBYREF | SbxUSHORT:
180 assignWithOverflowTo(*p->pUShort, n);
181 break;
182 case SbxLONG:
183 assignWithOverflowTo(p->nLong, n);
184 break;
185 case SbxBYREF | SbxLONG:
186 assignWithOverflowTo(*p->pLong, n);
187 break;
188 case SbxULONG:
189 assignWithOverflowTo(p->nULong, n);
190 break;
191 case SbxBYREF | SbxULONG:
192 assignWithOverflowTo(*p->pULong, n);
193 break;
194 case SbxCURRENCY:
195 assignWithOverflowTo(p->nInt64, CurFrom(n));
196 break;
197 case SbxBYREF | SbxCURRENCY:
198 assignWithOverflowTo(*p->pnInt64, CurFrom(n));
199 break;
200 case SbxSALINT64:
201 assignWithOverflowTo(p->nInt64, n);
202 break;
203 case SbxBYREF | SbxSALINT64:
204 assignWithOverflowTo(*p->pnInt64, n);
205 break;
206 case SbxSALUINT64:
207 assignWithOverflowTo(p->uInt64, n);
208 break;
209 case SbxBYREF | SbxSALUINT64:
210 assignWithOverflowTo(*p->puInt64, n);
211 break;
212
213 case SbxDECIMAL:
214 case SbxBYREF | SbxDECIMAL:
215 assignTo(*ImpCreateDecimal(p), n);
216 break;
217
218 case SbxSINGLE:
219 assignWithOverflowTo(p->nSingle, n);
220 break;
221 case SbxBYREF | SbxSINGLE:
222 assignWithOverflowTo(*p->pSingle, n);
223 break;
224
225 case SbxDATE:
226 case SbxDOUBLE:
227 assignWithOverflowTo(p->nDouble, n);
228 break;
229 case SbxBYREF | SbxDATE:
230 case SbxBYREF | SbxDOUBLE:
231 assignWithOverflowTo(*p->pDouble, n);
232 break;
233
234 case SbxBYREF | SbxSTRING:
235 case SbxSTRING:
236 case SbxLPSTR:
237 if (!p->pOUString)
238 p->pOUString = new OUString;
239 assignTo(*p->pOUString, n, bCoreString);
240 break;
241
242 case SbxOBJECT:
243 if (SbxValue* pVal = dynamic_cast<SbxValue*>(p->pObj))
244 {
245 SbxValues newVal(MySbxType);
246 ImpPut<MySbxType, N>(&newVal, n, bCoreString);
247 pVal->Put(newVal);
248 }
249 else
250 SbxBase::SetError(ERRCODE_BASIC_NO_OBJECT);
251 break;
252
253 default:
254 SbxBase::SetError(ERRCODE_BASIC_CONVERSION);
255 }
256 }
257 }
258
259 sal_Unicode ImpGetChar(const SbxValues* p) { return ImpGet<SbxCHAR, sal_Unicode>(p); }
260 sal_uInt8 ImpGetByte(const SbxValues* p) { return ImpGet<SbxBYTE, sal_uInt8>(p); }
261 sal_Int16 ImpGetInteger(const SbxValues* p) { return ImpGet<SbxINTEGER, sal_Int16>(p); }
262 sal_uInt16 ImpGetUShort(const SbxValues* p) { return ImpGet<SbxUSHORT, sal_uInt16>(p); }
263 sal_Int32 ImpGetLong(const SbxValues* p) { return ImpGet<SbxLONG, sal_Int32>(p); }
264 sal_uInt32 ImpGetULong(const SbxValues* p) { return ImpGet<SbxULONG, sal_uInt32>(p); }
265 sal_Int64 ImpGetInt64(const SbxValues* p) { return ImpGet<SbxSALINT64, sal_Int64>(p); }
266 sal_uInt64 ImpGetUInt64(const SbxValues* p) { return ImpGet<SbxSALUINT64, sal_uInt64>(p); }
267 double ImpGetDouble(const SbxValues* p) { return ImpGet<SbxDOUBLE, double>(p); }
268 float ImpGetSingle(const SbxValues* p) { return ImpGet<SbxSINGLE, float>(p); }
269
270 void ImpPutChar(SbxValues* p, sal_Unicode n) { ImpPut<SbxCHAR>(p, n, false); }
271 void ImpPutByte(SbxValues* p, sal_uInt8 n) { ImpPut<SbxBYTE>(p, n, false); }
272 void ImpPutInteger(SbxValues* p, sal_Int16 n) { ImpPut<SbxINTEGER>(p, n, false); }
273 void ImpPutUShort(SbxValues* p, sal_uInt16 n) { ImpPut<SbxUSHORT>(p, n, false); }
274 void ImpPutLong(SbxValues* p, sal_Int32 n) { ImpPut<SbxLONG>(p, n, false); }
275 void ImpPutULong(SbxValues* p, sal_uInt32 n) { ImpPut<SbxULONG>(p, n, false); }
276 void ImpPutInt64(SbxValues* p, sal_Int64 n) { ImpPut<SbxSALINT64>(p, n, false); }
277 void ImpPutUInt64(SbxValues* p, sal_uInt64 n) { ImpPut<SbxSALUINT64>(p, n, false); }
278 void ImpPutDouble(SbxValues* p, double n, bool bCoreString) { ImpPut<SbxDOUBLE>(p, n, bCoreString); }
279 void ImpPutSingle(SbxValues* p, float n) { ImpPut<SbxSINGLE>(p, n, false); }
280
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */