From 6ecf6782b72e0bc6b8e68ae6a3de0a089f2f0cdc Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Tue, 28 Jun 2016 10:00:38 +0200 Subject: [PATCH] added Idle --- src/uvw.hpp | 1 + src/uvw/idle.hpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/uvw/idle.hpp diff --git a/src/uvw.hpp b/src/uvw.hpp index 263cec47..5978f081 100644 --- a/src/uvw.hpp +++ b/src/uvw.hpp @@ -1,4 +1,5 @@ #include "uvw/check.hpp" +#include "uvw/idle.hpp" #include "uvw/loop.hpp" #include "uvw/prepare.hpp" #include "uvw/stream.hpp" diff --git a/src/uvw/idle.hpp b/src/uvw/idle.hpp new file mode 100644 index 00000000..44a2b1ba --- /dev/null +++ b/src/uvw/idle.hpp @@ -0,0 +1,47 @@ +#pragma once + + +#include +#include +#include +#include "resource.hpp" +#include "util.hpp" + + +namespace uvw { + + +class Idle final: public Resource { + static void startCallback(Idle &idle, std::function &cb, uv_idle_t *) { + cb(UVWError{}, idle); + } + + explicit Idle(std::shared_ptr ref) + : Resource{HandleType{}, std::move(ref)} + { + initialized = (uv_idle_init(parent(), get()) == 0); + } + +public: + template + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new Idle{std::forward(args)...}}; + } + + void start(std::function cb) noexcept { + using CBF = CallbackFactory; + auto func = CBF::on<&Idle::startCallback>(*this, cb); + auto err = uv_idle_start(get(), func); + if(err) { error(err); } + } + + UVWError stop() noexcept { return UVWError{uv_idle_stop(get())}; } + + explicit operator bool() const noexcept { return initialized; } + +private: + bool initialized; +}; + + +}