Leandro Lucarella, el 17 de enero a las 17:08 me escribiste:
> Patch and example attached. Documentation still missing (I'll do that when
> the patch gets a technical OK =). I'll probably split the patch in
> smaller, incremental changes.
I think not many people undestand the invisible diff format, so I'll
attach it again in unified diff format =P
--
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
Novocaine for the soul
you better give me something
to fill the hole
before I sputter out
diff --git a/ev++.h b/ev++.h
index e447998..5cdf326 100644
--- a/ev++.h
+++ b/ev++.h
@@ -43,24 +43,404 @@
#ifdef EV_H
# include EV_H
#else
-# include <ev.h>
+# include "ev.h"
+#endif
+
+#ifndef EV_CXX_EXCEPTIONS
+#define EV_CXX_EXCEPTIONS 1
+#endif
+
+#undef throw
+#if EV_CXX_EXCEPTIONS
+# include <stdexcept>
+# define throw(exception) throw (exception)
+#else
+# define throw(exception)
#endif
namespace ev {
+ typedef ev_tstamp tstamp;
+
+ enum {
+ UNDEF = EV_UNDEF,
+ NONE = EV_NONE,
+ READ = EV_READ,
+ WRITE = EV_WRITE,
+ TIMEOUT = EV_TIMEOUT,
+ PERIODIC = EV_PERIODIC,
+ SIGNAL = EV_SIGNAL,
+ CHILD = EV_CHILD,
+ STAT = EV_STAT,
+ IDLE = EV_IDLE,
+ CHECK = EV_CHECK,
+ PREPARE = EV_PREPARE,
+ FORK = EV_FORK,
+ EMBED = EV_EMBED,
+ ERROR = EV_ERROR,
+ };
+
+ enum
+ {
+ AUTO = EVFLAG_AUTO,
+ NOENV = EVFLAG_NOENV,
+ FORKCHECK = EVFLAG_FORKCHECK,
+ SELECT = EVBACKEND_SELECT,
+ POLL = EVBACKEND_POLL,
+ EPOLL = EVBACKEND_EPOLL,
+ KQUEUE = EVBACKEND_KQUEUE,
+ DEVPOLL = EVBACKEND_DEVPOLL,
+ PORT = EVBACKEND_PORT
+ };
+
+ enum
+ {
+ NONBLOCK = EVLOOP_NONBLOCK,
+ ONESHOT = EVLOOP_ONESHOT
+ };
+
+ enum how_t
+ {
+ ONE = EVUNLOOP_ONE,
+ ALL = EVUNLOOP_ALL
+ };
+
+
+#if EV_CXX_EXCEPTIONS
+ struct bad_loop: std::runtime_error
+ {
+ bad_loop()
+ : std::runtime_error("loop can't be initialized") {}
+ };
+#endif
+
+#ifdef EV_AX
+# undef EV_AX
+#endif
+
+#ifdef EV_AX_
+# undef EV_AX_
+#endif
+
+#if EV_MULTIPLICITY
+# define EV_AX raw_loop
+# define EV_AX_ raw_loop,
+#else
+# define EV_AX
+# define EV_AX_
+#endif
+
+#if 0 // XXX EV_MULTIPLICITY
+ extern "C"
+ {
+ extern struct ev_loop* default_loop_ptr;
+ }
+#endif
+
+ struct loop_ref
+ {
+
+ loop_ref (EV_P) throw (bad_loop)
+#if EV_MULTIPLICITY
+ : EV_AX (EV_A)
+#endif
+ {
+#if EV_CXX_EXCEPTIONS
+ if (!EV_A)
+ throw bad_loop ();
+#endif
+ }
+
+ bool operator== (const loop_ref &other) const throw ()
+ {
+#if EV_MULTIPLICITY
+ return this->EV_AX == other.EV_AX;
+#else
+ return true;
+#endif
+ }
+
+ bool operator!= (const loop_ref &other) const throw ()
+ {
+#if EV_MULTIPLICITY
+ return ! (*this == other);
+#else
+ return false;
+#endif
+ }
+
+#if EV_MULTIPLICITY
+ bool operator== (struct ev_loop *other) const throw ()
+ {
+ return this->EV_AX == other;
+ }
+
+ bool operator!= (struct ev_loop *other) const throw ()
+ {
+ return ! (*this == other);
+ }
+
+ bool operator== (const struct ev_loop *other) const throw ()
+ {
+ return this->EV_AX == other;
+ }
+
+ bool operator!= (const struct ev_loop *other) const throw ()
+ {
+ return (*this == other);
+ }
+
+ operator struct ev_loop * () const throw ()
+ {
+ return EV_AX;
+ }
+
+ operator const struct ev_loop * () const throw ()
+ {
+ return EV_AX;
+ }
+
+ bool is_default () const throw ()
+ {
+ return EV_AX == ev_default_loop (0); // XXX default_loop_ptr;
+ }
+#endif
+
+ void loop (int flags = 0) throw ()
+ {
+ ev_loop (EV_AX_ flags);
+ }
+
+ void unloop (how_t how = ONE) throw ()
+ {
+ ev_unloop (EV_AX_ how);
+ }
+
+ // XXX should be protected? Since if we call it by hand and then called
+ // automatically by the destructor, it could blow...
+ void destroy () throw ()
+ {
+#if EV_MULTIPLICITY
+ if (!is_default ())
+ ev_loop_destroy (EV_AX);
+ else
+#endif
+ ev_default_destroy ();
+ EV_AX = 0;
+ }
+
+ void fix_fork () throw ()
+ {
+#if EV_MULTIPLICITY
+ if (!is_default ())
+ ev_loop_fork (EV_AX);
+ else
+#endif
+ ev_default_fork ();
+ }
+
+ unsigned int count () const throw ()
+ {
+ return ev_loop_count (EV_AX);
+ }
+
+ unsigned int backend () const throw ()
+ {
+ return ev_backend (EV_AX);
+ }
+
+ tstamp now () const throw ()
+ {
+ return ev_now (EV_AX);
+ }
+
+ void ref () throw ()
+ {
+ ev_ref (EV_AX);
+ }
+
+ void unref () throw ()
+ {
+ ev_unref (EV_AX);
+ }
+
+ void set_io_collect_interval (tstamp interval) throw ()
+ {
+ ev_set_io_collect_interval (EV_AX_ interval);
+ }
+
+ void set_timeout_collect_interval (tstamp interval) throw ()
+ {
+ ev_set_timeout_collect_interval (EV_AX_ interval);
+ }
+
+ // function callback
+ void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void* arg = 0) throw ()
+ {
+ ev_once (EV_AX_ fd, events, timeout, cb, arg);
+ }
+
+ // method callback
+ template<class K, void (K::*method)(int)>
+ void once (int fd, int events, tstamp timeout, K *object) throw ()
+ {
+ once (fd, events, timeout, method_thunk<K, method>, object);
+ }
+
+ template<class K, void (K::*method)(int)>
+ static void method_thunk (int revents, void* arg)
+ {
+ K *obj = static_cast<K *>(arg);
+ (obj->*method) (revents);
+ }
+
+ // const method callback
+ template<class K, void (K::*method)(int) const>
+ void once (int fd, int events, tstamp timeout, const K *object) throw ()
+ {
+ once (fd, events, timeout, const_method_thunk<K, method>, object);
+ }
+
+ template<class K, void (K::*method)(int) const>
+ static void const_method_thunk (int revents, void* arg)
+ {
+ K *obj = static_cast<K *>(arg);
+ (obj->*method) (revents);
+ }
+
+ // simple method callback
+ template<class K, void (K::*method)()>
+ void once (int fd, int events, tstamp timeout, K *object) throw ()
+ {
+ once (fd, events, timeout, method_noargs_thunk<K, method>, object);
+ }
+
+ template<class K, void (K::*method)()>
+ static void method_noargs_thunk (int revents, void* arg)
+ {
+ K *obj = static_cast<K *>(arg);
+ (obj->*method) ();
+ }
+
+ // simpler function callback
+ template<void (*cb)(int)>
+ void once (int fd, int events, tstamp timeout) throw ()
+ {
+ once (fd, events, timeout, simpler_func_thunk<cb>);
+ }
+
+ template<void (*cb)(int)>
+ static void simpler_func_thunk (int revents, void* arg)
+ {
+ (*cb) (revents);
+ }
+
+ // simplest function callback
+ template<void (*cb)()>
+ void once (int fd, int events, tstamp timeout) throw ()
+ {
+ once (fd, events, timeout, simplest_func_thunk<cb>);
+ }
+
+ template<void (*cb)()>
+ static void simplest_func_thunk (int revents, void* arg)
+ {
+ (*cb) ();
+ }
+
+ void feed_fd_event (int fd, int revents) throw ()
+ {
+ ev_feed_fd_event (EV_AX_ fd, revents);
+ }
+
+ void feed_signal_event (int signum) throw ()
+ {
+ ev_feed_signal_event (EV_AX_ signum);
+ }
+
+#if EV_MULTIPLICITY
+ struct ev_loop* EV_AX;
+#endif
+
+ };
+
+#if EV_MULTIPLICITY
+ struct dynamic_loop: loop_ref
+ {
+
+ dynamic_loop (unsigned int flags = AUTO) throw (bad_loop)
+ : loop_ref (ev_loop_new (flags))
+ {
+ }
+
+ ~dynamic_loop () throw ()
+ {
+ destroy ();
+ }
+
+ private:
+
+ dynamic_loop (const dynamic_loop &);
+
+ dynamic_loop & operator= (const dynamic_loop &);
+
+ };
+#endif
+
+ struct default_loop: loop_ref
+ {
+
+ default_loop (unsigned int flags = AUTO) throw (bad_loop)
+ : loop_ref (ev_default_loop (flags))
+ {
+ // TODO error checking? Throw if the loop is already constructed?
+ // if 2 default_loops are constructed, ev_default_destroy will be called
+ // twice (is that a problem?).
+ }
+
+ ~default_loop () throw ()
+ {
+ destroy ();
+ }
+
+ private:
+
+ default_loop (const default_loop &);
+
+ default_loop & operator= (const default_loop &);
+
+ };
+
+ inline loop_ref get_default_loop ()
+ {
+ // TODO error checking? Throw if the loop is not initialized yet?
+ return ev_default_loop (0);
+ }
+
+#undef EV_AX
+#undef EV_AX_
+
+#undef EV_PX
+#undef EV_PX_
+#if EV_MULTIPLICITY
+# define EV_PX loop_ref EV_A
+# define EV_PX_ loop_ref EV_A_
+#else
+# define EV_PX
+# define EV_PX_
+#endif
+
template<class ev_watcher, class watcher>
struct base : ev_watcher
{
#if EV_MULTIPLICITY
- EV_P;
-
- void set (EV_P)
- {
- this->EV_A = EV_A;
- }
+ EV_PX;
#endif
- base ()
+ base (EV_PX)
+ #if EV_MULTIPLICITY
+ : EV_A (EV_A)
+ #endif
{
ev_init (this, 0);
}
@@ -141,41 +521,66 @@ namespace ev {
{
return ev_is_pending (static_cast<const ev_watcher *>(this));
}
- };
- enum {
- UNDEF = EV_UNDEF,
- NONE = EV_NONE,
- READ = EV_READ,
- WRITE = EV_WRITE,
- TIMEOUT = EV_TIMEOUT,
- PERIODIC = EV_PERIODIC,
- SIGNAL = EV_SIGNAL,
- CHILD = EV_CHILD,
- STAT = EV_STAT,
- IDLE = EV_IDLE,
- CHECK = EV_CHECK,
- PREPARE = EV_PREPARE,
- FORK = EV_FORK,
- EMBED = EV_EMBED,
- ERROR = EV_ERROR,
+ void feed_event (int revents)
+ {
+ ev_feed_event (EV_A_ static_cast<const ev_watcher *>(this), revents);
+ }
};
- typedef ev_tstamp tstamp;
+ inline tstamp now ()
+ {
+ return ev_time ();
+ }
- inline ev_tstamp now (EV_P)
+ inline void delay (tstamp interval)
{
- return ev_now (EV_A);
+ ev_sleep (interval);
+ }
+
+ inline int version_major ()
+ {
+ return ev_version_major ();
+ }
+
+ inline int version_minor ()
+ {
+ return ev_version_minor ();
+ }
+
+ inline unsigned int supported_backends ()
+ {
+ return ev_supported_backends ();
+ }
+
+ inline unsigned int recommended_backends ()
+ {
+ return ev_recommended_backends ();
+ }
+
+ inline unsigned int embeddable_backends ()
+ {
+ return ev_embeddable_backends ();
+ }
+
+ inline void set_allocator (void *(*cb)(void *ptr, long size))
+ {
+ ev_set_allocator (cb);
+ }
+
+ inline void set_syserr_cb (void (*cb)(const char *msg))
+ {
+ ev_set_syserr_cb (cb);
}
#if EV_MULTIPLICITY
- #define EV_CONSTRUCT \
- (EV_P = EV_DEFAULT) \
+ #define EV_CONSTRUCT(cppstem,cstem) \
+ (EV_PX = get_default_loop ()) \
+ : base<ev_ ## cstem, cppstem> (EV_A) \
{ \
- set (EV_A); \
}
#else
- #define EV_CONSTRUCT \
+ #define EV_CONSTRUCT(cppstem,cstem) \
() \
{ \
}
@@ -197,7 +602,7 @@ namespace ev {
ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
} \
\
- cppstem EV_CONSTRUCT \
+ cppstem EV_CONSTRUCT(cppstem,cstem) \
\
~cppstem () \
{ \
@@ -208,10 +613,9 @@ namespace ev {
\
private: \
\
- cppstem (const cppstem &o) \
- { /* disabled */ } \
+ cppstem (const cppstem &o); \
\
- void operator =(const cppstem &o) { /* disabled */ } \
+ cppstem & operator =(const cppstem &o); \
\
public:
@@ -376,10 +780,15 @@ namespace ev {
EV_END_WATCHER (fork, fork)
#endif
+ #undef EV_PX
+ #undef EV_PX_
#undef EV_CONSTRUCT
#undef EV_BEGIN_WATCHER
#undef EV_END_WATCHER
+
}
+#undef throw
+
#endif
#include <ev++.h>
#include <cstring>
#include <cstdio>
#include <cassert>
#include <unistd.h>
#include <fcntl.h>
/* called when data readable on stdin */
struct myclass
{
void io_cb (ev::io &w, int revents)
{
printf ("stdin ready\n");
char buff[BUFSIZ];
buff[0] = '\0';
int r = read (w.fd, buff, BUFSIZ);
printf ("%d bytes read: %s", r, buff);
w.stop (); /* just a syntax example */
// same as w.loop ().unloop (ev::ONE); in this example
ev::get_default_loop ().unloop (); /* leave one loop call */
}
void
timeout_cb (ev::timer &w, int revents)
{
printf ("timeout!\n");
// simulate activity on STDIN_FILENO
w.loop.feed_fd_event (0, ev::READ);
}
};
int
main (void)
{
ev::default_loop loop;
int flags;
if ((flags = fcntl (0, F_GETFL, 0)) < 0)
perror ("fcntl F_GETFL");
if (fcntl (0, F_SETFL, flags | O_NONBLOCK) < 0)
perror ("fcntl F_SETFL");
myclass obj;
// io event
ev::io iow;
iow.set <myclass, &myclass::io_cb> (&obj);
/* start watching fd 0 for reading (default) */
iow.start (/*STDIN_FILENO*/ 0, ev::READ);
// timeout event
ev::timer tw (ev::get_default_loop ()); // just to test
tw.set <myclass, &myclass::timeout_cb> (&obj);
/* simple non-repeating 5.5 second timeout */
tw.start (5.5);
/* loop till timeout or data ready */
loop.loop ();
assert (loop == ev::get_default_loop ());
assert (loop == ev_default_loop (0));
ev::dynamic_loop l;
assert (loop != l);
ev::loop_ref lr(l);
lr = ev_default_loop (0);
iow.loop = ev_default_loop (0);
ev::delay (.5);
return 0;
}
_______________________________________________
libev mailing list
[email protected]
http://lists.schmorp.de/cgi-bin/mailman/listinfo/libev