wall_timer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "ros/wall_timer.h"
29 #include "ros/timer_manager.h"
30 
31 namespace ros
32 {
33 
35  : started_(false)
36  , timer_handle_(-1)
37 { }
38 
40 {
41  ROS_DEBUG("WallTimer deregistering callbacks.");
42  stop();
43 }
44 
46 {
47  return started_;
48 }
49 
51 {
52  if (!started_)
53  {
54  VoidConstPtr tracked_object;
55  if (has_tracked_object_)
56  {
57  tracked_object = tracked_object_.lock();
58  }
59  timer_handle_ = TimerManager<WallTime, WallDuration, WallTimerEvent>::global().add(period_, callback_, callback_queue_, tracked_object, oneshot_);
60  started_ = true;
61  }
62 }
63 
65 {
66  if (started_)
67  {
68  started_ = false;
70  timer_handle_ = -1;
71  }
72 }
73 
75 {
76  return !period_.isZero();
77 }
78 
80 {
81  if (!isValid() || timer_handle_ == -1)
82  {
83  return false;
84  }
85 
87 }
88 
89 void WallTimer::Impl::setPeriod(const WallDuration& period, bool reset)
90 {
91  period_ = period;
93 }
94 
95 
97 : impl_(new Impl)
98 {
99  impl_->period_ = ops.period;
100  impl_->callback_ = ops.callback;
101  impl_->callback_queue_ = ops.callback_queue;
102  impl_->tracked_object_ = ops.tracked_object;
103  impl_->has_tracked_object_ = (ops.tracked_object != NULL);
104  impl_->oneshot_ = ops.oneshot;
105 }
106 
108 {
109  impl_ = rhs.impl_;
110 }
111 
113 {
114 }
115 
117 {
118  if (impl_)
119  {
120  impl_->start();
121  }
122 }
123 
125 {
126  if (impl_)
127  {
128  impl_->stop();
129  }
130 }
131 
133 {
134  if (impl_)
135  {
136  return impl_->hasPending();
137  }
138 
139  return false;
140 }
141 
142 void WallTimer::setPeriod(const WallDuration& period, bool reset)
143 {
144  if (impl_)
145  {
146  impl_->setPeriod(period, reset);
147  }
148 }
149 
150 }
boost::shared_ptr< void const >
ros::WallTimer::WallTimer
WallTimer()
Definition: wall_timer.h:49
ros::WallTimerOptions::period
WallDuration period
The period to call the callback at.
Definition: wall_timer_options.h:63
ros::WallTimer::isValid
bool isValid()
Definition: wall_timer.h:76
ros::WallTimer::hasPending
bool hasPending()
Returns whether or not the timer has any pending events to call.
Definition: wall_timer.cpp:132
ros
ros::WallTimer::impl_
ImplPtr impl_
Definition: wall_timer.h:124
ros::WallTimer::Impl
Definition: wall_timer.h:97
wall_timer.h
ros::WallTimer::Impl::isValid
bool isValid()
Definition: wall_timer.cpp:74
ros::WallTimerOptions::callback
WallTimerCallback callback
The callback to call.
Definition: wall_timer_options.h:64
ros::WallTimer::Impl::start
void start()
Definition: wall_timer.cpp:50
ros::WallTimer::Impl::hasPending
bool hasPending()
Definition: wall_timer.cpp:79
ros::WallTimerOptions::tracked_object
VoidConstPtr tracked_object
Definition: wall_timer_options.h:76
ros::WallTimer::start
void start()
Start the timer. Does nothing if the timer is already started.
Definition: wall_timer.cpp:116
ros::WallTimer
Manages a wall-clock timer callback.
Definition: wall_timer.h:46
ros::WallTimerOptions
Encapsulates all options available for starting a timer.
Definition: wall_timer_options.h:40
ros::TimerManager::add
int32_t add(const D &period, const boost::function< void(const E &)> &callback, CallbackQueueInterface *callback_queue, const VoidConstPtr &tracked_object, bool oneshot)
Definition: timer_manager.h:314
timer_manager.h
ROS_DEBUG
#define ROS_DEBUG(...)
ros::WallTimer::setPeriod
void setPeriod(const WallDuration &period, bool reset=true)
Set the period of this timer.
Definition: wall_timer.cpp:142
ros::WallTimer::~WallTimer
~WallTimer()
Definition: wall_timer.cpp:112
ros::WallTimer::Impl::hasStarted
bool hasStarted() const
Definition: wall_timer.cpp:45
ros::TimerManager::setPeriod
void setPeriod(int32_t handle, const D &period, bool reset=true)
Definition: timer_manager.h:454
ros::WallTimerOptions::callback_queue
CallbackQueueInterface * callback_queue
Queue to add callbacks to. If NULL, the global callback queue will be used.
Definition: wall_timer_options.h:66
ros::WallTimer::Impl::Impl
Impl()
Definition: wall_timer.cpp:34
ros::WallTimer::Impl::~Impl
~Impl()
Definition: wall_timer.cpp:39
ros::WallTimerOptions::oneshot
bool oneshot
Definition: wall_timer_options.h:78
ros::TimerManager::remove
void remove(int32_t handle)
Definition: timer_manager.h:363
ros::WallDuration
ros::WallTimer::Impl::setPeriod
void setPeriod(const WallDuration &period, bool reset=true)
Definition: wall_timer.cpp:89
ros::TimerManager::hasPending
bool hasPending(int32_t handle)
Definition: timer_manager.h:290
ros::WallTimer::stop
void stop()
Stop the timer. Once this call returns, no more callbacks will be called. Does nothing if the timer i...
Definition: wall_timer.cpp:124
ros::WallTimer::Impl::stop
void stop()
Definition: wall_timer.cpp:64
ros::TimerManager::global
static TimerManager & global()
Definition: timer_manager.h:117


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:36