Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / comphelper / errcode.hxx
blob8198512ff3e1cb6871586cddb1bace3e335d82a5
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 .
19 #pragma once
21 #include <rtl/ustring.hxx>
22 #include <comphelper/comphelperdllapi.h>
23 #include <ostream>
24 #include <o3tl/typed_flags_set.hxx>
25 #include <optional>
27 #if defined(DBG_UTIL)
28 #if __has_include(<version>)
29 #include <version>
30 #endif
31 #if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907
32 #include <source_location>
33 #define LIBO_ERRMSG_USE_SOURCE_LOCATION std
34 #elif __has_include(<experimental/source_location>)
35 #include <experimental/source_location>
36 #define LIBO_ERRMSG_USE_SOURCE_LOCATION std::experimental
37 #endif
38 #endif
42 01234567012345670123456701234567
43 || || || || |
44 Warning || || |
45 | || || || |
46 Unused || || |
47 | || || |
48 Subsystemarea| || |
49 | || |
50 | || |
51 | || |
52 Class| |
53 | |
54 | |
55 | |
56 Code
59 #define ERRCODE_ERROR_MASK 0x3fffffffUL
60 #define ERRCODE_WARNING_MASK 0x80000000UL
62 #define ERRCODE_CLASS_SHIFT 8
63 #define ERRCODE_AREA_SHIFT 13
64 #define ERRCODE_DYNAMIC_SHIFT 26
66 #define ERRCODE_CLASS_MASK (31UL << ERRCODE_CLASS_SHIFT)
68 enum class ErrCodeArea;
69 enum class ErrCodeClass;
71 enum class WarningFlag { Yes };
73 class SAL_WARN_UNUSED ErrCode final
75 public:
76 explicit constexpr ErrCode(WarningFlag, ErrCodeArea nArea, ErrCodeClass nClass, sal_uInt16 nCode)
77 : m_value(ERRCODE_WARNING_MASK | (sal_uInt32(nArea) << ERRCODE_AREA_SHIFT) | (sal_uInt32(nClass) << ERRCODE_CLASS_SHIFT) | nCode)
79 assert(nCode <= 0xff && "code out of range");
81 explicit constexpr ErrCode(ErrCodeArea nArea, ErrCodeClass nClass, sal_uInt16 nCode)
82 : m_value((sal_uInt32(nArea) << ERRCODE_AREA_SHIFT) | (sal_uInt32(nClass) << ERRCODE_CLASS_SHIFT) | nCode)
84 assert(nCode <= 0xff && "code out of range");
86 explicit constexpr ErrCode(ErrCodeArea nArea, sal_uInt16 nClassAndCode)
87 : m_value((sal_uInt32(nArea) << ERRCODE_AREA_SHIFT) | nClassAndCode) {}
88 explicit constexpr ErrCode(sal_uInt32 nValue)
89 : m_value(nValue) {}
90 constexpr ErrCode()
91 : m_value(0) {}
93 explicit operator sal_uInt32() const { return m_value; }
94 explicit operator bool() const { return m_value != 0; }
96 bool operator<(ErrCode const & other) const { return m_value < other.m_value; }
97 bool operator<=(ErrCode const & other) const { return m_value <= other.m_value; }
98 bool operator>(ErrCode const & other) const { return m_value > other.m_value; }
99 bool operator>=(ErrCode const & other) const { return m_value >= other.m_value; }
100 bool operator==(ErrCode const & other) const { return m_value == other.m_value; }
101 bool operator!=(ErrCode const & other) const { return m_value != other.m_value; }
103 /** convert to ERRCODE_NONE if it's a warning, else return the error */
104 ErrCode IgnoreWarning() const {
105 return (m_value & ERRCODE_WARNING_MASK)
106 ? ErrCode(0)
107 : ErrCode(static_cast<sal_uInt32>(m_value & ERRCODE_ERROR_MASK));
110 bool IsWarning() const {
111 return m_value & ERRCODE_WARNING_MASK;
114 ErrCode MakeWarning() const {
115 return ErrCode(m_value | ERRCODE_WARNING_MASK);
118 bool IsError() const {
119 return m_value && !IsWarning();
122 constexpr ErrCode StripWarning() const {
123 return ErrCode(m_value & ~ERRCODE_WARNING_MASK);
126 constexpr ErrCodeArea GetArea() const {
127 return static_cast<ErrCodeArea>((m_value >> ERRCODE_AREA_SHIFT) & 0x01fff);
130 constexpr ErrCodeClass GetClass() const {
131 return static_cast<ErrCodeClass>((m_value >> ERRCODE_CLASS_SHIFT) & 0x1f);
134 constexpr sal_uInt8 GetCode() const {
135 return static_cast<sal_uInt8>(m_value & 0xff);
138 OUString toHexString() const {
139 return "0x" + OUString::number(m_value, 16);
142 /// Return a string suitable for debug output, the same as the operator<< function
143 COMPHELPER_DLLPUBLIC OUString toString() const;
145 template <typename... Args> bool anyOf(Args... args) const
147 static_assert(sizeof...(args) > 0);
148 return (... || (*this == args));
151 private:
152 sal_uInt32 m_value;
155 COMPHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& os, const ErrCode& err);
157 enum class DialogMask
159 NONE = 0x0000,
160 ButtonsOk = 0x0001,
161 ButtonsCancel = 0x0002,
162 ButtonsRetry = 0x0004,
163 ButtonsNo = 0x0008,
164 ButtonsYes = 0x0010,
165 ButtonsYesNo = 0x0018,
167 ButtonDefaultsOk = 0x0100,
168 ButtonDefaultsCancel = 0x0200,
169 ButtonDefaultsYes = 0x0300,
170 ButtonDefaultsNo = 0x0400,
172 MessageError = 0x1000,
173 MessageWarning = 0x2000,
174 MessageInfo = 0x3000,
176 MAX = USHRT_MAX,
178 namespace o3tl
180 template<> struct typed_flags<DialogMask> : is_typed_flags<DialogMask, 0xffff> {};
183 /** Wrap up an ErrCode and an explanation and the source location where the error was created,
184 helps with debugging when finding the source of a problem.
186 class SAL_WARN_UNUSED ErrCodeMsg
188 public:
189 ErrCodeMsg() : mnCode(0), mnDialogMask(DialogMask::NONE) {}
190 #ifdef LIBO_ERRMSG_USE_SOURCE_LOCATION
191 ErrCodeMsg(ErrCode code, const OUString& arg, LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location loc = LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location::current())
192 : mnCode(code), maArg1(arg), mnDialogMask(DialogMask::NONE), moLoc(loc) {}
193 ErrCodeMsg(ErrCode code, const OUString& arg1, const OUString& arg2, LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location loc = LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location::current())
194 : mnCode(code), maArg1(arg1), maArg2(arg2), mnDialogMask(DialogMask::NONE), moLoc(loc) {}
195 ErrCodeMsg(ErrCode code, LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location loc = LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location::current())
196 : mnCode(code), mnDialogMask(DialogMask::NONE), moLoc(loc) {}
197 ErrCodeMsg(ErrCode code, const OUString& arg, DialogMask mask, LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location loc = LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location::current())
198 : mnCode(code), maArg1(arg), mnDialogMask(mask), moLoc(loc) {}
199 ErrCodeMsg(ErrCode code, const OUString& arg1, const OUString& arg2, DialogMask mask, LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location loc = LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location::current())
200 : mnCode(code), maArg1(arg1), maArg2(arg2), mnDialogMask(mask), moLoc(loc) {}
201 #else
202 ErrCodeMsg(ErrCode code, const OUString& arg)
203 : mnCode(code), maArg1(arg), mnDialogMask(DialogMask::NONE) {}
204 ErrCodeMsg(ErrCode code, const OUString& arg1, const OUString& arg2)
205 : mnCode(code), maArg1(arg1), maArg2(arg2), mnDialogMask(DialogMask::NONE) {}
206 ErrCodeMsg(ErrCode code)
207 : mnCode(code), mnDialogMask(DialogMask::NONE) {}
208 ErrCodeMsg(ErrCode code, const OUString& arg, DialogMask mask)
209 : mnCode(code), maArg1(arg), mnDialogMask(mask) {}
210 ErrCodeMsg(ErrCode code, const OUString& arg1, const OUString& arg2, DialogMask mask)
211 : mnCode(code), maArg1(arg1), maArg2(arg2), mnDialogMask(mask) {}
212 #endif
214 const ErrCode & GetCode() const { return mnCode; }
215 const OUString & GetArg1() const { return maArg1; }
216 const OUString & GetArg2() const { return maArg2; }
217 DialogMask GetDialogMask() const { return mnDialogMask; }
219 #ifdef LIBO_ERRMSG_USE_SOURCE_LOCATION
220 const std::optional<LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location> & GetSourceLocation() const { return moLoc; }
221 #endif
223 /** convert to ERRCODE_NONE if it's a warning, else return the error */
224 ErrCodeMsg IgnoreWarning() const { return mnCode.IsWarning() ? ErrCodeMsg(ErrCode(0)) : *this; }
226 bool IsWarning() const { return mnCode.IsWarning(); }
227 bool IsError() const { return mnCode.IsError(); }
228 explicit operator bool() const { return bool(mnCode); }
229 bool operator==(const ErrCodeMsg& rOther) const { return mnCode == rOther.mnCode; }
230 bool operator!=(const ErrCodeMsg& rOther) const { return mnCode != rOther.mnCode; }
232 /// Return a string suitable for debug output, the same as the operator<< function
233 COMPHELPER_DLLPUBLIC OUString toString() const;
235 private:
236 ErrCode mnCode;
237 OUString maArg1;
238 OUString maArg2;
239 DialogMask mnDialogMask;
240 #ifdef LIBO_ERRMSG_USE_SOURCE_LOCATION
241 std::optional<LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location> moLoc;
242 #endif
245 COMPHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& os, const ErrCodeMsg& err);
247 inline bool operator==(const ErrCodeMsg& lhs, ErrCode rhs) { return lhs.GetCode() == rhs; }
248 inline bool operator!=(const ErrCodeMsg& lhs, ErrCode rhs) { return lhs.GetCode() != rhs; }
249 inline bool operator==(ErrCode lhs, const ErrCodeMsg& rhs) { return lhs == rhs.GetCode(); }
250 inline bool operator!=(ErrCode lhs, const ErrCodeMsg& rhs) { return lhs != rhs.GetCode(); }
252 enum class ErrCodeArea {
253 Io = 0 ,
254 Sfx = 2 ,
255 Inet = 3 ,
256 Vcl = 4 ,
257 Svx = 8 ,
258 So = 9 ,
259 Sbx = 10,
260 Uui = 13,
261 Sc = 32,
262 Sd = 40,
263 Sw = 56,
266 enum class ErrCodeClass {
267 NONE = 0,
268 Abort = 1,
269 General = 2,
270 NotExists = 3,
271 AlreadyExists = 4,
272 Access = 5,
273 Path = 6,
274 Locking = 7,
275 Parameter = 8,
276 Space = 9,
277 NotSupported = 10,
278 Read = 11,
279 Write = 12,
280 Unknown = 13,
281 Version = 14,
282 Format = 15,
283 Create = 16,
284 Import = 17,
285 Export = 18,
286 So = 20,
287 Sbx = 21,
288 Runtime = 22,
289 Compiler = 23
292 #define ERRCODE_NONE ErrCode(0)
294 #define ERRCODE_IO_MISPLACEDCHAR ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 1 )
295 #define ERRCODE_IO_NOTEXISTS ErrCode( ErrCodeArea::Io, ErrCodeClass::NotExists, 2 )
296 #define ERRCODE_IO_ALREADYEXISTS ErrCode( ErrCodeArea::Io, ErrCodeClass::AlreadyExists, 3 )
297 #define ERRCODE_IO_NOTADIRECTORY ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 4 )
298 #define ERRCODE_IO_NOTAFILE ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 5 )
299 #define ERRCODE_IO_INVALIDDEVICE ErrCode( ErrCodeArea::Io, ErrCodeClass::Path, 6 )
300 #define ERRCODE_IO_ACCESSDENIED ErrCode( ErrCodeArea::Io, ErrCodeClass::Access, 7 )
301 #define ERRCODE_IO_LOCKVIOLATION ErrCode( ErrCodeArea::Io, ErrCodeClass::Locking, 8 )
302 #define ERRCODE_IO_OUTOFSPACE ErrCode( ErrCodeArea::Io, ErrCodeClass::Space, 9 )
303 #define ERRCODE_IO_ISWILDCARD ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 11 )
304 #define ERRCODE_IO_NOTSUPPORTED ErrCode( ErrCodeArea::Io, ErrCodeClass::NotSupported, 12 )
305 #define ERRCODE_IO_GENERAL ErrCode( ErrCodeArea::Io, ErrCodeClass::General, 13 )
306 #define ERRCODE_IO_TOOMANYOPENFILES ErrCode( ErrCodeArea::Io, ErrCodeClass::Space, 14 )
307 #define ERRCODE_IO_CANTREAD ErrCode( ErrCodeArea::Io, ErrCodeClass::Read, 15 )
308 #define ERRCODE_IO_CANTWRITE ErrCode( ErrCodeArea::Io, ErrCodeClass::Write, 16 )
309 #define ERRCODE_IO_OUTOFMEMORY ErrCode( ErrCodeArea::Io, ErrCodeClass::Space, 17 )
310 #define ERRCODE_IO_CANTSEEK ErrCode( ErrCodeArea::Io, ErrCodeClass::General, 18 )
311 #define ERRCODE_IO_CANTTELL ErrCode( ErrCodeArea::Io, ErrCodeClass::General, 19 )
312 #define ERRCODE_IO_WRONGVERSION ErrCode( ErrCodeArea::Io, ErrCodeClass::Version, 20 )
313 #define ERRCODE_IO_WRONGFORMAT ErrCode( ErrCodeArea::Io, ErrCodeClass::Format, 21 )
314 #define ERRCODE_IO_INVALIDCHAR ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 22 )
315 #define ERRCODE_IO_UNKNOWN ErrCode( ErrCodeArea::Io, ErrCodeClass::Unknown, 23 )
316 #define ERRCODE_IO_INVALIDACCESS ErrCode( ErrCodeArea::Io, ErrCodeClass::Access, 24 )
317 #define ERRCODE_IO_CANTCREATE ErrCode( ErrCodeArea::Io, ErrCodeClass::Create, 25 )
318 #define ERRCODE_IO_INVALIDPARAMETER ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 26 )
319 #define ERRCODE_IO_ABORT ErrCode( ErrCodeArea::Io, ErrCodeClass::Abort, 27 )
320 #define ERRCODE_IO_NOTEXISTSPATH ErrCode( ErrCodeArea::Io, ErrCodeClass::NotExists, 28 )
321 #define ERRCODE_IO_PENDING ErrCode( ErrCodeArea::Io, ErrCodeClass::NotExists, 29 )
322 #define ERRCODE_IO_RECURSIVE ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 30 )
323 #define ERRCODE_IO_NAMETOOLONG ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 31 )
324 #define ERRCODE_IO_INVALIDLENGTH ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 32 )
325 #define ERRCODE_IO_CURRENTDIR ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 33 )
326 #define ERRCODE_IO_NOTSAMEDEVICE ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 34 )
327 #define ERRCODE_IO_DEVICENOTREADY ErrCode( ErrCodeArea::Io, ErrCodeClass::Read, 35 )
328 #define ERRCODE_IO_BADCRC ErrCode( ErrCodeArea::Io, ErrCodeClass::Read, 36 )
329 #define ERRCODE_IO_WRITEPROTECTED ErrCode( ErrCodeArea::Io, ErrCodeClass::Access, 37 )
330 #define ERRCODE_IO_BROKENPACKAGE ErrCode( ErrCodeArea::Io, ErrCodeClass::Format, 38 )
331 #define ERRCODE_IO_NOTSTORABLEINBINARYFORMAT ErrCode( ErrCodeArea::Io, ErrCodeClass::Format, 39 )
332 #define ERRCODE_IO_FILTERDISABLED ErrCode( ErrCodeArea::Io, ErrCodeClass::Format, 40 )
334 // StreamErrorCodes
336 #define SVSTREAM_GENERALERROR ERRCODE_IO_GENERAL
337 #define SVSTREAM_FILE_NOT_FOUND ERRCODE_IO_NOTEXISTS
338 #define SVSTREAM_PATH_NOT_FOUND ERRCODE_IO_NOTEXISTSPATH
339 #define SVSTREAM_TOO_MANY_OPEN_FILES ERRCODE_IO_TOOMANYOPENFILES
340 #define SVSTREAM_ACCESS_DENIED ERRCODE_IO_ACCESSDENIED
341 #define SVSTREAM_SHARING_VIOLATION ERRCODE_IO_LOCKVIOLATION
342 #define SVSTREAM_LOCKING_VIOLATION ERRCODE_IO_LOCKVIOLATION
343 #define SVSTREAM_SHARE_BUFF_EXCEEDED ERRCODE_IO_LOCKVIOLATION
345 #define SVSTREAM_INVALID_ACCESS ERRCODE_IO_INVALIDACCESS
346 #define SVSTREAM_INVALID_HANDLE ERRCODE_IO_GENERAL
347 #define SVSTREAM_CANNOT_MAKE ERRCODE_IO_CANTCREATE
348 #define SVSTREAM_INVALID_PARAMETER ERRCODE_IO_INVALIDPARAMETER
350 #define SVSTREAM_READ_ERROR ERRCODE_IO_CANTREAD
351 #define SVSTREAM_WRITE_ERROR ERRCODE_IO_CANTWRITE
352 #define SVSTREAM_SEEK_ERROR ERRCODE_IO_CANTSEEK
354 #define SVSTREAM_OUTOFMEMORY ERRCODE_IO_OUTOFMEMORY
356 #define SVSTREAM_FILEFORMAT_ERROR ERRCODE_IO_WRONGFORMAT
357 #define SVSTREAM_WRONGVERSION ERRCODE_IO_WRONGVERSION
359 #define SVSTREAM_DISK_FULL ERRCODE_IO_OUTOFSPACE
361 #define PRINTER_ABORT ERRCODE_IO_ABORT
362 #define PRINTER_GENERALERROR ERRCODE_IO_GENERAL
364 #define ERRCODE_ABORT ERRCODE_IO_ABORT
366 #define ERRCODE_INET_NAME_RESOLVE ErrCode(ErrCodeArea::Inet, ErrCodeClass::Read, 1)
367 #define ERRCODE_INET_CONNECT ErrCode(ErrCodeArea::Inet, ErrCodeClass::Read, 2)
368 #define ERRCODE_INET_READ ErrCode(ErrCodeArea::Inet, ErrCodeClass::Read, 3)
369 #define ERRCODE_INET_WRITE ErrCode(ErrCodeArea::Inet, ErrCodeClass::Write, 4)
370 #define ERRCODE_INET_GENERAL ErrCode(ErrCodeArea::Inet, ErrCodeClass::Write, 5)
371 #define ERRCODE_INET_OFFLINE ErrCode(ErrCodeArea::Inet, ErrCodeClass::Read, 6)
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */