1 // boost/system/error_code.hpp ---------------------------------------------//
3 // Copyright Beman Dawes 2006, 2007
4 // Copyright Christoper Kohlhoff 2007
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)
9 // See library home page at http://www.boost.org/libs/system
11 #ifndef BOOST_SYSTEM_ERROR_CODE_HPP
12 #define BOOST_SYSTEM_ERROR_CODE_HPP
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>
24 // TODO: undef these macros if not already defined
25 #include <boost/cerrno.hpp>
27 #if !defined(BOOST_POSIX_API) && !defined(BOOST_WINDOWS_API)
28 # error BOOST_POSIX_API or BOOST_WINDOWS_API must be defined
31 #include <boost/config/abi_prefix.hpp> // must be the last #include
33 #ifndef BOOST_SYSTEM_NOEXCEPT
34 #define BOOST_SYSTEM_NOEXCEPT BOOST_NOEXCEPT
42 class error_code; // values defined by the operating system
43 class error_condition; // portable generic values defined below, but ultimately
44 // based on the POSIX standard
46 // "Concept" helpers ---------------------------------------------------//
49 struct is_error_code_enum { static const bool value = false; };
52 struct is_error_condition_enum { static const bool value = false; };
54 // generic error_conditions --------------------------------------------//
61 address_family_not_supported = EAFNOSUPPORT,
62 address_in_use = EADDRINUSE,
63 address_not_available = EADDRNOTAVAIL,
64 already_connected = EISCONN,
65 argument_list_too_long = E2BIG,
66 argument_out_of_domain = EDOM,
68 bad_file_descriptor = EBADF,
69 bad_message = EBADMSG,
71 connection_aborted = ECONNABORTED,
72 connection_already_in_progress = EALREADY,
73 connection_refused = ECONNREFUSED,
74 connection_reset = ECONNRESET,
75 cross_device_link = EXDEV,
76 destination_address_required = EDESTADDRREQ,
77 device_or_resource_busy = EBUSY,
78 directory_not_empty = ENOTEMPTY,
79 executable_format_error = ENOEXEC,
81 file_too_large = EFBIG,
82 filename_too_long = ENAMETOOLONG,
83 function_not_supported = ENOSYS,
84 host_unreachable = EHOSTUNREACH,
85 identifier_removed = EIDRM,
86 illegal_byte_sequence = EILSEQ,
87 inappropriate_io_control_operation = ENOTTY,
89 invalid_argument = EINVAL,
90 invalid_seek = ESPIPE,
92 is_a_directory = EISDIR,
93 message_size = EMSGSIZE,
94 network_down = ENETDOWN,
95 network_reset = ENETRESET,
96 network_unreachable = ENETUNREACH,
97 no_buffer_space = ENOBUFS,
98 no_child_process = ECHILD,
100 no_lock_available = ENOLCK,
101 no_message_available = ENODATA,
103 no_protocol_option = ENOPROTOOPT,
104 no_space_on_device = ENOSPC,
105 no_stream_resources = ENOSR,
106 no_such_device_or_address = ENXIO,
107 no_such_device = ENODEV,
108 no_such_file_or_directory = ENOENT,
109 no_such_process = ESRCH,
110 not_a_directory = ENOTDIR,
111 not_a_socket = ENOTSOCK,
112 not_a_stream = ENOSTR,
113 not_connected = ENOTCONN,
114 not_enough_memory = ENOMEM,
115 not_supported = ENOTSUP,
116 operation_canceled = ECANCELED,
117 operation_in_progress = EINPROGRESS,
118 operation_not_permitted = EPERM,
119 operation_not_supported = EOPNOTSUPP,
120 operation_would_block = EWOULDBLOCK,
121 owner_dead = EOWNERDEAD,
122 permission_denied = EACCES,
123 protocol_error = EPROTO,
124 protocol_not_supported = EPROTONOSUPPORT,
125 read_only_file_system = EROFS,
126 resource_deadlock_would_occur = EDEADLK,
127 resource_unavailable_try_again = EAGAIN,
128 result_out_of_range = ERANGE,
129 state_not_recoverable = ENOTRECOVERABLE,
130 stream_timeout = ETIME,
131 text_file_busy = ETXTBSY,
132 timed_out = ETIMEDOUT,
133 too_many_files_open_in_system = ENFILE,
134 too_many_files_open = EMFILE,
135 too_many_links = EMLINK,
136 too_many_symbolic_link_levels = ELOOP,
137 value_too_large = EOVERFLOW,
138 wrong_protocol_type = EPROTOTYPE
143 # ifndef BOOST_SYSTEM_NO_DEPRECATED
144 namespace posix = errc;
145 namespace posix_error = errc;
148 template<> struct is_error_condition_enum<errc::errc_t>
149 { static const bool value = true; };
152 // ----------------------------------------------------------------------//
154 // Operating system specific interfaces --------------------------------//
157 // The interface is divided into general and system-specific portions to
158 // meet these requirements:
160 // * Code calling an operating system API can create an error_code with
161 // a single category (system_category), even for POSIX-like operating
162 // systems that return some POSIX errno values and some native errno
163 // values. This code should not have to pay the cost of distinguishing
164 // between categories, since it is not yet known if that is needed.
166 // * Users wishing to write system-specific code should be given enums for
167 // at least the common error cases.
169 // * System specific code should fail at compile time if moved to another
172 // The system specific portions of the interface are located in headers
173 // with names reflecting the operating system. For example,
175 // <boost/system/cygwin_error.hpp>
176 // <boost/system/linux_error.hpp>
177 // <boost/system/windows_error.hpp>
179 // These headers are effectively empty for compiles on operating systems
180 // where they are not applicable.
182 // ----------------------------------------------------------------------//
184 // class error_category ------------------------------------------------//
186 class error_category : public noncopyable
189 virtual ~error_category(){}
191 virtual const char * name() const BOOST_SYSTEM_NOEXCEPT = 0;
192 virtual std::string message( int ev ) const = 0;
193 inline virtual error_condition default_error_condition( int ev ) const BOOST_SYSTEM_NOEXCEPT;
194 inline virtual bool equivalent( int code,
195 const error_condition & condition ) const BOOST_SYSTEM_NOEXCEPT;
196 inline virtual bool equivalent( const error_code & code,
197 int condition ) const BOOST_SYSTEM_NOEXCEPT;
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 { return this != &rhs; }
201 bool operator<( const error_category & rhs ) const BOOST_SYSTEM_NOEXCEPT
203 return std::less<const error_category*>()( this, &rhs );
207 // predefined error categories -----------------------------------------//
209 # ifdef BOOST_ERROR_CODE_HEADER_ONLY
210 inline const error_category & system_category() BOOST_SYSTEM_NOEXCEPT;
211 inline const error_category & generic_category() BOOST_SYSTEM_NOEXCEPT;
213 BOOST_SYSTEM_DECL const error_category & system_category() BOOST_SYSTEM_NOEXCEPT;
214 BOOST_SYSTEM_DECL const error_category & generic_category() BOOST_SYSTEM_NOEXCEPT;
216 // deprecated synonyms --------------------------------------------------//
218 # ifndef BOOST_SYSTEM_NO_DEPRECATED
219 inline const error_category & get_system_category() { return system_category(); }
220 inline const error_category & get_generic_category() { return generic_category(); }
221 inline const error_category & get_posix_category() { return generic_category(); }
222 static const error_category & posix_category BOOST_ATTRIBUTE_UNUSED = generic_category();
223 static const error_category & errno_ecat BOOST_ATTRIBUTE_UNUSED = generic_category();
224 static const error_category & native_ecat BOOST_ATTRIBUTE_UNUSED = system_category();
227 // class error_condition -----------------------------------------------//
229 // error_conditions are portable, error_codes are system or library specific
231 class error_condition
236 error_condition() BOOST_SYSTEM_NOEXCEPT : m_val(0), m_cat(&generic_category()) {}
237 error_condition( int val, const error_category & cat ) BOOST_SYSTEM_NOEXCEPT : m_val(val), m_cat(&cat) {}
239 template <class ErrorConditionEnum>
240 error_condition(ErrorConditionEnum e,
241 typename boost::enable_if<is_error_condition_enum<ErrorConditionEnum> >::type* = 0) BOOST_SYSTEM_NOEXCEPT
243 *this = make_error_condition(e);
248 void assign( int val, const error_category & cat ) BOOST_SYSTEM_NOEXCEPT
254 template<typename ErrorConditionEnum>
255 typename boost::enable_if<is_error_condition_enum<ErrorConditionEnum>, error_condition>::type &
256 operator=( ErrorConditionEnum val ) BOOST_SYSTEM_NOEXCEPT
258 *this = make_error_condition(val);
262 void clear() BOOST_SYSTEM_NOEXCEPT
265 m_cat = &generic_category();
269 int value() const BOOST_SYSTEM_NOEXCEPT { return m_val; }
270 const error_category & category() const BOOST_SYSTEM_NOEXCEPT { return *m_cat; }
271 std::string message() const { return m_cat->message(value()); }
273 typedef void (*unspecified_bool_type)();
274 static void unspecified_bool_true() {}
276 operator unspecified_bool_type() const BOOST_SYSTEM_NOEXCEPT // true if error
278 return m_val == 0 ? 0 : unspecified_bool_true;
281 bool operator!() const BOOST_SYSTEM_NOEXCEPT // true if no error
287 // the more symmetrical non-member syntax allows enum
288 // conversions work for both rhs and lhs.
289 inline friend bool operator==( const error_condition & lhs,
290 const error_condition & rhs ) BOOST_SYSTEM_NOEXCEPT
292 return lhs.m_cat == rhs.m_cat && lhs.m_val == rhs.m_val;
295 inline friend bool operator<( const error_condition & lhs,
296 const error_condition & rhs ) BOOST_SYSTEM_NOEXCEPT
297 // the more symmetrical non-member syntax allows enum
298 // conversions work for both rhs and lhs.
300 return lhs.m_cat < rhs.m_cat
301 || (lhs.m_cat == rhs.m_cat && lhs.m_val < rhs.m_val);
306 const error_category * m_cat;
310 // class error_code ----------------------------------------------------//
312 // We want error_code to be a value type that can be copied without slicing
313 // and without requiring heap allocation, but we also want it to have
314 // polymorphic behavior based on the error category. This is achieved by
315 // abstract base class error_category supplying the polymorphic behavior,
316 // and error_code containing a pointer to an object of a type derived
317 // from error_category.
323 error_code() BOOST_SYSTEM_NOEXCEPT : m_val(0), m_cat(&system_category()) {}
324 error_code( int val, const error_category & cat ) BOOST_SYSTEM_NOEXCEPT : m_val(val), m_cat(&cat) {}
326 template <class ErrorCodeEnum>
327 error_code(ErrorCodeEnum e,
328 typename boost::enable_if<is_error_code_enum<ErrorCodeEnum> >::type* = 0) BOOST_SYSTEM_NOEXCEPT
330 *this = make_error_code(e);
334 void assign( int val, const error_category & cat ) BOOST_SYSTEM_NOEXCEPT
340 template<typename ErrorCodeEnum>
341 typename boost::enable_if<is_error_code_enum<ErrorCodeEnum>, error_code>::type &
342 operator=( ErrorCodeEnum val ) BOOST_SYSTEM_NOEXCEPT
344 *this = make_error_code(val);
348 void clear() BOOST_SYSTEM_NOEXCEPT
351 m_cat = &system_category();
355 int value() const BOOST_SYSTEM_NOEXCEPT { return m_val; }
356 const error_category & category() const BOOST_SYSTEM_NOEXCEPT { return *m_cat; }
357 error_condition default_error_condition() const BOOST_SYSTEM_NOEXCEPT { return m_cat->default_error_condition(value()); }
358 std::string message() const { return m_cat->message(value()); }
360 typedef void (*unspecified_bool_type)();
361 static void unspecified_bool_true() {}
363 operator unspecified_bool_type() const BOOST_SYSTEM_NOEXCEPT // true if error
365 return m_val == 0 ? 0 : unspecified_bool_true;
368 bool operator!() const BOOST_SYSTEM_NOEXCEPT // true if no error
374 inline friend bool operator==( const error_code & lhs,
375 const error_code & rhs ) BOOST_SYSTEM_NOEXCEPT
376 // the more symmetrical non-member syntax allows enum
377 // conversions work for both rhs and lhs.
379 return lhs.m_cat == rhs.m_cat && lhs.m_val == rhs.m_val;
382 inline friend bool operator<( const error_code & lhs,
383 const error_code & rhs ) BOOST_SYSTEM_NOEXCEPT
384 // the more symmetrical non-member syntax allows enum
385 // conversions work for both rhs and lhs.
387 return lhs.m_cat < rhs.m_cat
388 || (lhs.m_cat == rhs.m_cat && lhs.m_val < rhs.m_val);
393 const error_category * m_cat;
397 // predefined error_code object used as "throw on error" tag
398 # ifndef BOOST_SYSTEM_NO_DEPRECATED
399 BOOST_SYSTEM_DECL extern error_code throws;
402 // Moving from a "throws" object to a "throws" function without breaking
403 // existing code is a bit of a problem. The workaround is to place the
404 // "throws" function in namespace boost rather than namespace boost::system.
406 } // namespace system
408 namespace detail { inline system::error_code * throws() { return 0; } }
409 // Misuse of the error_code object is turned into a noisy failure by
410 // poisoning the reference. This particular implementation doesn't
411 // produce warnings or errors from popular compilers, is very efficient
412 // (as determined by inspecting generated code), and does not suffer
413 // from order of initialization problems. In practice, it also seems
414 // cause user function error handling implementation errors to be detected
415 // very early in the development cycle.
417 inline system::error_code & throws()
418 { return *detail::throws(); }
422 // non-member functions ------------------------------------------------//
424 inline bool operator!=( const error_code & lhs,
425 const error_code & rhs ) BOOST_SYSTEM_NOEXCEPT
427 return !(lhs == rhs);
430 inline bool operator!=( const error_condition & lhs,
431 const error_condition & rhs ) BOOST_SYSTEM_NOEXCEPT
433 return !(lhs == rhs);
436 inline bool operator==( const error_code & code,
437 const error_condition & condition ) BOOST_SYSTEM_NOEXCEPT
439 return code.category().equivalent( code.value(), condition )
440 || condition.category().equivalent( code, condition.value() );
443 inline bool operator!=( const error_code & lhs,
444 const error_condition & rhs ) BOOST_SYSTEM_NOEXCEPT
446 return !(lhs == rhs);
449 inline bool operator==( const error_condition & condition,
450 const error_code & code ) BOOST_SYSTEM_NOEXCEPT
452 return condition.category().equivalent( code, condition.value() )
453 || code.category().equivalent( code.value(), condition );
456 inline bool operator!=( const error_condition & lhs,
457 const error_code & rhs ) BOOST_SYSTEM_NOEXCEPT
459 return !(lhs == rhs);
462 // TODO: both of these may move elsewhere, but the LWG hasn't spoken yet.
464 template <class charT, class traits>
465 inline std::basic_ostream<charT,traits>&
466 operator<< (std::basic_ostream<charT,traits>& os, error_code ec)
468 os << ec.category().name() << ':' << ec.value();
472 inline std::size_t hash_value( const error_code & ec )
474 return static_cast<std::size_t>(ec.value())
475 + reinterpret_cast<std::size_t>(&ec.category());
478 // make_* functions for errc::errc_t -----------------------------//
482 // explicit conversion:
483 inline error_code make_error_code( errc_t e ) BOOST_SYSTEM_NOEXCEPT
484 { return error_code( e, generic_category() ); }
486 // implicit conversion:
487 inline error_condition make_error_condition( errc_t e ) BOOST_SYSTEM_NOEXCEPT
488 { return error_condition( e, generic_category() ); }
491 // error_category default implementation -------------------------------//
493 error_condition error_category::default_error_condition( int ev ) const BOOST_SYSTEM_NOEXCEPT
495 return error_condition( ev, *this );
498 bool error_category::equivalent( int code,
499 const error_condition & condition ) const BOOST_SYSTEM_NOEXCEPT
501 return default_error_condition( code ) == condition;
504 bool error_category::equivalent( const error_code & code,
505 int condition ) const BOOST_SYSTEM_NOEXCEPT
507 return *this == code.category() && code.value() == condition;
510 } // namespace system
513 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
515 # ifdef BOOST_ERROR_CODE_HEADER_ONLY
516 # include <boost/system/detail/error_code.ipp>
519 #endif // BOOST_SYSTEM_ERROR_CODE_HPP