1 #pragma once
2 
3 
4 #include <utility>
5 #include <memory>
6 #include <uv.h>
7 #include "handle.hpp"
8 #include "loop.hpp"
9 
10 
11 namespace uvw {
12 
13 
14 /**
15  * @brief IdleEvent event.
16  *
17  * It will be emitted by IdleHandle according with its functionalities.
18  */
19 struct IdleEvent {};
20 
21 
22 /**
23  * @brief The IdleHandle handle.
24  *
25  * Idle handles will emit a IdleEvent event once per loop iteration, right
26  * before the PrepareHandle handles.
27  *
28  * The notable difference with prepare handles is that when there are active
29  * idle handles, the loop will perform a zero timeout poll instead of blocking
30  * for I/O.
31  *
32  * @note
33  * Despite the name, idle handles will emit events on every loop iteration, not
34  * when the loop is actually _idle_.
35  *
36  * To create an `IdleHandle` through a `Loop`, no arguments are required.
37  */
38 class IdleHandle final: public Handle<IdleHandle, uv_idle_t> {
startCallback(uv_idle_t * handle)39     static void startCallback(uv_idle_t *handle) {
40         IdleHandle &idle = *(static_cast<IdleHandle*>(handle->data));
41         idle.publish(IdleEvent{});
42     }
43 
44 public:
45     using Handle::Handle;
46 
47     /**
48      * @brief Initializes the handle.
49      * @return True in case of success, false otherwise.
50      */
init()51     bool init() {
52         return initialize(&uv_idle_init);
53     }
54 
55     /**
56      * @brief Starts the handle.
57      *
58      * A IdleEvent event will be emitted once per loop iteration, right before
59      * polling the PrepareHandle handles.
60      */
start()61     void start() {
62         invoke(&uv_idle_start, get(), &startCallback);
63     }
64 
65     /**
66      * @brief Stops the handle.
67      */
stop()68     void stop() {
69         invoke(&uv_idle_stop, get());
70     }
71 };
72 
73 
74 }
75