Skip to content

Commit

Permalink
Adds basic event functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
rioki committed Jan 1, 2014
1 parent e127a3a commit e00567b
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Makefile.am
@@ -1,5 +1,7 @@
ACLOCAL_AMFLAGS = -I m4

AM_CXXFLAGS = -g -Wall

pkginclude_HEADERS = c9y/c9y.h \
c9y/Thread.h \
c9y/Barrier.h \
Expand Down Expand Up @@ -28,4 +30,4 @@ c9y_test_LDADD = libc9y.la @TEST_LIBS@
c9y_test_SOURCES = test/main.cpp \
test/basics.cpp

EXTRA_DIST = README.md
EXTRA_DIST = README.md
82 changes: 80 additions & 2 deletions c9y/globals.cpp
Expand Up @@ -27,6 +27,39 @@ namespace c9y
uv_run(uv_default_loop(), uvm);
}

struct OnceBaton
{
uv_idle_t handle;
std::function<void ()> func;
};

void on_once(uv_idle_t* handle, int status)
{
OnceBaton* baton = reinterpret_cast<OnceBaton*>(handle->data);
baton->func();
uv_idle_stop(handle);
delete baton;
}

void once(std::function<void ()> func)
{
OnceBaton* baton = new OnceBaton;
baton->handle.data = baton;
baton->func = func;

int r = uv_idle_init(uv_default_loop(), &baton->handle);
if (r < 0)
{
throw std::logic_error(uv_strerror(r));
}

r = uv_idle_start(&baton->handle, on_once);
if (r < 0)
{
throw std::logic_error(uv_strerror(r));
}
}

struct Interval
{
uv_timer_t handle;
Expand Down Expand Up @@ -56,12 +89,57 @@ namespace c9y
{
throw std::logic_error(uv_strerror(r));
}
}


return baton;
}

void clear_interval(Interval* baton)
{
uv_timer_stop(&baton->handle);
once([baton] () {
delete baton;
});
}

struct Timeout
{
uv_timer_t handle;
std::function<void ()> func;
};

void on_timeout(uv_timer_t* handle, int status)
{
Timeout* baton = reinterpret_cast<Timeout*>(handle->data);
baton->func();
delete baton;
}

Timeout* set_timeout(unsigned int ms, std::function<void ()> func)
{
Timeout* baton = new Timeout;
baton->handle.data = baton;
baton->func = func;

int r = uv_timer_init(uv_default_loop(), &baton->handle);
if (r < 0)
{
throw std::logic_error(uv_strerror(r));
}

r = uv_timer_start(&baton->handle, on_timeout, ms, 0);
if (r < 0)
{
throw std::logic_error(uv_strerror(r));
}

return baton;
}

void clear_timeout(Timeout* baton)
{
uv_timer_stop(&baton->handle);
once([baton] () {
delete baton;
});
}
}
11 changes: 11 additions & 0 deletions c9y/globals.h
Expand Up @@ -20,6 +20,11 @@ namespace c9y
* Run the global event loop.
**/
void run(RunMode mode = NORMAL);

/**
* Execute this function once in the event loop.
**/
void once(std::function<void ()> func);

//! Handle for an interval
struct Interval;
Expand All @@ -33,6 +38,12 @@ namespace c9y
* Clear a given interval callback.
**/
void clear_interval(Interval* baton);

struct Timeout;

Timeout* set_timeout(unsigned int ms, std::function<void ()> func);

void clear_timeout(Timeout* baton);
}

#endif
24 changes: 24 additions & 0 deletions test/basics.cpp
Expand Up @@ -23,6 +23,18 @@ SUITE(basics)
// does not block
}

TEST(once)
{
unsigned int count = 0;
c9y::once([&] () {
count++;
});

c9y::run();

CHECK_EQUAL(1, count);
}

TEST(inverval)
{
unsigned int count = 0;
Expand All @@ -39,4 +51,16 @@ SUITE(basics)

CHECK_EQUAL(5, count);
}

TEST(timeout)
{
unsigned int count = 0;
c9y::Timeout* handle = c9y::set_timeout(10, [&] () {
count++;
});

c9y::run();

CHECK_EQUAL(1, count);
}
}

0 comments on commit e00567b

Please sign in to comment.