File Coverage

/usr/local/lib/perl5/site_perl/5.26.1/x86_64-linux/XS/libcatch.x/i/catch2/benchmark/detail/catch_benchmark_function.hpp
Criterion Covered Total %
statement 0 25 0.0
branch 0 20 0.0
condition n/a
subroutine n/a
pod n/a
total 0 45 0.0


line stmt bran cond sub pod time code
1              
2             // Copyright Catch2 Authors
3             // Distributed under the Boost Software License, Version 1.0.
4             // (See accompanying file LICENSE_1_0.txt or copy at
5             // https://www.boost.org/LICENSE_1_0.txt)
6              
7             // SPDX-License-Identifier: BSL-1.0
8             // Adapted from donated nonius code.
9              
10             #ifndef CATCH_BENCHMARK_FUNCTION_HPP_INCLUDED
11             #define CATCH_BENCHMARK_FUNCTION_HPP_INCLUDED
12              
13             #include
14             #include
15             #include
16             #include
17             #include
18              
19             #include
20              
21             namespace Catch {
22             namespace Benchmark {
23             namespace Detail {
24             template
25             struct is_related
26             : std::is_same, std::decay_t> {};
27              
28             /// We need to reinvent std::function because every piece of code that might add overhead
29             /// in a measurement context needs to have consistent performance characteristics so that we
30             /// can account for it in the measurement.
31             /// Implementations of std::function with optimizations that aren't always applicable, like
32             /// small buffer optimizations, are not uncommon.
33             /// This is effectively an implementation of std::function without any such optimizations;
34             /// it may be slow, but it is consistently slow.
35 0           struct BenchmarkFunction {
36             private:
37             struct callable {
38             virtual void call(Chronometer meter) const = 0;
39             virtual Catch::Detail::unique_ptr clone() const = 0;
40             virtual ~callable(); // = default;
41              
42 0           callable() = default;
43 0           callable(callable const&) = default;
44             callable& operator=(callable const&) = default;
45             };
46             template
47 0 0         struct model : public callable {
    0          
    0          
48 0           model(Fun&& fun_) : fun(CATCH_MOVE(fun_)) {}
49 0           model(Fun const& fun_) : fun(fun_) {}
50              
51 0           Catch::Detail::unique_ptr clone() const override {
52 0 0         return Catch::Detail::make_unique>( *this );
    0          
    0          
53             }
54              
55 0           void call(Chronometer meter) const override {
56 0 0         call(meter, is_callable());
    0          
    0          
57 0           }
58             void call(Chronometer meter, std::true_type) const {
59             fun(meter);
60             }
61 0           void call(Chronometer meter, std::false_type) const {
62 0           meter.measure(fun);
63 0           }
64              
65             Fun fun;
66             };
67              
68 0           struct do_nothing { void operator()() const {} };
69              
70             template
71             BenchmarkFunction(model* c) : f(c) {}
72              
73             public:
74 0           BenchmarkFunction()
75 0 0         : f(new model{ {} }) {}
76              
77             template
78             std::enable_if_t::value, int> = 0>
79 0           BenchmarkFunction(Fun&& fun)
80 0           : f(new model>(CATCH_FORWARD(fun))) {}
81              
82             BenchmarkFunction( BenchmarkFunction&& that ) noexcept:
83             f( CATCH_MOVE( that.f ) ) {}
84              
85 0           BenchmarkFunction(BenchmarkFunction const& that)
86 0           : f(that.f->clone()) {}
87              
88             BenchmarkFunction&
89 0           operator=( BenchmarkFunction&& that ) noexcept {
90 0           f = CATCH_MOVE( that.f );
91 0           return *this;
92             }
93              
94             BenchmarkFunction& operator=(BenchmarkFunction const& that) {
95             f = that.f->clone();
96             return *this;
97             }
98              
99 0           void operator()(Chronometer meter) const { f->call(meter); }
100              
101             private:
102             Catch::Detail::unique_ptr f;
103             };
104             } // namespace Detail
105             } // namespace Benchmark
106             } // namespace Catch
107              
108             #endif // CATCH_BENCHMARK_FUNCTION_HPP_INCLUDED