error_code.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // error_code.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_ERROR_CODE_HPP
  11. #define ASIO_ERROR_CODE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <string>
  17. #if defined(GENERATING_DOCUMENTATION)
  18. # define ASIO_WIN_OR_POSIX(e_win, e_posix) implementation_defined
  19. #elif defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  20. # define ASIO_WIN_OR_POSIX(e_win, e_posix) e_win
  21. #else
  22. # define ASIO_WIN_OR_POSIX(e_win, e_posix) e_posix
  23. #endif
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. namespace error
  27. {
  28. /// Available error code categories.
  29. enum error_category
  30. {
  31. /// System error codes.
  32. system_category = ASIO_WIN_OR_POSIX(0, 0),
  33. /// Error codes from NetDB functions.
  34. netdb_category = ASIO_WIN_OR_POSIX(system_category, 1),
  35. /// Error codes from getaddrinfo.
  36. addrinfo_category = ASIO_WIN_OR_POSIX(system_category, 2),
  37. /// Miscellaneous error codes.
  38. misc_category = ASIO_WIN_OR_POSIX(3, 3),
  39. /// SSL error codes.
  40. ssl_category = ASIO_WIN_OR_POSIX(4, 4)
  41. };
  42. // Category getters.
  43. inline error_category get_system_category() { return system_category; }
  44. inline error_category get_netdb_category() { return netdb_category; }
  45. inline error_category get_addrinfo_category() { return addrinfo_category; }
  46. inline error_category get_misc_category() { return misc_category; }
  47. inline error_category get_ssl_category() { return ssl_category; }
  48. } // namespace error
  49. /// Bring error category type into the asio namespace.
  50. typedef asio::error::error_category error_category;
  51. /// Class to represent an error code value.
  52. class error_code
  53. {
  54. public:
  55. /// The underlying representation of an error code.
  56. typedef int value_type;
  57. /// Default constructor.
  58. error_code()
  59. : value_(0),
  60. category_(error::system_category)
  61. {
  62. }
  63. /// Construct with specific error code and category.
  64. error_code(value_type v, error_category c)
  65. : value_(v),
  66. category_(c)
  67. {
  68. }
  69. /// Construct from an error code enum.
  70. template <typename ErrorEnum>
  71. error_code(ErrorEnum e)
  72. {
  73. *this = make_error_code(e);
  74. }
  75. /// Get the error value.
  76. value_type value() const
  77. {
  78. return value_;
  79. }
  80. /// Get the error category.
  81. error_category category() const
  82. {
  83. return category_;
  84. }
  85. /// Get the message associated with the error.
  86. ASIO_DECL std::string message() const;
  87. struct unspecified_bool_type_t
  88. {
  89. };
  90. typedef void (*unspecified_bool_type)(unspecified_bool_type_t);
  91. static void unspecified_bool_true(unspecified_bool_type_t) {}
  92. /// Operator returns non-null if there is a non-success error code.
  93. operator unspecified_bool_type() const
  94. {
  95. if (!value_)
  96. return 0;
  97. else
  98. return &error_code::unspecified_bool_true;
  99. }
  100. /// Operator to test if the error represents success.
  101. bool operator!() const
  102. {
  103. return !value_;
  104. }
  105. /// Equality operator to compare two error objects.
  106. friend bool operator==(const error_code& e1, const error_code& e2)
  107. {
  108. return e1.value_ == e2.value_ && e1.category_ == e2.category_;
  109. }
  110. /// Inequality operator to compare two error objects.
  111. friend bool operator!=(const error_code& e1, const error_code& e2)
  112. {
  113. return e1.value_ != e2.value_ || e1.category_ != e2.category_;
  114. }
  115. private:
  116. // The value associated with the error code.
  117. value_type value_;
  118. // The category associated with the error code.
  119. error_category category_;
  120. };
  121. } // namespace asio
  122. #include "asio/detail/pop_options.hpp"
  123. #undef ASIO_WIN_OR_POSIX
  124. #if defined(ASIO_HEADER_ONLY)
  125. # include "asio/impl/error_code.ipp"
  126. #endif // defined(ASIO_HEADER_ONLY)
  127. #endif // ASIO_ERROR_CODE_HPP