forked from larroy/uvpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop.hpp
108 lines (92 loc) · 2.24 KB
/
loop.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#pragma once
#include "error.hpp"
namespace uvpp
{
/**
* Class that represents the uv_loop instance.
*/
class loop
{
public:
/**
* Default constructor
* @param use_default indicates whether to use default loop or create a new loop.
*/
loop(bool use_default=false):
m_uv_loop(use_default ? uv_default_loop() : uv_loop_new())
{
}
/**
* Destructor
*/
~loop()
{
if(m_uv_loop)
{
uv_loop_delete(m_uv_loop);
m_uv_loop = nullptr;
}
}
loop(const loop&) = delete;
loop& operator=(const loop&) = delete;
loop(loop&& other):
m_uv_loop(other.m_uv_loop)
{
if (this != &other)
other.m_uv_loop = nullptr;
}
loop& operator=(loop&& other)
{
if (this != &other)
{
m_uv_loop = other.m_uv_loop;
other.m_uv_loop = nullptr;
}
return *this;
}
/**
* Returns internal handle for libuv functions.
*/
uv_loop_t* get() { return m_uv_loop; }
/**
* Starts the loop.
*/
bool run()
{
return uv_run(m_uv_loop, UV_RUN_DEFAULT) == 0;
}
/**
* Polls for new events without blocking.
*/
bool run_once()
{
return uv_run(m_uv_loop, UV_RUN_ONCE) == 0;
}
/**
* ...
* Internally, this function just calls uv_update_time() function.
*/
void update_time() { uv_update_time(m_uv_loop); }
/**
* ...
* Internally, this function just calls uv_now() function.
*/
int64_t now() { return uv_now(m_uv_loop); }
private:
uv_loop_t* m_uv_loop;
};
/**
* Starts the default loop.
*/
inline int run()
{
return uv_run(uv_default_loop(), UV_RUN_DEFAULT);
}
/**
* Polls for new events without blocking for the default loop.
*/
inline int run_once()
{
return uv_run(uv_default_loop(), UV_RUN_ONCE);
}
}