From 3264417f15e2ddd3685dd174fed68aaf11ec541d Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Wed, 28 Aug 2019 16:39:12 +0200 Subject: [PATCH] added Loop::create overload to allow using external resources (close #157) --- src/uvw/loop.hpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/uvw/loop.hpp b/src/uvw/loop.hpp index d174a393..da75f75c 100644 --- a/src/uvw/loop.hpp +++ b/src/uvw/loop.hpp @@ -169,6 +169,21 @@ public: return loop; } + /** + * @brief Initializes a new Loop instance from an existing resource. + * + * The lifetime of the resource must exceed that of the instance to which + * it's associated. Management of the memory associated with the resource is + * in charge of the user. + * + * @param loop A valid pointer to a correctly initialized resource. + * @return A pointer to the newly created loop. + */ + static std::shared_ptr create(uv_loop_t *loop) { + auto ptr = std::unique_ptr{loop, [](uv_loop_t *){}}; + return std::shared_ptr{new Loop{std::move(ptr)}}; + } + /** * @brief Gets the initialized default loop. *