LibreOffice Module vcl (master)  1
errcode.hxx
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 #ifndef INCLUDED_VCL_ERRCODE_HXX
21 #define INCLUDED_VCL_ERRCODE_HXX
22 
23 #include <rtl/ustring.hxx>
24 #include <vcl/dllapi.h>
25 #include <ostream>
26 
27 /*
28 
29 01234567012345670123456701234567
30 || || || || |
31 Warning || || |
32  | || || || |
33  Dynamic || || |
34  | || || |
35  Subsystemarea| || |
36  | || |
37  | || |
38  | || |
39  Class| |
40  | |
41  | |
42  | |
43  Code
44 */
45 
46 #define ERRCODE_ERROR_MASK 0x3fffffffUL
47 #define ERRCODE_WARNING_MASK 0x80000000UL
48 
49 #define ERRCODE_CLASS_SHIFT 8
50 #define ERRCODE_AREA_SHIFT 13
51 #define ERRCODE_DYNAMIC_SHIFT 26
52 
53 #define ERRCODE_CLASS_MASK (31UL << ERRCODE_CLASS_SHIFT)
54 
55 #define ERRCODE_DYNAMIC_COUNT 31UL
56 #define ERRCODE_DYNAMIC_MASK (31UL << ERRCODE_DYNAMIC_SHIFT)
57 
58 enum class ErrCodeArea;
59 enum class ErrCodeClass;
60 
61 enum class WarningFlag { Yes };
62 
64 {
65 public:
66  explicit constexpr ErrCode(WarningFlag, ErrCodeArea nArea, ErrCodeClass nClass, sal_uInt16 nCode)
67  : m_value(ERRCODE_WARNING_MASK | (sal_uInt32(nArea) << ERRCODE_AREA_SHIFT) | (sal_uInt32(nClass) << ERRCODE_CLASS_SHIFT) | nCode)
68  {
69  assert(nCode <= 0xff && "code out of range");
70  }
71  explicit constexpr ErrCode(ErrCodeArea nArea, ErrCodeClass nClass, sal_uInt16 nCode)
72  : m_value((sal_uInt32(nArea) << ERRCODE_AREA_SHIFT) | (sal_uInt32(nClass) << ERRCODE_CLASS_SHIFT) | nCode)
73  {
74  assert(nCode <= 0xff && "code out of range");
75  }
76  explicit constexpr ErrCode(ErrCodeArea nArea, sal_uInt16 nClassAndCode)
77  : m_value((sal_uInt32(nArea) << ERRCODE_AREA_SHIFT) | nClassAndCode) {}
78  explicit constexpr ErrCode(sal_uInt32 nValue)
79  : m_value(nValue) {}
80  constexpr ErrCode()
81  : m_value(0) {}
82 
83  explicit operator sal_uInt32() const { return m_value; }
84  explicit operator bool() const { return m_value != 0; }
85 
86  bool operator<(ErrCode const & other) const { return m_value < other.m_value; }
87  bool operator<=(ErrCode const & other) const { return m_value <= other.m_value; }
88  bool operator>(ErrCode const & other) const { return m_value > other.m_value; }
89  bool operator>=(ErrCode const & other) const { return m_value >= other.m_value; }
90  bool operator==(ErrCode const & other) const { return m_value == other.m_value; }
91  bool operator!=(ErrCode const & other) const { return m_value != other.m_value; }
92 
95  return (m_value & ERRCODE_WARNING_MASK)
96  ? ErrCode(0)
97  : ErrCode(static_cast<sal_uInt32>(m_value & ERRCODE_ERROR_MASK));
98  }
99 
100  bool IsWarning() const {
101  return m_value & ERRCODE_WARNING_MASK;
102  }
103 
105  return ErrCode(m_value | ERRCODE_WARNING_MASK);
106  }
107 
108  bool IsError() const {
109  return m_value && !IsWarning();
110  }
111 
112  bool IsDynamic() const {
113  return m_value & ERRCODE_DYNAMIC_MASK;
114  }
115 
116  sal_uInt32 GetDynamic() const {
117  return (m_value & ERRCODE_DYNAMIC_MASK) >> ERRCODE_DYNAMIC_SHIFT;
118  }
119 
121  return ErrCode(m_value & ~ERRCODE_DYNAMIC_MASK);
122  }
123 
124  constexpr ErrCode StripWarningAndDynamic() const {
125  return ErrCode(m_value & ~(ERRCODE_DYNAMIC_MASK | ERRCODE_WARNING_MASK));
126  }
127 
128  constexpr ErrCodeArea GetArea() const {
129  return static_cast<ErrCodeArea>((m_value >> ERRCODE_AREA_SHIFT) & 0x01fff);
130  }
131 
132  constexpr ErrCodeClass GetClass() const {
133  return static_cast<ErrCodeClass>((m_value >> ERRCODE_CLASS_SHIFT) & 0x1f);
134  }
135 
136  constexpr sal_uInt8 GetCode() const {
137  return static_cast<sal_uInt8>(m_value & 0xff);
138  }
139 
140  OUString toHexString() const {
141  return "0x" + OUString::number(m_value, 16);
142  }
143 
144  bool anyOf(ErrCode v) const {
145  return *this == v;
146  }
147 
148  template<typename... Args>
149  bool anyOf(ErrCode first, Args... args) const {
150  return *this == first || anyOf(args...);
151  }
152 private:
153  sal_uInt32 m_value;
154 };
155 
156 VCL_DLLPUBLIC std::ostream& operator<<(std::ostream& os, const ErrCode& err);
157 
158 enum class ErrCodeArea {
159  Io = 0 ,
160  Sfx = 2 ,
161  Inet = 3 ,
162  Vcl = 4 ,
163  Svx = 8 ,
164  So = 9 ,
165  Sbx = 10,
166  Uui = 13,
167  Sc = 32,
168  Sd = 40,
169  Sw = 56,
170 };
171 
172 enum class ErrCodeClass {
173  NONE = 0,
174  Abort = 1,
175  General = 2,
176  NotExists = 3,
177  AlreadyExists = 4,
178  Access = 5,
179  Path = 6,
180  Locking = 7,
181  Parameter = 8,
182  Space = 9,
183  NotSupported = 10,
184  Read = 11,
185  Write = 12,
186  Unknown = 13,
187  Version = 14,
188  Format = 15,
189  Create = 16,
190  Import = 17,
191  Export = 18,
192  So = 20,
193  Sbx = 21,
194  Runtime = 22,
195  Compiler = 23
196 };
197 
198 #define ERRCODE_NONE ErrCode(0)
199 
200 #define ERRCODE_IO_MISPLACEDCHAR ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 1 )
201 #define ERRCODE_IO_NOTEXISTS ErrCode( ErrCodeArea::Io, ErrCodeClass::NotExists, 2 )
202 #define ERRCODE_IO_ALREADYEXISTS ErrCode( ErrCodeArea::Io, ErrCodeClass::AlreadyExists, 3 )
203 #define ERRCODE_IO_NOTADIRECTORY ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 4 )
204 #define ERRCODE_IO_NOTAFILE ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 5 )
205 #define ERRCODE_IO_INVALIDDEVICE ErrCode( ErrCodeArea::Io, ErrCodeClass::Path, 6 )
206 #define ERRCODE_IO_ACCESSDENIED ErrCode( ErrCodeArea::Io, ErrCodeClass::Access, 7 )
207 #define ERRCODE_IO_LOCKVIOLATION ErrCode( ErrCodeArea::Io, ErrCodeClass::Locking, 8 )
208 #define ERRCODE_IO_OUTOFSPACE ErrCode( ErrCodeArea::Io, ErrCodeClass::Space, 9 )
209 #define ERRCODE_IO_ISWILDCARD ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 11 )
210 #define ERRCODE_IO_NOTSUPPORTED ErrCode( ErrCodeArea::Io, ErrCodeClass::NotSupported, 12 )
211 #define ERRCODE_IO_GENERAL ErrCode( ErrCodeArea::Io, ErrCodeClass::General, 13 )
212 #define ERRCODE_IO_TOOMANYOPENFILES ErrCode( ErrCodeArea::Io, ErrCodeClass::Space, 14 )
213 #define ERRCODE_IO_CANTREAD ErrCode( ErrCodeArea::Io, ErrCodeClass::Read, 15 )
214 #define ERRCODE_IO_CANTWRITE ErrCode( ErrCodeArea::Io, ErrCodeClass::Write, 16 )
215 #define ERRCODE_IO_OUTOFMEMORY ErrCode( ErrCodeArea::Io, ErrCodeClass::Space, 17 )
216 #define ERRCODE_IO_CANTSEEK ErrCode( ErrCodeArea::Io, ErrCodeClass::General, 18 )
217 #define ERRCODE_IO_CANTTELL ErrCode( ErrCodeArea::Io, ErrCodeClass::General, 19 )
218 #define ERRCODE_IO_WRONGVERSION ErrCode( ErrCodeArea::Io, ErrCodeClass::Version, 20 )
219 #define ERRCODE_IO_WRONGFORMAT ErrCode( ErrCodeArea::Io, ErrCodeClass::Format, 21 )
220 #define ERRCODE_IO_INVALIDCHAR ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 22 )
221 #define ERRCODE_IO_UNKNOWN ErrCode( ErrCodeArea::Io, ErrCodeClass::Unknown, 23 )
222 #define ERRCODE_IO_INVALIDACCESS ErrCode( ErrCodeArea::Io, ErrCodeClass::Access, 24 )
223 #define ERRCODE_IO_CANTCREATE ErrCode( ErrCodeArea::Io, ErrCodeClass::Create, 25 )
224 #define ERRCODE_IO_INVALIDPARAMETER ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 26 )
225 #define ERRCODE_IO_ABORT ErrCode( ErrCodeArea::Io, ErrCodeClass::Abort, 27 )
226 #define ERRCODE_IO_NOTEXISTSPATH ErrCode( ErrCodeArea::Io, ErrCodeClass::NotExists, 28 )
227 #define ERRCODE_IO_PENDING ErrCode( ErrCodeArea::Io, ErrCodeClass::NotExists, 29 )
228 #define ERRCODE_IO_RECURSIVE ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 30 )
229 #define ERRCODE_IO_NAMETOOLONG ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 31 )
230 #define ERRCODE_IO_INVALIDLENGTH ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 32 )
231 #define ERRCODE_IO_CURRENTDIR ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 33 )
232 #define ERRCODE_IO_NOTSAMEDEVICE ErrCode( ErrCodeArea::Io, ErrCodeClass::Parameter, 34 )
233 #define ERRCODE_IO_DEVICENOTREADY ErrCode( ErrCodeArea::Io, ErrCodeClass::Read, 35 )
234 #define ERRCODE_IO_BADCRC ErrCode( ErrCodeArea::Io, ErrCodeClass::Read, 36 )
235 #define ERRCODE_IO_WRITEPROTECTED ErrCode( ErrCodeArea::Io, ErrCodeClass::Access, 37 )
236 #define ERRCODE_IO_BROKENPACKAGE ErrCode( ErrCodeArea::Io, ErrCodeClass::Format, 38 )
237 #define ERRCODE_IO_NOTSTORABLEINBINARYFORMAT ErrCode( ErrCodeArea::Io, ErrCodeClass::Format, 39 )
238 #define ERRCODE_IO_FILTERDISABLED ErrCode( ErrCodeArea::Io, ErrCodeClass::Format, 40 )
239 
240 // StreamErrorCodes
241 
242 #define SVSTREAM_GENERALERROR ERRCODE_IO_GENERAL
243 #define SVSTREAM_FILE_NOT_FOUND ERRCODE_IO_NOTEXISTS
244 #define SVSTREAM_PATH_NOT_FOUND ERRCODE_IO_NOTEXISTSPATH
245 #define SVSTREAM_TOO_MANY_OPEN_FILES ERRCODE_IO_TOOMANYOPENFILES
246 #define SVSTREAM_ACCESS_DENIED ERRCODE_IO_ACCESSDENIED
247 #define SVSTREAM_SHARING_VIOLATION ERRCODE_IO_LOCKVIOLATION
248 #define SVSTREAM_LOCKING_VIOLATION ERRCODE_IO_LOCKVIOLATION
249 #define SVSTREAM_SHARE_BUFF_EXCEEDED ERRCODE_IO_LOCKVIOLATION
250 
251 #define SVSTREAM_INVALID_ACCESS ERRCODE_IO_INVALIDACCESS
252 #define SVSTREAM_INVALID_HANDLE ERRCODE_IO_GENERAL
253 #define SVSTREAM_CANNOT_MAKE ERRCODE_IO_CANTCREATE
254 #define SVSTREAM_INVALID_PARAMETER ERRCODE_IO_INVALIDPARAMETER
255 
256 #define SVSTREAM_READ_ERROR ERRCODE_IO_CANTREAD
257 #define SVSTREAM_WRITE_ERROR ERRCODE_IO_CANTWRITE
258 #define SVSTREAM_SEEK_ERROR ERRCODE_IO_CANTSEEK
259 
260 #define SVSTREAM_OUTOFMEMORY ERRCODE_IO_OUTOFMEMORY
261 
262 #define SVSTREAM_FILEFORMAT_ERROR ERRCODE_IO_WRONGFORMAT
263 #define SVSTREAM_WRONGVERSION ERRCODE_IO_WRONGVERSION
264 
265 #define SVSTREAM_DISK_FULL ERRCODE_IO_OUTOFSPACE
266 
267 #define PRINTER_ABORT ERRCODE_IO_ABORT
268 #define PRINTER_GENERALERROR ERRCODE_IO_GENERAL
269 
270 #define ERRCODE_ABORT ERRCODE_IO_ABORT
271 
272 #define ERRCODE_INET_NAME_RESOLVE ErrCode(ErrCodeArea::Inet, ErrCodeClass::Read, 1)
273 #define ERRCODE_INET_CONNECT ErrCode(ErrCodeArea::Inet, ErrCodeClass::Read, 2)
274 #define ERRCODE_INET_READ ErrCode(ErrCodeArea::Inet, ErrCodeClass::Read, 3)
275 #define ERRCODE_INET_WRITE ErrCode(ErrCodeArea::Inet, ErrCodeClass::Write, 4)
276 #define ERRCODE_INET_GENERAL ErrCode(ErrCodeArea::Inet, ErrCodeClass::Write, 5)
277 #define ERRCODE_INET_OFFLINE ErrCode(ErrCodeArea::Inet, ErrCodeClass::Read, 6)
278 
279 #endif
280 
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr ErrCode(WarningFlag, ErrCodeArea nArea, ErrCodeClass nClass, sal_uInt16 nCode)
Definition: errcode.hxx:66
ErrCodeArea
Definition: errcode.hxx:158
#define ERRCODE_DYNAMIC_MASK
Definition: errcode.hxx:56
sal_uInt32 m_value
Definition: errcode.hxx:153
constexpr sal_uInt8 GetCode() const
Definition: errcode.hxx:136
ErrCode IgnoreWarning() const
convert to ERRCODE_NONE if it's a warning, else return the error
Definition: errcode.hxx:94
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
VCL_DLLPUBLIC std::ostream & operator<<(std::ostream &os, const ErrCode &err)
Definition: errcode.cxx:22
tuple args
#define ERRCODE_WARNING_MASK
Definition: errcode.hxx:47
constexpr ErrCode StripWarningAndDynamic() const
Definition: errcode.hxx:124
#define ERRCODE_ERROR_MASK
Definition: errcode.hxx:46
#define ERRCODE_DYNAMIC_SHIFT
Definition: errcode.hxx:51
WarningFlag
Definition: errcode.hxx:61
constexpr ErrCodeArea GetArea() const
Definition: errcode.hxx:128
constexpr ErrCodeClass GetClass() const
Definition: errcode.hxx:132
bool operator!=(ErrCode const &other) const
Definition: errcode.hxx:91
bool anyOf(ErrCode v) const
Definition: errcode.hxx:144
bool operator==(ErrCode const &other) const
Definition: errcode.hxx:90
#define ERRCODE_AREA_SHIFT
Definition: errcode.hxx:50
#define ERRCODE_CLASS_SHIFT
Definition: errcode.hxx:49
ErrCodeClass
Definition: errcode.hxx:172
bool operator<=(ErrCode const &other) const
Definition: errcode.hxx:87
constexpr ErrCode(ErrCodeArea nArea, sal_uInt16 nClassAndCode)
Definition: errcode.hxx:76
constexpr ErrCode()
Definition: errcode.hxx:80
bool operator>=(ErrCode const &other) const
Definition: errcode.hxx:89
#define SAL_WARN_UNUSED
bool IsError() const
Definition: errcode.hxx:108
sal_uInt32 GetDynamic() const
Definition: errcode.hxx:116
constexpr ErrCode(sal_uInt32 nValue)
Definition: errcode.hxx:78
unsigned char sal_uInt8
bool operator>(ErrCode const &other) const
Definition: errcode.hxx:88
bool anyOf(ErrCode first, Args...args) const
Definition: errcode.hxx:149
bool IsWarning() const
Definition: errcode.hxx:100
bool IsDynamic() const
Definition: errcode.hxx:112
OUString toHexString() const
Definition: errcode.hxx:140
constexpr ErrCode(ErrCodeArea nArea, ErrCodeClass nClass, sal_uInt16 nCode)
Definition: errcode.hxx:71
ErrCode MakeWarning() const
Definition: errcode.hxx:104
ErrCode StripDynamic() const
Definition: errcode.hxx:120
bool operator<(ErrCode const &other) const
Definition: errcode.hxx:86