forked from d5/node.native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop.h
More file actions
107 lines (90 loc) · 2.4 KB
/
loop.h
File metadata and controls
107 lines (90 loc) · 2.4 KB
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
#ifndef __LOOP_H__
#define __LOOP_H__
#include "base.h"
#include "error.h"
namespace native
{
/*!
* Class that represents the 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)
: uv_loop_(use_default ? uv_default_loop() : uv_loop_new())
{ }
/*!
* Destructor
*/
~loop()
{
if(uv_loop_)
{
uv_loop_delete(uv_loop_);
uv_loop_ = nullptr;
}
}
/*!
* Returns internal handle for libuv functions.
*/
uv_loop_t* get() { return uv_loop_; }
/*!
* Starts the loop.
* Internally, this function just calls uv_run() function.
*/
bool run() { return uv_run(uv_loop_)==0; }
/*!
* Polls for new events without blocking.
* Internally, this function just calls uv_run_once() function.
*/
bool run_once() { return uv_run_once(uv_loop_)==0; }
/*!
* Increments loop's reference count by 1.
* Internally, this function just calls uv_ref() function.
*/
void ref() { uv_ref(uv_loop_); }
/*!
* Decrements loop's reference count by 1.
* Internally, this function just calls uv_unref() function.
*/
void unref() { uv_unref(uv_loop_); }
/*!
* ...
* Internally, this function just calls uv_update_time() function.
*/
void update_time() { uv_update_time(uv_loop_); }
/*!
* ...
* Internally, this function just calls uv_now() function.
*/
int64_t now() { return uv_now(uv_loop_); }
/*!
* Returns the last error occured in the loop.
*/
error last_error() { return uv_last_error(uv_loop_); }
private:
loop(const loop&);
void operator =(const loop&);
private:
uv_loop_t* uv_loop_;
};
/*!
* Starts the default loop.
*/
int run()
{
return uv_run(uv_default_loop());
}
/*!
* Polls for new events without blocking for the default loop.
*/
int run_once()
{
return uv_run(uv_default_loop());
}
}
#endif