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
16
17#include "utilities.hpp"
18#include "system_info.hpp"
19
20namespace Cvb
21{
22
23CVB_BEGIN_INLINE_NS
24
25template <>
26inline HandleGuard<Utilities::StopWatch>::HandleGuard(void * handle) noexcept
27 : HandleGuard<Utilities::StopWatch>(handle, [](void* handle) { CVB_CALL_CAPI(TWDestroy(handle)); })
28{
29}
30
31namespace Utilities
32{
33
34
35
37
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
90
91 StopWatch(const StopWatch& other) = delete;
92 StopWatch& operator=(const StopWatch& other) = delete;
93 StopWatch(StopWatch&& other) noexcept = default;
94 StopWatch& operator=(StopWatch&& other) noexcept = default;
95 ~StopWatch() = default;
96
98
104 void* Handle() const noexcept
105 {
106 return handle_.Handle();
107 }
108
109
111
119 static std::unique_ptr<StopWatch> FromHandle(HandleGuard<StopWatch>&& guard, StopWatchMode mode)
120 {
121 if (!guard.Handle())
122 throw std::runtime_error("handle must not be null");
123
124 return std::unique_ptr<StopWatch>(new StopWatch(std::move(guard), mode));
125 }
126
127
129
136 void Start()
137 {
138 if(!CVB_CALL_CAPI(TWStartStopWatch(Handle(), static_cast<short>(0))))
139 SystemInfo::ThrowLastError();
140 }
141
143
148 {
150 if(!CVB_CALL_CAPI(TWReadStopWatch(Handle(), static_cast<short>(0), &value)))
151 SystemInfo::ThrowLastError();
152 return std::chrono::milliseconds(static_cast<std::size_t>(round(value)));
153 }
154
156
160 StopWatchMode Mode() const noexcept
161 {
162 return mode_;
163 }
164
165
166
167 private:
168
169 StopWatch(HandleGuard<StopWatch>&& guard, StopWatchMode mode) noexcept
170 : handle_(std::move(guard))
171 , mode_(mode)
172 {
173 }
174
175 HandleGuard<StopWatch> handle_;
176
177
179};
180
181}
182
183using Utilities::StopWatch;
184
185CVB_END_INLINE_NS
186
187}
188
189
190
Speed measurement object.
Definition: stop_watch.hpp:47
void Start()
Start (or re-start) the stopwatch.
Definition: stop_watch.hpp:136
std::chrono::milliseconds TimeSpan() const
Gets the time, that has elapsed since start (or since the construction of the object,...
Definition: stop_watch.hpp:147
static std::unique_ptr< StopWatch > FromHandle(HandleGuard< StopWatch > &&guard, StopWatchMode mode)
Creates a stop watch from a classic API handle.
Definition: stop_watch.hpp:119
StopWatch(StopWatchMode mode=StopWatchMode::MultiCPU)
Create a StopWatch object.
Definition: stop_watch.hpp:85
static std::unique_ptr< StopWatch > Create(StopWatchMode mode=StopWatchMode::MultiCPU)
Create a StopWatch object.
Definition: stop_watch.hpp:59
void * Handle() const noexcept
Classic API stop watch handle.
Definition: stop_watch.hpp:104
StopWatchMode Mode() const noexcept
Mode for which the stop watch was created.
Definition: stop_watch.hpp:160
StopWatchMode
Mode at which the StopWatch should work.
Definition: utilities.hpp:31
Root namespace for the Image Manager interface.
Definition: c_barcode.h:15
T quiet_NaN(T... args)