1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <config_options.h>
22 #include <rtl/ustring.hxx>
23 #include <comphelper/comphelperdllapi.h>
25 #include <o3tl/typed_flags_set.hxx>
30 #if __has_include(<version>)
33 #if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907
34 #include <source_location>
35 #define LIBO_ERRMSG_USE_SOURCE_LOCATION std
36 #elif __has_include(<experimental/source_location>)
37 #include <experimental/source_location>
38 #define LIBO_ERRMSG_USE_SOURCE_LOCATION std::experimental
44 01234567012345670123456701234567
61 #define ERRCODE_ERROR_MASK 0x3fffffffUL
62 #define ERRCODE_WARNING_MASK 0x80000000UL
64 #define ERRCODE_CLASS_SHIFT 8
65 #define ERRCODE_AREA_SHIFT 13
66 #define ERRCODE_DYNAMIC_SHIFT 26
68 #define ERRCODE_CLASS_MASK (31UL << ERRCODE_CLASS_SHIFT)
70 enum class ErrCodeArea
;
71 enum class ErrCodeClass
;
73 enum class WarningFlag
{ Yes
};
75 class SAL_WARN_UNUSED ErrCode final
78 explicit constexpr ErrCode(WarningFlag
, ErrCodeArea nArea
, ErrCodeClass nClass
, sal_uInt16 nCode
)
79 : m_value(ERRCODE_WARNING_MASK
| (sal_uInt32(nArea
) << ERRCODE_AREA_SHIFT
) | (sal_uInt32(nClass
) << ERRCODE_CLASS_SHIFT
) | nCode
)
81 assert(nCode
<= 0xff && "code out of range");
83 explicit constexpr ErrCode(ErrCodeArea nArea
, ErrCodeClass nClass
, sal_uInt16 nCode
)
84 : m_value((sal_uInt32(nArea
) << ERRCODE_AREA_SHIFT
) | (sal_uInt32(nClass
) << ERRCODE_CLASS_SHIFT
) | nCode
)
86 assert(nCode
<= 0xff && "code out of range");
88 explicit constexpr ErrCode(ErrCodeArea nArea
, sal_uInt16 nClassAndCode
)
89 : m_value((sal_uInt32(nArea
) << ERRCODE_AREA_SHIFT
) | nClassAndCode
) {}
90 explicit constexpr ErrCode(sal_uInt32 nValue
)
95 explicit operator sal_uInt32() const { return m_value
; }
96 explicit operator bool() const { return m_value
!= 0; }
98 auto operator<=>(ErrCode
const & other
) const = default;
100 /** convert to ERRCODE_NONE if it's a warning, else return the error */
101 ErrCode
IgnoreWarning() const {
102 return (m_value
& ERRCODE_WARNING_MASK
)
104 : ErrCode(static_cast<sal_uInt32
>(m_value
& ERRCODE_ERROR_MASK
));
107 bool IsWarning() const {
108 return m_value
& ERRCODE_WARNING_MASK
;
111 ErrCode
MakeWarning() const {
112 return ErrCode(m_value
| ERRCODE_WARNING_MASK
);
115 bool IsError() const {
116 return m_value
&& !IsWarning();
119 constexpr ErrCode
StripWarning() const {
120 return ErrCode(m_value
& ~ERRCODE_WARNING_MASK
);
123 constexpr ErrCodeArea
GetArea() const {
124 return static_cast<ErrCodeArea
>((m_value
>> ERRCODE_AREA_SHIFT
) & 0x01fff);
127 constexpr ErrCodeClass
GetClass() const {
128 return static_cast<ErrCodeClass
>((m_value
>> ERRCODE_CLASS_SHIFT
) & 0x1f);
131 constexpr sal_uInt8
GetCode() const {
132 return static_cast<sal_uInt8
>(m_value
& 0xff);
135 OUString
toHexString() const {
136 return "0x" + OUString::number(m_value
, 16);
139 /// Return a string suitable for debug output, the same as the operator<< function
140 COMPHELPER_DLLPUBLIC OUString
toString() const;
142 template <typename
... Args
> bool anyOf(Args
... args
) const
144 static_assert(sizeof...(args
) > 0);
145 return (... || (*this == args
));
152 COMPHELPER_DLLPUBLIC
std::ostream
& operator<<(std::ostream
& os
, const ErrCode
& err
);
154 enum class DialogMask
158 ButtonsCancel
= 0x0002,
159 ButtonsRetry
= 0x0004,
162 ButtonsYesNo
= 0x0018,
164 ButtonDefaultsOk
= 0x0100,
165 ButtonDefaultsCancel
= 0x0200,
166 ButtonDefaultsYes
= 0x0300,
167 ButtonDefaultsNo
= 0x0400,
169 MessageError
= 0x1000,
170 MessageWarning
= 0x2000,
171 MessageInfo
= 0x3000,
177 template<> struct typed_flags
<DialogMask
> : is_typed_flags
<DialogMask
, 0xffff> {};
180 /** Wrap up an ErrCode and an explanation and the source location where the error was created,
181 helps with debugging when finding the source of a problem.
183 class SAL_WARN_UNUSED ErrCodeMsg
186 ErrCodeMsg() : mnCode(0), mnDialogMask(DialogMask::NONE
) {}
187 #ifdef LIBO_ERRMSG_USE_SOURCE_LOCATION
188 ErrCodeMsg(ErrCode code
, const OUString
& arg
, LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location loc
= LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location::current())
189 : mnCode(code
), maArg1(arg
), mnDialogMask(DialogMask::NONE
), moLoc(loc
) {}
190 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())
191 : mnCode(code
), maArg1(arg1
), maArg2(arg2
), mnDialogMask(DialogMask::NONE
), moLoc(loc
) {}
192 ErrCodeMsg(ErrCode code
, LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location loc
= LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location::current())
193 : mnCode(code
), mnDialogMask(DialogMask::NONE
), moLoc(loc
) {}
194 ErrCodeMsg(ErrCode code
, const OUString
& arg
, DialogMask mask
, LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location loc
= LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location::current())
195 : mnCode(code
), maArg1(arg
), mnDialogMask(mask
), moLoc(loc
) {}
196 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())
197 : mnCode(code
), maArg1(arg1
), maArg2(arg2
), mnDialogMask(mask
), moLoc(loc
) {}
199 ErrCodeMsg(ErrCode code
, const OUString
& arg
)
200 : mnCode(code
), maArg1(arg
), mnDialogMask(DialogMask::NONE
) {}
201 ErrCodeMsg(ErrCode code
, const OUString
& arg1
, const OUString
& arg2
)
202 : mnCode(code
), maArg1(arg1
), maArg2(arg2
), mnDialogMask(DialogMask::NONE
) {}
203 ErrCodeMsg(ErrCode code
)
204 : mnCode(code
), mnDialogMask(DialogMask::NONE
) {}
205 ErrCodeMsg(ErrCode code
, const OUString
& arg
, DialogMask mask
)
206 : mnCode(code
), maArg1(arg
), mnDialogMask(mask
) {}
207 ErrCodeMsg(ErrCode code
, const OUString
& arg1
, const OUString
& arg2
, DialogMask mask
)
208 : mnCode(code
), maArg1(arg1
), maArg2(arg2
), mnDialogMask(mask
) {}
211 const ErrCode
& GetCode() const { return mnCode
; }
212 const OUString
& GetArg1() const { return maArg1
; }
213 const OUString
& GetArg2() const { return maArg2
; }
214 DialogMask
GetDialogMask() const { return mnDialogMask
; }
216 #ifdef LIBO_ERRMSG_USE_SOURCE_LOCATION
217 const std::optional
<LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location
> & GetSourceLocation() const { return moLoc
; }
220 /** convert to ERRCODE_NONE if it's a warning, else return the error */
221 ErrCodeMsg
IgnoreWarning() const { return mnCode
.IsWarning() ? ErrCodeMsg(ErrCode(0)) : *this; }
223 bool IsWarning() const { return mnCode
.IsWarning(); }
224 bool IsError() const { return mnCode
.IsError(); }
225 explicit operator bool() const { return bool(mnCode
); }
226 bool operator==(const ErrCodeMsg
& rOther
) const { return mnCode
== rOther
.mnCode
; }
227 bool operator!=(const ErrCodeMsg
& rOther
) const { return mnCode
!= rOther
.mnCode
; }
229 /// Return a string suitable for debug output, the same as the operator<< function
230 UNLESS_MERGELIBS(COMPHELPER_DLLPUBLIC
) OUString
toString() const;
236 DialogMask mnDialogMask
;
237 #ifdef LIBO_ERRMSG_USE_SOURCE_LOCATION
238 std::optional
<LIBO_ERRMSG_USE_SOURCE_LOCATION::source_location
> moLoc
;
242 COMPHELPER_DLLPUBLIC
std::ostream
& operator<<(std::ostream
& os
, const ErrCodeMsg
& err
);
244 inline bool operator==(const ErrCodeMsg
& lhs
, ErrCode rhs
) { return lhs
.GetCode() == rhs
; }
245 inline bool operator!=(const ErrCodeMsg
& lhs
, ErrCode rhs
) { return lhs
.GetCode() != rhs
; }
246 inline bool operator==(ErrCode lhs
, const ErrCodeMsg
& rhs
) { return lhs
== rhs
.GetCode(); }
247 inline bool operator!=(ErrCode lhs
, const ErrCodeMsg
& rhs
) { return lhs
!= rhs
.GetCode(); }
249 enum class ErrCodeArea
{
263 enum class ErrCodeClass
{
289 #define ERRCODE_NONE ErrCode(0)
291 #define ERRCODE_IO_MISPLACEDCHAR ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 1 )
292 #define ERRCODE_IO_NOTEXISTS ErrCode( ErrCodeArea::Io, ErrCodeClass::NotExists, 2 )
293 #define ERRCODE_IO_ALREADYEXISTS ErrCode( ErrCodeArea::Io, ErrCodeClass::AlreadyExists, 3 )
294 #define ERRCODE_IO_NOTADIRECTORY ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 4 )
295 #define ERRCODE_IO_NOTAFILE ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 5 )
296 #define ERRCODE_IO_INVALIDDEVICE ErrCode( ErrCodeArea::Io, ErrCodeClass::Path, 6 )
297 #define ERRCODE_IO_ACCESSDENIED ErrCode( ErrCodeArea::Io, ErrCodeClass::Access, 7 )
298 #define ERRCODE_IO_LOCKVIOLATION ErrCode( ErrCodeArea::Io, ErrCodeClass::Locking, 8 )
299 #define ERRCODE_IO_OUTOFSPACE ErrCode( ErrCodeArea::Io, ErrCodeClass::Space, 9 )
300 #define ERRCODE_IO_ISWILDCARD ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 11 )
301 #define ERRCODE_IO_NOTSUPPORTED ErrCode( ErrCodeArea::Io, ErrCodeClass::NotSupported, 12 )
302 #define ERRCODE_IO_GENERAL ErrCode( ErrCodeArea::Io, ErrCodeClass::General, 13 )
303 #define ERRCODE_IO_TOOMANYOPENFILES ErrCode( ErrCodeArea::Io, ErrCodeClass::Space, 14 )
304 #define ERRCODE_IO_CANTREAD ErrCode( ErrCodeArea::Io, ErrCodeClass::Read, 15 )
305 #define ERRCODE_IO_CANTWRITE ErrCode( ErrCodeArea::Io, ErrCodeClass::Write, 16 )
306 #define ERRCODE_IO_OUTOFMEMORY ErrCode( ErrCodeArea::Io, ErrCodeClass::Space, 17 )
307 #define ERRCODE_IO_CANTSEEK ErrCode( ErrCodeArea::Io, ErrCodeClass::General, 18 )
308 #define ERRCODE_IO_CANTTELL ErrCode( ErrCodeArea::Io, ErrCodeClass::General, 19 )
309 #define ERRCODE_IO_WRONGVERSION ErrCode( ErrCodeArea::Io, ErrCodeClass::Version, 20 )
310 #define ERRCODE_IO_WRONGFORMAT ErrCode( ErrCodeArea::Io, ErrCodeClass::Format, 21 )
311 #define ERRCODE_IO_INVALIDCHAR ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 22 )
312 #define ERRCODE_IO_UNKNOWN ErrCode( ErrCodeArea::Io, ErrCodeClass::Unknown, 23 )
313 #define ERRCODE_IO_INVALIDACCESS ErrCode( ErrCodeArea::Io, ErrCodeClass::Access, 24 )
314 #define ERRCODE_IO_CANTCREATE ErrCode( ErrCodeArea::Io, ErrCodeClass::Create, 25 )
315 #define ERRCODE_IO_INVALIDPARAMETER ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 26 )
316 #define ERRCODE_IO_ABORT ErrCode( ErrCodeArea::Io, ErrCodeClass::Abort, 27 )
317 #define ERRCODE_IO_NOTEXISTSPATH ErrCode( ErrCodeArea::Io, ErrCodeClass::NotExists, 28 )
318 #define ERRCODE_IO_PENDING ErrCode( ErrCodeArea::Io, ErrCodeClass::NotExists, 29 )
319 #define ERRCODE_IO_RECURSIVE ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 30 )
320 #define ERRCODE_IO_NAMETOOLONG ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 31 )
321 #define ERRCODE_IO_INVALIDLENGTH ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 32 )
322 #define ERRCODE_IO_CURRENTDIR ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 33 )
323 #define ERRCODE_IO_NOTSAMEDEVICE ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 34 )
324 #define ERRCODE_IO_DEVICENOTREADY ErrCode( ErrCodeArea::Io, ErrCodeClass::Read, 35 )
325 #define ERRCODE_IO_BADCRC ErrCode( ErrCodeArea::Io, ErrCodeClass::Read, 36 )
326 #define ERRCODE_IO_WRITEPROTECTED ErrCode( ErrCodeArea::Io, ErrCodeClass::Access, 37 )
327 #define ERRCODE_IO_BROKENPACKAGE ErrCode( ErrCodeArea::Io, ErrCodeClass::Format, 38 )
328 #define ERRCODE_IO_NOTSTORABLEINBINARYFORMAT ErrCode( ErrCodeArea::Io, ErrCodeClass::Format, 39 )
329 #define ERRCODE_IO_FILTERDISABLED ErrCode( ErrCodeArea::Io, ErrCodeClass::Format, 40 )
333 #define SVSTREAM_GENERALERROR ERRCODE_IO_GENERAL
334 #define SVSTREAM_FILE_NOT_FOUND ERRCODE_IO_NOTEXISTS
335 #define SVSTREAM_PATH_NOT_FOUND ERRCODE_IO_NOTEXISTSPATH
336 #define SVSTREAM_TOO_MANY_OPEN_FILES ERRCODE_IO_TOOMANYOPENFILES
337 #define SVSTREAM_ACCESS_DENIED ERRCODE_IO_ACCESSDENIED
338 #define SVSTREAM_SHARING_VIOLATION ERRCODE_IO_LOCKVIOLATION
339 #define SVSTREAM_LOCKING_VIOLATION ERRCODE_IO_LOCKVIOLATION
340 #define SVSTREAM_SHARE_BUFF_EXCEEDED ERRCODE_IO_LOCKVIOLATION
342 #define SVSTREAM_INVALID_ACCESS ERRCODE_IO_INVALIDACCESS
343 #define SVSTREAM_INVALID_HANDLE ERRCODE_IO_GENERAL
344 #define SVSTREAM_CANNOT_MAKE ERRCODE_IO_CANTCREATE
345 #define SVSTREAM_INVALID_PARAMETER ERRCODE_IO_INVALIDPARAMETER
347 #define SVSTREAM_READ_ERROR ERRCODE_IO_CANTREAD
348 #define SVSTREAM_WRITE_ERROR ERRCODE_IO_CANTWRITE
349 #define SVSTREAM_SEEK_ERROR ERRCODE_IO_CANTSEEK
351 #define SVSTREAM_OUTOFMEMORY ERRCODE_IO_OUTOFMEMORY
353 #define SVSTREAM_FILEFORMAT_ERROR ERRCODE_IO_WRONGFORMAT
354 #define SVSTREAM_WRONGVERSION ERRCODE_IO_WRONGVERSION
356 #define SVSTREAM_DISK_FULL ERRCODE_IO_OUTOFSPACE
358 #define PRINTER_ABORT ERRCODE_IO_ABORT
359 #define PRINTER_GENERALERROR ERRCODE_IO_GENERAL
361 #define ERRCODE_ABORT ERRCODE_IO_ABORT
363 #define ERRCODE_INET_NAME_RESOLVE ErrCode(ErrCodeArea::Inet, ErrCodeClass::Read, 1)
364 #define ERRCODE_INET_CONNECT ErrCode(ErrCodeArea::Inet, ErrCodeClass::Read, 2)
365 #define ERRCODE_INET_READ ErrCode(ErrCodeArea::Inet, ErrCodeClass::Read, 3)
366 #define ERRCODE_INET_WRITE ErrCode(ErrCodeArea::Inet, ErrCodeClass::Write, 4)
367 #define ERRCODE_INET_GENERAL ErrCode(ErrCodeArea::Inet, ErrCodeClass::Write, 5)
368 #define ERRCODE_INET_OFFLINE ErrCode(ErrCodeArea::Inet, ErrCodeClass::Read, 6)
369 #define ERRCODE_INET_CONNECT_MSG ErrCode(ErrCodeArea::Inet, ErrCodeClass::Read, 7)
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */