added Loop::walk

This commit is contained in:
Michele Caini 2016-07-04 18:00:14 +02:00
parent cd7584eb5f
commit b4eb62f223
3 changed files with 35 additions and 7 deletions

View File

@ -22,7 +22,11 @@ template<> struct HandleType<uv_tcp_t> { };
template<typename T>
class Handle: public Emitter<T>, public std::enable_shared_from_this<T> {
class Handle
: public BaseHandle,
public Emitter<T>,
public std::enable_shared_from_this<T>
{
struct BaseWrapper {
virtual ~BaseWrapper() = default;
virtual void * get() const noexcept = 0;
@ -93,14 +97,14 @@ public:
Loop& loop() const noexcept { return *pLoop; }
bool active() const noexcept { return !(uv_is_active(get<uv_handle_t>()) == 0); }
bool closing() const noexcept { return !(uv_is_closing(get<uv_handle_t>()) == 0); }
bool active() const noexcept override { return !(uv_is_active(get<uv_handle_t>()) == 0); }
bool closing() const noexcept override { return !(uv_is_closing(get<uv_handle_t>()) == 0); }
void reference() noexcept { uv_ref(get<uv_handle_t>()); }
void unreference() noexcept { uv_unref(get<uv_handle_t>()); }
bool referenced() const noexcept { return !(uv_has_ref(get<uv_handle_t>()) == 0); }
void reference() noexcept override { uv_ref(get<uv_handle_t>()); }
void unreference() noexcept override { uv_unref(get<uv_handle_t>()); }
bool referenced() const noexcept override { return !(uv_has_ref(get<uv_handle_t>()) == 0); }
void close() noexcept {
void close() noexcept override {
if(!closing()) {
uv_close(get<uv_handle_t>(), &Handle<T>::closeCallback);
}

View File

@ -13,6 +13,17 @@
namespace uvw {
class BaseHandle {
public:
virtual bool active() const noexcept = 0;
virtual bool closing() const noexcept = 0;
virtual void reference() noexcept = 0;
virtual void unreference() noexcept = 0;
virtual bool referenced() const noexcept = 0;
virtual void close() noexcept = 0;
};
class Loop final: public Emitter<Loop>, public std::enable_shared_from_this<Loop> {
template<typename>
friend class Handle;
@ -96,6 +107,16 @@ public:
uv_stop(loop.get());
}
void walk(std::function<void(BaseHandle &)> callback) noexcept {
// remember: non-capturing lambdas decay to pointers to functions
uv_walk(loop.get(), [](uv_handle_t *handle, void *func) {
BaseHandle &ref = *static_cast<BaseHandle*>(handle->data);
std::function<void(BaseHandle &)> &f =
*static_cast<std::function<void(BaseHandle &)>*>(func);
f(ref);
}, &callback);
}
private:
std::unique_ptr<uv_loop_t, Deleter> loop;
};

View File

@ -43,6 +43,9 @@ void listen(uvw::Loop &loop) {
client->on<uvw::EndEvent>([](const uvw::EndEvent &, uvw::Tcp &client) {
std::cout << "end" << std::endl;
int count = 0;
client.loop().walk([&count](uvw::BaseHandle &handle) { ++count; });
std::cout << "still alive: " << count << " handles" << std::endl;
client.close();
});