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/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("Timer deregistering callbacks.");
42  stop();
43 }
44 
46 {
47  return started_;
48 }
49 
51 {
52  return !period_.isZero();
53 }
54 
56 {
57  return !period_.isZero();
58 }
59 
61 {
62  if (!started_)
63  {
64  VoidConstPtr tracked_object;
66  {
67  tracked_object = tracked_object_.lock();
68  }
69 
71  started_ = true;
72  }
73 }
74 
76 {
77  if (started_)
78  {
79  started_ = false;
81  timer_handle_ = -1;
82  }
83 }
84 
86 {
87  if (!isValid() || timer_handle_ == -1)
88  {
89  return false;
90  }
91 
93 }
94 
95 void Timer::Impl::setPeriod(const Duration& period, bool reset)
96 {
97  period_ = period;
99 }
100 
102 : impl_(new Impl)
103 {
104  impl_->period_ = ops.period;
105  impl_->callback_ = ops.callback;
106  impl_->callback_queue_ = ops.callback_queue;
107  impl_->tracked_object_ = ops.tracked_object;
108  impl_->has_tracked_object_ = (ops.tracked_object != NULL);
109  impl_->oneshot_ = ops.oneshot;
110 }
111 
112 Timer::Timer(const Timer& rhs)
113 {
114  impl_ = rhs.impl_;
115 }
116 
118 {
119 }
120 
122 {
123  if (impl_)
124  {
125  impl_->start();
126  }
127 }
128 
130 {
131  if (impl_)
132  {
133  impl_->stop();
134  }
135 }
136 
138 {
139  if (impl_)
140  {
141  return impl_->hasPending();
142  }
143 
144  return false;
145 }
146 
147 void Timer::setPeriod(const Duration& period, bool reset)
148 {
149  if (impl_)
150  {
151  impl_->setPeriod(period, reset);
152  }
153 }
154 
155 }
bool hasPending()
Definition: timer.cpp:85
void remove(int32_t handle)
int32_t timer_handle_
Definition: timer.h:114
void stop()
Stop the timer. Once this call returns, no more callbacks will be called. Does nothing if the timer i...
Definition: timer.cpp:129
void setPeriod(const Duration &period, bool reset=true)
Set the period of this timer.
Definition: timer.cpp:147
void start()
Start the timer. Does nothing if the timer is already started.
Definition: timer.cpp:121
VoidConstWPtr tracked_object_
Definition: timer.h:119
CallbackQueueInterface * callback_queue
Queue to add callbacks to. If NULL, the global callback queue will be used.
Definition: timer_options.h:65
VoidConstPtr tracked_object
Definition: timer_options.h:75
TimerCallback callback_
Definition: timer.h:117
bool hasStarted() const
Definition: timer.cpp:45
bool hasPending(int32_t handle)
void stop()
Definition: timer.cpp:75
Duration period_
Definition: timer.h:116
int32_t add(const D &period, const boost::function< void(const E &)> &callback, CallbackQueueInterface *callback_queue, const VoidConstPtr &tracked_object, bool oneshot)
void setPeriod(const Duration &period, bool reset=true)
Definition: timer.cpp:95
Timer()
Definition: timer.h:49
bool isValid()
Definition: timer.cpp:50
void start()
Definition: timer.cpp:60
TimerCallback callback
The callback to call.
Definition: timer_options.h:63
Encapsulates all options available for starting a timer.
Definition: timer_options.h:40
Duration period
The period to call the callback at.
Definition: timer_options.h:62
Manages a timer callback.
Definition: timer.h:46
CallbackQueueInterface * callback_queue_
Definition: timer.h:118
static TimerManager & global()
bool oneshot_
Definition: timer.h:121
bool started_
Definition: timer.h:113
bool has_tracked_object_
Definition: timer.h:120
bool hasPending()
Returns whether or not the timer has any pending events to call.
Definition: timer.cpp:137
ImplPtr impl_
Definition: timer.h:126
#define ROS_DEBUG(...)
void setPeriod(int32_t handle, const D &period, bool reset=true)


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
autogenerated on Mon Feb 28 2022 23:33:27