$search
00001 /* 00002 * Copyright (C) 2009, Willow Garage, Inc. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are met: 00006 * * Redistributions of source code must retain the above copyright notice, 00007 * this list of conditions and the following disclaimer. 00008 * * Redistributions in binary form must reproduce the above copyright 00009 * notice, this list of conditions and the following disclaimer in the 00010 * documentation and/or other materials provided with the distribution. 00011 * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its 00012 * contributors may be used to endorse or promote products derived from 00013 * this software without specific prior written permission. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00016 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00017 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00019 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00020 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00021 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00023 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00024 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00025 * POSSIBILITY OF SUCH DAMAGE. 00026 */ 00027 00028 #include "ros/timer.h" 00029 #include "ros/timer_manager.h" 00030 00031 namespace ros 00032 { 00033 00034 Timer::Impl::Impl() 00035 : started_(false) 00036 , timer_handle_(-1) 00037 { } 00038 00039 Timer::Impl::~Impl() 00040 { 00041 ROS_DEBUG("Timer deregistering callbacks."); 00042 stop(); 00043 } 00044 00045 bool Timer::Impl::isValid() 00046 { 00047 return !period_.isZero(); 00048 } 00049 00050 void Timer::Impl::start() 00051 { 00052 if (!started_) 00053 { 00054 VoidConstPtr tracked_object; 00055 if (has_tracked_object_) 00056 { 00057 tracked_object = tracked_object_.lock(); 00058 } 00059 00060 timer_handle_ = TimerManager<Time, Duration, TimerEvent>::global().add(period_, callback_, callback_queue_, tracked_object, oneshot_); 00061 started_ = true; 00062 } 00063 } 00064 00065 void Timer::Impl::stop() 00066 { 00067 if (started_) 00068 { 00069 started_ = false; 00070 TimerManager<Time, Duration, TimerEvent>::global().remove(timer_handle_); 00071 timer_handle_ = -1; 00072 } 00073 } 00074 00075 bool Timer::Impl::hasPending() 00076 { 00077 if (!isValid() || timer_handle_ == -1) 00078 { 00079 return false; 00080 } 00081 00082 return TimerManager<Time, Duration, TimerEvent>::global().hasPending(timer_handle_); 00083 } 00084 00085 void Timer::Impl::setPeriod(const Duration& period) 00086 { 00087 period_ = period; 00088 TimerManager<Time, Duration, TimerEvent>::global().setPeriod(timer_handle_, period); 00089 } 00090 00091 Timer::Timer(const TimerOptions& 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 Timer::Timer(const Timer& rhs) 00103 { 00104 impl_ = rhs.impl_; 00105 } 00106 00107 Timer::~Timer() 00108 { 00109 } 00110 00111 void Timer::start() 00112 { 00113 if (impl_) 00114 { 00115 impl_->start(); 00116 } 00117 } 00118 00119 void Timer::stop() 00120 { 00121 if (impl_) 00122 { 00123 impl_->stop(); 00124 } 00125 } 00126 00127 bool Timer::hasPending() 00128 { 00129 if (impl_) 00130 { 00131 return impl_->hasPending(); 00132 } 00133 00134 return false; 00135 } 00136 00137 void Timer::setPeriod(const Duration& period) 00138 { 00139 if (impl_) 00140 { 00141 impl_->setPeriod(period); 00142 } 00143 } 00144 00145 }