basic_result_value_observers.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Value observers for a very simple basic_result type
  2. (C) 2017-2019 Niall Douglas <http://www.nedproductions.biz/> (2 commits)
  3. File Created: Oct 2017
  4. Boost Software License - Version 1.0 - August 17th, 2003
  5. Permission is hereby granted, free of charge, to any person or organization
  6. obtaining a copy of the software and accompanying documentation covered by
  7. this license (the "Software") to use, reproduce, display, distribute,
  8. execute, and transmit the Software, and to prepare derivative works of the
  9. Software, and to permit third-parties to whom the Software is furnished to
  10. do so, all subject to the following:
  11. The copyright notices in the Software and this entire statement, including
  12. the above license grant, this restriction and the following disclaimer,
  13. must be included in all copies of the Software, in whole or in part, and
  14. all derivative works of the Software, unless such copies or derivative
  15. works are solely in the form of machine-executable object code generated by
  16. a source language processor.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  20. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  21. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef BOOST_OUTCOME_RESULT_VALUE_OBSERVERS_HPP
  26. #define BOOST_OUTCOME_RESULT_VALUE_OBSERVERS_HPP
  27. #include "basic_result_storage.hpp"
  28. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  29. namespace detail
  30. {
  31. template <class Base, class R, class NoValuePolicy> class basic_result_value_observers : public Base
  32. {
  33. public:
  34. using value_type = R;
  35. using Base::Base;
  36. constexpr value_type &assume_value() & noexcept
  37. {
  38. NoValuePolicy::narrow_value_check(static_cast<basic_result_value_observers &>(*this));
  39. return this->_state._value; // NOLINT
  40. }
  41. constexpr const value_type &assume_value() const &noexcept
  42. {
  43. NoValuePolicy::narrow_value_check(static_cast<const basic_result_value_observers &>(*this));
  44. return this->_state._value; // NOLINT
  45. }
  46. constexpr value_type &&assume_value() && noexcept
  47. {
  48. NoValuePolicy::narrow_value_check(static_cast<basic_result_value_observers &&>(*this));
  49. return static_cast<value_type &&>(this->_state._value); // NOLINT
  50. }
  51. constexpr const value_type &&assume_value() const &&noexcept
  52. {
  53. NoValuePolicy::narrow_value_check(static_cast<const basic_result_value_observers &&>(*this));
  54. return static_cast<const value_type &&>(this->_state._value); // NOLINT
  55. }
  56. constexpr value_type &value() &
  57. {
  58. NoValuePolicy::wide_value_check(static_cast<basic_result_value_observers &>(*this));
  59. return this->_state._value; // NOLINT
  60. }
  61. constexpr const value_type &value() const &
  62. {
  63. NoValuePolicy::wide_value_check(static_cast<const basic_result_value_observers &>(*this));
  64. return this->_state._value; // NOLINT
  65. }
  66. constexpr value_type &&value() &&
  67. {
  68. NoValuePolicy::wide_value_check(static_cast<basic_result_value_observers &&>(*this));
  69. return static_cast<value_type &&>(this->_state._value); // NOLINT
  70. }
  71. constexpr const value_type &&value() const &&
  72. {
  73. NoValuePolicy::wide_value_check(static_cast<const basic_result_value_observers &&>(*this));
  74. return static_cast<const value_type &&>(this->_state._value); // NOLINT
  75. }
  76. };
  77. template <class Base, class NoValuePolicy> class basic_result_value_observers<Base, void, NoValuePolicy> : public Base
  78. {
  79. public:
  80. using Base::Base;
  81. constexpr void assume_value() const noexcept { NoValuePolicy::narrow_value_check(*this); }
  82. constexpr void value() const { NoValuePolicy::wide_value_check(*this); }
  83. };
  84. } // namespace detail
  85. BOOST_OUTCOME_V2_NAMESPACE_END
  86. #endif