catch_complete_invoke.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Created by Joachim on 16/04/2019.
  3. * Adapted from donated nonius code.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. // Invoke with a special case for void
  9. #ifndef TWOBLUECUBES_CATCH_DETAIL_COMPLETE_INVOKE_HPP_INCLUDED
  10. #define TWOBLUECUBES_CATCH_DETAIL_COMPLETE_INVOKE_HPP_INCLUDED
  11. #include "../../catch_enforce.h"
  12. #include "../../catch_meta.hpp"
  13. #include <type_traits>
  14. #include <utility>
  15. namespace Catch {
  16. namespace Benchmark {
  17. namespace Detail {
  18. template <typename T>
  19. struct CompleteType { using type = T; };
  20. template <>
  21. struct CompleteType<void> { struct type {}; };
  22. template <typename T>
  23. using CompleteType_t = typename CompleteType<T>::type;
  24. template <typename Result>
  25. struct CompleteInvoker {
  26. template <typename Fun, typename... Args>
  27. static Result invoke(Fun&& fun, Args&&... args) {
  28. return std::forward<Fun>(fun)(std::forward<Args>(args)...);
  29. }
  30. };
  31. template <>
  32. struct CompleteInvoker<void> {
  33. template <typename Fun, typename... Args>
  34. static CompleteType_t<void> invoke(Fun&& fun, Args&&... args) {
  35. std::forward<Fun>(fun)(std::forward<Args>(args)...);
  36. return {};
  37. }
  38. };
  39. // invoke and not return void :(
  40. template <typename Fun, typename... Args>
  41. CompleteType_t<FunctionReturnType<Fun, Args...>> complete_invoke(Fun&& fun, Args&&... args) {
  42. return CompleteInvoker<FunctionReturnType<Fun, Args...>>::invoke(std::forward<Fun>(fun), std::forward<Args>(args)...);
  43. }
  44. const std::string benchmarkErrorMsg = "a benchmark failed to run successfully";
  45. } // namespace Detail
  46. template <typename Fun>
  47. Detail::CompleteType_t<FunctionReturnType<Fun>> user_code(Fun&& fun) {
  48. CATCH_TRY{
  49. return Detail::complete_invoke(std::forward<Fun>(fun));
  50. } CATCH_CATCH_ALL{
  51. getResultCapture().benchmarkFailed(translateActiveException());
  52. CATCH_RUNTIME_ERROR(Detail::benchmarkErrorMsg);
  53. }
  54. }
  55. } // namespace Benchmark
  56. } // namespace Catch
  57. #endif // TWOBLUECUBES_CATCH_DETAIL_COMPLETE_INVOKE_HPP_INCLUDED