$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/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 }