CVB++ 15.0
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#include "utilities.hpp"
16#include "system_info.hpp"
17
18namespace Cvb
19{
20
21 CVB_BEGIN_INLINE_NS
22
23 template <>
24 inline HandleGuard<Utilities::StopWatch>::HandleGuard(void *handle) noexcept
25 : HandleGuard<Utilities::StopWatch>(handle, [](void *handle) { CVB_CALL_CAPI(TWDestroy(handle)); })
26 {
27 }
28
29 namespace Utilities
30 {
31
33
43 {
44
45 public:
47
55 {
56 auto stopWatch = Internal::DoBoolCallObjectOut<StopWatch>(
57 [&](void *&handle) {
58 switch (mode)
59 {
60 default:
62 return CVB_CALL_CAPI(TWCreateEx(&handle));
63
65 return CVB_CALL_CAPI(TWCreate(&handle));
66 }
67 },
68 mode);
69 stopWatch->Start();
70 return stopWatch;
71 }
72
74
81 : StopWatch(std::move(*Create(mode)))
82 {
83 }
84
85 StopWatch(const StopWatch &other) = delete;
86 StopWatch &operator=(const StopWatch &other) = delete;
87 StopWatch(StopWatch &&other) noexcept = default;
88 StopWatch &operator=(StopWatch &&other) noexcept = default;
89 ~StopWatch() = default;
90
92
98 void *Handle() const noexcept
99 {
100 return handle_.Handle();
101 }
102
104
112 static std::unique_ptr<StopWatch> FromHandle(HandleGuard<StopWatch> &&guard, StopWatchMode mode)
113 {
114 if (!guard.Handle())
115 throw std::runtime_error("handle must not be null");
116
117 return std::unique_ptr<StopWatch>(new StopWatch(std::move(guard), mode));
118 }
119
121
128 void Start()
129 {
130 if (!CVB_CALL_CAPI(TWStartStopWatch(Handle(), static_cast<short>(0))))
131 SystemInfo::ThrowLastError();
132 }
133
136
141 {
143 if (!CVB_CALL_CAPI(TWReadStopWatch(Handle(), static_cast<short>(0), &value)))
144 SystemInfo::ThrowLastError();
145 return std::chrono::milliseconds(static_cast<std::size_t>(round(value)));
146 }
147
149
153 StopWatchMode Mode() const noexcept
154 {
155 return mode_;
156 }
157
158 private:
159 StopWatch(HandleGuard<StopWatch> &&guard, StopWatchMode mode) noexcept
160 : handle_(std::move(guard))
161 , mode_(mode)
162 {
163 }
164
165 HandleGuard<StopWatch> handle_;
166
168 };
169
170 } // namespace Utilities
171
173
174 CVB_END_INLINE_NS
175
176} // namespace Cvb
StopWatch(StopWatchMode mode=StopWatchMode::MultiCPU)
Create a StopWatch object.
Definition stop_watch.hpp:80
Speed measurement object.
Definition stop_watch.hpp:43
void Start()
Start (or re-start) the stopwatch.
Definition stop_watch.hpp:128
std::chrono::milliseconds TimeSpan() const
Gets the time, that has elapsed since start (or since the construction of the object,...
Definition stop_watch.hpp:140
static std::unique_ptr< StopWatch > FromHandle(HandleGuard< StopWatch > &&guard, StopWatchMode mode)
Creates a stop watch from a classic API handle.
Definition stop_watch.hpp:112
StopWatch(StopWatchMode mode=StopWatchMode::MultiCPU)
Create a StopWatch object.
Definition stop_watch.hpp:80
static std::unique_ptr< StopWatch > Create(StopWatchMode mode=StopWatchMode::MultiCPU)
Create a StopWatch object.
Definition stop_watch.hpp:54
void * Handle() const noexcept
Classic API stop watch handle.
Definition stop_watch.hpp:98
StopWatchMode Mode() const noexcept
Mode for which the stop watch was created.
Definition stop_watch.hpp:153
cvbbool_t TWReadStopWatch(TIMEWATCH hTimeWatch, short Index, double *pElapsedTime)
cvbbool_t TWDestroy(TIMEWATCH hTimeWatch)
cvbbool_t TWStartStopWatch(TIMEWATCH hTimeWatch, short Index)
cvbbool_t TWCreateEx(TIMEWATCH *phTimeWatch)
cvbbool_t TWCreate(TIMEWATCH *phTimeWatch)
T move(T... args)
Namespace for helpers and utilities, which are not directly related to image processing.
Definition version.hpp:16
StopWatchMode
Mode at which the StopWatch should work.
Definition utilities.hpp:28
@ SingleCPU
Definition utilities.hpp:42
@ MultiCPU
Definition utilities.hpp:34
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
T quiet_NaN(T... args)