00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "ros/wall_timer.h"
00029 #include "ros/timer_manager.h"
00030
00031 namespace ros
00032 {
00033
00034 WallTimer::Impl::Impl()
00035 : started_(false)
00036 , timer_handle_(-1)
00037 { }
00038
00039 WallTimer::Impl::~Impl()
00040 {
00041 ROS_DEBUG("WallTimer deregistering callbacks.");
00042 stop();
00043 }
00044
00045 void WallTimer::Impl::start()
00046 {
00047 if (!started_)
00048 {
00049 VoidConstPtr tracked_object;
00050 if (has_tracked_object_)
00051 {
00052 tracked_object = tracked_object_.lock();
00053 }
00054 timer_handle_ = TimerManager<WallTime, WallDuration, WallTimerEvent>::global().add(period_, callback_, callback_queue_, tracked_object, oneshot_);
00055 started_ = true;
00056 }
00057 }
00058
00059 void WallTimer::Impl::stop()
00060 {
00061 if (started_)
00062 {
00063 started_ = false;
00064 TimerManager<WallTime, WallDuration, WallTimerEvent>::global().remove(timer_handle_);
00065 timer_handle_ = -1;
00066 }
00067 }
00068
00069 bool WallTimer::Impl::isValid()
00070 {
00071 return !period_.isZero();
00072 }
00073
00074 bool WallTimer::Impl::hasPending()
00075 {
00076 if (!isValid() || timer_handle_ == -1)
00077 {
00078 return false;
00079 }
00080
00081 return TimerManager<WallTime, WallDuration, WallTimerEvent>::global().hasPending(timer_handle_);
00082 }
00083
00084 void WallTimer::Impl::setPeriod(const WallDuration& period)
00085 {
00086 period_ = period;
00087 TimerManager<WallTime, WallDuration, WallTimerEvent>::global().setPeriod(timer_handle_, period);
00088 }
00089
00090
00091 WallTimer::WallTimer(const WallTimerOptions& ops)
00092 : impl_(new Impl)
00093 {
00094 impl_->period_ = ops.period;
00095 impl_->callback_ = ops.callback;
00096 impl_->callback_queue_ = ops.callback_queue;
00097 impl_->tracked_object_ = ops.tracked_object;
00098 impl_->has_tracked_object_ = ops.tracked_object;
00099 impl_->oneshot_ = ops.oneshot;
00100 }
00101
00102 WallTimer::WallTimer(const WallTimer& rhs)
00103 {
00104 impl_ = rhs.impl_;
00105 }
00106
00107 WallTimer::~WallTimer()
00108 {
00109 }
00110
00111 void WallTimer::start()
00112 {
00113 if (impl_)
00114 {
00115 impl_->start();
00116 }
00117 }
00118
00119 void WallTimer::stop()
00120 {
00121 if (impl_)
00122 {
00123 impl_->stop();
00124 }
00125 }
00126
00127 bool WallTimer::hasPending()
00128 {
00129 if (impl_)
00130 {
00131 return impl_->hasPending();
00132 }
00133
00134 return false;
00135 }
00136
00137 void WallTimer::setPeriod(const WallDuration& period)
00138 {
00139 if (impl_)
00140 {
00141 impl_->setPeriod(period);
00142 }
00143 }
00144
00145 }