CppBenchmark 1.0.5.0
C++ Benchmark Library
Loading...
Searching...
No Matches
context.h
Go to the documentation of this file.
1
9#ifndef CPPBENCHMARK_CONTEXT_H
10#define CPPBENCHMARK_CONTEXT_H
11
13
14#include <atomic>
15
16namespace CppBenchmark {
17
19
26class Context : public Phase
27{
28 friend class BenchmarkBase;
29 friend class Benchmark;
30
31public:
32 Context() = delete;
33 Context(const Context&) noexcept = default;
34 Context(Context&&) noexcept = default;
35 virtual ~Context() noexcept = default;
36
37 Context& operator=(const Context&) noexcept = default;
38 Context& operator=(Context&&) noexcept = default;
39
41 int x() const noexcept { return _x; }
43 int y() const noexcept { return _y; }
45 int z() const noexcept { return _z; }
46
48
53 PhaseMetrics& metrics() noexcept { return *_metrics; }
54
56 bool canceled() const noexcept { return *_canceled; }
58 void Cancel() noexcept { *_canceled = true; }
59
61 virtual std::string description() const;
62
63 // Implementation of Phase
64 const std::string& name() const noexcept override { return _current->name(); }
65 const PhaseMetrics& metrics() const noexcept override { return _current->metrics(); }
66 std::shared_ptr<Phase> StartPhase(const std::string& phase) override { return _current->StartPhase(phase); }
67 std::shared_ptr<Phase> StartPhaseThreadSafe(const std::string& phase) override { return _current->StartPhaseThreadSafe(phase); }
68 void StopPhase() override { _current->StopPhase(); }
69 std::shared_ptr<PhaseScope> ScopePhase(const std::string& phase) override { return _current->ScopePhase(phase); }
70 std::shared_ptr<PhaseScope> ScopePhaseThreadSafe(const std::string& phase) override { return _current->ScopePhaseThreadSafe(phase); }
71
72protected:
74 int _x;
76 int _y;
78 int _z;
84 std::shared_ptr<std::atomic<bool>> _canceled;
85
87
92 Context(int x, int y, int z) noexcept
93 : _x(x), _y(y), _z(z),
94 _current(nullptr),
95 _metrics(nullptr),
96 _canceled(std::make_shared<std::atomic<bool>>(false))
97 {}
98};
99
100} // namespace CppBenchmark
101
102#endif // CPPBENCHMARK_CONTEXT_H
Benchmark base class.
Benchmark class.
Definition benchmark.h:22
Benchmark running context.
Definition context.h:27
void StopPhase() override
Stop measurement of the current phase.
Definition context.h:68
Context(int x, int y, int z) noexcept
Create benchmark running context.
Definition context.h:92
const PhaseMetrics & metrics() const noexcept override
Get phase metrics.
Definition context.h:65
PhaseCore * _current
Current benchmark phase.
Definition context.h:80
PhaseMetrics & metrics() noexcept
Benchmark mutable metrics.
Definition context.h:53
std::shared_ptr< PhaseScope > ScopePhaseThreadSafe(const std::string &phase) override
Start a new sub-phase with a given name in a multi-thread environment and wrap it in a PhaseScope.
Definition context.h:70
int x() const noexcept
Benchmark first parameter. Valid only if not negative!
Definition context.h:41
void Cancel() noexcept
Cancel benchmark execution.
Definition context.h:58
const std::string & name() const noexcept override
Get phase name.
Definition context.h:64
std::shared_ptr< PhaseScope > ScopePhase(const std::string &phase) override
Start a new sub-phase with a given name in a single-thread environment and wrap it in a PhaseScope.
Definition context.h:69
Context(const Context &) noexcept=default
std::shared_ptr< Phase > StartPhaseThreadSafe(const std::string &phase) override
Start a new sub-phase with a given name in a multi-thread environment.
Definition context.h:67
Context(Context &&) noexcept=default
std::shared_ptr< std::atomic< bool > > _canceled
Benchmark canceled flag.
Definition context.h:84
virtual std::string description() const
Get description of the current benchmark running context.
Definition context.cpp:13
int y() const noexcept
Benchmark second parameter. Valid only if not negative!
Definition context.h:43
PhaseMetrics * _metrics
Current benchmark metrics.
Definition context.h:82
std::shared_ptr< Phase > StartPhase(const std::string &phase) override
Start a new sub-phase with a given name in a single-thread environment.
Definition context.h:66
bool canceled() const noexcept
Is benchmark execution canceled?
Definition context.h:56
int _x
Benchmark first parameter. Valid only if not negative!
Definition context.h:74
int _z
Benchmark third parameter. Valid only if not negative!
Definition context.h:78
int _y
Benchmark second parameter. Valid only if not negative!
Definition context.h:76
int z() const noexcept
Benchmark third parameter. Valid only if not negative!
Definition context.h:45
Benchmark phase core.
Definition phase_core.h:27
const std::string & name() const noexcept override
Get phase name.
Definition phase_core.h:53
void StopPhase() override
Stop measurement of the current phase.
Definition phase_core.h:57
std::shared_ptr< PhaseScope > ScopePhaseThreadSafe(const std::string &phase) override
Start a new sub-phase with a given name in a multi-thread environment and wrap it in a PhaseScope.
Definition phase_core.h:60
std::shared_ptr< Phase > StartPhaseThreadSafe(const std::string &phase) override
Start a new sub-phase with a given name in a multi-thread environment.
std::shared_ptr< PhaseScope > ScopePhase(const std::string &phase) override
Start a new sub-phase with a given name in a single-thread environment and wrap it in a PhaseScope.
Definition phase_core.h:58
const PhaseMetrics & metrics() const noexcept override
Get phase metrics.
Definition phase_core.h:54
std::shared_ptr< Phase > StartPhase(const std::string &phase) override
Start a new sub-phase with a given name in a single-thread environment.
Benchmark phase base class.
Definition phase.h:26
Benchmark phase metrics.
C++ Benchmark project definitions.
Definition barrier.h:15
Benchmark phase core definition.