libtasks Documentation  1.6
disk_io_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_IO_TASK_H_
9 #define _TASKS_IO_TASK_H_
10 
11 #include <tasks/event_task.h>
12 #include <tasks/ev_wrapper.h>
13 #include <tasks/tools/buffer.h>
14 #include <tasks/logging.h>
15 #include <future>
16 
17 namespace tasks {
18 
19 class worker;
20 
21 class disk_io_task : public event_task {
22  public:
23  disk_io_task(int fd, int events, tools::buffer* buf);
24  virtual ~disk_io_task();
25 
26  inline std::string get_string() const {
27  std::ostringstream os;
28  os << "disk_io_task(" << this << "," << m_fd << ":" << m_events << ")";
29  return os.str();
30  }
31 
32  inline std::streamsize bytes() const { return m_bytes; }
33 
34  virtual bool handle_event(worker* worker, int events);
35  virtual void init_watcher() {}
36  virtual void start_watcher(worker* /* worker */) {}
37  virtual void stop_watcher(worker* /* worker */) {}
38 
39  virtual void dispose(worker* worker = nullptr);
40 
41  static std::shared_future<std::streamsize> add_task(disk_io_task* task) {
42  assert(nullptr != task);
43  return task->op();
44  }
45 
46  private:
47  int m_fd = -1;
48  int m_events = EV_UNDEF;
49  tools::buffer* m_buf = nullptr;
50  std::streamsize m_bytes = -1;
51 
52  std::shared_future<std::streamsize> op();
53 };
54 
55 } // tasks
56 
57 #endif // _TASKS_IO_TASK_H_
static std::shared_future< std::streamsize > add_task(disk_io_task *task)
Definition: disk_io_task.h:41
The base class for any task.
Definition: task.h:19
std::streamsize m_bytes
Definition: disk_io_task.h:50
tools::buffer * m_buf
Definition: disk_io_task.h:49
std::string get_string() const
Definition: disk_io_task.h:26
std::streamsize bytes() const
Definition: disk_io_task.h:32
virtual void start_watcher(worker *)
Activate the underlying watcher to listen for I/O or timer events.
Definition: disk_io_task.h:36
virtual bool handle_event(worker *worker, int events)
virtual void dispose(worker *worker=nullptr)
virtual void stop_watcher(worker *)
Deactivate the underlying watcher.
Definition: disk_io_task.h:37
std::shared_future< std::streamsize > op()
disk_io_task(int fd, int events, tools::buffer *buf)
virtual void init_watcher()
Initialize the underlying watcher object.
Definition: disk_io_task.h:35