1 | // boost/system/error_code.hpp ---------------------------------------------// |
2 | |
3 | // Copyright Beman Dawes 2006, 2007 |
4 | // Copyright Christoper Kohlhoff 2007 |
5 | |
6 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
7 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
8 | |
9 | // See library home page at http://www.boost.org/libs/system |
10 | |
11 | #ifndef BOOST_ERROR_CODE_HPP |
12 | #define BOOST_ERROR_CODE_HPP |
13 | |
14 | #include <boost/system/config.hpp> |
15 | #include <boost/cstdint.hpp> |
16 | #include <boost/assert.hpp> |
17 | #include <boost/noncopyable.hpp> |
18 | #include <boost/utility/enable_if.hpp> |
19 | #include <ostream> |
20 | #include <string> |
21 | #include <stdexcept> |
22 | #include <functional> |
23 | |
24 | // TODO: undef these macros if not already defined |
25 | #include <boost/cerrno.hpp> |
26 | |
27 | #if !defined(BOOST_POSIX_API) && !defined(BOOST_WINDOWS_API) |
28 | # error BOOST_POSIX_API or BOOST_WINDOWS_API must be defined |
29 | #endif |
30 | |
31 | #include <boost/config/abi_prefix.hpp> // must be the last #include |
32 | |
33 | #ifndef BOOST_SYSTEM_NOEXCEPT |
34 | #define BOOST_SYSTEM_NOEXCEPT BOOST_NOEXCEPT |
35 | #endif |
36 | |
37 | namespace boost |
38 | { |
39 | namespace system |
40 | { |
41 | |
42 | class error_code; |
43 | class error_condition; |
44 | |
45 | // "Concept" helpers ---------------------------------------------------// |
46 | |
47 | template< class T > |
48 | struct is_error_code_enum { static const bool value = false; }; |
49 | |
50 | template< class T > |
51 | struct is_error_condition_enum { static const bool value = false; }; |
52 | |
53 | // generic error_conditions --------------------------------------------// |
54 | |
55 | namespace errc |
56 | { |
57 | enum errc_t |
58 | { |
59 | success = 0, |
60 | address_family_not_supported = EAFNOSUPPORT, |
61 | address_in_use = EADDRINUSE, |
62 | address_not_available = EADDRNOTAVAIL, |
63 | already_connected = EISCONN, |
64 | argument_list_too_long = E2BIG, |
65 | argument_out_of_domain = EDOM, |
66 | bad_address = EFAULT, |
67 | bad_file_descriptor = EBADF, |
68 | bad_message = EBADMSG, |
69 | broken_pipe = EPIPE, |
70 | connection_aborted = ECONNABORTED, |
71 | connection_already_in_progress = EALREADY, |
72 | connection_refused = ECONNREFUSED, |
73 | connection_reset = ECONNRESET, |
74 | cross_device_link = EXDEV, |
75 | destination_address_required = EDESTADDRREQ, |
76 | device_or_resource_busy = EBUSY, |
77 | directory_not_empty = ENOTEMPTY, |
78 | executable_format_error = ENOEXEC, |
79 | file_exists = EEXIST, |
80 | file_too_large = EFBIG, |
81 | filename_too_long = ENAMETOOLONG, |
82 | function_not_supported = ENOSYS, |
83 | host_unreachable = EHOSTUNREACH, |
84 | identifier_removed = EIDRM, |
85 | illegal_byte_sequence = EILSEQ, |
86 | inappropriate_io_control_operation = ENOTTY, |
87 | interrupted = EINTR, |
88 | invalid_argument = EINVAL, |
89 | invalid_seek = ESPIPE, |
90 | io_error = EIO, |
91 | is_a_directory = EISDIR, |
92 | message_size = EMSGSIZE, |
93 | network_down = ENETDOWN, |
94 | network_reset = ENETRESET, |
95 | network_unreachable = ENETUNREACH, |
96 | no_buffer_space = ENOBUFS, |
97 | no_child_process = ECHILD, |
98 | no_link = ENOLINK, |
99 | no_lock_available = ENOLCK, |
100 | no_message_available = ENODATA, |
101 | no_message = ENOMSG, |
102 | no_protocol_option = ENOPROTOOPT, |
103 | no_space_on_device = ENOSPC, |
104 | no_stream_resources = ENOSR, |
105 | no_such_device_or_address = ENXIO, |
106 | no_such_device = ENODEV, |
107 | no_such_file_or_directory = ENOENT, |
108 | no_such_process = ESRCH, |
109 | not_a_directory = ENOTDIR, |
110 | not_a_socket = ENOTSOCK, |
111 | not_a_stream = ENOSTR, |
112 | not_connected = ENOTCONN, |
113 | not_enough_memory = ENOMEM, |
114 | not_supported = ENOTSUP, |
115 | operation_canceled = ECANCELED, |
116 | operation_in_progress = EINPROGRESS, |
117 | operation_not_permitted = EPERM, |
118 | operation_not_supported = EOPNOTSUPP, |
119 | operation_would_block = EWOULDBLOCK, |
120 | owner_dead = EOWNERDEAD, |
121 | permission_denied = EACCES, |
122 | protocol_error = EPROTO, |
123 | protocol_not_supported = EPROTONOSUPPORT, |
124 | read_only_file_system = EROFS, |
125 | resource_deadlock_would_occur = EDEADLK, |
126 | resource_unavailable_try_again = EAGAIN, |
127 | result_out_of_range = ERANGE, |
128 | state_not_recoverable = ENOTRECOVERABLE, |
129 | stream_timeout = ETIME, |
130 | text_file_busy = ETXTBSY, |
131 | timed_out = ETIMEDOUT, |
132 | too_many_files_open_in_system = ENFILE, |
133 | too_many_files_open = EMFILE, |
134 | too_many_links = EMLINK, |
135 | too_many_symbolic_link_levels = ELOOP, |
136 | value_too_large = EOVERFLOW, |
137 | wrong_protocol_type = EPROTOTYPE |
138 | }; |
139 | |
140 | } // namespace errc |
141 | |
142 | # ifndef BOOST_SYSTEM_NO_DEPRECATED |
143 | namespace posix = errc; |
144 | namespace posix_error = errc; |
145 | # endif |
146 | |
147 | template<> struct is_error_condition_enum<errc::errc_t> |
148 | { static const bool value = true; }; |
149 | |
150 | |
151 | // ----------------------------------------------------------------------// |
152 | |
153 | // Operating system specific interfaces --------------------------------// |
154 | |
155 | |
156 | // The interface is divided into general and system-specific portions to |
157 | // meet these requirements: |
158 | // |
159 | // * Code calling an operating system API can create an error_code with |
160 | // a single category (system_category), even for POSIX-like operating |
161 | // systems that return some POSIX errno values and some native errno |
162 | // values. This code should not have to pay the cost of distinguishing |
163 | // between categories, since it is not yet known if that is needed. |
164 | // |
165 | // * Users wishing to write system-specific code should be given enums for |
166 | // at least the common error cases. |
167 | // |
168 | // * System specific code should fail at compile time if moved to another |
169 | // operating system. |
170 | |
171 | // The system specific portions of the interface are located in headers |
172 | // with names reflecting the operating system. For example, |
173 | // |
174 | // <boost/system/cygwin_error.hpp> |
175 | // <boost/system/linux_error.hpp> |
176 | // <boost/system/windows_error.hpp> |
177 | // |
178 | // These headers are effectively empty for compiles on operating systems |
179 | // where they are not applicable. |
180 | |
181 | // ----------------------------------------------------------------------// |
182 | |
183 | // class error_category ------------------------------------------------// |
184 | |
185 | class error_category : public noncopyable |
186 | { |
187 | public: |
188 | virtual ~error_category(){} |
189 | |
190 | virtual const char * name() const BOOST_SYSTEM_NOEXCEPT = 0; |
191 | virtual std::string message( int ev ) const = 0; |
192 | inline virtual error_condition default_error_condition( int ev ) const BOOST_SYSTEM_NOEXCEPT; |
193 | inline virtual bool equivalent( int code, |
194 | const error_condition & condition ) const BOOST_SYSTEM_NOEXCEPT; |
195 | inline virtual bool equivalent( const error_code & code, |
196 | int condition ) const BOOST_SYSTEM_NOEXCEPT; |
197 | |
198 | bool operator==(const error_category & rhs) const BOOST_SYSTEM_NOEXCEPT { return this == &rhs; } |
199 | bool operator!=(const error_category & rhs) const BOOST_SYSTEM_NOEXCEPT { return this != &rhs; } |
200 | bool operator<( const error_category & rhs ) const BOOST_SYSTEM_NOEXCEPT |
201 | { |
202 | return std::less<const error_category*>()( this, &rhs ); |
203 | } |
204 | }; |
205 | |
206 | // predefined error categories -----------------------------------------// |
207 | |
208 | # ifdef BOOST_ERROR_CODE_HEADER_ONLY |
209 | inline const error_category & system_category() BOOST_SYSTEM_NOEXCEPT; |
210 | inline const error_category & generic_category() BOOST_SYSTEM_NOEXCEPT; |
211 | #else |
212 | BOOST_SYSTEM_DECL const error_category & system_category() BOOST_SYSTEM_NOEXCEPT; |
213 | BOOST_SYSTEM_DECL const error_category & generic_category() BOOST_SYSTEM_NOEXCEPT; |
214 | #endif |
215 | // deprecated synonyms --------------------------------------------------// |
216 | |
217 | # ifndef BOOST_SYSTEM_NO_DEPRECATED |
218 | inline const error_category & get_system_category() { return system_category(); } |
219 | inline const error_category & get_generic_category() { return generic_category(); } |
220 | inline const error_category & get_posix_category() { return generic_category(); } |
221 | static const error_category & posix_category = generic_category(); |
222 | static const error_category & errno_ecat = generic_category(); |
223 | static const error_category & native_ecat = system_category(); |
224 | # endif |
225 | |
226 | // class error_condition -----------------------------------------------// |
227 | |
228 | // error_conditions are portable, error_codes are system or library specific |
229 | |
230 | class error_condition |
231 | { |
232 | public: |
233 | |
234 | // constructors: |
235 | error_condition() BOOST_SYSTEM_NOEXCEPT : m_val(0), m_cat(&generic_category()) {} |
236 | error_condition( int val, const error_category & cat ) BOOST_SYSTEM_NOEXCEPT : m_val(val), m_cat(&cat) {} |
237 | |
238 | template <class ErrorConditionEnum> |
239 | error_condition(ErrorConditionEnum e, |
240 | typename boost::enable_if<is_error_condition_enum<ErrorConditionEnum> >::type* = 0) BOOST_SYSTEM_NOEXCEPT |
241 | { |
242 | *this = make_error_condition(e); |
243 | } |
244 | |
245 | // modifiers: |
246 | |
247 | void assign( int val, const error_category & cat ) BOOST_SYSTEM_NOEXCEPT |
248 | { |
249 | m_val = val; |
250 | m_cat = &cat; |
251 | } |
252 | |
253 | template<typename ErrorConditionEnum> |
254 | typename boost::enable_if<is_error_condition_enum<ErrorConditionEnum>, error_condition>::type & |
255 | operator=( ErrorConditionEnum val ) BOOST_SYSTEM_NOEXCEPT |
256 | { |
257 | *this = make_error_condition(val); |
258 | return *this; |
259 | } |
260 | |
261 | void clear() BOOST_SYSTEM_NOEXCEPT |
262 | { |
263 | m_val = 0; |
264 | m_cat = &generic_category(); |
265 | } |
266 | |
267 | // observers: |
268 | int value() const BOOST_SYSTEM_NOEXCEPT { return m_val; } |
269 | const error_category & category() const BOOST_SYSTEM_NOEXCEPT { return *m_cat; } |
270 | std::string message() const { return m_cat->message(ev: value()); } |
271 | |
272 | typedef void (*unspecified_bool_type)(); |
273 | static void unspecified_bool_true() {} |
274 | |
275 | operator unspecified_bool_type() const BOOST_SYSTEM_NOEXCEPT // true if error |
276 | { |
277 | return m_val == 0 ? 0 : unspecified_bool_true; |
278 | } |
279 | |
280 | bool operator!() const BOOST_SYSTEM_NOEXCEPT // true if no error |
281 | { |
282 | return m_val == 0; |
283 | } |
284 | |
285 | // relationals: |
286 | // the more symmetrical non-member syntax allows enum |
287 | // conversions work for both rhs and lhs. |
288 | inline friend bool operator==( const error_condition & lhs, |
289 | const error_condition & rhs ) BOOST_SYSTEM_NOEXCEPT |
290 | { |
291 | return lhs.m_cat == rhs.m_cat && lhs.m_val == rhs.m_val; |
292 | } |
293 | |
294 | inline friend bool operator<( const error_condition & lhs, |
295 | const error_condition & rhs ) BOOST_SYSTEM_NOEXCEPT |
296 | // the more symmetrical non-member syntax allows enum |
297 | // conversions work for both rhs and lhs. |
298 | { |
299 | return lhs.m_cat < rhs.m_cat |
300 | || (lhs.m_cat == rhs.m_cat && lhs.m_val < rhs.m_val); |
301 | } |
302 | |
303 | private: |
304 | int m_val; |
305 | const error_category * m_cat; |
306 | |
307 | }; |
308 | |
309 | // class error_code ----------------------------------------------------// |
310 | |
311 | // We want error_code to be a value type that can be copied without slicing |
312 | // and without requiring heap allocation, but we also want it to have |
313 | // polymorphic behavior based on the error category. This is achieved by |
314 | // abstract base class error_category supplying the polymorphic behavior, |
315 | // and error_code containing a pointer to an object of a type derived |
316 | // from error_category. |
317 | class error_code |
318 | { |
319 | public: |
320 | |
321 | // constructors: |
322 | error_code() BOOST_SYSTEM_NOEXCEPT : m_val(0), m_cat(&system_category()) {} |
323 | error_code( int val, const error_category & cat ) BOOST_SYSTEM_NOEXCEPT : m_val(val), m_cat(&cat) {} |
324 | |
325 | template <class ErrorCodeEnum> |
326 | error_code(ErrorCodeEnum e, |
327 | typename boost::enable_if<is_error_code_enum<ErrorCodeEnum> >::type* = 0) BOOST_SYSTEM_NOEXCEPT |
328 | { |
329 | *this = make_error_code(e); |
330 | } |
331 | |
332 | // modifiers: |
333 | void assign( int val, const error_category & cat ) BOOST_SYSTEM_NOEXCEPT |
334 | { |
335 | m_val = val; |
336 | m_cat = &cat; |
337 | } |
338 | |
339 | template<typename ErrorCodeEnum> |
340 | typename boost::enable_if<is_error_code_enum<ErrorCodeEnum>, error_code>::type & |
341 | operator=( ErrorCodeEnum val ) BOOST_SYSTEM_NOEXCEPT |
342 | { |
343 | *this = make_error_code(val); |
344 | return *this; |
345 | } |
346 | |
347 | void clear() BOOST_SYSTEM_NOEXCEPT |
348 | { |
349 | m_val = 0; |
350 | m_cat = &system_category(); |
351 | } |
352 | |
353 | // observers: |
354 | int value() const BOOST_SYSTEM_NOEXCEPT { return m_val; } |
355 | const error_category & category() const BOOST_SYSTEM_NOEXCEPT { return *m_cat; } |
356 | error_condition default_error_condition() const BOOST_SYSTEM_NOEXCEPT { return m_cat->default_error_condition(ev: value()); } |
357 | std::string message() const { return m_cat->message(ev: value()); } |
358 | |
359 | typedef void (*unspecified_bool_type)(); |
360 | static void unspecified_bool_true() {} |
361 | |
362 | operator unspecified_bool_type() const BOOST_SYSTEM_NOEXCEPT // true if error |
363 | { |
364 | return m_val == 0 ? 0 : unspecified_bool_true; |
365 | } |
366 | |
367 | bool operator!() const BOOST_SYSTEM_NOEXCEPT // true if no error |
368 | { |
369 | return m_val == 0; |
370 | } |
371 | |
372 | // relationals: |
373 | inline friend bool operator==( const error_code & lhs, |
374 | const error_code & rhs ) BOOST_SYSTEM_NOEXCEPT |
375 | // the more symmetrical non-member syntax allows enum |
376 | // conversions work for both rhs and lhs. |
377 | { |
378 | return lhs.m_cat == rhs.m_cat && lhs.m_val == rhs.m_val; |
379 | } |
380 | |
381 | inline friend bool operator<( const error_code & lhs, |
382 | const error_code & rhs ) BOOST_SYSTEM_NOEXCEPT |
383 | // the more symmetrical non-member syntax allows enum |
384 | // conversions work for both rhs and lhs. |
385 | { |
386 | return lhs.m_cat < rhs.m_cat |
387 | || (lhs.m_cat == rhs.m_cat && lhs.m_val < rhs.m_val); |
388 | } |
389 | |
390 | private: |
391 | int m_val; |
392 | const error_category * m_cat; |
393 | |
394 | }; |
395 | |
396 | // predefined error_code object used as "throw on error" tag |
397 | # ifndef BOOST_SYSTEM_NO_DEPRECATED |
398 | BOOST_SYSTEM_DECL extern error_code throws; |
399 | # endif |
400 | |
401 | // Moving from a "throws" object to a "throws" function without breaking |
402 | // existing code is a bit of a problem. The workaround is to place the |
403 | // "throws" function in namespace boost rather than namespace boost::system. |
404 | |
405 | } // namespace system |
406 | |
407 | namespace detail { inline system::error_code * throws() { return 0; } } |
408 | // Misuse of the error_code object is turned into a noisy failure by |
409 | // poisoning the reference. This particular implementation doesn't |
410 | // produce warnings or errors from popular compilers, is very efficient |
411 | // (as determined by inspecting generated code), and does not suffer |
412 | // from order of initialization problems. In practice, it also seems |
413 | // cause user function error handling implementation errors to be detected |
414 | // very early in the development cycle. |
415 | |
416 | inline system::error_code & throws() |
417 | { return *detail::throws(); } |
418 | |
419 | namespace system |
420 | { |
421 | // non-member functions ------------------------------------------------// |
422 | |
423 | inline bool operator!=( const error_code & lhs, |
424 | const error_code & rhs ) BOOST_SYSTEM_NOEXCEPT |
425 | { |
426 | return !(lhs == rhs); |
427 | } |
428 | |
429 | inline bool operator!=( const error_condition & lhs, |
430 | const error_condition & rhs ) BOOST_SYSTEM_NOEXCEPT |
431 | { |
432 | return !(lhs == rhs); |
433 | } |
434 | |
435 | inline bool operator==( const error_code & code, |
436 | const error_condition & condition ) BOOST_SYSTEM_NOEXCEPT |
437 | { |
438 | return code.category().equivalent( code: code.value(), condition ) |
439 | || condition.category().equivalent( code, condition: condition.value() ); |
440 | } |
441 | |
442 | inline bool operator!=( const error_code & lhs, |
443 | const error_condition & rhs ) BOOST_SYSTEM_NOEXCEPT |
444 | { |
445 | return !(lhs == rhs); |
446 | } |
447 | |
448 | inline bool operator==( const error_condition & condition, |
449 | const error_code & code ) BOOST_SYSTEM_NOEXCEPT |
450 | { |
451 | return condition.category().equivalent( code, condition: condition.value() ) |
452 | || code.category().equivalent( code: code.value(), condition ); |
453 | } |
454 | |
455 | inline bool operator!=( const error_condition & lhs, |
456 | const error_code & rhs ) BOOST_SYSTEM_NOEXCEPT |
457 | { |
458 | return !(lhs == rhs); |
459 | } |
460 | |
461 | // TODO: both of these may move elsewhere, but the LWG hasn't spoken yet. |
462 | |
463 | template <class charT, class traits> |
464 | inline std::basic_ostream<charT,traits>& |
465 | operator<< (std::basic_ostream<charT,traits>& os, error_code ec) |
466 | { |
467 | os << ec.category().name() << ':' << ec.value(); |
468 | return os; |
469 | } |
470 | |
471 | inline std::size_t hash_value( const error_code & ec ) |
472 | { |
473 | return static_cast<std::size_t>(ec.value()) |
474 | + reinterpret_cast<std::size_t>(&ec.category()); |
475 | } |
476 | |
477 | // make_* functions for errc::errc_t -----------------------------// |
478 | |
479 | namespace errc |
480 | { |
481 | // explicit conversion: |
482 | inline error_code make_error_code( errc_t e ) BOOST_SYSTEM_NOEXCEPT |
483 | { return error_code( e, generic_category() ); } |
484 | |
485 | // implicit conversion: |
486 | inline error_condition make_error_condition( errc_t e ) BOOST_SYSTEM_NOEXCEPT |
487 | { return error_condition( e, generic_category() ); } |
488 | } |
489 | |
490 | // error_category default implementation -------------------------------// |
491 | |
492 | error_condition error_category::default_error_condition( int ev ) const BOOST_SYSTEM_NOEXCEPT |
493 | { |
494 | return error_condition( ev, *this ); |
495 | } |
496 | |
497 | bool error_category::equivalent( int code, |
498 | const error_condition & condition ) const BOOST_SYSTEM_NOEXCEPT |
499 | { |
500 | return default_error_condition( ev: code ) == condition; |
501 | } |
502 | |
503 | bool error_category::equivalent( const error_code & code, |
504 | int condition ) const BOOST_SYSTEM_NOEXCEPT |
505 | { |
506 | return *this == code.category() && code.value() == condition; |
507 | } |
508 | |
509 | } // namespace system |
510 | } // namespace boost |
511 | |
512 | #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas |
513 | |
514 | # ifdef BOOST_ERROR_CODE_HEADER_ONLY |
515 | # include <boost/system/detail/error_code.ipp> |
516 | # endif |
517 | |
518 | #endif // BOOST_ERROR_CODE_HPP |
519 | |
520 | |
521 | |