Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
4.5KB

  1. //
  2. // error_code.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. #if defined(ASIO_HAS_STD_SYSTEM_ERROR)
  17. # include <system_error>
  18. #else // defined(ASIO_HAS_STD_SYSTEM_ERROR)
  19. # include <string>
  20. # include "asio/detail/noncopyable.hpp"
  21. # if !defined(ASIO_NO_IOSTREAM)
  22. # include <iosfwd>
  23. # endif // !defined(ASIO_NO_IOSTREAM)
  24. #endif // defined(ASIO_HAS_STD_SYSTEM_ERROR)
  25. #include "asio/detail/push_options.hpp"
  26. namespace asio {
  27. #if defined(ASIO_HAS_STD_SYSTEM_ERROR)
  28. typedef std::error_category error_category;
  29. #else // defined(ASIO_HAS_STD_SYSTEM_ERROR)
  30. /// Base class for all error categories.
  31. class error_category : private noncopyable
  32. {
  33. public:
  34. /// Destructor.
  35. virtual ~error_category()
  36. {
  37. }
  38. /// Returns a string naming the error gategory.
  39. virtual const char* name() const = 0;
  40. /// Returns a string describing the error denoted by @c value.
  41. virtual std::string message(int value) const = 0;
  42. /// Equality operator to compare two error categories.
  43. bool operator==(const error_category& rhs) const
  44. {
  45. return this == &rhs;
  46. }
  47. /// Inequality operator to compare two error categories.
  48. bool operator!=(const error_category& rhs) const
  49. {
  50. return !(*this == rhs);
  51. }
  52. };
  53. #endif // defined(ASIO_HAS_STD_SYSTEM_ERROR)
  54. /// Returns the error category used for the system errors produced by asio.
  55. extern ASIO_DECL const error_category& system_category();
  56. #if defined(ASIO_HAS_STD_SYSTEM_ERROR)
  57. typedef std::error_code error_code;
  58. #else // defined(ASIO_HAS_STD_SYSTEM_ERROR)
  59. /// Class to represent an error code value.
  60. class error_code
  61. {
  62. public:
  63. /// Default constructor.
  64. error_code()
  65. : value_(0),
  66. category_(&system_category())
  67. {
  68. }
  69. /// Construct with specific error code and category.
  70. error_code(int v, const error_category& c)
  71. : value_(v),
  72. category_(&c)
  73. {
  74. }
  75. /// Construct from an error code enum.
  76. template <typename ErrorEnum>
  77. error_code(ErrorEnum e)
  78. {
  79. *this = make_error_code(e);
  80. }
  81. /// Clear the error value to the default.
  82. void clear()
  83. {
  84. value_ = 0;
  85. category_ = &system_category();
  86. }
  87. /// Assign a new error value.
  88. void assign(int v, const error_category& c)
  89. {
  90. value_ = v;
  91. category_ = &c;
  92. }
  93. /// Get the error value.
  94. int value() const
  95. {
  96. return value_;
  97. }
  98. /// Get the error category.
  99. const error_category& category() const
  100. {
  101. return *category_;
  102. }
  103. /// Get the message associated with the error.
  104. std::string message() const
  105. {
  106. return category_->message(value_);
  107. }
  108. struct unspecified_bool_type_t
  109. {
  110. };
  111. typedef void (*unspecified_bool_type)(unspecified_bool_type_t);
  112. static void unspecified_bool_true(unspecified_bool_type_t) {}
  113. /// Operator returns non-null if there is a non-success error code.
  114. operator unspecified_bool_type() const
  115. {
  116. if (value_ == 0)
  117. return 0;
  118. else
  119. return &error_code::unspecified_bool_true;
  120. }
  121. /// Operator to test if the error represents success.
  122. bool operator!() const
  123. {
  124. return value_ == 0;
  125. }
  126. /// Equality operator to compare two error objects.
  127. friend bool operator==(const error_code& e1, const error_code& e2)
  128. {
  129. return e1.value_ == e2.value_ && e1.category_ == e2.category_;
  130. }
  131. /// Inequality operator to compare two error objects.
  132. friend bool operator!=(const error_code& e1, const error_code& e2)
  133. {
  134. return e1.value_ != e2.value_ || e1.category_ != e2.category_;
  135. }
  136. private:
  137. // The value associated with the error code.
  138. int value_;
  139. // The category associated with the error code.
  140. const error_category* category_;
  141. };
  142. # if !defined(ASIO_NO_IOSTREAM)
  143. /// Output an error code.
  144. template <typename Elem, typename Traits>
  145. std::basic_ostream<Elem, Traits>& operator<<(
  146. std::basic_ostream<Elem, Traits>& os, const error_code& ec)
  147. {
  148. os << ec.category().name() << ':' << ec.value();
  149. return os;
  150. }
  151. # endif // !defined(ASIO_NO_IOSTREAM)
  152. #endif // defined(ASIO_HAS_STD_SYSTEM_ERROR)
  153. } // namespace asio
  154. #include "asio/detail/pop_options.hpp"
  155. #if defined(ASIO_HEADER_ONLY)
  156. # include "asio/impl/error_code.ipp"
  157. #endif // defined(ASIO_HEADER_ONLY)
  158. #endif // ASIO_ERROR_CODE_HPP