1 // boost/system/error_code.hpp ---------------------------------------------//
\r
3 // Copyright Beman Dawes 2006, 2007
\r
4 // Copyright Christoper Kohlhoff 2007
\r
6 // Distributed under the Boost Software License, Version 1.0. (See accompanying
\r
7 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
9 // See library home page at http://www.boost.org/libs/system
\r
11 #ifndef BOOST_ERROR_CODE_HPP
\r
12 #define BOOST_ERROR_CODE_HPP
\r
14 #include <boost/system/config.hpp>
\r
15 #include <boost/cstdint.hpp>
\r
16 #include <boost/assert.hpp>
\r
17 #include <boost/operators.hpp>
\r
18 #include <boost/noncopyable.hpp>
\r
19 #include <boost/utility/enable_if.hpp>
\r
22 #include <stdexcept>
\r
23 #include <functional>
\r
25 // TODO: undef these macros if not already defined
\r
26 #include <boost/cerrno.hpp>
\r
28 #if !defined(BOOST_POSIX_API) && !defined(BOOST_WINDOWS_API)
\r
29 # error BOOST_POSIX_API or BOOST_WINDOWS_API must be defined
\r
32 #include <boost/config/abi_prefix.hpp> // must be the last #include
\r
40 class error_condition;
\r
42 // "Concept" helpers ---------------------------------------------------//
\r
45 struct is_error_code_enum { static const bool value = false; };
\r
48 struct is_error_condition_enum { static const bool value = false; };
\r
50 // generic error_conditions --------------------------------------------//
\r
57 address_family_not_supported = EAFNOSUPPORT,
\r
58 address_in_use = EADDRINUSE,
\r
59 address_not_available = EADDRNOTAVAIL,
\r
60 already_connected = EISCONN,
\r
61 argument_list_too_long = E2BIG,
\r
62 argument_out_of_domain = EDOM,
\r
63 bad_address = EFAULT,
\r
64 bad_file_descriptor = EBADF,
\r
65 bad_message = EBADMSG,
\r
66 broken_pipe = EPIPE,
\r
67 connection_aborted = ECONNABORTED,
\r
68 connection_already_in_progress = EALREADY,
\r
69 connection_refused = ECONNREFUSED,
\r
70 connection_reset = ECONNRESET,
\r
71 cross_device_link = EXDEV,
\r
72 destination_address_required = EDESTADDRREQ,
\r
73 device_or_resource_busy = EBUSY,
\r
74 directory_not_empty = ENOTEMPTY,
\r
75 executable_format_error = ENOEXEC,
\r
76 file_exists = EEXIST,
\r
77 file_too_large = EFBIG,
\r
78 filename_too_long = ENAMETOOLONG,
\r
79 function_not_supported = ENOSYS,
\r
80 host_unreachable = EHOSTUNREACH,
\r
81 identifier_removed = EIDRM,
\r
82 illegal_byte_sequence = EILSEQ,
\r
83 inappropriate_io_control_operation = ENOTTY,
\r
84 interrupted = EINTR,
\r
85 invalid_argument = EINVAL,
\r
86 invalid_seek = ESPIPE,
\r
88 is_a_directory = EISDIR,
\r
89 message_size = EMSGSIZE,
\r
90 network_down = ENETDOWN,
\r
91 network_reset = ENETRESET,
\r
92 network_unreachable = ENETUNREACH,
\r
93 no_buffer_space = ENOBUFS,
\r
94 no_child_process = ECHILD,
\r
96 no_lock_available = ENOLCK,
\r
97 no_message_available = ENODATA,
\r
98 no_message = ENOMSG,
\r
99 no_protocol_option = ENOPROTOOPT,
\r
100 no_space_on_device = ENOSPC,
\r
101 no_stream_resources = ENOSR,
\r
102 no_such_device_or_address = ENXIO,
\r
103 no_such_device = ENODEV,
\r
104 no_such_file_or_directory = ENOENT,
\r
105 no_such_process = ESRCH,
\r
106 not_a_directory = ENOTDIR,
\r
107 not_a_socket = ENOTSOCK,
\r
108 not_a_stream = ENOSTR,
\r
109 not_connected = ENOTCONN,
\r
110 not_enough_memory = ENOMEM,
\r
111 not_supported = ENOTSUP,
\r
112 operation_canceled = ECANCELED,
\r
113 operation_in_progress = EINPROGRESS,
\r
114 operation_not_permitted = EPERM,
\r
115 operation_not_supported = EOPNOTSUPP,
\r
116 operation_would_block = EWOULDBLOCK,
\r
117 owner_dead = EOWNERDEAD,
\r
118 permission_denied = EACCES,
\r
119 protocol_error = EPROTO,
\r
120 protocol_not_supported = EPROTONOSUPPORT,
\r
121 read_only_file_system = EROFS,
\r
122 resource_deadlock_would_occur = EDEADLK,
\r
123 resource_unavailable_try_again = EAGAIN,
\r
124 result_out_of_range = ERANGE,
\r
125 state_not_recoverable = ENOTRECOVERABLE,
\r
126 stream_timeout = ETIME,
\r
127 text_file_busy = ETXTBSY,
\r
128 timed_out = ETIMEDOUT,
\r
129 too_many_files_open_in_system = ENFILE,
\r
130 too_many_files_open = EMFILE,
\r
131 too_many_links = EMLINK,
\r
132 too_many_symbolic_link_levels = ELOOP,
\r
133 value_too_large = EOVERFLOW,
\r
134 wrong_protocol_type = EPROTOTYPE
\r
137 } // namespace errc
\r
139 # ifndef BOOST_SYSTEM_NO_DEPRECATED
\r
140 namespace posix = errc;
\r
141 namespace posix_error = errc;
\r
144 template<> struct is_error_condition_enum<errc::errc_t>
\r
145 { static const bool value = true; };
\r
148 // ----------------------------------------------------------------------//
\r
150 // Operating system specific interfaces --------------------------------//
\r
153 // The interface is divided into general and system-specific portions to
\r
154 // meet these requirements:
\r
156 // * Code calling an operating system API can create an error_code with
\r
157 // a single category (system_category), even for POSIX-like operating
\r
158 // systems that return some POSIX errno values and some native errno
\r
159 // values. This code should not have to pay the cost of distinguishing
\r
160 // between categories, since it is not yet known if that is needed.
\r
162 // * Users wishing to write system-specific code should be given enums for
\r
163 // at least the common error cases.
\r
165 // * System specific code should fail at compile time if moved to another
\r
166 // operating system.
\r
168 // The system specific portions of the interface are located in headers
\r
169 // with names reflecting the operating system. For example,
\r
171 // <boost/system/cygwin_error.hpp>
\r
172 // <boost/system/linux_error.hpp>
\r
173 // <boost/system/windows_error.hpp>
\r
175 // These headers are effectively empty for compiles on operating systems
\r
176 // where they are not applicable.
\r
178 // ----------------------------------------------------------------------//
\r
180 // class error_category ------------------------------------------------//
\r
182 class error_category : public noncopyable
\r
185 virtual ~error_category(){}
\r
187 virtual const char * name() const = 0;
\r
188 virtual std::string message( int ev ) const = 0;
\r
189 virtual error_condition default_error_condition( int ev ) const;
\r
190 virtual bool equivalent( int code,
\r
191 const error_condition & condition ) const;
\r
192 virtual bool equivalent( const error_code & code,
\r
193 int condition ) const;
\r
195 bool operator==(const error_category & rhs) const { return this == &rhs; }
\r
196 bool operator!=(const error_category & rhs) const { return this != &rhs; }
\r
197 bool operator<( const error_category & rhs ) const
\r
199 return std::less<const error_category*>()( this, &rhs );
\r
203 // predefined error categories -----------------------------------------//
\r
205 BOOST_SYSTEM_DECL const error_category & get_system_category();
\r
206 BOOST_SYSTEM_DECL const error_category & get_generic_category();
\r
208 static const error_category & system_category = get_system_category();
\r
209 static const error_category & generic_category = get_generic_category();
\r
211 # ifndef BOOST_SYSTEM_NO_DEPRECATED
\r
212 // deprecated synonyms
\r
213 inline const error_category & get_posix_category() { return get_generic_category(); }
\r
214 static const error_category & posix_category = get_generic_category();
\r
215 static const error_category & errno_ecat = get_generic_category();
\r
216 static const error_category & native_ecat = get_system_category();
\r
219 // class error_condition -----------------------------------------------//
\r
221 // error_conditions are portable, error_codes are system or library specific
\r
223 class error_condition
\r
228 error_condition() : m_val(0), m_cat(&get_generic_category()) {}
\r
229 error_condition( int val, const error_category & cat ) : m_val(val), m_cat(&cat) {}
\r
231 template <class ErrorConditionEnum>
\r
232 error_condition(ErrorConditionEnum e,
\r
233 typename boost::enable_if<is_error_condition_enum<ErrorConditionEnum> >::type* = 0)
\r
235 *this = make_error_condition(e);
\r
240 void assign( int val, const error_category & cat )
\r
246 template<typename ErrorConditionEnum>
\r
247 typename boost::enable_if<is_error_condition_enum<ErrorConditionEnum>, error_condition>::type &
\r
248 operator=( ErrorConditionEnum val )
\r
250 *this = make_error_condition(val);
\r
257 m_cat = &get_generic_category();
\r
261 int value() const { return m_val; }
\r
262 const error_category & category() const { return *m_cat; }
\r
263 std::string message() const { return m_cat->message(value()); }
\r
265 typedef void (*unspecified_bool_type)();
\r
266 static void unspecified_bool_true() {}
\r
268 operator unspecified_bool_type() const // true if error
\r
270 return m_val == 0 ? 0 : unspecified_bool_true;
\r
273 bool operator!() const // true if no error
\r
279 // the more symmetrical non-member syntax allows enum
\r
280 // conversions work for both rhs and lhs.
\r
281 inline friend bool operator==( const error_condition & lhs,
\r
282 const error_condition & rhs )
\r
284 return lhs.m_cat == rhs.m_cat && lhs.m_val == rhs.m_val;
\r
287 inline friend bool operator<( const error_condition & lhs,
\r
288 const error_condition & rhs )
\r
289 // the more symmetrical non-member syntax allows enum
\r
290 // conversions work for both rhs and lhs.
\r
292 return lhs.m_cat < rhs.m_cat
\r
293 || (lhs.m_cat == rhs.m_cat && lhs.m_val < rhs.m_val);
\r
298 const error_category * m_cat;
\r
302 // class error_code ----------------------------------------------------//
\r
304 // We want error_code to be a value type that can be copied without slicing
\r
305 // and without requiring heap allocation, but we also want it to have
\r
306 // polymorphic behavior based on the error category. This is achieved by
\r
307 // abstract base class error_category supplying the polymorphic behavior,
\r
308 // and error_code containing a pointer to an object of a type derived
\r
309 // from error_category.
\r
315 error_code() : m_val(0), m_cat(&get_system_category()) {}
\r
316 error_code( int val, const error_category & cat ) : m_val(val), m_cat(&cat) {}
\r
318 template <class ErrorCodeEnum>
\r
319 error_code(ErrorCodeEnum e,
\r
320 typename boost::enable_if<is_error_code_enum<ErrorCodeEnum> >::type* = 0)
\r
322 *this = make_error_code(e);
\r
326 void assign( int val, const error_category & cat )
\r
332 template<typename ErrorCodeEnum>
\r
333 typename boost::enable_if<is_error_code_enum<ErrorCodeEnum>, error_code>::type &
\r
334 operator=( ErrorCodeEnum val )
\r
336 *this = make_error_code(val);
\r
343 m_cat = &get_system_category();
\r
347 int value() const { return m_val; }
\r
348 const error_category & category() const { return *m_cat; }
\r
349 error_condition default_error_condition() const { return m_cat->default_error_condition(value()); }
\r
350 std::string message() const { return m_cat->message(value()); }
\r
352 typedef void (*unspecified_bool_type)();
\r
353 static void unspecified_bool_true() {}
\r
355 operator unspecified_bool_type() const // true if error
\r
357 return m_val == 0 ? 0 : unspecified_bool_true;
\r
360 bool operator!() const // true if no error
\r
366 inline friend bool operator==( const error_code & lhs,
\r
367 const error_code & rhs )
\r
368 // the more symmetrical non-member syntax allows enum
\r
369 // conversions work for both rhs and lhs.
\r
371 return lhs.m_cat == rhs.m_cat && lhs.m_val == rhs.m_val;
\r
374 inline friend bool operator<( const error_code & lhs,
\r
375 const error_code & rhs )
\r
376 // the more symmetrical non-member syntax allows enum
\r
377 // conversions work for both rhs and lhs.
\r
379 return lhs.m_cat < rhs.m_cat
\r
380 || (lhs.m_cat == rhs.m_cat && lhs.m_val < rhs.m_val);
\r
385 const error_category * m_cat;
\r
389 // predefined error_code object used as "throw on error" tag
\r
390 # ifndef BOOST_SYSTEM_NO_DEPRECATED
\r
391 BOOST_SYSTEM_DECL extern error_code throws;
\r
394 // Moving from a "throws" object to a "throws" function without breaking
\r
395 // existing code is a bit of a problem. The workaround is to place the
\r
396 // "throws" function in namespace boost rather than namespace boost::system.
\r
398 } // namespace system
\r
400 namespace detail { inline system::error_code * throws() { return 0; } }
\r
401 // Misuse of the error_code object is turned into a noisy failure by
\r
402 // poisoning the reference. This particular implementation doesn't
\r
403 // produce warnings or errors from popular compilers, is very efficient
\r
404 // (as determined by inspecting generated code), and does not suffer
\r
405 // from order of initialization problems. In practice, it also seems
\r
406 // cause user function error handling implementation errors to be detected
\r
407 // very early in the development cycle.
\r
409 inline system::error_code & throws()
\r
410 { return *detail::throws(); }
\r
414 // non-member functions ------------------------------------------------//
\r
416 inline bool operator!=( const error_code & lhs,
\r
417 const error_code & rhs )
\r
419 return !(lhs == rhs);
\r
422 inline bool operator!=( const error_condition & lhs,
\r
423 const error_condition & rhs )
\r
425 return !(lhs == rhs);
\r
428 inline bool operator==( const error_code & code,
\r
429 const error_condition & condition )
\r
431 return code.category().equivalent( code.value(), condition )
\r
432 || condition.category().equivalent( code, condition.value() );
\r
435 inline bool operator!=( const error_code & lhs,
\r
436 const error_condition & rhs )
\r
438 return !(lhs == rhs);
\r
441 inline bool operator==( const error_condition & condition,
\r
442 const error_code & code )
\r
444 return condition.category().equivalent( code, condition.value() )
\r
445 || code.category().equivalent( code.value(), condition );
\r
448 inline bool operator!=( const error_condition & lhs,
\r
449 const error_code & rhs )
\r
451 return !(lhs == rhs);
\r
454 // TODO: both of these may move elsewhere, but the LWG hasn't spoken yet.
\r
456 template <class charT, class traits>
\r
457 inline std::basic_ostream<charT,traits>&
\r
458 operator<< (std::basic_ostream<charT,traits>& os, error_code ec)
\r
460 os << ec.category().name() << ':' << ec.value();
\r
464 inline std::size_t hash_value( const error_code & ec )
\r
466 return static_cast<std::size_t>(ec.value())
\r
467 + reinterpret_cast<std::size_t>(&ec.category());
\r
470 // make_* functions for errc::errc_t -----------------------------//
\r
474 // explicit conversion:
\r
475 inline error_code make_error_code( errc_t e )
\r
476 { return error_code( e, get_generic_category() ); }
\r
478 // implicit conversion:
\r
479 inline error_condition make_error_condition( errc_t e )
\r
480 { return error_condition( e, get_generic_category() ); }
\r
483 // error_category default implementation -------------------------------//
\r
485 inline error_condition error_category::default_error_condition( int ev ) const
\r
487 return error_condition( ev, *this );
\r
490 inline bool error_category::equivalent( int code,
\r
491 const error_condition & condition ) const
\r
493 return default_error_condition( code ) == condition;
\r
496 inline bool error_category::equivalent( const error_code & code,
\r
497 int condition ) const
\r
499 return *this == code.category() && code.value() == condition;
\r
502 } // namespace system
\r
503 } // namespace boost
\r
505 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
\r
507 # ifdef BOOST_ERROR_CODE_HEADER_ONLY
\r
508 # include <boost/../libs/system/src/error_code.cpp>
\r
511 #endif // BOOST_ERROR_CODE_HPP
\r