diff --git a/README.md b/README.md index b5962a49..7ad02011 100644 --- a/README.md +++ b/README.md @@ -120,15 +120,15 @@ The following sections will explain in short what it means to initialize and ter #### Handles -Initialization is usually performed under the hood and can be even passed over, as far as handles are created using the `Loop::resource` member method.
+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.
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.
-Thus the rule quickly becomes *always close your handles*. It's simple as calling the `close` member method on them. +Therefore the rule quickly becomes *always close your handles*. It's simple as calling the `close` member function on them. #### Requests -Usually initializing a request object is not required. Anyway, the recommended way to create a request is still through the `Loop::resource` member method.
+Usually initializing a request object is not required. Anyway, the recommended way to create a request is still through the `Loop::resource` member function.
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.
-Thus the rule quickly becomes *feel free to make a request and forget about it*. It's simple as calling a member method on them. +Therefore the rule quickly becomes *feel free to make a request and forget about it*. It's simple as calling a member function on them. #### The Loop and the Resource @@ -136,8 +136,8 @@ The first thing to do to use `uvw` is to create a loop. In case the default one auto loop = uvw::Loop::getDefault(); -Note that loop objects don't require to be closed explicitly, even if they offer the `close` member method in case an user wants to do that.
-Loops can be started using the `run` member method. The two calls below are equivalent: +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 @@ -161,11 +161,14 @@ 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)); - auto data = std::static_pointer_cast(resource->data()); + std::shared_ptr data = resource->data(); -`uvw` expects a `std::shared_pointer` and returns it, thus any kind of data is welcome. +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: -Remember from the previous section that a handle will keep itself alive until one invokes the `close` member method on it.
+ 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 */ }); @@ -203,8 +206,8 @@ There exist two methods to attach an event to a resource: * `resource.on(listener)`: to be used for long-running listeners Both of them return an object of type `ResourceType::Connection` (as an example, `TcpHandle::Connection`).
-A connection object can be used later as an argument to the `erase` member method of the resource to remove the listener.
-There exists also the `clear` member method to drop all the listeners at once. +A connection object can be used later as an argument to the `erase` member function of the resource to remove the listener.
+There exists also the `clear` member function to drop all the listeners at once. Almost all the resources use to emit `ErrorEvent` events in case of errors.
All the other events are specific for the given resource and documented in the API reference. diff --git a/src/uvw/resource.hpp b/src/uvw/resource.hpp index e780fd96..56767ae7 100644 --- a/src/uvw/resource.hpp +++ b/src/uvw/resource.hpp @@ -101,8 +101,9 @@ public: * @brief Gets user-defined data. `uvw` won't use this field in any case. * @return User-defined data if any, an invalid pointer otherwise. */ - std::shared_ptr data() const { - return userData; + template + std::shared_ptr data() const { + return std::static_pointer_cast(userData); } /** diff --git a/test/uvw/resource.cpp b/test/uvw/resource.cpp index 1b366156..d04b6470 100644 --- a/test/uvw/resource.cpp +++ b/test/uvw/resource.cpp @@ -23,6 +23,7 @@ TEST(Resource, Basics) { ASSERT_NO_THROW(resource->data(data)); ASSERT_EQ(*std::static_pointer_cast(resource->data()), 42); + ASSERT_EQ(*resource->data(), 42); resource->close(); loop->run();