COSA
An Object-Oriented Platform for Arduino Programming
Job.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_JOB_HH
22 #define COSA_JOB_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/Linkage.hh"
26 
33 class Job : public Link {
34 public:
43  class Scheduler {
44  public:
48  Scheduler() : m_queue() {}
49 
57  virtual bool start(Job* job);
58 
66  virtual bool stop(Job* job);
67 
73  virtual void dispatch();
74 
80  virtual uint32_t time() = 0;
81 
82  protected:
85  };
86 
92  Job(Scheduler* scheduler) :
93  Link(),
94  m_expires(0L),
95  m_scheduler(scheduler)
96  {}
97 
102  void expire_at(uint32_t time)
103  {
104  m_expires = time;
105  }
106 
111  void expire_after(uint32_t time)
112  {
113  m_expires += time;
114  }
115 
120  uint32_t expire_at() const
121  {
122  return (m_expires);
123  }
124 
129  int32_t expire_after() const
130  {
131  if (m_scheduler != NULL) return (m_expires - m_scheduler->time());
132  return (m_expires);
133  }
134 
139  uint32_t time() const
140  {
141  if (m_scheduler != NULL) return (m_scheduler->time());
142  return (0UL);
143  }
144 
149  bool is_started() const
150  __attribute__((always_inline))
151  {
152  return (pred() != this);
153  }
154 
159  bool start()
160  {
161  if (UNLIKELY(m_scheduler == NULL)) return (false);
162  return (m_scheduler->start(this));
163  }
164 
169  bool stop()
170  {
171  if (UNLIKELY(m_scheduler == NULL)) return (true);
172  return (m_scheduler->stop(this));
173  }
174 
185  virtual void on_expired()
186  {
188  }
189 
197  virtual void on_event(uint8_t type, uint16_t value)
198  {
199  UNUSED(value);
200  if (type == Event::TIMEOUT_TYPE) run();
201  }
202 
210  virtual void run() {}
211 
212 protected:
214  uint32_t m_expires;
215 
218 };
219 #endif
uint32_t m_expires
Definition: Job.hh:214
int32_t expire_after() const
Definition: Job.hh:129
#define NULL
Definition: Types.h:101
Definition: Job.hh:33
virtual void on_event(uint8_t type, uint16_t value)
Definition: Job.hh:197
Job(Scheduler *scheduler)
Definition: Job.hh:92
Scheduler()
Definition: Job.hh:48
bool is_started() const
Definition: Job.hh:149
virtual void run()
Definition: Job.hh:210
uint32_t time() const
Definition: Job.hh:139
#define UNUSED(x)
Definition: ATmega328P.hh:31
virtual bool stop(Job *job)
bool stop()
Definition: Job.hh:169
virtual void on_expired()
Definition: Job.hh:185
Scheduler * m_scheduler
Definition: Job.hh:217
virtual uint32_t time()=0
void expire_after(uint32_t time)
Definition: Job.hh:111
Linkage * pred() const
Definition: Linkage.hh:60
bool start()
Definition: Job.hh:159
uint32_t expire_at() const
Definition: Job.hh:120
Head m_queue
Definition: Job.hh:84
#define UNLIKELY(x)
Definition: Types.h:153
virtual void dispatch()
static bool push(uint8_t type, Handler *target, uint16_t value=0)
Definition: Event.hh:180
Definition: Linkage.hh:132
virtual bool start(Job *job)
void expire_at(uint32_t time)
Definition: Job.hh:102