diff --git a/README_8md_source.html b/README_8md_source.html index ac558492..70810010 100644 --- a/README_8md_source.html +++ b/README_8md_source.html @@ -22,7 +22,7 @@
uvw -  1.12.0 +  1.13.0
@@ -63,7 +63,7 @@ $(function() {
README.md
-
1 ![uvw - libuv wrapper in modern C++](https://user-images.githubusercontent.com/1812216/46069406-c977a600-c17b-11e8-9a47-9bba6f412c57.png)
2 
3 <!--
4 @cond TURN_OFF_DOXYGEN
5 -->
6 [![Build Status](https://travis-ci.org/skypjack/uvw.svg?branch=master)](https://travis-ci.org/skypjack/uvw)
7 [![Build status](https://ci.appveyor.com/api/projects/status/m5ndm8gnu8isg2to?svg=true)](https://ci.appveyor.com/project/skypjack/uvw)
8 [![Coverage Status](https://coveralls.io/repos/github/skypjack/uvw/badge.svg?branch=master)](https://coveralls.io/github/skypjack/uvw?branch=master)
9 [![Download](https://api.bintray.com/packages/skypjack/conan/uvw%3Askypjack/images/download.svg)](https://bintray.com/skypjack/conan/uvw%3Askypjack/_latestVersion)
10 [![Gitter chat](https://badges.gitter.im/skypjack/uvw.png)](https://gitter.im/skypjack/uvw)
11 [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=W2HF9FESD5LJY&lc=IT&item_name=Michele%20Caini&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted)
12 
13 [![Patreon](https://c5.patreon.com/external/logo/become_a_patron_button.png)](https://www.patreon.com/bePatron?u=11330786)
14 
15 <!--
16 @endcond TURN_OFF_DOXYGEN
17 -->
18 
19 # Introduction
20 
21 `uvw` is a header-only, event based, tiny and easy to use *libuv* wrapper in modern C++.<br/>
22 The basic idea is to hide completely the *C-ish* interface of *libuv* behind a graceful C++ API. Currently, no `uv_*_t` data structure is actually exposed by the library.<br/>
23 Note that `uvw` stays true to the API of *libuv* and it doesn't add anything to its interface. For the same reasons, users of the library must follow the same rules who are used to follow with *libuv*.<br/>
24 As an example, a *handle* should be initialized before any other operation and closed once it is no longer in use.
25 
26 ## Code Example
27 
28 ```cpp
29 #include <uvw.hpp>
30 #include <memory>
31 
32 void listen(uvw::Loop &loop) {
33  std::shared_ptr<uvw::TCPHandle> tcp = loop.resource<uvw::TCPHandle>();
34 
35  tcp->once<uvw::ListenEvent>([](const uvw::ListenEvent &, uvw::TCPHandle &srv) {
36  std::shared_ptr<uvw::TCPHandle> client = srv.loop().resource<uvw::TCPHandle>();
37 
38  client->on<uvw::CloseEvent>([ptr = srv.shared_from_this()](const uvw::CloseEvent &, uvw::TCPHandle &) { ptr->close(); });
39  client->on<uvw::EndEvent>([](const uvw::EndEvent &, uvw::TCPHandle &client) { client.close(); });
40 
41  srv.accept(*client);
42  client->read();
43  });
44 
45  tcp->bind("127.0.0.1", 4242);
46  tcp->listen();
47 }
48 
49 void conn(uvw::Loop &loop) {
50  auto tcp = loop.resource<uvw::TCPHandle>();
51 
52  tcp->on<uvw::ErrorEvent>([](const uvw::ErrorEvent &, uvw::TCPHandle &) { /* handle errors */ });
53 
54  tcp->once<uvw::ConnectEvent>([](const uvw::ConnectEvent &, uvw::TCPHandle &tcp) {
55  auto dataWrite = std::unique_ptr<char[]>(new char[2]{ 'b', 'c' });
56  tcp.write(std::move(dataWrite), 2);
57  tcp.close();
58  });
59 
60  tcp->connect(std::string{"127.0.0.1"}, 4242);
61 }
62 
63 int main() {
64  auto loop = uvw::Loop::getDefault();
65  listen(*loop);
66  conn(*loop);
67  loop->run();
68 }
69 ```
70 
71 ## Motivation
72 
73 The main reason for which `uvw` has been written is the fact that it does not exist a valid *libuv* wrapper in C++. That's all.
74 
75 # Build Instructions
76 
77 ## Requirements
78 
79 To be able to use `uvw`, users must provide the following system-wide tools:
80 
81 * A full-featured compiler that supports at least C++14.
82 * `libuv` (which version depends on the tag of `uvw` in use).
83 
84 The requirements below are mandatory to compile the tests and to extract the documentation:
85 
86 * CMake version 3.2 or later.
87 * Doxygen version 1.8 or later.
88 
89 Note that `libuv` is part of the dependencies of the project and it will be cloned by `cmake` (see below for further details).<br/>
90 Because of that, users have not to install it to compile and execute the tests.
91 
92 ## Library
93 
94 `uvw` is a header-only library.<br/>
95 This means that including the `uvw.hpp` header or one of the other `uvw/*.hpp` headers is enough to use it.<br/>
96 It's a matter of adding the following line at the top of a file:
97 
98 ```cpp
99 #include <uvw.hpp>
100 ```
101 
102 Then pass the proper `-I` argument to the compiler to add the `src` directory to the include paths.<br/>
103 Note that users are demanded to correctly setup include directories and libraries search paths for *libuv*.
104 
105 ## Versioning
106 
107 Starting with tag _v1.12.0_ of `libuv`, `uvw` follows the [semantic versioning](http://semver.org/) scheme.<br/>
108 The problem is that any version of `uvw` also requires to track explicitly the version of `libuv` to which it is bound.<br/>
109 Because of that, the latter wil be appended to the version of `uvw`. As an example:
110 
111  vU.V.W_libuv-vX.Y
112 
113 In particular, the following applies:
114 
115 * _U.V.W_ are major, minor and patch versions of `uvw`.
116 * _X.Y_ is the version of `libuv` to which to refer (where any patch version is valid).
117 
118 In other terms, tags will look like this from now on:
119 
120  v1.0.0_libuv-v1.12
121 
122 Branch `master` of `uvw` will be a work in progress branch that follows branch _v1.x_ of `libuv` (at least as long as it remains their _master_ branch).<br/>
123 
124 ## Documentation
125 
126 The documentation is based on [`doxygen`](http://www.stack.nl/~dimitri/doxygen/). To build it:
127 
128 * `$ cd build`
129 * `$ cmake ..`
130 * `$ make docs`
131 
132 The API reference will be created in HTML format within the directory `build/docs/html`.<br/>
133 To navigate it with your favorite browser:
134 
135 * `$ cd build`
136 * `$ your_favorite_browser docs/html/index.html`
137 
138 The API reference is also available [online](https://skypjack.github.io/uvw/) for the latest version.
139 
140 ### Note
141 
142 The documentation is mostly inspired by the official [libuv API documentation](http://docs.libuv.org/en/v1.x/) for obvious reasons.<br/>
143 If you are mainly interested in the way `uvw` imports `libuv` in a `cmake` based project, I suggest you to take a look at [this](https://github.com/skypjack/libuv_cmake) repository instead.
144 
145 ## Tests
146 
147 To compile and run the tests, `uvw` requires *libuv* and *googletest*.<br/>
148 `cmake` will download and compile both the libraries before to compile anything else.
149 
150 To build the tests:
151 
152 * `$ cd build`
153 * `$ cmake .. -DBUILD_TESTING=ON`
154 * `$ make`
155 * `$ ctest -j4 -R uvw`
156 
157 Omit `-R uvw` if you also want to test `libuv` and other dependencies.
158 
159 # Crash Course
160 
161 ## Vademecum
162 
163 There is only one rule when using `uvw`: always initialize the resources and terminate them.
164 
165 Resources belong mainly to two families: _handles_ and _requests_.<br/>
166 Handles represent long-lived objects capable of performing certain operations while active.<br/>
167 Requests represent (typically) short-lived operations performed either over a handle or standalone.
168 
169 The following sections will explain in short what it means to initialize and terminate these kinds of resources.<br/>
170 For more details, please refer to the [online documentation](https://skypjack.github.io/uvw/).
171 
172 ## Handles
173 
174 Initialization is usually performed under the hood and can be even passed over, as far as handles are created using the `Loop::resource` member function.<br/>
175 On the other side, handles keep themselves alive until one explicitly closes them. Because of that, memory usage will grow up if users simply forget about a handle.<br/>
176 Therefore the rule quickly becomes *always close your handles*. It's as simple as calling the `close` member function on them.
177 
178 ## Requests
179 
180 Usually initializing a request object is not required. Anyway, the recommended way to create a request is still through the `Loop::resource` member function.<br/>
181 Requests will keep themselves alive as long as they are bound to unfinished underlying activities. This means that users have not to discard explicitly a request.<br/>
182 Therefore the rule quickly becomes *feel free to make a request and forget about it*. It's as simple as calling a member function on them.
183 
184 ## The Loop and the Resource
185 
186 The first thing to do to use `uvw` is to create a loop. In case the default one is enough, it's easy as doing this:
187 
188 ```cpp
189 auto loop = uvw::Loop::getDefault();
190 ```
191 
192 Note that loop objects don't require to be closed explicitly, even if they offer the `close` member function in case an user wants to do that.<br/>
193 Loops can be started using the `run` member function. The two calls below are equivalent:
194 
195 ```cpp
196 loop->run();
197 loop->run<uvw::Loop::Mode::DEFAULT>();
198 ```
199 
200 Available modes are: `DEFAULT`, `ONCE`, `NOWAIT`. Please refer to the documentation of *libuv* for further details.
201 
202 In order to create a resource and to bind it to the given loop, just do the following:
203 
204 ```cpp
205 auto tcp = loop.resource<uvw::TCPHandle>();
206 ```
207 
208 The line above will create and initialize a tcp handle, then a shared pointer to that resource will be returned.<br/>
209 Users should check if pointers have been correctly initialized: in case of errors, they won't be.<br/>
210 Another way to create a resource is:
211 
212 ```cpp
213 auto tcp = TCPHandle::create(loop);
214 tcp->init();
215 ```
216 
217 Pretty annoying indeed. Using a loop is the recommended approach.
218 
219 The resources also accept arbitrary user-data that won't be touched in any case.<br/>
220 Users can set and get them through the `data` member function as it follows:
221 
222 ```cpp
223 resource->data(std::make_shared<int>(42));
224 std::shared_ptr<void> data = resource->data();
225 ```
226 
227 Resources expect a `std::shared_pointer<void>` and return it, therefore any kind of data is welcome.<br/>
228 Users can explicitly specify a type other than `void` when calling the `data` member function:
229 
230 ```cpp
231 std::shared_ptr<int> data = resource->data<int>();
232 ```
233 
234 Remember from the previous section that a handle will keep itself alive until one invokes the `close` member function on it.<br/>
235 To know what are the handles that are still alive and bound to a given loop, just do the following:
236 
237 ```cpp
238 loop.walk([](uvw::BaseHandle &){ /* application code here */ });
239 ```
240 
241 `BaseHandle` exposes a few methods and cannot be promoted to the original type of the handle (even though `type` and `category` member functions fill the gap somehow).<br/>
242 Anyway, it can be used to close the handle that originated from it. As an example, all the pending handles can be closed easily as it follows:
243 
244 ```cpp
245 loop.walk([](uvw::BaseHandle &h){ h.close(); });
246 ```
247 
248 No need to keep track of them.
249 
250 To know what are the available resources' types, please refer the API reference.
251 
252 ## The event-based approach
253 
254 For `uvw` offers an event-based approach, resources are small event emitters to which listeners can be attached.<br/>
255 Attaching a listener to a resource is the recommended way to be notified about changes.<br/>
256 Listeners must be callable objects of type `void(EventType &, ResourceType &)`, where:
257 
258 * `EventType` is the type of the event for which they have been designed.
259 * `ResourceType` is the type of the resource that has originated the event.
260 
261 It means that the following function types are all valid:
262 
263 * `void(EventType &, ResourceType &)`
264 * `void(const EventType &, ResourceType &)`
265 * `void(EventType &, const ResourceType &)`
266 * `void(const EventType &, const ResourceType &)`
267 
268 Once more, please note that there is no need to keep around references to the resources: they will pass themselves as an argument whenever an event is published.
269 
270 There exist two methods to attach an event to a resource:
271 
272 * `resource.once<EventType>(listener)`: the listener will be automatically removed after the first event of the given type.
273 * `resource.on<EventType>(listener)`: to be used for long-running listeners.
274 
275 Both of them return an object of type `ResourceType::Connection` (as an example, `TCPHandle::Connection`).<br/>
276 A connection object can be used later as an argument to the `erase` member function of the resource to remove the listener.<br/>
277 There exists also the `clear` member function to drop all the listeners at once.
278 
279 Almost all the resources use to emit `ErrorEvent` events in case of errors.<br/>
280 All the other events are specific for the given resource and documented in the API reference.
281 
282 The code below shows how to create a simple tcp server using `uvw`:
283 
284 ```cpp
285 auto loop = uvw::Loop::getDefault();
286 auto tcp = loop.resource<uvw::TCPHandle>();
287 
288 tcp->on<uvw::ErrorEvent>([](const uvw::ErrorEvent &, uvw::TCPHandle &) { /* something went wrong */ });
289 
290 tcp->on<uvw::ListenEvent>([](const uvw::ListenEvent &, uvw::TCPHandle &srv) {
291  std::shared_ptr<uvw::TCPHandle> client = srv.loop().resource<uvw::TCPHandle>();
292  client->once<uvw::EndEvent>([](const uvw::EndEvent &, uvw::TCPHandle &client) { client.close(); });
293  client->on<uvw::DataEvent>([](const uvw::DataEvent &, uvw::TCPHandle &) { /* data received */ });
294  srv.accept(*client);
295  client->read();
296 });
297 
298 tcp->bind("127.0.0.1", 4242);
299 tcp->listen();
300 ```
301 
302 Note also that `uvw::TCPHandle` already supports _IPv6_ out-of-the-box. The statement above is equivalent to `tcp->bind<uvw::IPv4>("127.0.0.1", 4242)`.<br/>
303 It's suffice to explicitly specify `uvw::IPv6` as the underlying protocol to use it.
304 
305 The API reference is the recommended documentation for further details about resources and their methods.
306 
307 ## Going raw
308 
309 In case users need to use functionalities not wrapped yet by `uvw` or if they
310 want to get the underlying data structures as defined by `libuv` for some other
311 reasons, almost all the classes in `uvw` give direct access to them.<br/>
312 Please, note that this functions should not be used directly unless users know
313 exactly what they are doing and what are the risks. Going raw is dangerous,
314 mainly because the lifetime management of a loop, a handle or a request is
315 completely in charge to the library and working around it could quickly break
316 things.
317 
318 That being said, _going raw_ is a matter of using the `raw` member functions:
319 
320 ```cpp
321 auto loop = uvw::Loop::getDefault();
322 auto tcp = loop.resource<uvw::TCPHandle>();
323 
324 uv_loop_t *raw = loop->raw();
325 uv_tcp_t *handle = tcp->raw();
326 ```
327 
328 Go the raw way at your own risk, but do not expect any support in case of bugs.
329 
330 # Contributors
331 
332 If you want to contribute, please send patches as pull requests against the branch master.<br/>
333 Check the [contributors list](https://github.com/skypjack/uvw/blob/master/AUTHORS) to see who has partecipated so far.
334 
335 # License
336 
337 Code and documentation Copyright (c) 2016-2018 Michele Caini.<br/>
338 Logo Copyright (c) 2018 Richard Caseres.
339 
340 Code released under
341 [the MIT license](https://github.com/skypjack/uvw/blob/master/LICENSE).
342 Documentation released under
343 [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).<br/>
344 Logo released under
345 [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/).
346 
347 <!--
348 @cond TURN_OFF_DOXYGEN
349 -->
350 # Support
351 
352 ## Patreon
353 
354 Become a [patron](https://www.patreon.com/bePatron?c=1772573) and get access to
355 extra content, help me spend more time on the projects you love and create new
356 ones for you. Your support will help me to continue the work done so far and
357 make it more professional and feature-rich every day.<br/>
358 It takes very little to
359 [become a patron](https://www.patreon.com/bePatron?c=1772573) and thus help the
360 software you use every day. Don't miss the chance.
361 
362 ## Donation
363 
364 Developing and maintaining `uvw` takes some time and lots of coffee. It still lacks a proper test suite, documentation is partially incomplete and not all functionalities have been fully implemented yet.<br/>
365 If you want to support this project, you can offer me an espresso. I'm from Italy, we're used to turning the best coffee ever in code. If you find that it's not enough, feel free to support me the way you prefer.<br/>
366 Take a look at the donation button at the top of the page for more details or just click [here](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=W2HF9FESD5LJY&lc=IT&item_name=Michele%20Caini&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted).
367 
368 ## Hire me
369 
370 If you start using `uvw` and need help, if you want a new feature and want me
371 to give it the highest priority, if you have any other reason to contact me:
372 do not hesitate. I'm available for hiring.<br/>
373 Feel free to take a look at my [profile](https://github.com/skypjack) and
374 contact me by mail.
375 <!--
376 @endcond TURN_OFF_DOXYGEN
377 -->
+
1 ![uvw - libuv wrapper in modern C++](https://user-images.githubusercontent.com/1812216/46069406-c977a600-c17b-11e8-9a47-9bba6f412c57.png)
2 
3 <!--
4 @cond TURN_OFF_DOXYGEN
5 -->
6 [![Build Status](https://travis-ci.org/skypjack/uvw.svg?branch=master)](https://travis-ci.org/skypjack/uvw)
7 [![Build status](https://ci.appveyor.com/api/projects/status/m5ndm8gnu8isg2to?svg=true)](https://ci.appveyor.com/project/skypjack/uvw)
8 [![Coverage Status](https://coveralls.io/repos/github/skypjack/uvw/badge.svg?branch=master)](https://coveralls.io/github/skypjack/uvw?branch=master)
9 [![Download](https://api.bintray.com/packages/skypjack/conan/uvw%3Askypjack/images/download.svg)](https://bintray.com/skypjack/conan/uvw%3Askypjack/_latestVersion)
10 [![Gitter chat](https://badges.gitter.im/skypjack/uvw.png)](https://gitter.im/skypjack/uvw)
11 [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=W2HF9FESD5LJY&lc=IT&item_name=Michele%20Caini&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted)
12 
13 [![Patreon](https://c5.patreon.com/external/logo/become_a_patron_button.png)](https://www.patreon.com/bePatron?u=11330786)
14 
15 <!--
16 @endcond TURN_OFF_DOXYGEN
17 -->
18 
19 # Introduction
20 
21 `uvw` is a header-only, event based, tiny and easy to use *libuv* wrapper in modern C++.<br/>
22 The basic idea is to hide completely the *C-ish* interface of *libuv* behind a graceful C++ API. Currently, no `uv_*_t` data structure is actually exposed by the library.<br/>
23 Note that `uvw` stays true to the API of *libuv* and it doesn't add anything to its interface. For the same reasons, users of the library must follow the same rules who are used to follow with *libuv*.<br/>
24 As an example, a *handle* should be initialized before any other operation and closed once it is no longer in use.
25 
26 ## Code Example
27 
28 ```cpp
29 #include <uvw.hpp>
30 #include <memory>
31 
32 void listen(uvw::Loop &loop) {
33  std::shared_ptr<uvw::TCPHandle> tcp = loop.resource<uvw::TCPHandle>();
34 
35  tcp->once<uvw::ListenEvent>([](const uvw::ListenEvent &, uvw::TCPHandle &srv) {
36  std::shared_ptr<uvw::TCPHandle> client = srv.loop().resource<uvw::TCPHandle>();
37 
38  client->on<uvw::CloseEvent>([ptr = srv.shared_from_this()](const uvw::CloseEvent &, uvw::TCPHandle &) { ptr->close(); });
39  client->on<uvw::EndEvent>([](const uvw::EndEvent &, uvw::TCPHandle &client) { client.close(); });
40 
41  srv.accept(*client);
42  client->read();
43  });
44 
45  tcp->bind("127.0.0.1", 4242);
46  tcp->listen();
47 }
48 
49 void conn(uvw::Loop &loop) {
50  auto tcp = loop.resource<uvw::TCPHandle>();
51 
52  tcp->on<uvw::ErrorEvent>([](const uvw::ErrorEvent &, uvw::TCPHandle &) { /* handle errors */ });
53 
54  tcp->once<uvw::ConnectEvent>([](const uvw::ConnectEvent &, uvw::TCPHandle &tcp) {
55  auto dataWrite = std::unique_ptr<char[]>(new char[2]{ 'b', 'c' });
56  tcp.write(std::move(dataWrite), 2);
57  tcp.close();
58  });
59 
60  tcp->connect(std::string{"127.0.0.1"}, 4242);
61 }
62 
63 int main() {
64  auto loop = uvw::Loop::getDefault();
65  listen(*loop);
66  conn(*loop);
67  loop->run();
68 }
69 ```
70 
71 ## Motivation
72 
73 The main reason for which `uvw` has been written is the fact that it does not exist a valid *libuv* wrapper in C++. That's all.
74 
75 # Build Instructions
76 
77 ## Requirements
78 
79 To be able to use `uvw`, users must provide the following system-wide tools:
80 
81 * A full-featured compiler that supports at least C++14.
82 * `libuv` (which version depends on the tag of `uvw` in use).
83 
84 The requirements below are mandatory to compile the tests and to extract the documentation:
85 
86 * CMake version 3.2 or later.
87 * Doxygen version 1.8 or later.
88 
89 Note that `libuv` is part of the dependencies of the project and it will be cloned by `cmake` (see below for further details).<br/>
90 Because of that, users have not to install it to compile and execute the tests.
91 
92 ## Library
93 
94 `uvw` is a header-only library.<br/>
95 This means that including the `uvw.hpp` header or one of the other `uvw/*.hpp` headers is enough to use it.<br/>
96 It's a matter of adding the following line at the top of a file:
97 
98 ```cpp
99 #include <uvw.hpp>
100 ```
101 
102 Then pass the proper `-I` argument to the compiler to add the `src` directory to the include paths.<br/>
103 Note that users are demanded to correctly setup include directories and libraries search paths for *libuv*.
104 
105 ## Versioning
106 
107 Starting with tag _v1.12.0_ of `libuv`, `uvw` follows the [semantic versioning](http://semver.org/) scheme.<br/>
108 The problem is that any version of `uvw` also requires to track explicitly the version of `libuv` to which it is bound.<br/>
109 Because of that, the latter wil be appended to the version of `uvw`. As an example:
110 
111  vU.V.W_libuv-vX.Y
112 
113 In particular, the following applies:
114 
115 * _U.V.W_ are major, minor and patch versions of `uvw`.
116 * _X.Y_ is the version of `libuv` to which to refer (where any patch version is valid).
117 
118 In other terms, tags will look like this from now on:
119 
120  v1.0.0_libuv-v1.12
121 
122 Branch `master` of `uvw` will be a work in progress branch that follows branch _v1.x_ of `libuv` (at least as long as it remains their _master_ branch).<br/>
123 
124 ## Documentation
125 
126 The documentation is based on [`doxygen`](http://www.stack.nl/~dimitri/doxygen/). To build it:
127 
128 * `$ cd build`
129 * `$ cmake ..`
130 * `$ make docs`
131 
132 The API reference will be created in HTML format within the directory `build/docs/html`.<br/>
133 To navigate it with your favorite browser:
134 
135 * `$ cd build`
136 * `$ your_favorite_browser docs/html/index.html`
137 
138 The API reference is also available [online](https://skypjack.github.io/uvw/) for the latest version.
139 
140 ### Note
141 
142 The documentation is mostly inspired by the official [libuv API documentation](http://docs.libuv.org/en/v1.x/) for obvious reasons.<br/>
143 If you are mainly interested in the way `uvw` imports `libuv` in a `cmake` based project, I suggest you to take a look at [this](https://github.com/skypjack/libuv_cmake) repository instead.
144 
145 ## Tests
146 
147 To compile and run the tests, `uvw` requires *libuv* and *googletest*.<br/>
148 `cmake` will download and compile both the libraries before to compile anything else.
149 
150 To build the tests:
151 
152 * `$ cd build`
153 * `$ cmake .. -DBUILD_TESTING=ON`
154 * `$ make`
155 * `$ ctest -j4 -R uvw`
156 
157 Omit `-R uvw` if you also want to test `libuv` and other dependencies.
158 
159 # Crash Course
160 
161 ## Vademecum
162 
163 There is only one rule when using `uvw`: always initialize the resources and terminate them.
164 
165 Resources belong mainly to two families: _handles_ and _requests_.<br/>
166 Handles represent long-lived objects capable of performing certain operations while active.<br/>
167 Requests represent (typically) short-lived operations performed either over a handle or standalone.
168 
169 The following sections will explain in short what it means to initialize and terminate these kinds of resources.<br/>
170 For more details, please refer to the [online documentation](https://skypjack.github.io/uvw/).
171 
172 ## Handles
173 
174 Initialization is usually performed under the hood and can be even passed over, as far as handles are created using the `Loop::resource` member function.<br/>
175 On the other side, handles keep themselves alive until one explicitly closes them. Because of that, memory usage will grow up if users simply forget about a handle.<br/>
176 Therefore the rule quickly becomes *always close your handles*. It's as simple as calling the `close` member function on them.
177 
178 ## Requests
179 
180 Usually initializing a request object is not required. Anyway, the recommended way to create a request is still through the `Loop::resource` member function.<br/>
181 Requests will keep themselves alive as long as they are bound to unfinished underlying activities. This means that users have not to discard explicitly a request.<br/>
182 Therefore the rule quickly becomes *feel free to make a request and forget about it*. It's as simple as calling a member function on them.
183 
184 ## The Loop and the Resource
185 
186 The first thing to do to use `uvw` is to create a loop. In case the default one is enough, it's easy as doing this:
187 
188 ```cpp
189 auto loop = uvw::Loop::getDefault();
190 ```
191 
192 Note that loop objects don't require to be closed explicitly, even if they offer the `close` member function in case an user wants to do that.<br/>
193 Loops can be started using the `run` member function. The two calls below are equivalent:
194 
195 ```cpp
196 loop->run();
197 loop->run<uvw::Loop::Mode::DEFAULT>();
198 ```
199 
200 Available modes are: `DEFAULT`, `ONCE`, `NOWAIT`. Please refer to the documentation of *libuv* for further details.
201 
202 In order to create a resource and to bind it to the given loop, just do the following:
203 
204 ```cpp
205 auto tcp = loop.resource<uvw::TCPHandle>();
206 ```
207 
208 The line above will create and initialize a tcp handle, then a shared pointer to that resource will be returned.<br/>
209 Users should check if pointers have been correctly initialized: in case of errors, they won't be.<br/>
210 Another way to create a resource is:
211 
212 ```cpp
213 auto tcp = TCPHandle::create(loop);
214 tcp->init();
215 ```
216 
217 Pretty annoying indeed. Using a loop is the recommended approach.
218 
219 The resources also accept arbitrary user-data that won't be touched in any case.<br/>
220 Users can set and get them through the `data` member function as it follows:
221 
222 ```cpp
223 resource->data(std::make_shared<int>(42));
224 std::shared_ptr<void> data = resource->data();
225 ```
226 
227 Resources expect a `std::shared_pointer<void>` and return it, therefore any kind of data is welcome.<br/>
228 Users can explicitly specify a type other than `void` when calling the `data` member function:
229 
230 ```cpp
231 std::shared_ptr<int> data = resource->data<int>();
232 ```
233 
234 Remember from the previous section that a handle will keep itself alive until one invokes the `close` member function on it.<br/>
235 To know what are the handles that are still alive and bound to a given loop, just do the following:
236 
237 ```cpp
238 loop.walk([](uvw::BaseHandle &){ /* application code here */ });
239 ```
240 
241 `BaseHandle` exposes a few methods and cannot be promoted to the original type of the handle (even though `type` and `category` member functions fill the gap somehow).<br/>
242 Anyway, it can be used to close the handle that originated from it. As an example, all the pending handles can be closed easily as it follows:
243 
244 ```cpp
245 loop.walk([](uvw::BaseHandle &h){ h.close(); });
246 ```
247 
248 No need to keep track of them.
249 
250 To know what are the available resources' types, please refer the API reference.
251 
252 ## The event-based approach
253 
254 For `uvw` offers an event-based approach, resources are small event emitters to which listeners can be attached.<br/>
255 Attaching a listener to a resource is the recommended way to be notified about changes.<br/>
256 Listeners must be callable objects of type `void(EventType &, ResourceType &)`, where:
257 
258 * `EventType` is the type of the event for which they have been designed.
259 * `ResourceType` is the type of the resource that has originated the event.
260 
261 It means that the following function types are all valid:
262 
263 * `void(EventType &, ResourceType &)`
264 * `void(const EventType &, ResourceType &)`
265 * `void(EventType &, const ResourceType &)`
266 * `void(const EventType &, const ResourceType &)`
267 
268 Once more, please note that there is no need to keep around references to the resources: they will pass themselves as an argument whenever an event is published.
269 
270 There exist two methods to attach an event to a resource:
271 
272 * `resource.once<EventType>(listener)`: the listener will be automatically removed after the first event of the given type.
273 * `resource.on<EventType>(listener)`: to be used for long-running listeners.
274 
275 Both of them return an object of type `ResourceType::Connection` (as an example, `TCPHandle::Connection`).<br/>
276 A connection object can be used later as an argument to the `erase` member function of the resource to remove the listener.<br/>
277 There exists also the `clear` member function to drop all the listeners at once.
278 
279 Almost all the resources use to emit `ErrorEvent` events in case of errors.<br/>
280 All the other events are specific for the given resource and documented in the API reference.
281 
282 The code below shows how to create a simple tcp server using `uvw`:
283 
284 ```cpp
285 auto loop = uvw::Loop::getDefault();
286 auto tcp = loop.resource<uvw::TCPHandle>();
287 
288 tcp->on<uvw::ErrorEvent>([](const uvw::ErrorEvent &, uvw::TCPHandle &) { /* something went wrong */ });
289 
290 tcp->on<uvw::ListenEvent>([](const uvw::ListenEvent &, uvw::TCPHandle &srv) {
291  std::shared_ptr<uvw::TCPHandle> client = srv.loop().resource<uvw::TCPHandle>();
292  client->once<uvw::EndEvent>([](const uvw::EndEvent &, uvw::TCPHandle &client) { client.close(); });
293  client->on<uvw::DataEvent>([](const uvw::DataEvent &, uvw::TCPHandle &) { /* data received */ });
294  srv.accept(*client);
295  client->read();
296 });
297 
298 tcp->bind("127.0.0.1", 4242);
299 tcp->listen();
300 ```
301 
302 Note also that `uvw::TCPHandle` already supports _IPv6_ out-of-the-box. The statement above is equivalent to `tcp->bind<uvw::IPv4>("127.0.0.1", 4242)`.<br/>
303 It's suffice to explicitly specify `uvw::IPv6` as the underlying protocol to use it.
304 
305 The API reference is the recommended documentation for further details about resources and their methods.
306 
307 ## Going raw
308 
309 In case users need to use functionalities not wrapped yet by `uvw` or if they
310 want to get the underlying data structures as defined by `libuv` for some other
311 reasons, almost all the classes in `uvw` give direct access to them.<br/>
312 Please, note that this functions should not be used directly unless users know
313 exactly what they are doing and what are the risks. Going raw is dangerous,
314 mainly because the lifetime management of a loop, a handle or a request is
315 completely in charge to the library and working around it could quickly break
316 things.
317 
318 That being said, _going raw_ is a matter of using the `raw` member functions:
319 
320 ```cpp
321 auto loop = uvw::Loop::getDefault();
322 auto tcp = loop.resource<uvw::TCPHandle>();
323 
324 uv_loop_t *raw = loop->raw();
325 uv_tcp_t *handle = tcp->raw();
326 ```
327 
328 Go the raw way at your own risk, but do not expect any support in case of bugs.
329 
330 # Contributors
331 
332 If you want to contribute, please send patches as pull requests against the branch master.<br/>
333 Check the [contributors list](https://github.com/skypjack/uvw/blob/master/AUTHORS) to see who has partecipated so far.
334 
335 # License
336 
337 Code and documentation Copyright (c) 2016-2019 Michele Caini.<br/>
338 Logo Copyright (c) 2018-2019 Richard Caseres.
339 
340 Code released under
341 [the MIT license](https://github.com/skypjack/uvw/blob/master/LICENSE).
342 Documentation released under
343 [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).<br/>
344 Logo released under
345 [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/).
346 
347 <!--
348 @cond TURN_OFF_DOXYGEN
349 -->
350 # Support
351 
352 ## Patreon
353 
354 Become a [patron](https://www.patreon.com/bePatron?c=1772573) and get access to
355 extra content, help me spend more time on the projects you love and create new
356 ones for you. Your support will help me to continue the work done so far and
357 make it more professional and feature-rich every day.<br/>
358 It takes very little to
359 [become a patron](https://www.patreon.com/bePatron?c=1772573) and thus help the
360 software you use every day. Don't miss the chance.
361 
362 ## Donation
363 
364 Developing and maintaining `uvw` takes some time and lots of coffee. It still lacks a proper test suite, documentation is partially incomplete and not all functionalities have been fully implemented yet.<br/>
365 If you want to support this project, you can offer me an espresso. I'm from Italy, we're used to turning the best coffee ever in code. If you find that it's not enough, feel free to support me the way you prefer.<br/>
366 Take a look at the donation button at the top of the page for more details or just click [here](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=W2HF9FESD5LJY&lc=IT&item_name=Michele%20Caini&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted).
367 
368 ## Hire me
369 
370 If you start using `uvw` and need help, if you want a new feature and want me
371 to give it the highest priority, if you have any other reason to contact me:
372 do not hesitate. I'm available for hiring.<br/>
373 Feel free to take a look at my [profile](https://github.com/skypjack) and
374 contact me by mail.
375 <!--
376 @endcond TURN_OFF_DOXYGEN
377 -->