stop_watch.hpp
1 #pragma once
2 
3 #include <algorithm>
4 #include <exception>
5 #include <memory>
6 #include <new>
7 #include <system_error>
8 #include <utility>
9 #include <cmath>
10 
11 #include "../_cexports/c_utilities.h"
12 
13 #include "../global.hpp"
14 
15 
16 
17 #include "utilities.hpp"
18 #include "system_info.hpp"
19 
20 namespace Cvb
21 {
22 
23 CVB_BEGIN_INLINE_NS
24 
25 template <>
26 inline HandleGuard<Utilities::StopWatch>::HandleGuard(void * handle) noexcept
27  : HandleGuard<Utilities::StopWatch>(handle, [](void* handle) { CVB_CALL_CAPI(TWDestroy(handle)); })
28 {
29 }
30 
31 namespace Utilities
32 {
33 
34 
35 
37 
46 class StopWatch
47 {
48 
49  public:
50 
52 
60  {
61  auto stopWatch = Internal::DoBoolCallObjectOut<StopWatch>([&](void* & handle)
62  {
63  switch (mode)
64  {
65  default:
67  return CVB_CALL_CAPI(TWCreateEx(&handle));
68 
70  return CVB_CALL_CAPI(TWCreate(&handle));
71  }
72 
73  }, mode);
74  stopWatch->Start();
75  return stopWatch;
76  }
77 
79 
86  : StopWatch(std::move(*Create(mode)))
87  {
88  }
89 
91 
95  StopWatch(StopWatch&& other) noexcept = default;
96 
97 
98  StopWatch& operator=(StopWatch&& other) noexcept = default;
99 
101 
107  void* Handle() const noexcept
108  {
109  return handle_.Handle();
110  }
111 
112 
114 
122  static std::unique_ptr<StopWatch> FromHandle(HandleGuard<StopWatch>&& guard, StopWatchMode mode)
123  {
124  if (!guard.Handle())
125  throw std::runtime_error("handle must not be null");
126 
127  return std::unique_ptr<StopWatch>(new StopWatch(std::move(guard), mode));
128  }
129 
130 
132 
139  void Start()
140  {
141  if(!CVB_CALL_CAPI(TWStartStopWatch(Handle(), static_cast<short>(0))))
142  SystemInfo::ThrowLastError();
143  }
144 
146 
151  {
153  if(!CVB_CALL_CAPI(TWReadStopWatch(Handle(), static_cast<short>(0), &value)))
154  SystemInfo::ThrowLastError();
155  return std::chrono::milliseconds(static_cast<std::size_t>(round(value)));
156  }
157 
159 
163  StopWatchMode Mode() const noexcept
164  {
165  return mode_;
166  }
167 
168 
169 
170  private:
171 
172  StopWatch(HandleGuard<StopWatch>&& guard, StopWatchMode mode) noexcept
173  : handle_(std::move(guard))
174  , mode_(mode)
175  {
176  }
177 
178  HandleGuard<StopWatch> handle_;
179 
180 
182 };
183 
184 }
185 
186 using Utilities::StopWatch;
187 
188 CVB_END_INLINE_NS
189 
190 }
191 
192 
193 
void * Handle() const noexcept
Classic API stop watch handle.
Definition: stop_watch.hpp:107
static std::unique_ptr< StopWatch > Create(StopWatchMode mode=StopWatchMode::MultiCPU)
Create a StopWatch object.
Definition: stop_watch.hpp:59
StopWatchMode Mode() const noexcept
Mode for which the stop watch was created.
Definition: stop_watch.hpp:163
static std::unique_ptr< StopWatch > FromHandle(HandleGuard< StopWatch > &&guard, StopWatchMode mode)
Creates a stop watch from a classic API handle.
Definition: stop_watch.hpp:122
StopWatch(StopWatchMode mode=StopWatchMode::MultiCPU)
Create a StopWatch object.
Definition: stop_watch.hpp:85
Root namespace for the Image Manager interface.
Definition: version.hpp:11
STL class.
std::chrono::milliseconds TimeSpan() const
Gets the time, that has elapsed since start (or since the construction of the object,...
Definition: stop_watch.hpp:150
void Start()
Start (or re-start) the stopwatch.
Definition: stop_watch.hpp:139
T quiet_NaN(T... args)
StopWatchMode
Mode at which the StopWatch should work.
Definition: utilities.hpp:30
Speed measurement object.
Definition: stop_watch.hpp:46