cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
timer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <string>
5#include <vector>
6
7#include <cpp-toolbox/cpp-toolbox_export.hpp>
8
9namespace toolbox::utils
10{
11
40class CPP_TOOLBOX_EXPORT stop_watch_timer_t
41{
42public:
47 stop_watch_timer_t(std::string name = "default_timer");
48
53 void set_name(const std::string& name);
54
59 auto get_name() const -> const std::string&;
60
65 void start();
66
71 void stop();
72
77 void reset();
78
84 auto elapsed_time_ms() const -> double;
85
91 auto elapsed_time() const -> double;
92
97 void print_stats() const;
98
99private:
100 std::string name_;
101 std::chrono::nanoseconds total_duration_;
102 std::chrono::time_point<std::chrono::high_resolution_clock>
103 start_time_;
104 bool running_;
105};
106
126class CPP_TOOLBOX_EXPORT simple_timer_interface_t
127{
128public:
132 virtual ~simple_timer_interface_t() = default;
133
138 virtual void start(int index) = 0;
139
144 virtual void stop(int index) = 0;
145
150 virtual void reset(int index) = 0;
151
156 virtual void print_stats(int index) const = 0;
157
161 virtual void print_all_stats() const = 0;
162
168 virtual auto elapsed_time_ms(int index) const -> double = 0;
169
175 virtual auto elapsed_time(int index) const -> double = 0;
176
181 virtual auto size() const -> size_t = 0;
182};
183
203class CPP_TOOLBOX_EXPORT simple_timer_array_t : public simple_timer_interface_t
204{
205public:
210 explicit simple_timer_array_t(int size);
211
216 explicit simple_timer_array_t(const std::vector<std::string>& names);
217
218 // Delete copy constructor and assignment operator
221
222 // Allow move semantics
225
226 ~simple_timer_array_t() override = default;
227
228 // Implement interface methods
229 void start(int index) override;
230 void stop(int index) override;
231 void reset(int index) override;
232 auto elapsed_time_ms(int index) const -> double override;
233 auto elapsed_time(int index) const -> double override;
234 void print_stats(int index) const override;
235 void print_all_stats() const override;
236 auto size() const -> size_t override;
237
238private:
244 void check_id(int index) const;
245
246 std::vector<stop_watch_timer_t> timers_;
247};
248
260inline auto create_timer(simple_timer_interface_t** timer_interface, int size)
261 -> bool
262{
263 *timer_interface =
264 static_cast<simple_timer_interface_t*>(new simple_timer_array_t(size));
265 return (*timer_interface != nullptr) ? true : false;
266}
267
281 const std::vector<std::string>& names)
282 -> bool
283{
284 *timer_interface = reinterpret_cast<simple_timer_interface_t*>(
285 new simple_timer_array_t(names));
286 return (*timer_interface != nullptr) ? true : false;
287}
288
301inline auto delete_timer(simple_timer_interface_t** timer_interface) -> bool
302{
303 if (*timer_interface != nullptr) {
304 delete *timer_interface;
305 *timer_interface = nullptr;
306 }
307 return true;
308}
309
322inline auto start_timer(simple_timer_interface_t** timer_interface, int id)
323 -> bool
324{
325 if (*timer_interface != nullptr) {
326 (*timer_interface)->start(id);
327 }
328 return true;
329}
330
345inline auto stop_timer(simple_timer_interface_t** timer_interface, int id)
346 -> bool
347{
348 if (*timer_interface != nullptr) {
349 (*timer_interface)->stop(id);
350 }
351 return true;
352}
353
368inline auto reset_timer(simple_timer_interface_t** timer_interface, int id)
369 -> bool
370{
371 if (*timer_interface != nullptr) {
372 (*timer_interface)->reset(id);
373 }
374 return true;
375}
376
391inline auto display_timer(simple_timer_interface_t** timer_interface, int id)
392 -> bool
393{
394 if (*timer_interface != nullptr) {
395 (*timer_interface)->print_stats(id);
396 }
397 return true;
398}
399
411inline auto display_all_timer(simple_timer_interface_t** timer_interface)
412 -> bool
413{
414 if (*timer_interface != nullptr) {
415 (*timer_interface)->print_all_stats();
416 }
417 return true;
418}
419
439inline auto get_timer_elapsed(simple_timer_interface_t** timer_interface,
440 int id) -> double
441{
442 if (*timer_interface != nullptr) {
443 return (*timer_interface)->elapsed_time(id);
444 }
445 return 0.0;
446}
447
472inline auto get_timer_elapsed_ms(simple_timer_interface_t** timer_interface,
473 int id) -> double
474{
475 if (*timer_interface != nullptr) {
476 return (*timer_interface)->elapsed_time_ms(id);
477 }
478 return 0.0;
479}
480
481} // namespace toolbox::utils
Concrete implementation of simple_timer_interface_t using an array of timers.
Definition timer.hpp:204
simple_timer_array_t(const simple_timer_array_t &)=delete
auto operator=(simple_timer_array_t &&) -> simple_timer_array_t &=default
void print_all_stats() const override
Print statistics for all timers.
simple_timer_array_t(int size)
Construct a simple_timer_array_t with specified size.
void stop(int index) override
Stop the timer at specified index.
auto elapsed_time_ms(int index) const -> double override
Get elapsed time in milliseconds for specified timer.
~simple_timer_array_t() override=default
simple_timer_array_t(const std::vector< std::string > &names)
Construct a simple_timer_array_t with specified names.
void reset(int index) override
Reset the timer at specified index.
auto elapsed_time(int index) const -> double override
Get elapsed time in seconds for specified timer.
auto size() const -> size_t override
Get the number of timers in the collection.
auto operator=(const simple_timer_array_t &) -> simple_timer_array_t &=delete
simple_timer_array_t(simple_timer_array_t &&)=default
void start(int index) override
Start the timer at specified index.
void print_stats(int index) const override
Print statistics for the timer at specified index.
Interface for managing a collection of timers.
Definition timer.hpp:127
virtual ~simple_timer_interface_t()=default
Virtual destructor to ensure proper cleanup.
virtual void stop(int index)=0
Stop the timer at specified index.
virtual auto size() const -> size_t=0
Get the number of timers in the collection.
virtual void print_stats(int index) const =0
Print statistics for the timer at specified index.
virtual void start(int index)=0
Start the timer at specified index.
virtual void reset(int index)=0
Reset the timer at specified index.
virtual auto elapsed_time_ms(int index) const -> double=0
Get elapsed time in milliseconds for specified timer.
virtual void print_all_stats() const =0
Print statistics for all timers.
virtual auto elapsed_time(int index) const -> double=0
Get elapsed time in seconds for specified timer.
A high-resolution stopwatch timer for measuring elapsed time.
Definition timer.hpp:41
auto get_name() const -> const std::string &
Get the name of the timer.
stop_watch_timer_t(std::string name="default_timer")
Construct a new stop_watch_timer_t object.
void set_name(const std::string &name)
Set the name of the timer.
< 用于 std::out_of_range/For std::out_of_range
Definition click.hpp:11
auto get_timer_elapsed_ms(simple_timer_interface_t **timer_interface, int id) -> double
Get the elapsed time in milliseconds for a specific timer.
Definition timer.hpp:472
auto create_timer_with_names(simple_timer_interface_t **timer_interface, const std::vector< std::string > &names) -> bool
Create a timer collection with specified names.
Definition timer.hpp:280
auto display_all_timer(simple_timer_interface_t **timer_interface) -> bool
Display statistics for all timers.
Definition timer.hpp:411
auto display_timer(simple_timer_interface_t **timer_interface, int id) -> bool
Display statistics for a specific timer.
Definition timer.hpp:391
auto start_timer(simple_timer_interface_t **timer_interface, int id) -> bool
Start a specific timer.
Definition timer.hpp:322
auto reset_timer(simple_timer_interface_t **timer_interface, int id) -> bool
Reset a specific timer.
Definition timer.hpp:368
auto delete_timer(simple_timer_interface_t **timer_interface) -> bool
Delete a timer collection.
Definition timer.hpp:301
auto get_timer_elapsed(simple_timer_interface_t **timer_interface, int id) -> double
Get the elapsed time in seconds for a specific timer.
Definition timer.hpp:439
auto stop_timer(simple_timer_interface_t **timer_interface, int id) -> bool
Stop a specific timer.
Definition timer.hpp:345