libutil++  1.9.3
 All Classes Functions Variables
Task.h
1 /*
2 ** libutil++
3 ** $Id: Task.h 1653 2016-02-28 19:54:59Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 #ifndef __libutilxx__sella__util__Task_H__
10 #define __libutilxx__sella__util__Task_H__
11 
12 #include "../../common.h"
13 
14 #include <sys/time.h>
15 #include <stdexcept>
16 
17 #include <string>
18 #include <memory>
19 #include <vector>
20 #include <chrono>
21 #include <functional>
22 
23 #define SELLA_UTIL_TASK_DELAY_SEC_DEF 1
24 #define SELLA_UTIL_TASK_DELAY_USEC_DEF 500000
25 
26 namespace sella {
27  namespace util {
28  class Task;
29  static const timeval delay_def = { SELLA_UTIL_TASK_DELAY_SEC_DEF, SELLA_UTIL_TASK_DELAY_USEC_DEF };
30  static const timeval notset = { 0, 0 };
31  }
32 }
33 
35  public:
36  typedef std::shared_ptr<Task> shared;
37  typedef Task *ptr;
38  typedef std::function<void(void)> callback;
39 
40  typedef struct {
41  size_t operator()(const shared &a) const {
42  return (size_t) a.get();
43  }
44  } shared_hash;
45  public:
46  Task();
47  Task(Task::callback &callback, void* object = NULL, int priority = 0);
48  Task(const Task &other);
49  Task(const Task &&other);
50  virtual ~Task();
51  Task& operator=(const Task &other);
52  Task& operator=(const Task &&other);
53 
54  void clear(void);
55 
56  Task& setTimer(const std::chrono::system_clock::time_point &time);
57  Task& setTimer(timeval &timer);
58  Task& setTimer(time_t &timer);
59  Task& setDelay(const std::chrono::system_clock::duration &duration);
60  Task& setDelay(time_t sec = 0, suseconds_t usec = 0);
61  Task& setNoDelay(void); /* Possible to starve task queue. */
62  Task& setPriority(int priority); /* Higher is better */
63  Task& setCallback(Task::callback &callback, void *object = NULL);
64  Task& setObject(void *object);
65  Task& setData(std::vector<void*> &data);
66  Task& addData(void *data);
67 
68  const timeval& getTimer(void) const;
69  const timeval getDelay(void) const;
70  uint64_t getDelayMsec(void) const;
71  uint64_t getDelayUsec(void) const;
72  int getPriority(void) const;
73  const Task::callback& getCallback(void) const;
74  void* getObject(void) const;
75  std::vector<void*> getData(void) const;
76  void* getData(size_t pos) const throw (std::out_of_range);
77 
78  bool isReady(void) const;
79 
80  void execute(void);
81 
82  static const Task::shared shared_ptr(void) throw ();
83  static const Task::shared shared_ptr(Task::callback &callback, void *object = NULL, int priority = 0);
84 
85  static const timeval getDelay(const shared task, const timeval def = delay_def);
86  static bool compareTimer(const shared task, const timeval now = notset);
87 
88  protected:
89  timeval timer;
90  int priority;
91 
92  callback function;
93  void *object;
94  std::vector<void*> data;
95 
96  private:
97 };
98 
99 #endif
100 
101 /*
102 ** vim: noet ts=3 sw=3
103 */