Skip to content

Commit

Permalink
Add fromRaw function to loop
Browse files Browse the repository at this point in the history
  • Loading branch information
tversteeg committed Aug 27, 2019
1 parent ea4b6c8 commit 8f9aed9
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 8f9aed9

Please sign in to comment.