libtasks Documentation  1.6
event_task.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 ADTECH GmbH
3  * Licensed under MIT (https://github.com/adtechlabs/libtasks/blob/master/COPYING)
4  *
5  * Author: Andreas Pohl
6  */
7 
8 #ifndef _TASKS_EVENT_TASK_H_
9 #define _TASKS_EVENT_TASK_H_
10 
11 #include <tasks/task.h>
12 #include <tasks/error_base.h>
13 #include <tasks/dispatcher.h>
14 
15 namespace tasks {
16 
19 typedef std::function<void(struct ev_loop*)> task_func_t;
20 
21 class event_task : public task, public error_base {
22  public:
23  typedef std::function<void(worker* worker, const tasks_exception& e)> error_func_worker_t;
24  typedef std::function<void(const tasks_exception& e)> error_func_void_t;
25  struct error_func_t {
28 
30  switch (m_type) {
31  case 0:
32  m_f_worker(worker, e);
33  break;
34  case 1:
35  m_f_void(e);
36  break;
37  }
38  }
39 
40  int m_type = 0;
43  };
44 
45  virtual ~event_task() {}
46 
56  virtual bool handle_event(worker* worker, int events) = 0;
57 
59  virtual void init_watcher() = 0;
60 
62  virtual void stop_watcher(worker* worker) = 0;
63 
65  virtual void start_watcher(worker* worker) = 0;
66 
68  inline worker* assigned_worker() const { return m_worker; }
69 
79 
82  void notify_error(worker* worker = nullptr);
83 
86  inline void on_error(error_func_worker_t f) {
87  m_error_funcs.push_back(error_func_t(f));
88  }
89 
91  inline void on_error(error_func_void_t f) {
92  m_error_funcs.push_back(error_func_t(f));
93  }
94 
95  private:
96  worker* m_worker = nullptr;
97  std::vector<error_func_t> m_error_funcs;
98 };
99 
100 } // tasks
101 
102 #endif // _TASKS_EVENT_TASK_H_
The base class for any task.
Definition: task.h:19
std::function< void(struct ev_loop *)> task_func_t
Definition: event_task.h:19
void notify_error(worker *worker=nullptr)
Definition: event_task.cpp:22
worker * assigned_worker() const
Returns a pointer to the assigned worker.
Definition: event_task.h:68
void operator()(worker *worker, const tasks_exception &e)
Definition: event_task.h:29
virtual void init_watcher()=0
Initialize the underlying watcher object.
std::function< void(const tasks_exception &e)> error_func_void_t
Definition: event_task.h:24
virtual bool handle_event(worker *worker, int events)=0
worker * m_worker
Definition: event_task.h:96
A helper class for basic error reporting.
Definition: error_base.h:18
virtual ~event_task()
Definition: event_task.h:45
void on_error(error_func_worker_t f)
Definition: event_task.h:86
error_func_void_t m_f_void
Definition: event_task.h:42
error_func_t(error_func_worker_t f)
Definition: event_task.h:26
Tasks execption class.
error_func_t(error_func_void_t f)
Definition: event_task.h:27
void assign_worker(worker *worker)
Definition: event_task.cpp:13
virtual void start_watcher(worker *worker)=0
Activate the underlying watcher to listen for I/O or timer events.
std::vector< error_func_t > m_error_funcs
Definition: event_task.h:97
std::function< void(worker *worker, const tasks_exception &e)> error_func_worker_t
Definition: event_task.h:23
error_func_worker_t m_f_worker
Definition: event_task.h:41
void on_error(error_func_void_t f)
Install an error callback.
Definition: event_task.h:91
virtual void stop_watcher(worker *worker)=0
Deactivate the underlying watcher.