diff --git a/README.md b/README.md index a7db2ddd..8af57c4f 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ As an example, a *handle* should be initialized before any other operation and c ## Code Example -``` +```cpp #include #include @@ -86,7 +86,9 @@ Because of that, users have not to install it to compile and execute the tests. This means that including the `uvw.hpp` header or one of the other `uvw/*.hpp` headers is enough to use it.
It's a matter of adding the following line at the top of a file: - #include +```cpp +#include +``` Then pass the proper `-I` argument to the compiler to add the `src` directory to the include paths.
Note that users are demanded to correctly setup include directories and libraries search paths for *libuv*. @@ -154,49 +156,65 @@ Therefore the rule quickly becomes *feel free to make a request and forget about 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: - auto loop = uvw::Loop::getDefault(); +```cpp +auto loop = uvw::Loop::getDefault(); +``` 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.
Loops can be started using the `run` member function. The two calls below are equivalent: - loop->run(); - loop->run +```cpp +loop->run(); +loop->run +``` Available modes are: `DEFAULT`, `ONCE`, `NOWAIT`. Please refer to the documentation of *libuv* for further details. In order to create a resource and to bind it to the given loop, just do the following: - auto tcp = loop.resource(); +```cpp +auto tcp = loop.resource(); +``` The line above will create and initialize a tcp handle, then a shared pointer to that resource will be returned.
Users should check if pointers have been correctly initialized: in case of errors, they won't be.
Another way to create a resource is: - auto tcp = TcpHandle::create(loop); - tcp->init(); +```cpp +auto tcp = TcpHandle::create(loop); +tcp->init(); +``` Pretty annoying indeed. Using a loop is the recommended approach. The resources also accept arbitrary user-data that won't be touched in any case.
Users can set and get them through the `data` member function as it follows: - resource->data(std::make_shared(42)); - std::shared_ptr data = resource->data(); +```cpp +resource->data(std::make_shared(42)); +std::shared_ptr data = resource->data(); +``` Resources expect a `std::shared_pointer` and return it, therefore any kind of data is welcome.
Users can explicitly specify a type other than `void` when calling the `data` member function: - std::shared_ptr data = resource->data(); +```cpp +std::shared_ptr data = resource->data(); +``` Remember from the previous section that a handle will keep itself alive until one invokes the `close` member function on it.
To know what are the handles that are still alive and bound to a given loop, just do the following: - loop.walk([](uvw::BaseHandle &){ /* application code here */ }); +```cpp +loop.walk([](uvw::BaseHandle &){ /* application code here */ }); +``` `BaseHandle` exposes a few methods and cannot be used to know the original type of the handle.
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: - loop.walk([](uvw::BaseHandle &h){ h.close(); }); +```cpp +loop.walk([](uvw::BaseHandle &h){ h.close(); }); +``` No need to keep track of them. @@ -234,7 +252,7 @@ All the other events are specific for the given resource and documented in the A The code below shows how to create a simple tcp server using `uvw`: -``` +```cpp auto loop = uvw::Loop::getDefault(); auto tcp = loop.resource();