error_code_checker.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * https://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * Copyright (c) 2023 Andrey Semashev
  7. */
  8. /*!
  9. * \file scope/error_code_checker.hpp
  10. *
  11. * This header contains definition of \c error_code_checker type.
  12. */
  13. #ifndef BOOST_SCOPE_ERROR_CODE_CHECKER_HPP_INCLUDED_
  14. #define BOOST_SCOPE_ERROR_CODE_CHECKER_HPP_INCLUDED_
  15. #include <boost/core/addressof.hpp>
  16. #include <boost/scope/detail/config.hpp>
  17. #include <boost/scope/detail/header.hpp>
  18. #ifdef BOOST_HAS_PRAGMA_ONCE
  19. #pragma once
  20. #endif
  21. namespace boost {
  22. namespace scope {
  23. /*!
  24. * \brief A predicate for checking whether an error code indicates error.
  25. *
  26. * The predicate captures a reference to an external error code object, which it
  27. * tests for an error indication when called. The error code object must remain
  28. * valid for the whole lifetime duration of the predicate.
  29. *
  30. * For an error code object `ec`, an expression `!ec` must be valid, never throw exceptions,
  31. * and return a value contextually convertible to `bool`. If the returned value converts
  32. * to `false`, then this is taken as an error indication, and the predicate returns `true`.
  33. * Otherwise, the predicate returns `false`.
  34. *
  35. * A few examples of error code types:
  36. *
  37. * \li `std::error_code` or `boost::system::error_code`,
  38. * \li `std::expected`, `boost::outcome_v2::basic_outcome` or `boost::outcome_v2::basic_result`,
  39. * \li `int`, where the value of 0 indicates no error,
  40. * \li `bool`, where the value of `false` indicates no error,
  41. * \li `T*`, where a null pointer indicates no error.
  42. *
  43. * \tparam ErrorCode Error code type.
  44. */
  45. template< typename ErrorCode >
  46. class error_code_checker
  47. {
  48. public:
  49. //! Predicate result type
  50. using result_type = bool;
  51. private:
  52. ErrorCode* m_error_code;
  53. public:
  54. /*!
  55. * \brief Constructs the predicate.
  56. *
  57. * Upon construction, the predicate saves a reference to the external error code object.
  58. * The referenced object must remain valid for the whole lifetime duration of the predicate.
  59. *
  60. * **Throws:** Nothing.
  61. */
  62. explicit error_code_checker(ErrorCode& ec) noexcept :
  63. m_error_code(boost::addressof(ec))
  64. {
  65. }
  66. /*!
  67. * \brief Checks if the error code indicates error.
  68. *
  69. * **Throws:** Nothing.
  70. *
  71. * \returns As if `!!ec`, where `ec` is the error code object passed to the predicate constructor.
  72. */
  73. result_type operator()() const noexcept
  74. {
  75. return !!(*m_error_code);
  76. }
  77. };
  78. /*!
  79. * \brief Creates a predicate for checking whether an exception is being thrown
  80. *
  81. * **Throws:** Nothing.
  82. */
  83. template< typename ErrorCode >
  84. inline error_code_checker< ErrorCode > check_error_code(ErrorCode& ec) noexcept
  85. {
  86. return error_code_checker< ErrorCode >(ec);
  87. }
  88. } // namespace scope
  89. } // namespace boost
  90. #include <boost/scope/detail/footer.hpp>
  91. #endif // BOOST_SCOPE_ERROR_CODE_CHECKER_HPP_INCLUDED_