event_loop_cxx.h
Go to the documentation of this file.
1 #ifndef EVENT_LOOP_CXX_H
2 #define EVENT_LOOP_CXX_H
3 
12 #include "event_loop.h"
13 #include "cgul_exception_cxx.h"
14 
24 
25 public:
26 
31 
36 
41 
46 
60  static event_loop_cxx*
62  {
63  /* Get the current underlying event_loop singleton. (It can
64  * change if "put_instance()" is called.) */
65  cgul_exception_t local = NULL;
66  event_loop_t instance = event_loop__get_instance(&local);
67  if (local) {
68  throw cgul_exception_cxx(local);
69  }
70  /* The original m_obj is freed early via the static constructor. */
71  s_instance_wrapper.m_obj = instance;
72  return &s_instance_wrapper;
73  }
74 
83  static void
85  {
87  s_instance_wrapper.m_obj = NULL;
88  }
89 
105  {
106  cgul_exception_t local = NULL;
107  m_obj = event_loop__new(&local);
108  if (local) {
109  throw cgul_exception_cxx(local);
110  }
111  }
112 
116  virtual
118  {
119  /* Note that set_obj() is programmed to reject any attempt to
120  * set "m_obj" to be the "event_loop" singleton so "m_obj"
121  * should only ever be a per-thread "event_loop" instance
122  * which we are free to delete. */
123  if (m_obj) {
124  event_loop__delete(m_obj);
125  m_obj = NULL;
126  }
127  }
128 
134  virtual void
136  {
137  cgul_exception_t local = NULL;
138  event_loop__start(&local, m_obj);
139  if (local) {
140  throw cgul_exception_cxx(local);
141  }
142  }
143 
151  virtual void
153  {
154  cgul_exception_t local = NULL;
155  event_loop__stop(&local, m_obj);
156  if (local) {
157  throw cgul_exception_cxx(local);
158  }
159  }
160 
186  virtual void
187  add_signal_handler(int signal_number,
188  signal_cb_t cb,
189  void* cb_data)
190  {
191  cgul_exception_t local = NULL;
193  &local, m_obj, signal_number, cb, cb_data);
194  if (local) {
195  throw cgul_exception_cxx(local);
196  }
197  }
198 
240  virtual void
242  int signal_number,
243  event_loop_listener* obj,
244  void (event_loop_listener::*listener)(int))
245  {
246  cgul_exception_t local = NULL;
247  event_loop__add_signal_handler_listener(
248  &local, m_obj, signal_number, obj, listener);
249  if (local) {
250  throw cgul_exception_cxx(local);
251  }
252  }
253 
263  virtual void
264  remove_signal_handler(int signal_number)
265  {
266  cgul_exception_t local = NULL;
267  event_loop__remove_signal_handler(&local, m_obj, signal_number);
268  if (local) {
269  throw cgul_exception_cxx(local);
270  }
271  }
272 
301  virtual timeout_t
302  add_timeout(double period,
303  timeout_cb_t cb,
304  void* cb_data)
305  {
306  timeout_t rv = NULL;
307  cgul_exception_t local = NULL;
308  rv = event_loop__add_timeout(&local, m_obj, period, cb, cb_data);
309  if (local) {
310  throw cgul_exception_cxx(local);
311  }
312  return rv;
313  }
314 
362  virtual timeout_t
364  double period,
365  event_loop_listener* obj,
366  void (event_loop_listener::*listener)(timeout_t))
367  {
368  timeout_t rv = NULL;
369  cgul_exception_t local = NULL;
370  rv = event_loop__add_timeout_listener(
371  &local, m_obj, period, obj, listener);
372  if (local) {
373  throw cgul_exception_cxx(local);
374  }
375  return rv;
376  }
377 
392  virtual void
393  remove_timeout(timeout_t timeout)
394  {
395  cgul_exception_t local = NULL;
396  event_loop__remove_timeout(&local, m_obj, timeout);
397  if (local) {
398  throw cgul_exception_cxx(local);
399  }
400  }
401 
424  virtual void
425  add_read_fd(int fd,
426  fd_ready_cb_t cb,
427  void* cb_data)
428  {
429  cgul_exception_t local = NULL;
430  event_loop__add_read_fd(&local, m_obj, fd, cb, cb_data);
431  if (local) {
432  throw cgul_exception_cxx(local);
433  }
434  }
435 
471  virtual void
473  int fd,
474  event_loop_listener* obj,
475  void (event_loop_listener::*listener)(int))
476  {
477  cgul_exception_t local = NULL;
478  event_loop__add_read_fd_listener(&local, m_obj, fd, obj, listener);
479  if (local) {
480  throw cgul_exception_cxx(local);
481  }
482  }
483 
491  virtual void
493  {
494  cgul_exception_t local = NULL;
495  event_loop__remove_read_fd(&local, m_obj, fd);
496  if (local) {
497  throw cgul_exception_cxx(local);
498  }
499  }
500 
523  virtual void
524  add_write_fd(int fd,
525  fd_ready_cb_t cb,
526  void* cb_data)
527  {
528  cgul_exception_t local = NULL;
529  event_loop__add_write_fd(&local, m_obj, fd, cb, cb_data);
530  if (local) {
531  throw cgul_exception_cxx(local);
532  }
533  }
534 
570  virtual void
572  int fd,
573  event_loop_listener* obj,
574  void (event_loop_listener::*listener)(int))
575  {
576  cgul_exception_t local = NULL;
577  event_loop__add_write_fd_listener(&local, m_obj, fd, obj, listener);
578  if (local) {
579  throw cgul_exception_cxx(local);
580  }
581  }
582 
590  virtual void
592  {
593  cgul_exception_t local = NULL;
594  event_loop__remove_write_fd(&local, m_obj, fd);
595  if (local) {
596  throw cgul_exception_cxx(local);
597  }
598  }
599 
622  virtual void
624  fd_ready_cb_t cb,
625  void* cb_data)
626  {
627  cgul_exception_t local = NULL;
628  event_loop__add_exception_fd(&local, m_obj, fd, cb, cb_data);
629  if (local) {
630  throw cgul_exception_cxx(local);
631  }
632  }
633 
669  virtual void
671  int fd,
672  event_loop_listener* obj,
673  void (event_loop_listener::*listener)(int))
674  {
675  cgul_exception_t local = NULL;
676  event_loop__add_exception_fd_listener(&local, m_obj, fd, obj, listener);
677  if (local) {
678  throw cgul_exception_cxx(local);
679  }
680  }
681 
689  virtual void
691  {
692  cgul_exception_t local = NULL;
693  event_loop__remove_exception_fd(&local, m_obj, fd);
694  if (local) {
695  throw cgul_exception_cxx(local);
696  }
697  }
698 
704  virtual event_loop_t
705  get_obj() const
706  {
707  return m_obj;
708  }
709 
721  virtual event_loop_t
723  {
724  event_loop_t rv = m_obj;
725  m_obj = NULL;
726  return rv;
727  }
728 
745  virtual void
747  {
748  /* Do not let the user set the underlying object to be the
749  * singleton "event_loop" instance because access to the
750  * singleton should only be through the get_instance() method;
751  * otherwise, the singleton would be deleted when this object
752  * goes out of scope. */
753  if (rhs && (rhs != m_obj) && (rhs != get_instance()->get_obj())) {
754  if (m_obj) {
755  event_loop__delete(m_obj);
756  }
757  m_obj = rhs;
758  }
759  }
760 
761 private:
762 
763  /* Hide these. */
765  event_loop_cxx& operator=(const event_loop_cxx&);
766 
767  /* Static Constructor */
768  class StaticConstructor {
769  public:
770  StaticConstructor();
771  };
772 
774  static StaticConstructor s_staticConstructor;
775 
777  static event_loop_cxx s_instance_wrapper;
778 
780  event_loop_t m_obj;
781 
782 };
783 
784 #endif /* EVENT_LOOP_CXX_H */
void(* event_loop__fd_ready_cb_t)(cgul_exception_t *cex, int fd, void *data)
Definition: event_loop.h:57
static event_loop_cxx * get_instance()
Definition: event_loop_cxx.h:61
event_loop__signal_cb_t signal_cb_t
Definition: event_loop_cxx.h:45
virtual void set_obj(event_loop_t rhs)
Definition: event_loop_cxx.h:746
virtual timeout_t add_timeout(double period, event_loop_listener *obj, void(event_loop_listener::*listener)(timeout_t))
Definition: event_loop_cxx.h:363
virtual event_loop_t get_obj() const
Definition: event_loop_cxx.h:705
virtual void start()
Definition: event_loop_cxx.h:135
virtual void add_read_fd(int fd, fd_ready_cb_t cb, void *cb_data)
Definition: event_loop_cxx.h:425
void event_loop__remove_exception_fd(cgul_exception_t *cex, event_loop_t loop, int fd)
event_loop_t event_loop__get_instance(cgul_exception_t *cex)
void * event_loop__timeout_t
Definition: event_loop.h:36
virtual void add_exception_fd(int fd, event_loop_listener *obj, void(event_loop_listener::*listener)(int))
Definition: event_loop_cxx.h:670
virtual void add_signal_handler(int signal_number, signal_cb_t cb, void *cb_data)
Definition: event_loop_cxx.h:187
void event_loop__remove_write_fd(cgul_exception_t *cex, event_loop_t loop, int fd)
C++ bindings for event_loop
Definition: event_loop_cxx.h:23
virtual void add_signal_handler(int signal_number, event_loop_listener *obj, void(event_loop_listener::*listener)(int))
Definition: event_loop_cxx.h:241
virtual event_loop_t take_obj()
Definition: event_loop_cxx.h:722
void event_loop__stop(cgul_exception_t *cex, event_loop_t loop)
virtual void remove_read_fd(int fd)
Definition: event_loop_cxx.h:492
virtual void stop()
Definition: event_loop_cxx.h:152
virtual void remove_exception_fd(int fd)
Definition: event_loop_cxx.h:690
void event_loop__start(cgul_exception_t *cex, event_loop_t loop)
event_loop__fd_ready_cb_t fd_ready_cb_t
Definition: event_loop_cxx.h:35
void event_loop__remove_read_fd(cgul_exception_t *cex, event_loop_t loop, int fd)
void event_loop__put_instance()
virtual void remove_write_fd(int fd)
Definition: event_loop_cxx.h:591
virtual ~event_loop_cxx()
Definition: event_loop_cxx.h:117
void event_loop__delete(event_loop_t loop)
void(* event_loop__timeout_cb_t)(cgul_exception_t *cex, event_loop__timeout_t timeout, void *data)
Definition: event_loop.h:78
virtual void remove_timeout(timeout_t timeout)
Definition: event_loop_cxx.h:393
typedefCGUL_BEGIN_C struct event_loop * event_loop_t
Definition: event_loop.h:33
event loop implementation
virtual void remove_signal_handler(int signal_number)
Definition: event_loop_cxx.h:264
static void put_instance()
Definition: event_loop_cxx.h:84
event_loop_cxx()
Definition: event_loop_cxx.h:104
event_loop__timeout_cb_t timeout_cb_t
Definition: event_loop_cxx.h:40
virtual void add_write_fd(int fd, fd_ready_cb_t cb, void *cb_data)
Definition: event_loop_cxx.h:524
void(* event_loop__signal_cb_t)(cgul_exception_t *cex, int signal_number, void *data)
Definition: event_loop.h:98
event_loop__timeout_t event_loop__add_timeout(cgul_exception_t *cex, event_loop_t loop, double period, event_loop__timeout_cb_t cb, void *cb_data)
event_loop_t event_loop__new(cgul_exception_t *cex)
void event_loop__add_write_fd(cgul_exception_t *cex, event_loop_t loop, int fd, event_loop__fd_ready_cb_t cb, void *cb_data)
void event_loop__add_read_fd(cgul_exception_t *cex, event_loop_t loop, int fd, event_loop__fd_ready_cb_t cb, void *cb_data)
void event_loop__remove_timeout(cgul_exception_t *cex, event_loop_t loop, event_loop__timeout_t timeout)
virtual timeout_t add_timeout(double period, timeout_cb_t cb, void *cb_data)
Definition: event_loop_cxx.h:302
virtual void add_write_fd(int fd, event_loop_listener *obj, void(event_loop_listener::*listener)(int))
Definition: event_loop_cxx.h:571
virtual void add_exception_fd(int fd, fd_ready_cb_t cb, void *cb_data)
Definition: event_loop_cxx.h:623
virtual void add_read_fd(int fd, event_loop_listener *obj, void(event_loop_listener::*listener)(int))
Definition: event_loop_cxx.h:472
void event_loop__add_signal_handler(cgul_exception_t *cex, event_loop_t loop, int signal_number, event_loop__signal_cb_t cb, void *cb_data)
void event_loop__add_exception_fd(cgul_exception_t *cex, event_loop_t loop, int fd, event_loop__fd_ready_cb_t cb, void *cb_data)
void event_loop__remove_signal_handler(cgul_exception_t *cex, event_loop_t loop, int signal_number)
event_loop__timeout_t timeout_t
Definition: event_loop_cxx.h:30