Skip to content

Commit

Permalink
Merge 8f9aed9 into ea4b6c8
Browse files Browse the repository at this point in the history
  • Loading branch information
tversteeg committed Aug 27, 2019
2 parents ea4b6c8 + 8f9aed9 commit bcdfd62
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/uvw/loop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,35 @@ class Loop final: public Emitter<Loop>, public std::enable_shared_from_this<Loop
return loop;
}

/**
* @brief Wrap an existing loop libuv pointer.
*
* It may return an empty pointer in case of failure.<br>
* This function is a way to use external libraries that expose their own
* loop, such as luv.
*
* @param raw_loop The raw libuv loop pointer.
*
* @return The wrapped loop.
*/
static std::shared_ptr<Loop> fromRaw(uv_loop_t* raw_loop) {
static std::weak_ptr<Loop> ref;
std::shared_ptr<Loop> loop;

if(ref.expired()) {
if(raw_loop) {
auto ptr = std::unique_ptr<uv_loop_t, Deleter>(raw_loop, [](uv_loop_t *){});
loop = std::shared_ptr<Loop>{new Loop{std::move(ptr)}};
}

ref = loop;
} else {
loop = ref.lock();
}

return loop;
}

Loop(const Loop &) = delete;
Loop(Loop &&other) = delete;
Loop & operator=(const Loop &) = delete;
Expand Down

0 comments on commit bcdfd62

Please sign in to comment.