1 | #ifndef BANDIT_REPORTERS_H |
---|---|
2 | #define BANDIT_REPORTERS_H |
3 | |
4 | #include <bandit/reporters/dots_reporter.h> |
5 | #include <bandit/reporters/single_line_reporter.h> |
6 | #include <bandit/reporters/xunit_reporter.h> |
7 | #include <bandit/reporters/info_reporter.h> |
8 | #include <bandit/reporters/spec_reporter.h> |
9 | #include <bandit/reporters/crash_reporter.h> |
10 | |
11 | namespace bandit { |
12 | namespace detail { |
13 | struct listener; |
14 | |
15 | struct reporter { |
16 | // A function is required to initialize a static listener variable in a header file |
17 | // and this struct aims at encapsulating this function |
18 | static void register_listener(listener* reporter) { |
19 | if (reporter == nullptr) { |
20 | throw std::runtime_error("Invalid null reporter passed to " |
21 | "bandit::detail::register_listener"); |
22 | } |
23 | get_reporter_address() = reporter; |
24 | } |
25 | static listener& registered_listener() { |
26 | auto reporter = get_reporter_address(); |
27 | if (reporter == nullptr) { |
28 | throw std::runtime_error("No reporter set. Please call " |
29 | "bandit::detail::register_listener with a non-null reporter"); |
30 | } |
31 | return *reporter; |
32 | } |
33 | |
34 | private: |
35 | static listener*& get_reporter_address() { |
36 | static listener* reporter_ = nullptr; |
37 | return reporter_; |
38 | } |
39 | }; |
40 | |
41 | inline void register_listener(listener* reporter) { |
42 | reporter::register_listener(reporter); |
43 | } |
44 | inline listener& registered_listener() { |
45 | return reporter::registered_listener(); |
46 | } |
47 | } |
48 | } |
49 | #endif |
50 |