mimic++ v10.0.1
Loading...
Searching...
No Matches
GlobalReporter.hpp
Go to the documentation of this file.
1// Copyright Dominic (DNKpp) Koepke 2024 - 2026.
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#ifndef MIMICPP_REPORTING_GLOBAL_REPORTER_HPP
7#define MIMICPP_REPORTING_GLOBAL_REPORTER_HPP
8
9#pragma once
10
11#include "mimic++/Fwd.hpp"
18
19#ifndef MIMICPP_DETAIL_IS_MODULE
20 #include <exception>
21 #include <iostream>
22 #include <memory>
23 #include <utility>
24 #include <vector>
25#endif
26
27namespace mimicpp::reporting::detail
28{
29#ifdef __cpp_lib_atomic_shared_ptr
30
31 [[nodiscard]]
32 inline std::atomic<std::shared_ptr<IReporter>>& reporter() noexcept
33 {
34 static std::atomic<std::shared_ptr<IReporter>> reporter{
35 std::make_shared<DefaultReporter>(std::cerr)};
36 return reporter;
37 }
38
39 inline void set_reporter(std::shared_ptr<IReporter> newReporter) noexcept
40 {
41 reporter().store(std::move(newReporter));
42 }
43
44 [[nodiscard]]
45 inline std::shared_ptr<IReporter> get_reporter() noexcept
46 {
47 return reporter().load();
48 }
49
50#else // libc++ doesn't support std::atomic<std::shared_ptr> yet, so fallback to older free-functions approach instead
51
52 [[nodiscard]]
53 inline std::shared_ptr<IReporter>* reporter() noexcept
54 {
55 static std::shared_ptr<IReporter> reporter{
56 std::make_shared<DefaultReporter>(std::cerr)};
57 return std::addressof(reporter);
58 }
59
60 inline void set_reporter(std::shared_ptr<IReporter> newReporter)
61 {
62 std::atomic_store(reporter(), std::move(newReporter));
63 }
64
65 [[nodiscard]]
66 inline std::shared_ptr<IReporter> get_reporter() noexcept
67 {
68 return std::atomic_load(reporter());
69 }
70
71#endif
72
73 [[noreturn]]
74 inline void report_no_matches(CallReport callReport, std::vector<MatchReport> noMatchReports)
75 {
76 get_reporter()
77 ->report_no_matches(std::move(callReport), std::move(noMatchReports));
78
79 // GCOVR_EXCL_START
80 // ReSharper disable once CppDFAUnreachableCode
82 // GCOVR_EXCL_STOP
83 }
84
85 [[noreturn]]
86 inline void report_inapplicable_matches(CallReport callReport, std::vector<MatchReport> reports)
87 {
88 get_reporter()
89 ->report_inapplicable_matches(std::move(callReport), std::move(reports));
90
91 // GCOVR_EXCL_START
92 // ReSharper disable once CppDFAUnreachableCode
94 // GCOVR_EXCL_STOP
95 }
96
97 inline void report_full_match(CallReport callReport, MatchReport report) noexcept
98 {
99 get_reporter()
100 ->report_full_match(std::move(callReport), std::move(report));
101 }
102
103 inline void report_unfulfilled_expectation(ExpectationReport expectationReport)
104 {
105 get_reporter()
106 ->report_unfulfilled_expectation(std::move(expectationReport));
107 }
108
109 inline void report_error(StringT message)
110 {
111 get_reporter()
112 ->report_error(std::move(message));
113 }
114
115 inline void report_unhandled_exception(CallReport callReport, ExpectationReport expectationReport, std::exception_ptr const& exception)
116 {
117 get_reporter()
118 ->report_unhandled_exception(std::move(callReport), std::move(expectationReport), exception);
119 }
120}
121
123{
132 template <std::derived_from<IReporter> T, typename... Args>
133 requires std::constructible_from<T, Args...>
134 void install_reporter(Args&&... args)
135 {
136 detail::set_reporter(
137 std::make_shared<T>(std::forward<Args>(args)...));
138 }
139
140 namespace detail
141 {
142 template <typename T>
143 class ReporterInstaller
144 {
145 public:
146 template <typename... Args>
147 explicit ReporterInstaller(Args&&... args)
148 {
150 std::forward<Args>(args)...);
151 }
152 };
153 }
154}
155
156#endif
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
void install_reporter(Args &&... args)
Replaces the previous reporter with a newly constructed one.
Definition GlobalReporter.hpp:134
Definition BasicReporter.hpp:26
void unreachable()
Invokes undefined behavior.
Definition C++23Backports.hpp:40
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:397