GA::kit v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
timeout.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <functional>
5#include <iostream>
6#include <sstream>
7#include <string>
8#include <vector>
9#include "ga/timer.h"
10
11namespace ga {
12
13//
14// Timeout represents a function callback
15// with an attached timer, for scheduling execution
16//
17
18struct Timeout
19{
20
22 std::function<void( void )> callback;
23 std::string name;
24 void clear()
25 {
26 timer.clear();
27 callback = nullptr;
28 name = "";
29 }
30};
31
32//
33// TimeoutManager
34// owns a set of function callbacks that fire after a specified wait
35//
36
38{
39
40public:
41 // -----------------------------------------------------------------------------------
42 // register a Timeout callback to be called 'delayMs' milliseconds from now
43 // returns the name of the Timeout (generated from the function pointer if left blank)
44 // -----------------------------------------------------------------------------------
45 inline std::string setTimeout( long long delayMs, std::function<void( void )> callback, std::string name = "" )
46 {
47 if ( !callback ) return "";
48
49 m_timeouts.push_back( Timeout() );
50 m_timeouts.back().callback = callback;
51 m_timeouts.back().timer.startNow( delayMs );
52
53 if ( name.empty() ) {
54 // use the callback pointer address as the name, if name is empty
55 // get underlying address of function ptr - https://stackoverflow.com/a/44236212/5195277
56 std::stringstream ss;
57 ss << *( size_t* )( char* )&m_timeouts.back().callback;
58 name = ss.str();
59 }
60
61 m_timeouts.back().name = name;
62
63 // sort the timeouts chronologically (by end time)
64 std::sort( m_timeouts.begin(), m_timeouts.end(),
65 []( Timeout& a, Timeout& b ) {
66 return a.timer.getEnd() < b.timer.getEnd();
67 } );
68 return name;
69 }
70
71 // ------------------------
72 // fire and remove timeouts
73 // ------------------------
74 inline void updateTimeouts()
75 {
76
77 m_timeouts.erase(
78 std::remove_if( m_timeouts.begin(), m_timeouts.end(), []( ga::Timeout& evt ) -> bool {
79 if ( evt.timer.isDone() ) {
80 if ( evt.callback ) {
81 try {
82 evt.callback();
83 // TODO: ga::log
84 // ga::log::verbose << "[" << evt.name << "] Timeout fired";
85 } catch ( std::exception& e ) {
86 // TODO: ga::log
87 std::cout << "ERROR [" << evt.name << "] Timeout callback exception:\n\t" << e.what() << std::endl;
88 }
89 }
90 return true;
91 }
92 return !evt.timer.isSet(); // prune unset events
93 } ),
94 m_timeouts.end() );
95 }
96
97 // ------------------------
98 // cancel a timeout by name
99 // ------------------------
100 inline bool cancelTimeout( std::string name )
101 {
102 bool did = false;
103
104 m_timeouts.erase(
105 std::remove_if( m_timeouts.begin(), m_timeouts.end(), [&]( Timeout& evt ) -> bool {
106 if ( evt.name == name ) {
107 evt.clear();
108 return did = true;
109 }
110 return false;
111 } ),
112 m_timeouts.end() );
113 return did;
114 }
115
116 // ---------------------------------------
117 // return the registered timeout callbacks
118 // ---------------------------------------
119 inline std::vector<Timeout>& getTimeouts()
120 {
121 return m_timeouts;
122 }
123
124protected:
125 std::vector<Timeout> m_timeouts;
126};
127
128} // namespace ga
Definition: timeout.h:38
bool cancelTimeout(std::string name)
Definition: timeout.h:100
std::string setTimeout(long long delayMs, std::function< void(void)> callback, std::string name="")
Definition: timeout.h:45
std::vector< Timeout > & getTimeouts()
Definition: timeout.h:119
void updateTimeouts()
Definition: timeout.h:74
std::vector< Timeout > m_timeouts
Definition: timeout.h:125
Definition: timer.h:46
virtual void clear()
Definition: timer.cpp:27
virtual bool isSet()
Definition: timer.h:68
Definition: color.h:9
Definition: timeout.h:19
std::function< void(void)> callback
Definition: timeout.h:22
void clear()
Definition: timeout.h:24
std::string name
Definition: timeout.h:23
Timer timer
Definition: timeout.h:21