guanaqo 1.0.0-alpha.24
Utilities for scientific software
Loading...
Searching...
No Matches
timed.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @ingroup timing
5/// RAII timing helper.
6
7#include <chrono>
8#include <functional>
9
10namespace guanaqo {
11
12/// @addtogroup timing
13/// @{
14
15template <class T>
16struct Timed;
17
18template <class Rep, class Period>
19struct Timed<std::chrono::duration<Rep, Period>> {
20 Timed(std::chrono::duration<Rep, Period> &time) : time(time) {
21 time -= std::chrono::steady_clock::now().time_since_epoch();
22 }
23 ~Timed() { time += std::chrono::steady_clock::now().time_since_epoch(); }
24 Timed(const Timed &) = delete;
25 Timed(Timed &&) = delete;
26 Timed &operator=(const Timed &) = delete;
27 Timed &operator=(Timed &&) = delete;
28 std::chrono::duration<Rep, Period> &time;
29};
30
31#ifndef DOXYGEN
32template <class T>
33Timed(T &) -> Timed<T>;
34#endif
35
36template <class T, class F, class... Args>
37decltype(auto) timed(T &time, F &&func, Args &&...args) {
38 Timed timer{time};
39 return std::invoke(std::forward<F>(func), std::forward<Args>(args)...);
40}
41
42/// @}
43
44} // namespace guanaqo
decltype(auto) timed(T &time, F &&func, Args &&...args)
Definition timed.hpp:37
std::chrono::duration< Rep, Period > & time
Definition timed.hpp:28
Timed(std::chrono::duration< Rep, Period > &time)
Definition timed.hpp:20