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_ERROR_CODE_HPP
12 #define BOOST_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
43 class error_condition;
45 // "Concept" helpers ---------------------------------------------------//
48 struct is_error_code_enum { static const bool value = false; };
51 struct is_error_condition_enum { static const bool value = false; };
53 // generic error_conditions --------------------------------------------//
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,
67 bad_file_descriptor = EBADF,
68 bad_message = EBADMSG,
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,
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,
88 invalid_argument = EINVAL,
89 invalid_seek = ESPIPE,
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,
99 no_lock_available = ENOLCK,
100 no_message_available = ENODATA,
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
142 # ifndef BOOST_SYSTEM_NO_DEPRECATED
143 namespace posix = errc;
144 namespace posix_error = errc;
147 template<> struct is_error_condition_enum<errc::errc_t>
148 { static const bool value = true; };
151 // ----------------------------------------------------------------------//
153 // Operating system specific interfaces --------------------------------//
156 // The interface is divided into general and system-specific portions to
157 // meet these requirements:
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.
165 // * Users wishing to write system-specific code should be given enums for
166 // at least the common error cases.
168 // * System specific code should fail at compile time if moved to another
171 // The system specific portions of the interface are located in headers
172 // with names reflecting the operating system. For example,
174 // <boost/system/cygwin_error.hpp>
175 // <boost/system/linux_error.hpp>
176 // <boost/system/windows_error.hpp>
178 // These headers are effectively empty for compiles on operating systems
179 // where they are not applicable.
181 // ----------------------------------------------------------------------//
183 // class error_category ------------------------------------------------//
185 class error_category : public noncopyable
188 virtual ~error_category(){}
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;
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
202 return std::less<const error_category*>()( this, &rhs );
206 // predefined error categories -----------------------------------------//
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;
212 BOOST_SYSTEM_DECL const error_category & system_category() BOOST_SYSTEM_NOEXCEPT;
213 BOOST_SYSTEM_DECL const error_category & generic_category() BOOST_SYSTEM_NOEXCEPT;
215 // deprecated synonyms --------------------------------------------------//
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();
226 // class error_condition -----------------------------------------------//
228 // error_conditions are portable, error_codes are system or library specific
230 class error_condition
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) {}
238 template <class ErrorConditionEnum>
239 error_condition(ErrorConditionEnum e,
240 typename boost::enable_if<is_error_condition_enum<ErrorConditionEnum> >::type* = 0) BOOST_SYSTEM_NOEXCEPT
242 *this = make_error_condition(e);
247 void assign( int val, const error_category & cat ) BOOST_SYSTEM_NOEXCEPT
253 template<typename ErrorConditionEnum>
254 typename boost::enable_if<is_error_condition_enum<ErrorConditionEnum>, error_condition>::type &
255 operator=( ErrorConditionEnum val ) BOOST_SYSTEM_NOEXCEPT
257 *this = make_error_condition(val);
261 void clear() BOOST_SYSTEM_NOEXCEPT
264 m_cat = &generic_category();
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(value()); }
272 typedef void (*unspecified_bool_type)();
273 static void unspecified_bool_true() {}
275 operator unspecified_bool_type() const BOOST_SYSTEM_NOEXCEPT // true if error
277 return m_val == 0 ? 0 : unspecified_bool_true;
280 bool operator!() const BOOST_SYSTEM_NOEXCEPT // true if no error
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
291 return lhs.m_cat == rhs.m_cat && lhs.m_val == rhs.m_val;
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.
299 return lhs.m_cat < rhs.m_cat
300 || (lhs.m_cat == rhs.m_cat && lhs.m_val < rhs.m_val);
305 const error_category * m_cat;
309 // class error_code ----------------------------------------------------//
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.
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) {}
325 template <class ErrorCodeEnum>
326 error_code(ErrorCodeEnum e,
327 typename boost::enable_if<is_error_code_enum<ErrorCodeEnum> >::type* = 0) BOOST_SYSTEM_NOEXCEPT
329 *this = make_error_code(e);
333 void assign( int val, const error_category & cat ) BOOST_SYSTEM_NOEXCEPT
339 template<typename ErrorCodeEnum>
340 typename boost::enable_if<is_error_code_enum<ErrorCodeEnum>, error_code>::type &
341 operator=( ErrorCodeEnum val ) BOOST_SYSTEM_NOEXCEPT
343 *this = make_error_code(val);
347 void clear() BOOST_SYSTEM_NOEXCEPT
350 m_cat = &system_category();
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(value()); }
357 std::string message() const { return m_cat->message(value()); }
359 typedef void (*unspecified_bool_type)();
360 static void unspecified_bool_true() {}
362 operator unspecified_bool_type() const BOOST_SYSTEM_NOEXCEPT // true if error
364 return m_val == 0 ? 0 : unspecified_bool_true;
367 bool operator!() const BOOST_SYSTEM_NOEXCEPT // true if no error
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.
378 return lhs.m_cat == rhs.m_cat && lhs.m_val == rhs.m_val;
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.
386 return lhs.m_cat < rhs.m_cat
387 || (lhs.m_cat == rhs.m_cat && lhs.m_val < rhs.m_val);
392 const error_category * m_cat;
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;
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.
405 } // namespace system
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.
416 inline system::error_code & throws()
417 { return *detail::throws(); }
421 // non-member functions ------------------------------------------------//
423 inline bool operator!=( const error_code & lhs,
424 const error_code & rhs ) BOOST_SYSTEM_NOEXCEPT
426 return !(lhs == rhs);
429 inline bool operator!=( const error_condition & lhs,
430 const error_condition & rhs ) BOOST_SYSTEM_NOEXCEPT
432 return !(lhs == rhs);
435 inline bool operator==( const error_code & code,
436 const error_condition & condition ) BOOST_SYSTEM_NOEXCEPT
438 return code.category().equivalent( code.value(), condition )
439 || condition.category().equivalent( code, condition.value() );
442 inline bool operator!=( const error_code & lhs,
443 const error_condition & rhs ) BOOST_SYSTEM_NOEXCEPT
445 return !(lhs == rhs);
448 inline bool operator==( const error_condition & condition,
449 const error_code & code ) BOOST_SYSTEM_NOEXCEPT
451 return condition.category().equivalent( code, condition.value() )
452 || code.category().equivalent( code.value(), condition );
455 inline bool operator!=( const error_condition & lhs,
456 const error_code & rhs ) BOOST_SYSTEM_NOEXCEPT
458 return !(lhs == rhs);
461 // TODO: both of these may move elsewhere, but the LWG hasn't spoken yet.
463 template <class charT, class traits>
464 inline std::basic_ostream<charT,traits>&
465 operator<< (std::basic_ostream<charT,traits>& os, error_code ec)
467 os << ec.category().name() << ':' << ec.value();
471 inline std::size_t hash_value( const error_code & ec )
473 return static_cast<std::size_t>(ec.value())
474 + reinterpret_cast<std::size_t>(&ec.category());
477 // make_* functions for errc::errc_t -----------------------------//
481 // explicit conversion:
482 inline error_code make_error_code( errc_t e ) BOOST_SYSTEM_NOEXCEPT
483 { return error_code( e, generic_category() ); }
485 // implicit conversion:
486 inline error_condition make_error_condition( errc_t e ) BOOST_SYSTEM_NOEXCEPT
487 { return error_condition( e, generic_category() ); }
490 // error_category default implementation -------------------------------//
492 error_condition error_category::default_error_condition( int ev ) const BOOST_SYSTEM_NOEXCEPT
494 return error_condition( ev, *this );
497 bool error_category::equivalent( int code,
498 const error_condition & condition ) const BOOST_SYSTEM_NOEXCEPT
500 return default_error_condition( code ) == condition;
503 bool error_category::equivalent( const error_code & code,
504 int condition ) const BOOST_SYSTEM_NOEXCEPT
506 return *this == code.category() && code.value() == condition;
509 } // namespace system
512 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
514 # ifdef BOOST_ERROR_CODE_HEADER_ONLY
515 # include <boost/system/detail/error_code.ipp>
518 #endif // BOOST_ERROR_CODE_HPP