Ice 3.7 C++11 API Reference
Timer.h
Go to the documentation of this file.
1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_UTIL_TIMER_H
6 #define ICE_UTIL_TIMER_H
7 
8 #include <IceUtil/Shared.h>
9 #include <IceUtil/Thread.h>
10 #include <IceUtil/Monitor.h>
11 #include <IceUtil/Time.h>
12 
13 #include <set>
14 #include <map>
15 #include <functional>
16 
17 namespace IceUtil
18 {
19 
20 class Timer;
22 
23 //
24 // Extend the TimerTask class and override the runTimerTask() method to execute
25 // code at a specific time or repeatedly.
26 //
28 #ifndef ICE_CPP11_MAPPING
29  : public virtual IceUtil::Shared
30 #endif
31 {
32 public:
33 
34  virtual ~TimerTask();
35 
36  virtual void runTimerTask() = 0;
37 };
39 
40 //
41 // The timer class is used to schedule tasks for one-time execution or
42 // repeated execution. Tasks are executed by the dedicated timer thread
43 // sequentially.
44 //
45 class ICE_API Timer : public virtual IceUtil::Shared, private IceUtil::Thread
46 {
47 public:
48 
49  //
50  // Construct a timer and starts its execution thread.
51  //
52  Timer();
53 
54  //
55  // Construct a timer and starts its execution thread with the priority.
56  //
57  Timer(int priority);
58 
59  //
60  // Destroy the timer and detach its execution thread if the calling thread
61  // is the timer thread, join the timer execution thread otherwise.
62  //
63  void destroy();
64 
65  //
66  // Schedule a task for execution after a given delay.
67  //
68  void schedule(const TimerTaskPtr&, const IceUtil::Time&);
69 
70  //
71  // Schedule a task for repeated execution with the given delay
72  // between each execution.
73  //
75 
76  //
77  // Cancel a task. Returns true if the task has not yet run or if
78  // it's a task scheduled for repeated execution. Returns false if
79  // the task has already run, was already cancelled or was never
80  // schedulded.
81  //
82  bool cancel(const TimerTaskPtr&);
83 
84 protected:
85 
86  virtual void run();
87  virtual void runTimerTask(const TimerTaskPtr&);
88 
89  struct Token
90  {
94 
95  inline Token(const IceUtil::Time&, const IceUtil::Time&, const TimerTaskPtr&);
96  inline bool operator<(const Token& r) const;
97  };
98 
101  std::set<Token> _tokens;
102 
103 #if (ICE_CPLUSPLUS >= 201703L)
104  class TimerTaskCompare
105 #else
106  class TimerTaskCompare : public std::binary_function<TimerTaskPtr, TimerTaskPtr, bool>
107 #endif
108  {
109  public:
110 
111  bool operator()(const TimerTaskPtr& lhs, const TimerTaskPtr& rhs) const
112  {
113  return lhs.get() < rhs.get();
114  }
115  };
116  std::map<TimerTaskPtr, IceUtil::Time, TimerTaskCompare> _tasks;
118 };
120 
121 inline
123  scheduledTime(st), delay(d), task(t)
124 {
125 }
126 
127 inline bool
129 {
130  if(scheduledTime < r.scheduledTime)
131  {
132  return true;
133  }
134  else if(scheduledTime > r.scheduledTime)
135  {
136  return false;
137  }
138 
139  return task.get() < r.task.get();
140 }
141 
142 }
143 
144 #endif
IceUtil::Timer::Timer
Timer()
IceUtil::Timer::scheduleRepeated
void scheduleRepeated(const TimerTaskPtr &, const IceUtil::Time &)
ICE_DEFINE_PTR
#define ICE_DEFINE_PTR(TPtr, T)
Definition: Config.h:359
IceUtil::Timer::runTimerTask
virtual void runTimerTask(const TimerTaskPtr &)
IceUtil::operator<
bool operator<(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition: Handle.h:109
IceUtil::Timer::Token::Token
Token(const IceUtil::Time &, const IceUtil::Time &, const TimerTaskPtr &)
Definition: Timer.h:122
IceUtil::Timer
Definition: Timer.h:46
IceUtil::Timer::run
virtual void run()
IceUtil::Thread
Definition: Thread.h:99
IceUtil
Definition: Optional.h:1095
ICE_API
#define ICE_API
Definition: Config.h:197
IceUtil::Timer::Token::delay
IceUtil::Time delay
Definition: Timer.h:92
IceUtil::Monitor< IceUtil::Mutex >
IceUtil::Timer::_wakeUpTime
IceUtil::Time _wakeUpTime
Definition: Timer.h:117
Shared.h
IceUtil::Time
Definition: Time.h:18
IceUtil::Timer::Token
Definition: Timer.h:90
Thread.h
IceUtil::TimerTask
Definition: Timer.h:31
IceUtil::Timer::TimerTaskCompare
Definition: Timer.h:108
Time.h
IceUtil::Timer::TimerTaskCompare::operator()
bool operator()(const TimerTaskPtr &lhs, const TimerTaskPtr &rhs) const
Definition: Timer.h:111
IceUtil::Timer::Token::operator<
bool operator<(const Token &r) const
Definition: Timer.h:128
IceUtil::Timer::_destroyed
bool _destroyed
Definition: Timer.h:100
IceUtil::Timer::cancel
bool cancel(const TimerTaskPtr &)
IceUtil::Timer::Timer
Timer(int priority)
IceUtil::Timer::schedule
void schedule(const TimerTaskPtr &, const IceUtil::Time &)
IceUtil::TimerTaskPtr
::std::shared_ptr< TimerTask > TimerTaskPtr
Definition: Timer.h:38
IceUtil::Handle
Definition: Handle.h:143
IceUtil::Timer::_tasks
std::map< TimerTaskPtr, IceUtil::Time, TimerTaskCompare > _tasks
Definition: Timer.h:116
IceUtil::Timer::_tokens
std::set< Token > _tokens
Definition: Timer.h:101
IceUtil::Timer::_monitor
IceUtil::Monitor< IceUtil::Mutex > _monitor
Definition: Timer.h:99
IceUtil::TimerTask::~TimerTask
virtual ~TimerTask()
IceUtil::Timer::Token::scheduledTime
IceUtil::Time scheduledTime
Definition: Timer.h:91
IceUtil::Shared
Definition: Shared.h:78
Monitor.h
IceUtil::Timer::destroy
void destroy()
IceUtil::Timer::Token::task
TimerTaskPtr task
Definition: Timer.h:93
IceUtil::TimerPtr
IceUtil::Handle< Timer > TimerPtr
Definition: Timer.h:20
IceUtil::TimerTask::runTimerTask
virtual void runTimerTask()=0