error_code.hpp 3.9 KB

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