Mirror of :pserver:anonymous@cvs.schmorp.de/schmorpforge libev http://software.schmorp.de/pkg/libev.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

808 lines
20 KiB

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