mimic++ v9.2.1
Loading...
Searching...
No Matches
GlobalReporter.hpp
Go to the documentation of this file.
1// Copyright Dominic (DNKpp) Koepke 2024 - 2025.
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(
75 CallReport callReport,
76 std::vector<NoMatchReport> noMatchReports)
77 {
78 get_reporter()
79 // GCOVR_EXCL_START
80 ->report_no_matches(
81 // GCOVR_EXCL_STOP
82 std::move(callReport),
83 std::move(noMatchReports));
84
85 // GCOVR_EXCL_START
86 // ReSharper disable once CppDFAUnreachableCode
88 // GCOVR_EXCL_STOP
89 }
90
91 [[noreturn]]
92 inline void report_inapplicable_matches(
93 CallReport callReport,
94 std::vector<ExpectationReport> expectationReports)
95 {
96 get_reporter()
97 // GCOVR_EXCL_START
98 ->report_inapplicable_matches(
99 // GCOVR_EXCL_STOP
100 std::move(callReport),
101 std::move(expectationReports));
102
103 // GCOVR_EXCL_START
104 // ReSharper disable once CppDFAUnreachableCode
106 // GCOVR_EXCL_STOP
107 }
108
109 inline void report_full_match(
110 CallReport callReport,
111 ExpectationReport expectationReport) noexcept
112 {
113 get_reporter()
114 ->report_full_match(
115 std::move(callReport),
116 std::move(expectationReport));
117 }
118
119 inline void report_unfulfilled_expectation(
120 ExpectationReport expectationReport)
121 {
122 get_reporter()
123 ->report_unfulfilled_expectation(std::move(expectationReport));
124 }
125
126 inline void report_error(StringT message)
127 {
128 get_reporter()
129 ->report_error(std::move(message));
130 }
131
132 inline void report_unhandled_exception(
133 CallReport callReport,
134 ExpectationReport expectationReport,
135 std::exception_ptr const& exception)
136 {
137 get_reporter()
138 ->report_unhandled_exception(
139 std::move(callReport),
140 std::move(expectationReport),
141 exception);
142 }
143}
144
146{
155 template <std::derived_from<IReporter> T, typename... Args>
156 requires std::constructible_from<T, Args...>
157 void install_reporter(Args&&... args)
158 {
159 detail::set_reporter(
160 std::make_shared<T>(std::forward<Args>(args)...));
161 }
162
163 namespace detail
164 {
165 template <typename T>
166 class ReporterInstaller
167 {
168 public:
169 template <typename... Args>
170 explicit ReporterInstaller(Args&&... args)
171 {
173 std::forward<Args>(args)...);
174 }
175 };
176 }
177}
178
179#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:157
Definition BasicReporter.hpp:27
void unreachable()
Invokes undefined behavior.
Definition C++23Backports.hpp:40
std::basic_string< CharT, CharTraitsT > StringT
Definition Fwd.hpp:391