1e3a38431SPaul Bohm /* 2e3a38431SPaul Bohm * libev simple C++ wrapper classes 3e3a38431SPaul Bohm * 4e3a38431SPaul Bohm * Copyright (c) 2007,2008,2010 Marc Alexander Lehmann <[email protected]> 5e3a38431SPaul Bohm * All rights reserved. 6e3a38431SPaul Bohm * 7e3a38431SPaul Bohm * Redistribution and use in source and binary forms, with or without modifica- 8e3a38431SPaul Bohm * tion, are permitted provided that the following conditions are met: 9e3a38431SPaul Bohm * 10e3a38431SPaul Bohm * 1. Redistributions of source code must retain the above copyright notice, 11e3a38431SPaul Bohm * this list of conditions and the following disclaimer. 12e3a38431SPaul Bohm * 13e3a38431SPaul Bohm * 2. Redistributions in binary form must reproduce the above copyright 14e3a38431SPaul Bohm * notice, this list of conditions and the following disclaimer in the 15e3a38431SPaul Bohm * documentation and/or other materials provided with the distribution. 16e3a38431SPaul Bohm * 17e3a38431SPaul Bohm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 18e3a38431SPaul Bohm * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 19e3a38431SPaul Bohm * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 20e3a38431SPaul Bohm * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 21e3a38431SPaul Bohm * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22e3a38431SPaul Bohm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23e3a38431SPaul Bohm * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24e3a38431SPaul Bohm * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 25e3a38431SPaul Bohm * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 26e3a38431SPaul Bohm * OF THE POSSIBILITY OF SUCH DAMAGE. 27e3a38431SPaul Bohm * 28e3a38431SPaul Bohm * Alternatively, the contents of this file may be used under the terms of 29e3a38431SPaul Bohm * the GNU General Public License ("GPL") version 2 or any later version, 30e3a38431SPaul Bohm * in which case the provisions of the GPL are applicable instead of 31e3a38431SPaul Bohm * the above. If you wish to allow the use of your version of this file 32e3a38431SPaul Bohm * only under the terms of the GPL and not to allow others to use your 33e3a38431SPaul Bohm * version of this file under the BSD license, indicate your decision 34e3a38431SPaul Bohm * by deleting the provisions above and replace them with the notice 35e3a38431SPaul Bohm * and other provisions required by the GPL. If you do not delete the 36e3a38431SPaul Bohm * provisions above, a recipient may use your version of this file under 37e3a38431SPaul Bohm * either the BSD or the GPL. 38e3a38431SPaul Bohm */ 39e3a38431SPaul Bohm 40e3a38431SPaul Bohm #ifndef EVPP_H__ 41e3a38431SPaul Bohm #define EVPP_H__ 42e3a38431SPaul Bohm 43e3a38431SPaul Bohm #ifdef EV_H 44e3a38431SPaul Bohm # include EV_H 45e3a38431SPaul Bohm #else 46e3a38431SPaul Bohm # include "ev.h" 47e3a38431SPaul Bohm #endif 48e3a38431SPaul Bohm 49e3a38431SPaul Bohm #ifndef EV_USE_STDEXCEPT 50e3a38431SPaul Bohm # define EV_USE_STDEXCEPT 1 51e3a38431SPaul Bohm #endif 52e3a38431SPaul Bohm 53e3a38431SPaul Bohm #if EV_USE_STDEXCEPT 54e3a38431SPaul Bohm # include <stdexcept> 55e3a38431SPaul Bohm #endif 56e3a38431SPaul Bohm 57e3a38431SPaul Bohm namespace ev { 58e3a38431SPaul Bohm 59e3a38431SPaul Bohm typedef ev_tstamp tstamp; 60e3a38431SPaul Bohm 61e3a38431SPaul Bohm enum { 62e3a38431SPaul Bohm UNDEF = EV_UNDEF, 63e3a38431SPaul Bohm NONE = EV_NONE, 64e3a38431SPaul Bohm READ = EV_READ, 65e3a38431SPaul Bohm WRITE = EV_WRITE, 66e3a38431SPaul Bohm #if EV_COMPAT3 67e3a38431SPaul Bohm TIMEOUT = EV_TIMEOUT, 68e3a38431SPaul Bohm #endif 69e3a38431SPaul Bohm TIMER = EV_TIMER, 70e3a38431SPaul Bohm PERIODIC = EV_PERIODIC, 71e3a38431SPaul Bohm SIGNAL = EV_SIGNAL, 72e3a38431SPaul Bohm CHILD = EV_CHILD, 73e3a38431SPaul Bohm STAT = EV_STAT, 74e3a38431SPaul Bohm IDLE = EV_IDLE, 75e3a38431SPaul Bohm CHECK = EV_CHECK, 76e3a38431SPaul Bohm PREPARE = EV_PREPARE, 77e3a38431SPaul Bohm FORK = EV_FORK, 78e3a38431SPaul Bohm ASYNC = EV_ASYNC, 79e3a38431SPaul Bohm EMBED = EV_EMBED, 80e3a38431SPaul Bohm # undef ERROR // some systems stupidly #define ERROR 81e3a38431SPaul Bohm ERROR = EV_ERROR 82e3a38431SPaul Bohm }; 83e3a38431SPaul Bohm 84e3a38431SPaul Bohm enum 85e3a38431SPaul Bohm { 86e3a38431SPaul Bohm AUTO = EVFLAG_AUTO, 87e3a38431SPaul Bohm NOENV = EVFLAG_NOENV, 88e3a38431SPaul Bohm FORKCHECK = EVFLAG_FORKCHECK, 89e3a38431SPaul Bohm 90e3a38431SPaul Bohm SELECT = EVBACKEND_SELECT, 91e3a38431SPaul Bohm POLL = EVBACKEND_POLL, 92e3a38431SPaul Bohm EPOLL = EVBACKEND_EPOLL, 93e3a38431SPaul Bohm KQUEUE = EVBACKEND_KQUEUE, 94e3a38431SPaul Bohm DEVPOLL = EVBACKEND_DEVPOLL, 95e3a38431SPaul Bohm PORT = EVBACKEND_PORT 96e3a38431SPaul Bohm }; 97e3a38431SPaul Bohm 98e3a38431SPaul Bohm enum 99e3a38431SPaul Bohm { 100e3a38431SPaul Bohm #if EV_COMPAT3 101e3a38431SPaul Bohm NONBLOCK = EVLOOP_NONBLOCK, 102e3a38431SPaul Bohm ONESHOT = EVLOOP_ONESHOT, 103e3a38431SPaul Bohm #endif 104e3a38431SPaul Bohm NOWAIT = EVRUN_NOWAIT, 105e3a38431SPaul Bohm ONCE = EVRUN_ONCE 106e3a38431SPaul Bohm }; 107e3a38431SPaul Bohm 108e3a38431SPaul Bohm enum how_t 109e3a38431SPaul Bohm { 110e3a38431SPaul Bohm ONE = EVBREAK_ONE, 111e3a38431SPaul Bohm ALL = EVBREAK_ALL 112e3a38431SPaul Bohm }; 113e3a38431SPaul Bohm 114e3a38431SPaul Bohm struct bad_loop 115e3a38431SPaul Bohm #if EV_USE_STDEXCEPT 116e3a38431SPaul Bohm : std::runtime_error 117e3a38431SPaul Bohm #endif 118e3a38431SPaul Bohm { 119e3a38431SPaul Bohm #if EV_USE_STDEXCEPT bad_loopbad_loop120e3a38431SPaul Bohm bad_loop () 121e3a38431SPaul Bohm : std::runtime_error ("libev event loop cannot be initialized, bad value of LIBEV_FLAGS?") 122e3a38431SPaul Bohm { 123e3a38431SPaul Bohm } 124e3a38431SPaul Bohm #endif 125e3a38431SPaul Bohm }; 126e3a38431SPaul Bohm 127e3a38431SPaul Bohm #ifdef EV_AX 128e3a38431SPaul Bohm # undef EV_AX 129e3a38431SPaul Bohm #endif 130e3a38431SPaul Bohm 131e3a38431SPaul Bohm #ifdef EV_AX_ 132e3a38431SPaul Bohm # undef EV_AX_ 133e3a38431SPaul Bohm #endif 134e3a38431SPaul Bohm 135e3a38431SPaul Bohm #if EV_MULTIPLICITY 136e3a38431SPaul Bohm # define EV_AX raw_loop 137e3a38431SPaul Bohm # define EV_AX_ raw_loop, 138e3a38431SPaul Bohm #else 139e3a38431SPaul Bohm # define EV_AX 140e3a38431SPaul Bohm # define EV_AX_ 141e3a38431SPaul Bohm #endif 142e3a38431SPaul Bohm 143e3a38431SPaul Bohm struct loop_ref 144e3a38431SPaul Bohm { throwloop_ref145e3a38431SPaul Bohm loop_ref (EV_P) throw () 146e3a38431SPaul Bohm #if EV_MULTIPLICITY 147e3a38431SPaul Bohm : EV_AX (EV_A) 148e3a38431SPaul Bohm #endif 149e3a38431SPaul Bohm { 150e3a38431SPaul Bohm } 151e3a38431SPaul Bohm 152e3a38431SPaul Bohm bool operator == (const loop_ref &other) const throw () 153e3a38431SPaul Bohm { 154e3a38431SPaul Bohm #if EV_MULTIPLICITY 155e3a38431SPaul Bohm return EV_AX == other.EV_AX; 156e3a38431SPaul Bohm #else 157e3a38431SPaul Bohm return true; 158e3a38431SPaul Bohm #endif 159e3a38431SPaul Bohm } 160e3a38431SPaul Bohm 161e3a38431SPaul Bohm bool operator != (const loop_ref &other) const throw () 162e3a38431SPaul Bohm { 163e3a38431SPaul Bohm #if EV_MULTIPLICITY 164e3a38431SPaul Bohm return ! (*this == other); 165e3a38431SPaul Bohm #else 166e3a38431SPaul Bohm return false; 167e3a38431SPaul Bohm #endif 168e3a38431SPaul Bohm } 169e3a38431SPaul Bohm 170e3a38431SPaul Bohm #if EV_MULTIPLICITY 171e3a38431SPaul Bohm bool operator == (const EV_P) const throw () 172e3a38431SPaul Bohm { 173e3a38431SPaul Bohm return this->EV_AX == EV_A; 174e3a38431SPaul Bohm } 175e3a38431SPaul Bohm 176e3a38431SPaul Bohm bool operator != (const EV_P) const throw () 177e3a38431SPaul Bohm { 178e3a38431SPaul Bohm return (*this == EV_A); 179e3a38431SPaul Bohm } 180e3a38431SPaul Bohm throwloop_ref181e3a38431SPaul Bohm operator struct ev_loop * () const throw () 182e3a38431SPaul Bohm { 183e3a38431SPaul Bohm return EV_AX; 184e3a38431SPaul Bohm } 185e3a38431SPaul Bohm throwloop_ref186e3a38431SPaul Bohm operator const struct ev_loop * () const throw () 187e3a38431SPaul Bohm { 188e3a38431SPaul Bohm return EV_AX; 189e3a38431SPaul Bohm } 190e3a38431SPaul Bohm is_defaultloop_ref191e3a38431SPaul Bohm bool is_default () const throw () 192e3a38431SPaul Bohm { 193e3a38431SPaul Bohm return EV_AX == ev_default_loop (0); 194e3a38431SPaul Bohm } 195e3a38431SPaul Bohm #endif 196e3a38431SPaul Bohm 197e3a38431SPaul Bohm #if EV_COMPAT3 198e3a38431SPaul Bohm void loop (int flags = 0) 199e3a38431SPaul Bohm { 200e3a38431SPaul Bohm ev_run (EV_AX_ flags); 201e3a38431SPaul Bohm } 202e3a38431SPaul Bohm throwloop_ref203e3a38431SPaul Bohm void unloop (how_t how = ONE) throw () 204e3a38431SPaul Bohm { 205e3a38431SPaul Bohm ev_break (EV_AX_ how); 206e3a38431SPaul Bohm } 207e3a38431SPaul Bohm #endif 208e3a38431SPaul Bohm 209e3a38431SPaul Bohm void run (int flags = 0) 210e3a38431SPaul Bohm { 211e3a38431SPaul Bohm ev_run (EV_AX_ flags); 212e3a38431SPaul Bohm } 213e3a38431SPaul Bohm throwloop_ref214e3a38431SPaul Bohm void break_loop (how_t how = ONE) throw () 215e3a38431SPaul Bohm { 216e3a38431SPaul Bohm ev_break (EV_AX_ how); 217e3a38431SPaul Bohm } 218e3a38431SPaul Bohm post_forkloop_ref219e3a38431SPaul Bohm void post_fork () throw () 220e3a38431SPaul Bohm { 221e3a38431SPaul Bohm ev_loop_fork (EV_AX); 222e3a38431SPaul Bohm } 223e3a38431SPaul Bohm backendloop_ref224e3a38431SPaul Bohm unsigned int backend () const throw () 225e3a38431SPaul Bohm { 226e3a38431SPaul Bohm return ev_backend (EV_AX); 227e3a38431SPaul Bohm } 228e3a38431SPaul Bohm nowloop_ref229e3a38431SPaul Bohm tstamp now () const throw () 230e3a38431SPaul Bohm { 231e3a38431SPaul Bohm return ev_now (EV_AX); 232e3a38431SPaul Bohm } 233e3a38431SPaul Bohm refloop_ref234e3a38431SPaul Bohm void ref () throw () 235e3a38431SPaul Bohm { 236e3a38431SPaul Bohm ev_ref (EV_AX); 237e3a38431SPaul Bohm } 238e3a38431SPaul Bohm unrefloop_ref239e3a38431SPaul Bohm void unref () throw () 240e3a38431SPaul Bohm { 241e3a38431SPaul Bohm ev_unref (EV_AX); 242e3a38431SPaul Bohm } 243e3a38431SPaul Bohm 244e3a38431SPaul Bohm #if EV_FEATURE_API iterationloop_ref245e3a38431SPaul Bohm unsigned int iteration () const throw () 246e3a38431SPaul Bohm { 247e3a38431SPaul Bohm return ev_iteration (EV_AX); 248e3a38431SPaul Bohm } 249e3a38431SPaul Bohm depthloop_ref250e3a38431SPaul Bohm unsigned int depth () const throw () 251e3a38431SPaul Bohm { 252e3a38431SPaul Bohm return ev_depth (EV_AX); 253e3a38431SPaul Bohm } 254e3a38431SPaul Bohm set_io_collect_intervalloop_ref255e3a38431SPaul Bohm void set_io_collect_interval (tstamp interval) throw () 256e3a38431SPaul Bohm { 257e3a38431SPaul Bohm ev_set_io_collect_interval (EV_AX_ interval); 258e3a38431SPaul Bohm } 259e3a38431SPaul Bohm set_timeout_collect_intervalloop_ref260e3a38431SPaul Bohm void set_timeout_collect_interval (tstamp interval) throw () 261e3a38431SPaul Bohm { 262e3a38431SPaul Bohm ev_set_timeout_collect_interval (EV_AX_ interval); 263e3a38431SPaul Bohm } 264e3a38431SPaul Bohm #endif 265e3a38431SPaul Bohm 266e3a38431SPaul Bohm // function callback throwloop_ref267e3a38431SPaul Bohm void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void *arg = 0) throw () 268e3a38431SPaul Bohm { 269e3a38431SPaul Bohm ev_once (EV_AX_ fd, events, timeout, cb, arg); 270e3a38431SPaul Bohm } 271e3a38431SPaul Bohm 272e3a38431SPaul Bohm // method callback 273e3a38431SPaul Bohm template<class K, void (K::*method)(int)> onceloop_ref274e3a38431SPaul Bohm void once (int fd, int events, tstamp timeout, K *object) throw () 275e3a38431SPaul Bohm { 276e3a38431SPaul Bohm once (fd, events, timeout, method_thunk<K, method>, object); 277e3a38431SPaul Bohm } 278e3a38431SPaul Bohm 279e3a38431SPaul Bohm // default method == operator () 280e3a38431SPaul Bohm template<class K> onceloop_ref281e3a38431SPaul Bohm void once (int fd, int events, tstamp timeout, K *object) throw () 282e3a38431SPaul Bohm { 283e3a38431SPaul Bohm once (fd, events, timeout, method_thunk<K, &K::operator ()>, object); 284e3a38431SPaul Bohm } 285e3a38431SPaul Bohm 286e3a38431SPaul Bohm template<class K, void (K::*method)(int)> method_thunkloop_ref287e3a38431SPaul Bohm static void method_thunk (int revents, void *arg) 288e3a38431SPaul Bohm { 289*93823e6cSPaul Bohm (static_cast<K *>(arg)->*method) 290e3a38431SPaul Bohm (revents); 291e3a38431SPaul Bohm } 292e3a38431SPaul Bohm 293e3a38431SPaul Bohm // no-argument method callback 294e3a38431SPaul Bohm template<class K, void (K::*method)()> onceloop_ref295e3a38431SPaul Bohm void once (int fd, int events, tstamp timeout, K *object) throw () 296e3a38431SPaul Bohm { 297e3a38431SPaul Bohm once (fd, events, timeout, method_noargs_thunk<K, method>, object); 298e3a38431SPaul Bohm } 299e3a38431SPaul Bohm 300e3a38431SPaul Bohm template<class K, void (K::*method)()> method_noargs_thunkloop_ref301e3a38431SPaul Bohm static void method_noargs_thunk (int revents, void *arg) 302e3a38431SPaul Bohm { 303*93823e6cSPaul Bohm (static_cast<K *>(arg)->*method) 304e3a38431SPaul Bohm (); 305e3a38431SPaul Bohm } 306e3a38431SPaul Bohm 307e3a38431SPaul Bohm // simpler function callback 308e3a38431SPaul Bohm template<void (*cb)(int)> onceloop_ref309e3a38431SPaul Bohm void once (int fd, int events, tstamp timeout) throw () 310e3a38431SPaul Bohm { 311e3a38431SPaul Bohm once (fd, events, timeout, simpler_func_thunk<cb>); 312e3a38431SPaul Bohm } 313e3a38431SPaul Bohm 314e3a38431SPaul Bohm template<void (*cb)(int)> simpler_func_thunkloop_ref315e3a38431SPaul Bohm static void simpler_func_thunk (int revents, void *arg) 316e3a38431SPaul Bohm { 317e3a38431SPaul Bohm (*cb) 318e3a38431SPaul Bohm (revents); 319e3a38431SPaul Bohm } 320e3a38431SPaul Bohm 321e3a38431SPaul Bohm // simplest function callback 322e3a38431SPaul Bohm template<void (*cb)()> onceloop_ref323e3a38431SPaul Bohm void once (int fd, int events, tstamp timeout) throw () 324e3a38431SPaul Bohm { 325e3a38431SPaul Bohm once (fd, events, timeout, simplest_func_thunk<cb>); 326e3a38431SPaul Bohm } 327e3a38431SPaul Bohm 328e3a38431SPaul Bohm template<void (*cb)()> simplest_func_thunkloop_ref329e3a38431SPaul Bohm static void simplest_func_thunk (int revents, void *arg) 330e3a38431SPaul Bohm { 331e3a38431SPaul Bohm (*cb) 332e3a38431SPaul Bohm (); 333e3a38431SPaul Bohm } 334e3a38431SPaul Bohm feed_fd_eventloop_ref335e3a38431SPaul Bohm void feed_fd_event (int fd, int revents) throw () 336e3a38431SPaul Bohm { 337e3a38431SPaul Bohm ev_feed_fd_event (EV_AX_ fd, revents); 338e3a38431SPaul Bohm } 339e3a38431SPaul Bohm feed_signal_eventloop_ref340e3a38431SPaul Bohm void feed_signal_event (int signum) throw () 341e3a38431SPaul Bohm { 342e3a38431SPaul Bohm ev_feed_signal_event (EV_AX_ signum); 343e3a38431SPaul Bohm } 344e3a38431SPaul Bohm 345e3a38431SPaul Bohm #if EV_MULTIPLICITY 346e3a38431SPaul Bohm struct ev_loop* EV_AX; 347e3a38431SPaul Bohm #endif 348e3a38431SPaul Bohm 349e3a38431SPaul Bohm }; 350e3a38431SPaul Bohm 351e3a38431SPaul Bohm #if EV_MULTIPLICITY 352e3a38431SPaul Bohm struct dynamic_loop : loop_ref 353e3a38431SPaul Bohm { 354e3a38431SPaul Bohm throwdynamic_loop355e3a38431SPaul Bohm dynamic_loop (unsigned int flags = AUTO) throw (bad_loop) 356e3a38431SPaul Bohm : loop_ref (ev_loop_new (flags)) 357e3a38431SPaul Bohm { 358e3a38431SPaul Bohm if (!EV_AX) 359e3a38431SPaul Bohm throw bad_loop (); 360e3a38431SPaul Bohm } 361e3a38431SPaul Bohm throwdynamic_loop362e3a38431SPaul Bohm ~dynamic_loop () throw () 363e3a38431SPaul Bohm { 364e3a38431SPaul Bohm ev_loop_destroy (EV_AX); 365e3a38431SPaul Bohm EV_AX = 0; 366e3a38431SPaul Bohm } 367e3a38431SPaul Bohm 368e3a38431SPaul Bohm private: 369e3a38431SPaul Bohm 370e3a38431SPaul Bohm dynamic_loop (const dynamic_loop &); 371e3a38431SPaul Bohm 372e3a38431SPaul Bohm dynamic_loop & operator= (const dynamic_loop &); 373e3a38431SPaul Bohm 374e3a38431SPaul Bohm }; 375e3a38431SPaul Bohm #endif 376e3a38431SPaul Bohm 377e3a38431SPaul Bohm struct default_loop : loop_ref 378e3a38431SPaul Bohm { throwdefault_loop379e3a38431SPaul Bohm default_loop (unsigned int flags = AUTO) throw (bad_loop) 380e3a38431SPaul Bohm #if EV_MULTIPLICITY 381e3a38431SPaul Bohm : loop_ref (ev_default_loop (flags)) 382e3a38431SPaul Bohm #endif 383e3a38431SPaul Bohm { 384e3a38431SPaul Bohm if ( 385e3a38431SPaul Bohm #if EV_MULTIPLICITY 386e3a38431SPaul Bohm !EV_AX 387e3a38431SPaul Bohm #else 388e3a38431SPaul Bohm !ev_default_loop (flags) 389e3a38431SPaul Bohm #endif 390e3a38431SPaul Bohm ) 391e3a38431SPaul Bohm throw bad_loop (); 392e3a38431SPaul Bohm } 393e3a38431SPaul Bohm 394e3a38431SPaul Bohm private: 395e3a38431SPaul Bohm default_loop (const default_loop &); 396e3a38431SPaul Bohm default_loop &operator = (const default_loop &); 397e3a38431SPaul Bohm }; 398e3a38431SPaul Bohm get_default_loop()399e3a38431SPaul Bohm inline loop_ref get_default_loop () throw () 400e3a38431SPaul Bohm { 401e3a38431SPaul Bohm #if EV_MULTIPLICITY 402e3a38431SPaul Bohm return ev_default_loop (0); 403e3a38431SPaul Bohm #else 404e3a38431SPaul Bohm return loop_ref (); 405e3a38431SPaul Bohm #endif 406e3a38431SPaul Bohm } 407e3a38431SPaul Bohm 408e3a38431SPaul Bohm #undef EV_AX 409e3a38431SPaul Bohm #undef EV_AX_ 410e3a38431SPaul Bohm 411e3a38431SPaul Bohm #undef EV_PX 412e3a38431SPaul Bohm #undef EV_PX_ 413e3a38431SPaul Bohm #if EV_MULTIPLICITY 414e3a38431SPaul Bohm # define EV_PX loop_ref EV_A 415e3a38431SPaul Bohm # define EV_PX_ loop_ref EV_A_ 416e3a38431SPaul Bohm #else 417e3a38431SPaul Bohm # define EV_PX 418e3a38431SPaul Bohm # define EV_PX_ 419e3a38431SPaul Bohm #endif 420e3a38431SPaul Bohm 421e3a38431SPaul Bohm template<class ev_watcher, class watcher> 422e3a38431SPaul Bohm struct base : ev_watcher 423e3a38431SPaul Bohm { 424e3a38431SPaul Bohm #if EV_MULTIPLICITY 425e3a38431SPaul Bohm EV_PX; 426e3a38431SPaul Bohm 427e3a38431SPaul Bohm // loop set setbase428e3a38431SPaul Bohm void set (EV_P) throw () 429e3a38431SPaul Bohm { 430e3a38431SPaul Bohm this->EV_A = EV_A; 431e3a38431SPaul Bohm } 432e3a38431SPaul Bohm #endif 433e3a38431SPaul Bohm throwbase434e3a38431SPaul Bohm base (EV_PX) throw () 435e3a38431SPaul Bohm #if EV_MULTIPLICITY 436e3a38431SPaul Bohm : EV_A (EV_A) 437e3a38431SPaul Bohm #endif 438e3a38431SPaul Bohm { 439e3a38431SPaul Bohm ev_init (this, 0); 440e3a38431SPaul Bohm } 441e3a38431SPaul Bohm set_base442e3a38431SPaul Bohm void set_ (const void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw () 443e3a38431SPaul Bohm { 444e3a38431SPaul Bohm this->data = (void *)data; 445e3a38431SPaul Bohm ev_set_cb (static_cast<ev_watcher *>(this), cb); 446e3a38431SPaul Bohm } 447e3a38431SPaul Bohm 448e3a38431SPaul Bohm // function callback 449e3a38431SPaul Bohm template<void (*function)(watcher &w, int)> throwbase450e3a38431SPaul Bohm void set (void *data = 0) throw () 451e3a38431SPaul Bohm { 452e3a38431SPaul Bohm set_ (data, function_thunk<function>); 453e3a38431SPaul Bohm } 454e3a38431SPaul Bohm 455e3a38431SPaul Bohm template<void (*function)(watcher &w, int)> function_thunkbase456e3a38431SPaul Bohm static void function_thunk (EV_P_ ev_watcher *w, int revents) 457e3a38431SPaul Bohm { 458e3a38431SPaul Bohm function 459e3a38431SPaul Bohm (*static_cast<watcher *>(w), revents); 460e3a38431SPaul Bohm } 461e3a38431SPaul Bohm 462e3a38431SPaul Bohm // method callback 463e3a38431SPaul Bohm template<class K, void (K::*method)(watcher &w, int)> setbase464e3a38431SPaul Bohm void set (K *object) throw () 465e3a38431SPaul Bohm { 466e3a38431SPaul Bohm set_ (object, method_thunk<K, method>); 467e3a38431SPaul Bohm } 468e3a38431SPaul Bohm 469e3a38431SPaul Bohm // default method == operator () 470e3a38431SPaul Bohm template<class K> setbase471e3a38431SPaul Bohm void set (K *object) throw () 472e3a38431SPaul Bohm { 473e3a38431SPaul Bohm set_ (object, method_thunk<K, &K::operator ()>); 474e3a38431SPaul Bohm } 475e3a38431SPaul Bohm 476e3a38431SPaul Bohm template<class K, void (K::*method)(watcher &w, int)> method_thunkbase477e3a38431SPaul Bohm static void method_thunk (EV_P_ ev_watcher *w, int revents) 478e3a38431SPaul Bohm { 479e3a38431SPaul Bohm (static_cast<K *>(w->data)->*method) 480e3a38431SPaul Bohm (*static_cast<watcher *>(w), revents); 481e3a38431SPaul Bohm } 482e3a38431SPaul Bohm 483e3a38431SPaul Bohm // no-argument callback 484e3a38431SPaul Bohm template<class K, void (K::*method)()> setbase485e3a38431SPaul Bohm void set (K *object) throw () 486e3a38431SPaul Bohm { 487e3a38431SPaul Bohm set_ (object, method_noargs_thunk<K, method>); 488e3a38431SPaul Bohm } 489e3a38431SPaul Bohm 490e3a38431SPaul Bohm template<class K, void (K::*method)()> method_noargs_thunkbase491e3a38431SPaul Bohm static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) 492e3a38431SPaul Bohm { 493e3a38431SPaul Bohm (static_cast<K *>(w->data)->*method) 494e3a38431SPaul Bohm (); 495e3a38431SPaul Bohm } 496e3a38431SPaul Bohm operatorbase497e3a38431SPaul Bohm void operator ()(int events = EV_UNDEF) 498e3a38431SPaul Bohm { 499e3a38431SPaul Bohm return 500e3a38431SPaul Bohm ev_cb (static_cast<ev_watcher *>(this)) 501e3a38431SPaul Bohm (static_cast<ev_watcher *>(this), events); 502e3a38431SPaul Bohm } 503e3a38431SPaul Bohm is_activebase504e3a38431SPaul Bohm bool is_active () const throw () 505e3a38431SPaul Bohm { 506e3a38431SPaul Bohm return ev_is_active (static_cast<const ev_watcher *>(this)); 507e3a38431SPaul Bohm } 508e3a38431SPaul Bohm is_pendingbase509e3a38431SPaul Bohm bool is_pending () const throw () 510e3a38431SPaul Bohm { 511e3a38431SPaul Bohm return ev_is_pending (static_cast<const ev_watcher *>(this)); 512e3a38431SPaul Bohm } 513e3a38431SPaul Bohm feed_eventbase514e3a38431SPaul Bohm void feed_event (int revents) throw () 515e3a38431SPaul Bohm { 516*93823e6cSPaul Bohm ev_feed_event (EV_A_ static_cast<ev_watcher *>(this), revents); 517e3a38431SPaul Bohm } 518e3a38431SPaul Bohm }; 519e3a38431SPaul Bohm now(EV_P)520*93823e6cSPaul Bohm inline tstamp now (EV_P) throw () 521e3a38431SPaul Bohm { 522*93823e6cSPaul Bohm return ev_now (EV_A); 523e3a38431SPaul Bohm } 524e3a38431SPaul Bohm delay(tstamp interval)525e3a38431SPaul Bohm inline void delay (tstamp interval) throw () 526e3a38431SPaul Bohm { 527e3a38431SPaul Bohm ev_sleep (interval); 528e3a38431SPaul Bohm } 529e3a38431SPaul Bohm version_major()530e3a38431SPaul Bohm inline int version_major () throw () 531e3a38431SPaul Bohm { 532e3a38431SPaul Bohm return ev_version_major (); 533e3a38431SPaul Bohm } 534e3a38431SPaul Bohm version_minor()535e3a38431SPaul Bohm inline int version_minor () throw () 536e3a38431SPaul Bohm { 537e3a38431SPaul Bohm return ev_version_minor (); 538e3a38431SPaul Bohm } 539e3a38431SPaul Bohm supported_backends()540e3a38431SPaul Bohm inline unsigned int supported_backends () throw () 541e3a38431SPaul Bohm { 542e3a38431SPaul Bohm return ev_supported_backends (); 543e3a38431SPaul Bohm } 544e3a38431SPaul Bohm recommended_backends()545e3a38431SPaul Bohm inline unsigned int recommended_backends () throw () 546e3a38431SPaul Bohm { 547e3a38431SPaul Bohm return ev_recommended_backends (); 548e3a38431SPaul Bohm } 549e3a38431SPaul Bohm embeddable_backends()550e3a38431SPaul Bohm inline unsigned int embeddable_backends () throw () 551e3a38431SPaul Bohm { 552e3a38431SPaul Bohm return ev_embeddable_backends (); 553e3a38431SPaul Bohm } 554e3a38431SPaul Bohm set_allocator(void * (* cb)(void * ptr,long size)throw ())555*93823e6cSPaul Bohm inline void set_allocator (void *(*cb)(void *ptr, long size) throw ()) throw () 556e3a38431SPaul Bohm { 557e3a38431SPaul Bohm ev_set_allocator (cb); 558e3a38431SPaul Bohm } 559e3a38431SPaul Bohm set_syserr_cb(void (* cb)(const char * msg)throw ())560*93823e6cSPaul Bohm inline void set_syserr_cb (void (*cb)(const char *msg) throw ()) throw () 561e3a38431SPaul Bohm { 562e3a38431SPaul Bohm ev_set_syserr_cb (cb); 563e3a38431SPaul Bohm } 564e3a38431SPaul Bohm 565e3a38431SPaul Bohm #if EV_MULTIPLICITY 566e3a38431SPaul Bohm #define EV_CONSTRUCT(cppstem,cstem) \ 567e3a38431SPaul Bohm (EV_PX = get_default_loop ()) throw () \ 568e3a38431SPaul Bohm : base<ev_ ## cstem, cppstem> (EV_A) \ 569e3a38431SPaul Bohm { \ 570e3a38431SPaul Bohm } 571e3a38431SPaul Bohm #else 572e3a38431SPaul Bohm #define EV_CONSTRUCT(cppstem,cstem) \ 573e3a38431SPaul Bohm () throw () \ 574e3a38431SPaul Bohm { \ 575e3a38431SPaul Bohm } 576e3a38431SPaul Bohm #endif 577e3a38431SPaul Bohm 578*93823e6cSPaul Bohm /* using a template here would require quite a few more lines, 579e3a38431SPaul Bohm * so a macro solution was chosen */ 580e3a38431SPaul Bohm #define EV_BEGIN_WATCHER(cppstem,cstem) \ 581e3a38431SPaul Bohm \ 582e3a38431SPaul Bohm struct cppstem : base<ev_ ## cstem, cppstem> \ 583e3a38431SPaul Bohm { \ 584e3a38431SPaul Bohm void start () throw () \ 585e3a38431SPaul Bohm { \ 586e3a38431SPaul Bohm ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 587e3a38431SPaul Bohm } \ 588e3a38431SPaul Bohm \ 589e3a38431SPaul Bohm void stop () throw () \ 590e3a38431SPaul Bohm { \ 591e3a38431SPaul Bohm ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 592e3a38431SPaul Bohm } \ 593e3a38431SPaul Bohm \ 594e3a38431SPaul Bohm cppstem EV_CONSTRUCT(cppstem,cstem) \ 595e3a38431SPaul Bohm \ 596e3a38431SPaul Bohm ~cppstem () throw () \ 597e3a38431SPaul Bohm { \ 598e3a38431SPaul Bohm stop (); \ 599e3a38431SPaul Bohm } \ 600e3a38431SPaul Bohm \ 601e3a38431SPaul Bohm using base<ev_ ## cstem, cppstem>::set; \ 602e3a38431SPaul Bohm \ 603e3a38431SPaul Bohm private: \ 604e3a38431SPaul Bohm \ 605e3a38431SPaul Bohm cppstem (const cppstem &o); \ 606e3a38431SPaul Bohm \ 607e3a38431SPaul Bohm cppstem &operator =(const cppstem &o); \ 608e3a38431SPaul Bohm \ 609e3a38431SPaul Bohm public: 610e3a38431SPaul Bohm 611e3a38431SPaul Bohm #define EV_END_WATCHER(cppstem,cstem) \ 612e3a38431SPaul Bohm }; 613e3a38431SPaul Bohm EV_BEGIN_WATCHER(io,io)614e3a38431SPaul Bohm EV_BEGIN_WATCHER (io, io) 615e3a38431SPaul Bohm void set (int fd, int events) throw () 616e3a38431SPaul Bohm { 617e3a38431SPaul Bohm int active = is_active (); 618e3a38431SPaul Bohm if (active) stop (); 619e3a38431SPaul Bohm ev_io_set (static_cast<ev_io *>(this), fd, events); 620e3a38431SPaul Bohm if (active) start (); 621e3a38431SPaul Bohm } 622e3a38431SPaul Bohm set(int events)623e3a38431SPaul Bohm void set (int events) throw () 624e3a38431SPaul Bohm { 625e3a38431SPaul Bohm int active = is_active (); 626e3a38431SPaul Bohm if (active) stop (); 627e3a38431SPaul Bohm ev_io_set (static_cast<ev_io *>(this), fd, events); 628e3a38431SPaul Bohm if (active) start (); 629e3a38431SPaul Bohm } 630e3a38431SPaul Bohm start(int fd,int events)631e3a38431SPaul Bohm void start (int fd, int events) throw () 632e3a38431SPaul Bohm { 633e3a38431SPaul Bohm set (fd, events); 634e3a38431SPaul Bohm start (); 635e3a38431SPaul Bohm } EV_END_WATCHER(io,io)636e3a38431SPaul Bohm EV_END_WATCHER (io, io) 637e3a38431SPaul Bohm 638e3a38431SPaul Bohm EV_BEGIN_WATCHER (timer, timer) 639e3a38431SPaul Bohm void set (ev_tstamp after, ev_tstamp repeat = 0.) throw () 640e3a38431SPaul Bohm { 641e3a38431SPaul Bohm int active = is_active (); 642e3a38431SPaul Bohm if (active) stop (); 643e3a38431SPaul Bohm ev_timer_set (static_cast<ev_timer *>(this), after, repeat); 644e3a38431SPaul Bohm if (active) start (); 645e3a38431SPaul Bohm } 646e3a38431SPaul Bohm throw()647e3a38431SPaul Bohm void start (ev_tstamp after, ev_tstamp repeat = 0.) throw () 648e3a38431SPaul Bohm { 649e3a38431SPaul Bohm set (after, repeat); 650e3a38431SPaul Bohm start (); 651e3a38431SPaul Bohm } 652e3a38431SPaul Bohm again()653e3a38431SPaul Bohm void again () throw () 654e3a38431SPaul Bohm { 655e3a38431SPaul Bohm ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); 656e3a38431SPaul Bohm } 657e3a38431SPaul Bohm remaining()658e3a38431SPaul Bohm ev_tstamp remaining () 659e3a38431SPaul Bohm { 660e3a38431SPaul Bohm return ev_timer_remaining (EV_A_ static_cast<ev_timer *>(this)); 661e3a38431SPaul Bohm } EV_END_WATCHER(timer,timer)662e3a38431SPaul Bohm EV_END_WATCHER (timer, timer) 663e3a38431SPaul Bohm 664e3a38431SPaul Bohm #if EV_PERIODIC_ENABLE 665e3a38431SPaul Bohm EV_BEGIN_WATCHER (periodic, periodic) 666e3a38431SPaul Bohm void set (ev_tstamp at, ev_tstamp interval = 0.) throw () 667e3a38431SPaul Bohm { 668e3a38431SPaul Bohm int active = is_active (); 669e3a38431SPaul Bohm if (active) stop (); 670e3a38431SPaul Bohm ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); 671e3a38431SPaul Bohm if (active) start (); 672e3a38431SPaul Bohm } 673e3a38431SPaul Bohm throw()674e3a38431SPaul Bohm void start (ev_tstamp at, ev_tstamp interval = 0.) throw () 675e3a38431SPaul Bohm { 676e3a38431SPaul Bohm set (at, interval); 677e3a38431SPaul Bohm start (); 678e3a38431SPaul Bohm } 679e3a38431SPaul Bohm again()680e3a38431SPaul Bohm void again () throw () 681e3a38431SPaul Bohm { 682e3a38431SPaul Bohm ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); 683e3a38431SPaul Bohm } EV_END_WATCHER(periodic,periodic)684e3a38431SPaul Bohm EV_END_WATCHER (periodic, periodic) 685e3a38431SPaul Bohm #endif 686e3a38431SPaul Bohm 687e3a38431SPaul Bohm #if EV_SIGNAL_ENABLE 688e3a38431SPaul Bohm EV_BEGIN_WATCHER (sig, signal) 689e3a38431SPaul Bohm void set (int signum) throw () 690e3a38431SPaul Bohm { 691e3a38431SPaul Bohm int active = is_active (); 692e3a38431SPaul Bohm if (active) stop (); 693e3a38431SPaul Bohm ev_signal_set (static_cast<ev_signal *>(this), signum); 694e3a38431SPaul Bohm if (active) start (); 695e3a38431SPaul Bohm } 696e3a38431SPaul Bohm start(int signum)697e3a38431SPaul Bohm void start (int signum) throw () 698e3a38431SPaul Bohm { 699e3a38431SPaul Bohm set (signum); 700e3a38431SPaul Bohm start (); 701e3a38431SPaul Bohm } EV_END_WATCHER(sig,signal)702e3a38431SPaul Bohm EV_END_WATCHER (sig, signal) 703e3a38431SPaul Bohm #endif 704e3a38431SPaul Bohm 705e3a38431SPaul Bohm #if EV_CHILD_ENABLE 706e3a38431SPaul Bohm EV_BEGIN_WATCHER (child, child) 707e3a38431SPaul Bohm void set (int pid, int trace = 0) throw () 708e3a38431SPaul Bohm { 709e3a38431SPaul Bohm int active = is_active (); 710e3a38431SPaul Bohm if (active) stop (); 711e3a38431SPaul Bohm ev_child_set (static_cast<ev_child *>(this), pid, trace); 712e3a38431SPaul Bohm if (active) start (); 713e3a38431SPaul Bohm } 714e3a38431SPaul Bohm throw()715e3a38431SPaul Bohm void start (int pid, int trace = 0) throw () 716e3a38431SPaul Bohm { 717e3a38431SPaul Bohm set (pid, trace); 718e3a38431SPaul Bohm start (); 719e3a38431SPaul Bohm } EV_END_WATCHER(child,child)720e3a38431SPaul Bohm EV_END_WATCHER (child, child) 721e3a38431SPaul Bohm #endif 722e3a38431SPaul Bohm 723e3a38431SPaul Bohm #if EV_STAT_ENABLE 724e3a38431SPaul Bohm EV_BEGIN_WATCHER (stat, stat) 725e3a38431SPaul Bohm void set (const char *path, ev_tstamp interval = 0.) throw () 726e3a38431SPaul Bohm { 727e3a38431SPaul Bohm int active = is_active (); 728e3a38431SPaul Bohm if (active) stop (); 729e3a38431SPaul Bohm ev_stat_set (static_cast<ev_stat *>(this), path, interval); 730e3a38431SPaul Bohm if (active) start (); 731e3a38431SPaul Bohm } 732e3a38431SPaul Bohm throw()733e3a38431SPaul Bohm void start (const char *path, ev_tstamp interval = 0.) throw () 734e3a38431SPaul Bohm { 735e3a38431SPaul Bohm stop (); 736e3a38431SPaul Bohm set (path, interval); 737e3a38431SPaul Bohm start (); 738e3a38431SPaul Bohm } 739e3a38431SPaul Bohm update()740e3a38431SPaul Bohm void update () throw () 741e3a38431SPaul Bohm { 742e3a38431SPaul Bohm ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); 743e3a38431SPaul Bohm } EV_END_WATCHER(stat,stat)744e3a38431SPaul Bohm EV_END_WATCHER (stat, stat) 745e3a38431SPaul Bohm #endif 746e3a38431SPaul Bohm 747e3a38431SPaul Bohm #if EV_IDLE_ENABLE 748e3a38431SPaul Bohm EV_BEGIN_WATCHER (idle, idle) 749e3a38431SPaul Bohm void set () throw () { } EV_END_WATCHER(idle,idle)750e3a38431SPaul Bohm EV_END_WATCHER (idle, idle) 751e3a38431SPaul Bohm #endif 752e3a38431SPaul Bohm 753e3a38431SPaul Bohm #if EV_PREPARE_ENABLE 754e3a38431SPaul Bohm EV_BEGIN_WATCHER (prepare, prepare) 755e3a38431SPaul Bohm void set () throw () { } EV_END_WATCHER(prepare,prepare)756e3a38431SPaul Bohm EV_END_WATCHER (prepare, prepare) 757e3a38431SPaul Bohm #endif 758e3a38431SPaul Bohm 759e3a38431SPaul Bohm #if EV_CHECK_ENABLE 760e3a38431SPaul Bohm EV_BEGIN_WATCHER (check, check) 761e3a38431SPaul Bohm void set () throw () { } EV_END_WATCHER(check,check)762e3a38431SPaul Bohm EV_END_WATCHER (check, check) 763e3a38431SPaul Bohm #endif 764e3a38431SPaul Bohm 765e3a38431SPaul Bohm #if EV_EMBED_ENABLE 766e3a38431SPaul Bohm EV_BEGIN_WATCHER (embed, embed) 767*93823e6cSPaul Bohm void set_embed (struct ev_loop *embedded_loop) throw () 768e3a38431SPaul Bohm { 769e3a38431SPaul Bohm int active = is_active (); 770e3a38431SPaul Bohm if (active) stop (); 771e3a38431SPaul Bohm ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); 772e3a38431SPaul Bohm if (active) start (); 773e3a38431SPaul Bohm } 774e3a38431SPaul Bohm start(struct ev_loop * embedded_loop)775e3a38431SPaul Bohm void start (struct ev_loop *embedded_loop) throw () 776e3a38431SPaul Bohm { 777e3a38431SPaul Bohm set (embedded_loop); 778e3a38431SPaul Bohm start (); 779e3a38431SPaul Bohm } 780e3a38431SPaul Bohm sweep()781e3a38431SPaul Bohm void sweep () 782e3a38431SPaul Bohm { 783e3a38431SPaul Bohm ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); 784e3a38431SPaul Bohm } EV_END_WATCHER(embed,embed)785e3a38431SPaul Bohm EV_END_WATCHER (embed, embed) 786e3a38431SPaul Bohm #endif 787e3a38431SPaul Bohm 788e3a38431SPaul Bohm #if EV_FORK_ENABLE 789e3a38431SPaul Bohm EV_BEGIN_WATCHER (fork, fork) 790e3a38431SPaul Bohm void set () throw () { } EV_END_WATCHER(fork,fork)791e3a38431SPaul Bohm EV_END_WATCHER (fork, fork) 792e3a38431SPaul Bohm #endif 793e3a38431SPaul Bohm 794e3a38431SPaul Bohm #if EV_ASYNC_ENABLE 795e3a38431SPaul Bohm EV_BEGIN_WATCHER (async, async) 796e3a38431SPaul Bohm void send () throw () 797e3a38431SPaul Bohm { 798e3a38431SPaul Bohm ev_async_send (EV_A_ static_cast<ev_async *>(this)); 799e3a38431SPaul Bohm } 800e3a38431SPaul Bohm async_pending()801e3a38431SPaul Bohm bool async_pending () throw () 802e3a38431SPaul Bohm { 803e3a38431SPaul Bohm return ev_async_pending (static_cast<ev_async *>(this)); 804e3a38431SPaul Bohm } 805e3a38431SPaul Bohm EV_END_WATCHER (async, async) 806e3a38431SPaul Bohm #endif 807e3a38431SPaul Bohm 808e3a38431SPaul Bohm #undef EV_PX 809e3a38431SPaul Bohm #undef EV_PX_ 810e3a38431SPaul Bohm #undef EV_CONSTRUCT 811e3a38431SPaul Bohm #undef EV_BEGIN_WATCHER 812e3a38431SPaul Bohm #undef EV_END_WATCHER 813e3a38431SPaul Bohm } 814e3a38431SPaul Bohm 815e3a38431SPaul Bohm #endif 816e3a38431SPaul Bohm 817