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.

ev++.h 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. * libev simple C++ wrapper classes
  3. *
  4. * Copyright (c) 2007,2008,2010,2018 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. UNDEF = EV_UNDEF,
  56. NONE = EV_NONE,
  57. READ = EV_READ,
  58. WRITE = EV_WRITE,
  59. #if EV_COMPAT3
  60. TIMEOUT = EV_TIMEOUT,
  61. #endif
  62. TIMER = EV_TIMER,
  63. PERIODIC = EV_PERIODIC,
  64. SIGNAL = EV_SIGNAL,
  65. CHILD = EV_CHILD,
  66. STAT = EV_STAT,
  67. IDLE = EV_IDLE,
  68. CHECK = EV_CHECK,
  69. PREPARE = EV_PREPARE,
  70. FORK = EV_FORK,
  71. ASYNC = EV_ASYNC,
  72. EMBED = EV_EMBED,
  73. # undef ERROR // some systems stupidly #define ERROR
  74. ERROR = EV_ERROR
  75. };
  76. enum
  77. {
  78. AUTO = EVFLAG_AUTO,
  79. NOENV = EVFLAG_NOENV,
  80. FORKCHECK = EVFLAG_FORKCHECK,
  81. SELECT = EVBACKEND_SELECT,
  82. POLL = EVBACKEND_POLL,
  83. EPOLL = EVBACKEND_EPOLL,
  84. KQUEUE = EVBACKEND_KQUEUE,
  85. DEVPOLL = EVBACKEND_DEVPOLL,
  86. PORT = EVBACKEND_PORT
  87. };
  88. enum
  89. {
  90. #if EV_COMPAT3
  91. NONBLOCK = EVLOOP_NONBLOCK,
  92. ONESHOT = EVLOOP_ONESHOT,
  93. #endif
  94. NOWAIT = EVRUN_NOWAIT,
  95. ONCE = EVRUN_ONCE
  96. };
  97. enum how_t
  98. {
  99. ONE = EVBREAK_ONE,
  100. ALL = EVBREAK_ALL
  101. };
  102. struct bad_loop
  103. #if EV_USE_STDEXCEPT
  104. : std::exception
  105. #endif
  106. {
  107. #if EV_USE_STDEXCEPT
  108. const char *what () const EV_NOEXCEPT
  109. {
  110. return "libev event loop cannot be initialized, bad value of LIBEV_FLAGS?";
  111. }
  112. #endif
  113. };
  114. #ifdef EV_AX
  115. # undef EV_AX
  116. #endif
  117. #ifdef EV_AX_
  118. # undef EV_AX_
  119. #endif
  120. #if EV_MULTIPLICITY
  121. # define EV_AX raw_loop
  122. # define EV_AX_ raw_loop,
  123. #else
  124. # define EV_AX
  125. # define EV_AX_
  126. #endif
  127. struct loop_ref
  128. {
  129. loop_ref (EV_P) EV_NOEXCEPT
  130. #if EV_MULTIPLICITY
  131. : EV_AX (EV_A)
  132. #endif
  133. {
  134. }
  135. bool operator == (const loop_ref &other) const EV_NOEXCEPT
  136. {
  137. #if EV_MULTIPLICITY
  138. return EV_AX == other.EV_AX;
  139. #else
  140. return true;
  141. #endif
  142. }
  143. bool operator != (const loop_ref &other) const EV_NOEXCEPT
  144. {
  145. #if EV_MULTIPLICITY
  146. return ! (*this == other);
  147. #else
  148. return false;
  149. #endif
  150. }
  151. #if EV_MULTIPLICITY
  152. bool operator == (const EV_P) const EV_NOEXCEPT
  153. {
  154. return this->EV_AX == EV_A;
  155. }
  156. bool operator != (const EV_P) const EV_NOEXCEPT
  157. {
  158. return ! (*this == EV_A);
  159. }
  160. operator struct ev_loop * () const EV_NOEXCEPT
  161. {
  162. return EV_AX;
  163. }
  164. operator const struct ev_loop * () const EV_NOEXCEPT
  165. {
  166. return EV_AX;
  167. }
  168. bool is_default () const EV_NOEXCEPT
  169. {
  170. return EV_AX == ev_default_loop (0);
  171. }
  172. #endif
  173. #if EV_COMPAT3
  174. void loop (int flags = 0)
  175. {
  176. ev_run (EV_AX_ flags);
  177. }
  178. void unloop (how_t how = ONE) EV_NOEXCEPT
  179. {
  180. ev_break (EV_AX_ how);
  181. }
  182. #endif
  183. void run (int flags = 0)
  184. {
  185. ev_run (EV_AX_ flags);
  186. }
  187. void break_loop (how_t how = ONE) EV_NOEXCEPT
  188. {
  189. ev_break (EV_AX_ how);
  190. }
  191. void post_fork () EV_NOEXCEPT
  192. {
  193. ev_loop_fork (EV_AX);
  194. }
  195. unsigned int backend () const EV_NOEXCEPT
  196. {
  197. return ev_backend (EV_AX);
  198. }
  199. tstamp now () const EV_NOEXCEPT
  200. {
  201. return ev_now (EV_AX);
  202. }
  203. void ref () EV_NOEXCEPT
  204. {
  205. ev_ref (EV_AX);
  206. }
  207. void unref () EV_NOEXCEPT
  208. {
  209. ev_unref (EV_AX);
  210. }
  211. #if EV_FEATURE_API
  212. unsigned int iteration () const EV_NOEXCEPT
  213. {
  214. return ev_iteration (EV_AX);
  215. }
  216. unsigned int depth () const EV_NOEXCEPT
  217. {
  218. return ev_depth (EV_AX);
  219. }
  220. void set_io_collect_interval (tstamp interval) EV_NOEXCEPT
  221. {
  222. ev_set_io_collect_interval (EV_AX_ interval);
  223. }
  224. void set_timeout_collect_interval (tstamp interval) EV_NOEXCEPT
  225. {
  226. ev_set_timeout_collect_interval (EV_AX_ interval);
  227. }
  228. #endif
  229. // function callback
  230. void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void *arg = 0) EV_NOEXCEPT
  231. {
  232. ev_once (EV_AX_ fd, events, timeout, cb, arg);
  233. }
  234. // method callback
  235. template<class K, void (K::*method)(int)>
  236. void once (int fd, int events, tstamp timeout, K *object) EV_NOEXCEPT
  237. {
  238. once (fd, events, timeout, method_thunk<K, method>, object);
  239. }
  240. // default method == operator ()
  241. template<class K>
  242. void once (int fd, int events, tstamp timeout, K *object) EV_NOEXCEPT
  243. {
  244. once (fd, events, timeout, method_thunk<K, &K::operator ()>, object);
  245. }
  246. template<class K, void (K::*method)(int)>
  247. static void method_thunk (int revents, void *arg)
  248. {
  249. (static_cast<K *>(arg)->*method)
  250. (revents);
  251. }
  252. // no-argument method callback
  253. template<class K, void (K::*method)()>
  254. void once (int fd, int events, tstamp timeout, K *object) EV_NOEXCEPT
  255. {
  256. once (fd, events, timeout, method_noargs_thunk<K, method>, object);
  257. }
  258. template<class K, void (K::*method)()>
  259. static void method_noargs_thunk (int revents, void *arg)
  260. {
  261. (static_cast<K *>(arg)->*method)
  262. ();
  263. }
  264. // simpler function callback
  265. template<void (*cb)(int)>
  266. void once (int fd, int events, tstamp timeout) EV_NOEXCEPT
  267. {
  268. once (fd, events, timeout, simpler_func_thunk<cb>);
  269. }
  270. template<void (*cb)(int)>
  271. static void simpler_func_thunk (int revents, void *arg)
  272. {
  273. (*cb)
  274. (revents);
  275. }
  276. // simplest function callback
  277. template<void (*cb)()>
  278. void once (int fd, int events, tstamp timeout) EV_NOEXCEPT
  279. {
  280. once (fd, events, timeout, simplest_func_thunk<cb>);
  281. }
  282. template<void (*cb)()>
  283. static void simplest_func_thunk (int revents, void *arg)
  284. {
  285. (*cb)
  286. ();
  287. }
  288. void feed_fd_event (int fd, int revents) EV_NOEXCEPT
  289. {
  290. ev_feed_fd_event (EV_AX_ fd, revents);
  291. }
  292. void feed_signal_event (int signum) EV_NOEXCEPT
  293. {
  294. ev_feed_signal_event (EV_AX_ signum);
  295. }
  296. #if EV_MULTIPLICITY
  297. struct ev_loop* EV_AX;
  298. #endif
  299. };
  300. #if EV_MULTIPLICITY
  301. struct dynamic_loop : loop_ref
  302. {
  303. dynamic_loop (unsigned int flags = AUTO)
  304. : loop_ref (ev_loop_new (flags))
  305. {
  306. if (!EV_AX)
  307. throw bad_loop ();
  308. }
  309. ~dynamic_loop () EV_NOEXCEPT
  310. {
  311. ev_loop_destroy (EV_AX);
  312. EV_AX = 0;
  313. }
  314. private:
  315. dynamic_loop (const dynamic_loop &);
  316. dynamic_loop & operator= (const dynamic_loop &);
  317. };
  318. #endif
  319. struct default_loop : loop_ref
  320. {
  321. default_loop (unsigned int flags = AUTO)
  322. #if EV_MULTIPLICITY
  323. : loop_ref (ev_default_loop (flags))
  324. #endif
  325. {
  326. if (
  327. #if EV_MULTIPLICITY
  328. !EV_AX
  329. #else
  330. !ev_default_loop (flags)
  331. #endif
  332. )
  333. throw bad_loop ();
  334. }
  335. private:
  336. default_loop (const default_loop &);
  337. default_loop &operator = (const default_loop &);
  338. };
  339. inline loop_ref get_default_loop () EV_NOEXCEPT
  340. {
  341. #if EV_MULTIPLICITY
  342. return ev_default_loop (0);
  343. #else
  344. return loop_ref ();
  345. #endif
  346. }
  347. #undef EV_AX
  348. #undef EV_AX_
  349. #undef EV_PX
  350. #undef EV_PX_
  351. #if EV_MULTIPLICITY
  352. # define EV_PX loop_ref EV_A
  353. # define EV_PX_ loop_ref EV_A_
  354. #else
  355. # define EV_PX
  356. # define EV_PX_
  357. #endif
  358. template<class ev_watcher, class watcher>
  359. struct base : ev_watcher
  360. {
  361. #if EV_MULTIPLICITY
  362. EV_PX;
  363. // loop set
  364. void set (EV_P) EV_NOEXCEPT
  365. {
  366. this->EV_A = EV_A;
  367. }
  368. #endif
  369. base (EV_PX) EV_NOEXCEPT
  370. #if EV_MULTIPLICITY
  371. : EV_A (EV_A)
  372. #endif
  373. {
  374. ev_init (this, 0);
  375. }
  376. void set_ (const void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) EV_NOEXCEPT
  377. {
  378. this->data = (void *)data;
  379. ev_set_cb (static_cast<ev_watcher *>(this), cb);
  380. }
  381. // function callback
  382. template<void (*function)(watcher &w, int)>
  383. void set (void *data = 0) EV_NOEXCEPT
  384. {
  385. set_ (data, function_thunk<function>);
  386. }
  387. template<void (*function)(watcher &w, int)>
  388. static void function_thunk (EV_P_ ev_watcher *w, int revents)
  389. {
  390. function
  391. (*static_cast<watcher *>(w), revents);
  392. }
  393. // method callback
  394. template<class K, void (K::*method)(watcher &w, int)>
  395. void set (K *object) EV_NOEXCEPT
  396. {
  397. set_ (object, method_thunk<K, method>);
  398. }
  399. // default method == operator ()
  400. template<class K>
  401. void set (K *object) EV_NOEXCEPT
  402. {
  403. set_ (object, method_thunk<K, &K::operator ()>);
  404. }
  405. template<class K, void (K::*method)(watcher &w, int)>
  406. static void method_thunk (EV_P_ ev_watcher *w, int revents)
  407. {
  408. (static_cast<K *>(w->data)->*method)
  409. (*static_cast<watcher *>(w), revents);
  410. }
  411. // no-argument callback
  412. template<class K, void (K::*method)()>
  413. void set (K *object) EV_NOEXCEPT
  414. {
  415. set_ (object, method_noargs_thunk<K, method>);
  416. }
  417. template<class K, void (K::*method)()>
  418. static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
  419. {
  420. (static_cast<K *>(w->data)->*method)
  421. ();
  422. }
  423. void operator ()(int events = EV_UNDEF)
  424. {
  425. return
  426. ev_cb (static_cast<ev_watcher *>(this))
  427. (static_cast<ev_watcher *>(this), events);
  428. }
  429. bool is_active () const EV_NOEXCEPT
  430. {
  431. return ev_is_active (static_cast<const ev_watcher *>(this));
  432. }
  433. bool is_pending () const EV_NOEXCEPT
  434. {
  435. return ev_is_pending (static_cast<const ev_watcher *>(this));
  436. }
  437. void feed_event (int revents) EV_NOEXCEPT
  438. {
  439. ev_feed_event (EV_A_ static_cast<ev_watcher *>(this), revents);
  440. }
  441. };
  442. inline tstamp now (EV_P) EV_NOEXCEPT
  443. {
  444. return ev_now (EV_A);
  445. }
  446. inline void delay (tstamp interval) EV_NOEXCEPT
  447. {
  448. ev_sleep (interval);
  449. }
  450. inline int version_major () EV_NOEXCEPT
  451. {
  452. return ev_version_major ();
  453. }
  454. inline int version_minor () EV_NOEXCEPT
  455. {
  456. return ev_version_minor ();
  457. }
  458. inline unsigned int supported_backends () EV_NOEXCEPT
  459. {
  460. return ev_supported_backends ();
  461. }
  462. inline unsigned int recommended_backends () EV_NOEXCEPT
  463. {
  464. return ev_recommended_backends ();
  465. }
  466. inline unsigned int embeddable_backends () EV_NOEXCEPT
  467. {
  468. return ev_embeddable_backends ();
  469. }
  470. inline void set_allocator (void *(*cb)(void *ptr, long size) EV_NOEXCEPT) EV_NOEXCEPT
  471. {
  472. ev_set_allocator (cb);
  473. }
  474. inline void set_syserr_cb (void (*cb)(const char *msg) EV_NOEXCEPT) EV_NOEXCEPT
  475. {
  476. ev_set_syserr_cb (cb);
  477. }
  478. #if EV_MULTIPLICITY
  479. #define EV_CONSTRUCT(cppstem,cstem) \
  480. (EV_PX = get_default_loop ()) EV_NOEXCEPT \
  481. : base<ev_ ## cstem, cppstem> (EV_A) \
  482. { \
  483. }
  484. #else
  485. #define EV_CONSTRUCT(cppstem,cstem) \
  486. () EV_NOEXCEPT \
  487. { \
  488. }
  489. #endif
  490. /* using a template here would require quite a few more lines,
  491. * so a macro solution was chosen */
  492. #define EV_BEGIN_WATCHER(cppstem,cstem) \
  493. \
  494. struct cppstem : base<ev_ ## cstem, cppstem> \
  495. { \
  496. void start () EV_NOEXCEPT \
  497. { \
  498. ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
  499. } \
  500. \
  501. void stop () EV_NOEXCEPT \
  502. { \
  503. ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
  504. } \
  505. \
  506. cppstem EV_CONSTRUCT(cppstem,cstem) \
  507. \
  508. ~cppstem () EV_NOEXCEPT \
  509. { \
  510. stop (); \
  511. } \
  512. \
  513. using base<ev_ ## cstem, cppstem>::set; \
  514. \
  515. private: \
  516. \
  517. cppstem (const cppstem &o); \
  518. \
  519. cppstem &operator =(const cppstem &o); \
  520. \
  521. public:
  522. #define EV_END_WATCHER(cppstem,cstem) \
  523. };
  524. EV_BEGIN_WATCHER (io, io)
  525. void set (int fd, int events) EV_NOEXCEPT
  526. {
  527. int active = is_active ();
  528. if (active) stop ();
  529. ev_io_set (static_cast<ev_io *>(this), fd, events);
  530. if (active) start ();
  531. }
  532. void set (int events) EV_NOEXCEPT
  533. {
  534. int active = is_active ();
  535. if (active) stop ();
  536. ev_io_set (static_cast<ev_io *>(this), fd, events);
  537. if (active) start ();
  538. }
  539. void start (int fd, int events) EV_NOEXCEPT
  540. {
  541. set (fd, events);
  542. start ();
  543. }
  544. EV_END_WATCHER (io, io)
  545. EV_BEGIN_WATCHER (timer, timer)
  546. void set (ev_tstamp after, ev_tstamp repeat = 0.) EV_NOEXCEPT
  547. {
  548. int active = is_active ();
  549. if (active) stop ();
  550. ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
  551. if (active) start ();
  552. }
  553. void start (ev_tstamp after, ev_tstamp repeat = 0.) EV_NOEXCEPT
  554. {
  555. set (after, repeat);
  556. start ();
  557. }
  558. void again () EV_NOEXCEPT
  559. {
  560. ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
  561. }
  562. ev_tstamp remaining ()
  563. {
  564. return ev_timer_remaining (EV_A_ static_cast<ev_timer *>(this));
  565. }
  566. EV_END_WATCHER (timer, timer)
  567. #if EV_PERIODIC_ENABLE
  568. EV_BEGIN_WATCHER (periodic, periodic)
  569. void set (ev_tstamp at, ev_tstamp interval = 0.) EV_NOEXCEPT
  570. {
  571. int active = is_active ();
  572. if (active) stop ();
  573. ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
  574. if (active) start ();
  575. }
  576. void start (ev_tstamp at, ev_tstamp interval = 0.) EV_NOEXCEPT
  577. {
  578. set (at, interval);
  579. start ();
  580. }
  581. void again () EV_NOEXCEPT
  582. {
  583. ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
  584. }
  585. EV_END_WATCHER (periodic, periodic)
  586. #endif
  587. #if EV_SIGNAL_ENABLE
  588. EV_BEGIN_WATCHER (sig, signal)
  589. void set (int signum) EV_NOEXCEPT
  590. {
  591. int active = is_active ();
  592. if (active) stop ();
  593. ev_signal_set (static_cast<ev_signal *>(this), signum);
  594. if (active) start ();
  595. }
  596. void start (int signum) EV_NOEXCEPT
  597. {
  598. set (signum);
  599. start ();
  600. }
  601. EV_END_WATCHER (sig, signal)
  602. #endif
  603. #if EV_CHILD_ENABLE
  604. EV_BEGIN_WATCHER (child, child)
  605. void set (int pid, int trace = 0) EV_NOEXCEPT
  606. {
  607. int active = is_active ();
  608. if (active) stop ();
  609. ev_child_set (static_cast<ev_child *>(this), pid, trace);
  610. if (active) start ();
  611. }
  612. void start (int pid, int trace = 0) EV_NOEXCEPT
  613. {
  614. set (pid, trace);
  615. start ();
  616. }
  617. EV_END_WATCHER (child, child)
  618. #endif
  619. #if EV_STAT_ENABLE
  620. EV_BEGIN_WATCHER (stat, stat)
  621. void set (const char *path, ev_tstamp interval = 0.) EV_NOEXCEPT
  622. {
  623. int active = is_active ();
  624. if (active) stop ();
  625. ev_stat_set (static_cast<ev_stat *>(this), path, interval);
  626. if (active) start ();
  627. }
  628. void start (const char *path, ev_tstamp interval = 0.) EV_NOEXCEPT
  629. {
  630. stop ();
  631. set (path, interval);
  632. start ();
  633. }
  634. void update () EV_NOEXCEPT
  635. {
  636. ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
  637. }
  638. EV_END_WATCHER (stat, stat)
  639. #endif
  640. #if EV_IDLE_ENABLE
  641. EV_BEGIN_WATCHER (idle, idle)
  642. void set () EV_NOEXCEPT { }
  643. EV_END_WATCHER (idle, idle)
  644. #endif
  645. #if EV_PREPARE_ENABLE
  646. EV_BEGIN_WATCHER (prepare, prepare)
  647. void set () EV_NOEXCEPT { }
  648. EV_END_WATCHER (prepare, prepare)
  649. #endif
  650. #if EV_CHECK_ENABLE
  651. EV_BEGIN_WATCHER (check, check)
  652. void set () EV_NOEXCEPT { }
  653. EV_END_WATCHER (check, check)
  654. #endif
  655. #if EV_EMBED_ENABLE
  656. EV_BEGIN_WATCHER (embed, embed)
  657. void set_embed (struct ev_loop *embedded_loop) EV_NOEXCEPT
  658. {
  659. int active = is_active ();
  660. if (active) stop ();
  661. ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
  662. if (active) start ();
  663. }
  664. void start (struct ev_loop *embedded_loop) EV_NOEXCEPT
  665. {
  666. set (embedded_loop);
  667. start ();
  668. }
  669. void sweep ()
  670. {
  671. ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
  672. }
  673. EV_END_WATCHER (embed, embed)
  674. #endif
  675. #if EV_FORK_ENABLE
  676. EV_BEGIN_WATCHER (fork, fork)
  677. void set () EV_NOEXCEPT { }
  678. EV_END_WATCHER (fork, fork)
  679. #endif
  680. #if EV_ASYNC_ENABLE
  681. EV_BEGIN_WATCHER (async, async)
  682. void send () EV_NOEXCEPT
  683. {
  684. ev_async_send (EV_A_ static_cast<ev_async *>(this));
  685. }
  686. bool async_pending () EV_NOEXCEPT
  687. {
  688. return ev_async_pending (static_cast<ev_async *>(this));
  689. }
  690. EV_END_WATCHER (async, async)
  691. #endif
  692. #undef EV_PX
  693. #undef EV_PX_
  694. #undef EV_CONSTRUCT
  695. #undef EV_BEGIN_WATCHER
  696. #undef EV_END_WATCHER
  697. }
  698. #endif