Time.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotAPI::RobotUnit
17 * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18 * @date 2017
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#pragma once
24
25#include <chrono>
26
29
30namespace armarx::detail
31{
32 template <class TimeT = std::chrono::microseconds>
33 struct TimerTag
34 {
35 //assume it is std::chrono
36
37 using ClockT = std::chrono::high_resolution_clock;
38 const ClockT::time_point beg;
39
40 TimerTag() : beg{ClockT::now()}
41 {
42 }
43
44 TimeT
46 {
47 return std::chrono::duration_cast<TimeT>(ClockT::now() - beg);
48 }
49 };
50
51 template <>
52 struct TimerTag<long>
53 {
54 //return micro seconds as long
55
56 using ClockT = std::chrono::high_resolution_clock;
57 const ClockT::time_point beg;
58
59 TimerTag() : beg{ClockT::now()}
60 {
61 }
62
63 long
65 {
66 return std::chrono::duration_cast<std::chrono::microseconds>(ClockT::now() - beg)
67 .count();
68 }
69 };
70
71 template <>
73 {
76 {
78 }
79 };
80
81 template <>
82 struct TimerTag<IceUtil::Time> : TimerTag<>
83 {
86 {
87 return IceUtil::Time::microSeconds(TimerTag<std::chrono::microseconds>::stop().count());
88 }
89 };
90
91 template <class Fun, class TimeT>
92 TimeT
94 {
95 fn();
96 return t.stop();
97 }
98
99 //for virtual time
100
101 template <class TimeT = IceUtil::Time>
103
104 template <>
106 {
107 const IceUtil::Time beg;
108
110 {
111 }
112
115 {
116 return {TimeUtil::GetTime() - beg};
117 }
118 };
119
120 template <>
122 {
123 const IceUtil::Time beg;
124
126 {
127 }
128
131 {
132 return TimeUtil::GetTime() - beg;
133 }
134 };
135
136 template <class Fun, class TimeT>
137 TimeT
139 {
140 fn();
141 return t.stop();
142 }
143} // namespace armarx::detail
144
145#define ARMARX_STOPWATCH(...) ::armarx::detail::TimerTag<__VA_ARGS__>{}* [&]
146#define ARMARX_VIRTUAL_STOPWATCH(...) ::armarx::detail::VirtualTimerTag<__VA_ARGS__>{}* [&]
provides utility functions for getting the current time
Definition TimeUtil.h:135
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
Implements a Variant type for timestamps.
StreamPrinter< Fnc > operator*(StreamPrinterTag, Fnc &&f)
Definition LoggingUtil.h:48
std::chrono::high_resolution_clock ClockT
Definition Time.h:56
const ClockT::time_point beg
Definition Time.h:57
std::chrono::high_resolution_clock ClockT
Definition Time.h:37
const ClockT::time_point beg
Definition Time.h:38