WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
scoped_profiler.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <concepts>
6#include <utility>
7
10
11namespace slp {
12
13/// Starts a profiler in the constructor and stops it in the destructor.
14template <typename Profiler>
15 requires std::same_as<Profiler, SetupProfiler> ||
16 std::same_as<Profiler, SolveProfiler>
18 public:
19 /// Starts a profiler.
20 ///
21 /// @param profiler The profiler.
22 explicit ScopedProfiler(Profiler& profiler) noexcept : m_profiler{&profiler} {
23 m_profiler->start();
24 }
25
26 /// Stops a profiler.
28 if (m_active) {
29 m_profiler->stop();
30 }
31 }
32
33 /// Move constructor.
34 ///
35 /// @param rhs The other ScopedProfiler.
37 : m_profiler{std::move(rhs.m_profiler)}, m_active{rhs.m_active} {
38 rhs.m_active = false;
39 }
40
43
44 /// Stops the profiler.
45 ///
46 /// If this is called, the destructor is a no-op.
47 void stop() {
48 if (m_active) {
49 m_profiler->stop();
50 m_active = false;
51 }
52 }
53
54 /// Returns the most recent solve duration in milliseconds as a double.
55 ///
56 /// @return The most recent solve duration in milliseconds as a double.
57 const std::chrono::duration<double>& current_duration() const {
58 return m_profiler->current_duration();
59 }
60
61 private:
62 Profiler* m_profiler;
63 bool m_active = true;
64};
65
66} // namespace slp
ScopedProfiler & operator=(const ScopedProfiler &)=delete
ScopedProfiler(ScopedProfiler &&rhs) noexcept
Move constructor.
Definition scoped_profiler.hpp:36
void stop()
Stops the profiler.
Definition scoped_profiler.hpp:47
~ScopedProfiler()
Stops a profiler.
Definition scoped_profiler.hpp:27
ScopedProfiler(Profiler &profiler) noexcept
Starts a profiler.
Definition scoped_profiler.hpp:22
ScopedProfiler(const ScopedProfiler &)=delete
const std::chrono::duration< double > & current_duration() const
Returns the most recent solve duration in milliseconds as a double.
Definition scoped_profiler.hpp:57
Definition expression_graph.hpp:11