/work/dcmtk-install/include/dcmtk/ofstd/oferror.h
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (C) 2017-2021, OFFIS e.V. |
4 | | * All rights reserved. See COPYRIGHT file for details. |
5 | | * |
6 | | * This software and supporting documentation were developed by |
7 | | * |
8 | | * OFFIS e.V. |
9 | | * R&D Division Health |
10 | | * Escherweg 2 |
11 | | * D-26121 Oldenburg, Germany |
12 | | * |
13 | | * |
14 | | * Module: ofstd |
15 | | * |
16 | | * Author: Nikolas Goldhammer |
17 | | * |
18 | | * Purpose: |
19 | | * Implementing platform abstracting error code handling. |
20 | | * |
21 | | */ |
22 | | |
23 | | #ifndef OFERROR_H |
24 | | #define OFERROR_H |
25 | | |
26 | | #include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */ |
27 | | |
28 | | // also require STL string, since otherwise OFsystem_error would become incompatible with OFString |
29 | | #if defined(HAVE_STL_SYSTEM_ERROR) && defined(HAVE_STL_STRING) |
30 | | |
31 | | #include <system_error> |
32 | | typedef STD_NAMESPACE error_category OFerror_category; |
33 | | typedef STD_NAMESPACE error_code OFerror_code; |
34 | | |
35 | | inline const OFerror_category& OFsystem_category() { return STD_NAMESPACE system_category(); } |
36 | | inline const OFerror_category& OFgeneric_category() { return STD_NAMESPACE generic_category(); } |
37 | | |
38 | | #else // fallback implementations |
39 | | |
40 | | |
41 | | #include "dcmtk/ofstd/ofstring.h" |
42 | | |
43 | | // for Doxygen such that it does not ignore the globally declared functions |
44 | | /// @file oferror.h Declares classes and functions for platform abstracting error code handling. |
45 | | |
46 | | /** OFerror_category serves as the base class for specific error category types, |
47 | | * such as OFsystem_category. It is possible to make your own error_category class. |
48 | | * The objects of error category classes are treated as singletons, passed by reference. |
49 | | * @note this implementation is meant to be a subset of the C++11's std::error_category |
50 | | * that lacks the following features: error condition support, iostream category, |
51 | | * future category. |
52 | | * See: http://en.cppreference.com/w/cpp/error/error_category to compare OFerror_category |
53 | | * against std::error_category. |
54 | | * |
55 | | * The following table describes the possible operations on two instances <i>a</i> and <i>b</i> |
56 | | * of %OFerror_category: |
57 | | * <table border> |
58 | | * <tr><th>Expression</th><th>Meaning</th></tr> |
59 | | * <tr> |
60 | | * <td><center><kbd>a != b</kbd></center></td> |
61 | | * <td>Compares two OFerror_category objects and evaluates to OFTrue if both objects refer to different error categories.</td> |
62 | | * </tr> |
63 | | * <tr> |
64 | | * <td><center><kbd>a == b</kbd></center></td> |
65 | | * <td>Compares two OFerror_category objects and evaluates to OFTrue if both objects refer to the same error category.</td> |
66 | | * </tr> |
67 | | * <tr> |
68 | | * <td><center><kbd>a < b</kbd></center></td> |
69 | | * <td> |
70 | | * Implements a total order on OFerror_category objects. The semantics of one category being compared less than another |
71 | | * are intentionally not defined, but, if one category compares less than to another it will stay that way during the |
72 | | * remaining execution of the program and one and the same category will never compare less than to itself. |
73 | | * </td> |
74 | | * </tr> |
75 | | * </Table> |
76 | | * @see OFgeneric_category() |
77 | | * @see OFsystem_category() |
78 | | */ |
79 | | class DCMTK_OFSTD_EXPORT OFerror_category |
80 | | { |
81 | | public: |
82 | | |
83 | | /** Default constructor, used by derived classes. |
84 | | */ |
85 | | inline OFerror_category() {} |
86 | | |
87 | | /** Virtual destructor, does nothing. |
88 | | */ |
89 | | virtual ~OFerror_category() {} |
90 | | |
91 | | /** Obtains the name of the category, for example "generic". |
92 | | * @return a character pointer that refers to the name of the category. |
93 | | */ |
94 | | virtual const char* name() const = 0; |
95 | | |
96 | | /** Constructs an explanatory string for an error code. |
97 | | * @param code an integer that shall be interpreted as an error code. |
98 | | * @return an explanatory error message for the given code as an OFString. |
99 | | */ |
100 | | virtual OFString message( int code ) const = 0; |
101 | | |
102 | | // operator implementations, see above table for documentation |
103 | | #ifndef DOXYGEN |
104 | 0 | inline OFBool operator==(const OFerror_category& rhs) const { return this == &rhs; } |
105 | 0 | inline OFBool operator!=(const OFerror_category& rhs) const { return this != &rhs; } |
106 | 0 | inline OFBool operator<(const OFerror_category& rhs) const { return this < &rhs; } |
107 | | #endif // NOT DOXYGEN |
108 | | |
109 | | private: |
110 | | |
111 | | // ensure singleton behavior by disabling the copy constructor and assignment |
112 | | // operator |
113 | | #ifndef DOXYGEN |
114 | | OFerror_category(const OFerror_category& other); |
115 | | OFerror_category& operator=(const OFerror_category& rhs); |
116 | | #endif // NOT DOXYGEN |
117 | | }; |
118 | | |
119 | | |
120 | | /** OFerror_code is a platform abstracting wrapper for platform specific error codes. |
121 | | * Each OFerror_code object holds an error code originating from the operating system |
122 | | * or some low-level interface and a pointer to an object of type OFerror_category, |
123 | | * which corresponds to the said interface. The error code values may be not unique across |
124 | | * different error categories. |
125 | | * @note This implementation is meant to be a subset of the C++11's std::error_code |
126 | | * that lacks the following features: error condition support, iostream category, |
127 | | * future category. |
128 | | * See: http://en.cppreference.com/w/cpp/error/error_code to compare OFerror_code against |
129 | | * std::error_code. |
130 | | * |
131 | | * The following table describes the possible operations on two instances <i>a</i> and <i>b</i> |
132 | | * of %OFerror_code: |
133 | | * <table border> |
134 | | * <tr><th>Expression</th><th>Meaning</th></tr> |
135 | | * <tr> |
136 | | * <td><center><kbd>if(a), while(a), ...</kbd></center></td> |
137 | | * <td> |
138 | | * Test whether the error code refers to an error message or means that everything went fine. |
139 | | * OFTrue if the object refers to an error, OFFalse otherwise. |
140 | | * </td> |
141 | | * </tr> |
142 | | * <tr> |
143 | | * <td><center><kbd>a != b</kbd></center></td> |
144 | | * <td> |
145 | | * Compares two OFerror_code objects, evaluates to OFTrue if both objects refer to a different |
146 | | * error code, that is, if either the code or the category (or both) differ, OFFalse otherwise. |
147 | | * </td> |
148 | | * </tr> |
149 | | * <tr> |
150 | | * <td><center><kbd>a == b</kbd></center></td> |
151 | | * <td> |
152 | | * Compares two OFerror_code objects, evaluates to OFTrue if both objects refer to the same |
153 | | * error code, that is, if both the code and the category are equal, OFFalse otherwise. |
154 | | * </td> |
155 | | * </tr> |
156 | | * <tr> |
157 | | * <td><center><kbd>a < b</kbd></center></td> |
158 | | * <td> |
159 | | * Implements a total order on OFerror_code objects. Will return OFTrue if the category of <i>a</i> |
160 | | * compares less than to the category of <i>b</i>. Will return OFFalse if the category of <i>b</i> |
161 | | * compares less than to the category of <i>a</i>. If both objects refer to the same category, they |
162 | | * will be ordered using the actual error code integer. |
163 | | * </td> |
164 | | * </tr> |
165 | | * </Table> |
166 | | */ |
167 | | class DCMTK_OFSTD_EXPORT OFerror_code |
168 | | { |
169 | | public: |
170 | | |
171 | | /** Default constructor. |
172 | | * Initializes an error code with value 0 (success) |
173 | | * and category OFsystem_category(). |
174 | | */ |
175 | | OFerror_code(); |
176 | | |
177 | | /** Constructs an error code from the given arguments. |
178 | | * @param code the actual error code. |
179 | | * @param category a reference to the OFerror_category that shall be used. |
180 | | */ |
181 | | OFerror_code( int code, const OFerror_category& category ); |
182 | | |
183 | | /** Replaces the contents with the given error code and category. |
184 | | * @param code the actual error code. |
185 | | * @param category a reference to the OFerror_category that shall be used. |
186 | | */ |
187 | | void assign( int code, const OFerror_category& category ); |
188 | | |
189 | | /** Sets the error code to value 0 (success) and the category |
190 | | * to OFsystem_category(). |
191 | | */ |
192 | | void clear(); |
193 | | |
194 | | /** Obtains the actual error code. |
195 | | * @return the error code as an integer. |
196 | | */ |
197 | | int value() const; |
198 | | |
199 | | /** Obtains the category linked to this error code. |
200 | | * @return a reference to the linked error category. |
201 | | */ |
202 | | const OFerror_category& category() const; |
203 | | |
204 | | /** Constructs an explanatory string for this error code using the |
205 | | * linked error category. |
206 | | * @return the error message as an OFString. |
207 | | */ |
208 | | OFString message() const; |
209 | | |
210 | | // declare overloaded operators, see above table for documentation |
211 | | #ifndef DOXYGEN |
212 | | #ifdef HAVE_CXX11 |
213 | | explicit |
214 | | #endif |
215 | | operator OFBool() const; |
216 | | OFBool operator!=( const OFerror_code& rhs ) const; |
217 | | OFBool operator==( const OFerror_code& rhs ) const; |
218 | | OFBool operator<( const OFerror_code& rhs ) const; |
219 | | #endif // NOT DOXYGEN |
220 | | |
221 | | private: |
222 | | |
223 | | /// holds the error value. |
224 | | int m_Code; |
225 | | |
226 | | /// holds the error category |
227 | | const OFerror_category* m_Category; |
228 | | }; |
229 | | |
230 | | /** Retrieves a reference to an OFerror_category object used for operating system |
231 | | * specific error codes. |
232 | | * The object is required to override the virtual function OFerror_category::name() |
233 | | * to return a pointer to the string "system". |
234 | | * @return a reference to the system error category. |
235 | | */ |
236 | | DCMTK_OFSTD_EXPORT const OFerror_category& OFsystem_category(); |
237 | | |
238 | | /** Retrieves a reference to an OFerror_category object used for generic error codes. |
239 | | * The object is required to override the virtual function OFerror_category::name() |
240 | | * to return a pointer to the string "generic". |
241 | | * @return a reference to the generic error category. |
242 | | */ |
243 | | DCMTK_OFSTD_EXPORT const OFerror_category& OFgeneric_category(); |
244 | | |
245 | | #endif // NOT HAVE_STL_SYSTEM_ERROR && HAVE_STL_STRING |
246 | | |
247 | | #endif // OFERROR_H |