Developer Application Interface (ARC API) v4.5.8
ARC, Inc. GenIV Application Interface
CArcTimer.h
1// +------------------------------------------------------------------------------------------------------------------+
2// | FILE: CArcTimer.h ( GenIV ) |
3// +------------------------------------------------------------------------------------------------------------------+
4// | PURPOSE: This file defines the ARC timer class. |
5// | |
6// | AUTHOR: Scott Streit DATE: July 30, 2020 |
7// | |
8// | Copyright 2020 Astronomical Research Cameras, Inc. All rights reserved. |
9// +------------------------------------------------------------------------------------------------------------------+
10
11#pragma once
12
13#include <chrono>
14#include <memory>
15
16#include <CArcBaseDllMain.h>
17#include <CArcBase.h>
18
19
20namespace arc
21{
22 namespace gen4
23 {
24
29 class GEN4_CARCBASE_API CArcTimer : public CArcBase
30 {
31 public:
32
34 ~CArcTimer( void );
35
42 template <typename T>
43 static arc::gen4::CArcTimer* create( std::function<void( void )> fnCallback, const T& tDuration )
44 {
45 CArcTimer* pTimer = new CArcTimer();
46
47 if ( pTimer != nullptr )
48 {
49 pTimer->start<T>( std::ref( fnCallback ), tDuration );
50 }
51
52 return pTimer;
53 }
54
56 void cancel( void ) noexcept;
57
58 private:
59
61 CArcTimer( void );
62
68 template <typename T>
69 void start( std::function<void( void )> fnCallBack, const T& tDuration )
70 {
71 m_fnCallBack = std::move( fnCallBack );
72
73 std::cout << "NEW TIMER (START) - ED!" << std::endl;
74
75 std::unique_ptr<std::thread> pThread( new std::thread( [ &, this ]()
76 {
77 try
78 {
79 if ( this->m_bCancel ) { std::cout << "TIMER (START) cancelled 1" << std::endl; return; }
80
81 auto start = std::chrono::high_resolution_clock::now();
82
83 while ( !this->m_bCancel )
84 {
85 auto now = std::chrono::high_resolution_clock::now();
86
87 if ( std::chrono::duration_cast< T >( now - start ) >= tDuration )
88 {
89 break;
90 }
91 }
92
93 if ( this->m_bCancel ) { std::cout << "TIMER (START) cancelled 2" << std::endl; return; }
94
95 if ( m_fnCallBack != nullptr )
96 {
97 m_fnCallBack();
98 }
99
100 else
101 {
102 std::cout << "FUNCTION CLALBACK (START) is nullptr!" << std::endl;
103 }
104 }
105 catch ( ... ) {}
106
107 std::cout << "TIMER (START) completed!" << std::endl;
108 } ) );
109
110 pThread->detach();
111 }
112
114 std::function<void(void)> m_fnCallBack;
115
117 bool m_bCancel;
118 };
119
120 } // end gen4 namespace
121} // end arc namespace
static arc::gen4::CArcTimer * create(std::function< void(void)> fnCallback, const T &tDuration)
Definition: CArcTimer.h:43
void cancel(void) noexcept
Definition: CArcBase.h:50