make ValueTransfer easier to understand
[LibreOffice.git] / include / formula / errorcodes.hxx
blob1a96a0c7022308bd65b2fb34c900c5d2011b9af2
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 #ifndef INCLUDED_FORMULA_ERRORCODES_HXX
21 #define INCLUDED_FORMULA_ERRORCODES_HXX
23 #include <rtl/math.hxx>
24 #include <sal/mathconf.h>
25 #include <sal/types.h>
27 #include <limits>
29 // Store as 16-bits, since error values are stored in tokens and formula results,
30 // and that can matter
31 enum class FormulaError : sal_uInt16
33 NONE = 0,
35 IllegalChar = 501,
36 IllegalArgument = 502,
37 IllegalFPOperation = 503, // #NUM!
38 IllegalParameter = 504,
39 Pair = 507,
40 PairExpected = 508,
41 OperatorExpected = 509,
42 VariableExpected = 510,
43 ParameterExpected = 511,
44 CodeOverflow = 512,
45 StringOverflow = 513,
46 StackOverflow = 514,
47 UnknownState = 515,
48 UnknownVariable = 516,
49 UnknownOpCode = 517,
50 UnknownStackVariable = 518,
51 NoValue = 519, // #VALUE!
52 UnknownToken = 520,
53 NoCode = 521, // #NULL!
54 CircularReference = 522,
55 NoConvergence = 523,
56 NoRef = 524, // #REF!
57 NoName = 525, // #NAME?
58 // ScInterpreter internal: no numeric value but numeric queried. If this is
59 // set as mnStringNoValueError no error is generated but 0 returned.
60 CellNoValue = 529,
61 // Interpreter: needed AddIn not found
62 NoAddin = 530,
63 // Interpreter: needed Macro not found
64 NoMacro = 531,
65 // Interpreter: Division by zero
66 DivisionByZero = 532, // #DIV/0!
67 // Compiler: a non-simple (str,err,val) value was put in an array
68 NestedArray = 533,
69 // ScInterpreter internal: no numeric value but numeric queried. If this is
70 // temporarily (!) set as mnStringNoValueError, the error is generated and can
71 // be used to distinguish that condition from all other (inherited) errors. Do
72 // not use for anything else! Never push or inherit the error otherwise!
73 NotNumericString = 534,
74 // ScInterpreter internal: jump matrix already has a result at this position,
75 // do not overwrite in case of empty code path.
76 JumpMatHasResult = 535,
77 // ScInterpreter internal: (matrix) element is not a numeric value, i.e.
78 // string or empty, to be distinguished from the general FormulaError::NoValue NAN and not
79 // to be used as result.
80 ElementNaN = 536,
81 // ScInterpreter/ScFormulaCell internal: keep dirty, retry interpreting next
82 // round.
83 RetryCircular = 537,
84 // If matrix could not be allocated.
85 MatrixSize = 538,
86 // Bad inline array content, non-value/non-string.
87 BadArrayContent = 539,
88 // Interpreter: signal result not available because updating links is not
89 // allowed (yet) and tell to try hybrid string as result.
90 LinkFormulaNeedingCheck = 540,
92 // Interpreter: NA() not available condition, not a real error
93 NotAvailable = 0x7fff
96 /** Unconditionally construct a double value of NAN where the lower bits
97 represent an interpreter error code. */
98 inline double CreateDoubleError( FormulaError nErr )
100 sal_math_Double smVal;
101 smVal.value = std::numeric_limits<double>::quiet_NaN();
102 smVal.nan_parts.fraction_lo = static_cast<unsigned>(nErr);
103 return smVal.value;
106 /** Recreate the error code of a coded double error, if any. */
107 inline FormulaError GetDoubleErrorValue( double fVal )
109 if ( std::isfinite( fVal ) )
110 return FormulaError::NONE;
111 if ( std::isinf( fVal ) )
112 return FormulaError::IllegalFPOperation; // normal INF
113 sal_uInt32 nErr = reinterpret_cast< sal_math_Double * >( &fVal)->nan_parts.fraction_lo;
114 if ( nErr & 0xffff0000 )
115 return FormulaError::NoValue; // just a normal NAN
116 if (!nErr)
117 // Another NAN, e.g. -nan(0x8000000000000) from calculating with -inf
118 return FormulaError::IllegalFPOperation;
119 // Any other error known to us as error code.
120 return static_cast<FormulaError>(nErr & 0x0000ffff);
123 /** Error values that are accepted as detailed "#ERRxxx!" constants.
125 Used in FormulaCompiler::GetErrorConstant() to prevent users from inventing
126 arbitrary values that already have or later might get a significant meaning.
128 inline bool isPublishedFormulaError( FormulaError nErr )
130 // Every value has to be handled explicitly, do not add a default case to
131 // let the compiler complain if a value is missing.
132 switch (nErr)
134 case FormulaError::NONE:
135 return false;
137 case FormulaError::IllegalChar:
138 case FormulaError::IllegalArgument:
139 case FormulaError::IllegalFPOperation:
140 case FormulaError::IllegalParameter:
141 case FormulaError::Pair:
142 case FormulaError::PairExpected:
143 case FormulaError::OperatorExpected:
144 case FormulaError::VariableExpected:
145 case FormulaError::ParameterExpected:
146 case FormulaError::CodeOverflow:
147 case FormulaError::StringOverflow:
148 case FormulaError::StackOverflow:
149 case FormulaError::UnknownState:
150 case FormulaError::UnknownVariable:
151 case FormulaError::UnknownOpCode:
152 case FormulaError::UnknownStackVariable:
153 case FormulaError::NoValue:
154 case FormulaError::UnknownToken:
155 case FormulaError::NoCode:
156 case FormulaError::CircularReference:
157 case FormulaError::NoConvergence:
158 case FormulaError::NoRef:
159 case FormulaError::NoName:
160 return true;
162 case FormulaError::CellNoValue:
163 return false;
165 case FormulaError::NoAddin:
166 case FormulaError::NoMacro:
167 case FormulaError::DivisionByZero:
168 case FormulaError::NestedArray:
169 case FormulaError::BadArrayContent:
170 return true;
172 case FormulaError::NotNumericString:
173 case FormulaError::JumpMatHasResult:
174 case FormulaError::ElementNaN:
175 case FormulaError::RetryCircular:
176 return false;
178 case FormulaError::MatrixSize:
179 case FormulaError::LinkFormulaNeedingCheck:
180 return true;
182 case FormulaError::NotAvailable:
183 return false;
185 return false;
188 #endif // INCLUDED_FORMULA_ERRORCODES_HXX
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */