libtasks Documentation  1.6
io_task_base.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_IO_TASK_BASE_H_
9 #define _TASKS_IO_TASK_BASE_H_
10 
11 #include <tasks/event_task.h>
12 #include <tasks/disposable.h>
13 #include <tasks/ev_wrapper.h>
14 #include <tasks/io_base.h>
15 #include <memory>
16 #include <sstream>
17 
18 namespace tasks {
19 
20 class worker;
21 
26 class io_task_base : public event_task, public disposable {
27  public:
28  io_task_base(int events);
29  virtual ~io_task_base() {}
30 
31  std::string get_string() const {
32  std::ostringstream os;
33  os << "io_task_base(" << this << ")";
34  return os.str();
35  }
36 
38  inline int events() const { return m_events; }
40  inline ev_io* watcher() const { return m_io.get(); }
41 
43  virtual void init_watcher();
44 
46  virtual void start_watcher(worker* worker);
48  virtual void stop_watcher(worker* worker);
50  virtual void update_watcher(worker* worker);
51 
53  virtual void dispose(worker* worker);
54 
55  protected:
57  virtual io_base& iob() = 0;
59  virtual const io_base& iob() const = 0;
60 
62  void set_events(int events);
63 
64  private:
65  std::unique_ptr<ev_io> m_io;
66  bool m_watcher_initialized = false;
67  int m_events = EV_UNDEF;
68  bool m_change_pending = false;
69 };
70 
71 } // tasks
72 
73 #endif // _TASKS_IO_TASK_BASE_H_
virtual void dispose(worker *worker)
Stop the watcher before being deleted.
std::unique_ptr< ev_io > m_io
Definition: io_task_base.h:65
Base class for objects/tasks that can be deleted.
Definition: disposable.h:23
virtual void init_watcher()
Initialize the watcher.
int events() const
Return the monitored events.
Definition: io_task_base.h:38
This is the base class for io event tasks. It controls an ev_io watcher that monitors io events on fi...
Definition: io_task_base.h:26
virtual void update_watcher(worker *worker)
Udate a watcher in the context of the given worker.
Base class for socket and term.
Definition: io_base.h:16
virtual ~io_task_base()
Definition: io_task_base.h:29
io_task_base(int events)
virtual void stop_watcher(worker *worker)
Stop a watcher in the context of the given worker.
virtual void start_watcher(worker *worker)
Start a watcher in the context of the given worker.
void set_events(int events)
Update the events the object monitors.
std::string get_string() const
Definition: io_task_base.h:31
virtual io_base & iob()=0
Return the io_base object.
ev_io * watcher() const
Return the io watcher object.
Definition: io_task_base.h:40