hooks.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Unit testing for outcomes
  2. (C) 2013-2019 Niall Douglas <http://www.nedproductions.biz/> (14 commits)
  3. Boost Software License - Version 1.0 - August 17th, 2003
  4. Permission is hereby granted, free of charge, to any person or organization
  5. obtaining a copy of the software and accompanying documentation covered by
  6. this license (the "Software") to use, reproduce, display, distribute,
  7. execute, and transmit the Software, and to prepare derivative works of the
  8. Software, and to permit third-parties to whom the Software is furnished to
  9. do so, all subject to the following:
  10. The copyright notices in the Software and this entire statement, including
  11. the above license grant, this restriction and the following disclaimer,
  12. must be included in all copies of the Software, in whole or in part, and
  13. all derivative works of the Software, unless such copies or derivative
  14. works are solely in the form of machine-executable object code generated by
  15. a source language processor.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  19. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  20. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. DEALINGS IN THE SOFTWARE.
  23. */
  24. #define _CRT_SECURE_NO_WARNINGS
  25. #include <boost/outcome/outcome.hpp>
  26. #include <boost/test/unit_test.hpp>
  27. #include <boost/test/unit_test_monitor.hpp>
  28. #include <iostream>
  29. namespace hook_test
  30. {
  31. using BOOST_OUTCOME_V2_NAMESPACE::in_place_type;
  32. // Use static storage to convey extended error info from result construction to outcome conversion
  33. static char extended_error_info[256];
  34. // Use the error_code type as the ADL bridge for the hooks by creating a type here
  35. struct error_code : public boost::system::error_code
  36. {
  37. using boost::system::error_code::error_code;
  38. error_code() = default;
  39. error_code(boost::system::error_code ec) // NOLINT
  40. : boost::system::error_code(ec)
  41. {
  42. }
  43. };
  44. // Localise result to using the local error_code so this namespace gets looked up for the hooks
  45. template <class R> using result = BOOST_OUTCOME_V2_NAMESPACE::result<R, error_code>;
  46. // Specialise the result construction hook for our localised result
  47. template <class U> constexpr inline void hook_result_construction(result<int> *res, U && /*unused*/) noexcept
  48. {
  49. // Write the value in the result into the static storage
  50. snprintf(extended_error_info, sizeof(extended_error_info), "%d", res->assume_value()); // NOLINT
  51. }
  52. template <class U> constexpr inline void hook_result_construction(result<std::string> *res, U && /*unused*/) noexcept
  53. {
  54. // Write the value in the result into the static storage
  55. snprintf(extended_error_info, sizeof(extended_error_info), "%s", res->assume_value().c_str()); // NOLINT
  56. }
  57. } // namespace hook_test
  58. BOOST_OUTCOME_AUTO_TEST_CASE(works_result_hooks, "Tests that you can hook result's construction")
  59. {
  60. using namespace hook_test;
  61. result<int> a(5);
  62. BOOST_CHECK(!strcmp(extended_error_info, "5")); // NOLINT
  63. result<std::string> b("niall");
  64. BOOST_CHECK(!strcmp(extended_error_info, "niall")); // NOLINT
  65. }
  66. //! [extended_error_coding2]
  67. namespace hook_test
  68. {
  69. // Localise outcome to using the local error_code so this namespace gets looked up for the hooks
  70. template <class R> using outcome = BOOST_OUTCOME_V2_NAMESPACE::outcome<R, error_code, std::string>;
  71. // Specialise the outcome copy and move conversion hook for our localised result
  72. template <class T, class U> constexpr inline void hook_outcome_copy_construction(outcome<T> *res, const result<U> & /*unused*/) noexcept
  73. {
  74. // when copy constructing from a result<T>, place extended_error_coding::extended_error_info into the payload
  75. std::cout << "hook_outcome_copy_construction fires" << std::endl;
  76. BOOST_OUTCOME_V2_NAMESPACE::hooks::override_outcome_exception(res, extended_error_info);
  77. }
  78. template <class T, class U> constexpr inline void hook_outcome_move_construction(outcome<T> *res, result<U> && /*unused*/) noexcept
  79. {
  80. // when move constructing from a result<T>, place extended_error_coding::extended_error_info into the payload
  81. std::cout << "hook_outcome_move_construction fires" << std::endl;
  82. BOOST_OUTCOME_V2_NAMESPACE::hooks::override_outcome_exception(res, extended_error_info);
  83. }
  84. } // namespace hook_test
  85. BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_hooks, "Tests that you can hook outcome's conversion from a result")
  86. {
  87. using namespace hook_test;
  88. outcome<int> a(result<int>(5));
  89. BOOST_REQUIRE(a.has_exception()); // NOLINT
  90. BOOST_CHECK(a.exception() == "5");
  91. outcome<std::string> b(result<std::string>("niall"));
  92. BOOST_CHECK(b.exception() == "niall");
  93. // Make sure hook does not fire for any other kind of outcome copy or move, only when converting from our custom result only
  94. outcome<int> c(5);
  95. outcome<long> d(c); // can't be the same type as source, else copy elision takes place and no ADL hook calling
  96. BOOST_CHECK(!d.has_exception());
  97. outcome<int> e(BOOST_OUTCOME_V2_NAMESPACE::result<int>(5));
  98. BOOST_CHECK(!e.has_exception());
  99. }