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 
30 namespace 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
45  stop()
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
64  stop()
65  {
66  return std::chrono::duration_cast<std::chrono::microseconds>(ClockT::now() - beg)
67  .count();
68  }
69  };
70 
71  template <>
73  {
75  stop()
76  {
78  }
79  };
80 
81  template <>
83  {
85  stop()
86  {
87  return IceUtil::Time::microSeconds(TimerTag<std::chrono::microseconds>::stop().count());
88  }
89  };
90 
91  template <class Fun, class TimeT>
92  TimeT
93  operator*(TimerTag<TimeT>&& t, Fun&& fn)
94  {
95  fn();
96  return t.stop();
97  }
98 
99  //for virtual time
100 
101  template <class TimeT = IceUtil::Time>
103 
104  template <>
106  {
108 
110  {
111  }
112 
115  {
116  return {TimeUtil::GetTime() - beg};
117  }
118  };
119 
120  template <>
122  {
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__>{}* [&]
armarx::detail::VirtualTimerTag< IceUtil::Time >::stop
TimestampVariant stop()
Definition: Time.h:130
armarx::detail::TimerTag<>::ClockT
std::chrono::high_resolution_clock ClockT
Definition: Time.h:37
armarx::detail::TimerTag
Definition: Time.h:33
armarx::TimestampVariant
Definition: TimestampVariant.h:54
armarx::detail::TimerTag< long >::ClockT
std::chrono::high_resolution_clock ClockT
Definition: Time.h:56
armarx::detail::TimerTag< long >::TimerTag
TimerTag()
Definition: Time.h:59
armarx::detail::TimerTag< long >::beg
const ClockT::time_point beg
Definition: Time.h:57
IceUtil
Definition: Instance.h:21
armarx::detail::VirtualTimerTag< IceUtil::Time >::beg
const IceUtil::Time beg
Definition: Time.h:123
armarx::detail::TimerTag< TimestampVariant >::stop
TimestampVariant stop()
Definition: Time.h:75
armarx::detail::VirtualTimerTag
Definition: Time.h:102
armarx::detail::operator*
StreamPrinter< Fnc > operator*(StreamPrinterTag, Fnc &&f)
Definition: LoggingUtil.h:44
armarx::detail::VirtualTimerTag< TimestampVariant >::stop
TimestampVariant stop()
Definition: Time.h:114
armarx::detail::TimerTag::TimerTag
TimerTag()
Definition: Time.h:40
armarx::detail::TimerTag< IceUtil::Time >::stop
TimestampVariant stop()
Definition: Time.h:85
armarx::detail::TimerTag::stop
TimeT stop()
Definition: Time.h:45
TimestampVariant.h
armarx::detail
Definition: ApplicationNetworkStats.cpp:34
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::detail::VirtualTimerTag< IceUtil::Time >::VirtualTimerTag
VirtualTimerTag()
Definition: Time.h:125
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
armarx::detail::TimerTag::beg
const ClockT::time_point beg
Definition: Time.h:38
TimeUtil.h
armarx::detail::VirtualTimerTag< TimestampVariant >::VirtualTimerTag
VirtualTimerTag()
Definition: Time.h:109
armarx::detail::TimerTag< long >::stop
long stop()
Definition: Time.h:64
armarx::detail::VirtualTimerTag< TimestampVariant >::beg
const IceUtil::Time beg
Definition: Time.h:107