added Idle

This commit is contained in:
Michele Caini 2016-06-28 10:00:38 +02:00
parent fc17af440f
commit 6ecf6782b7
2 changed files with 48 additions and 0 deletions

View File

@ -1,4 +1,5 @@
#include "uvw/check.hpp"
#include "uvw/idle.hpp"
#include "uvw/loop.hpp"
#include "uvw/prepare.hpp"
#include "uvw/stream.hpp"

47
src/uvw/idle.hpp Normal file
View File

@ -0,0 +1,47 @@
#pragma once
#include <utility>
#include <memory>
#include <uv.h>
#include "resource.hpp"
#include "util.hpp"
namespace uvw {
class Idle final: public Resource<Idle> {
static void startCallback(Idle &idle, std::function<void(UVWError, Idle &)> &cb, uv_idle_t *) {
cb(UVWError{}, idle);
}
explicit Idle(std::shared_ptr<Loop> ref)
: Resource{HandleType<uv_idle_t>{}, std::move(ref)}
{
initialized = (uv_idle_init(parent(), get<uv_idle_t>()) == 0);
}
public:
template<typename... Args>
static std::shared_ptr<Idle> create(Args&&... args) {
return std::shared_ptr<Idle>{new Idle{std::forward<Args>(args)...}};
}
void start(std::function<void(UVWError, Idle &)> cb) noexcept {
using CBF = CallbackFactory<void(uv_idle_t *)>;
auto func = CBF::on<&Idle::startCallback>(*this, cb);
auto err = uv_idle_start(get<uv_idle_t>(), func);
if(err) { error(err); }
}
UVWError stop() noexcept { return UVWError{uv_idle_stop(get<uv_idle_t>())}; }
explicit operator bool() const noexcept { return initialized; }
private:
bool initialized;
};
}