Point Cloud Library (PCL)  1.14.1-dev
time_trigger.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 
39 #pragma once
40 
41 #include <pcl/pcl_macros.h>
42 
43 #include <boost/signals2.hpp>
44 
45 #include <condition_variable>
46 #include <functional>
47 #include <mutex>
48 #include <thread>
49 
50 namespace pcl
51 {
52  /** \brief Timer class that invokes registered callback methods periodically.
53  * \ingroup common
54  */
56  {
57  public:
58  using callback_type = std::function<void ()>;
59 
60  /** \brief Timer class that calls a callback method periodically. Due to possible blocking calls, only one callback method can be registered per instance.
61  * \param[in] interval_seconds interval in seconds
62  * \param[in] callback callback to be invoked periodically
63  */
64  TimeTrigger (double interval_seconds, const callback_type& callback);
65 
66  /** \brief Timer class that calls a callback method periodically. Due to possible blocking calls, only one callback method can be registered per instance.
67  * \param[in] interval_seconds interval in seconds
68  */
69  TimeTrigger (double interval_seconds = 1.0);
70 
71  /** \brief Destructor. */
73 
74  /** \brief registers a callback
75  * \param[in] callback callback function to the list of callbacks. signature has to be std::function<void()>
76  * \return connection the connection, which can be used to disable/enable and remove callback from list
77  */
78  boost::signals2::connection registerCallback (const callback_type& callback);
79 
80  /** \brief Resets the timer interval
81  * \param[in] interval_seconds interval in seconds
82  */
83  void
84  setInterval (double interval_seconds);
85 
86  /** \brief Start the Trigger. */
87  void
88  start ();
89 
90  /** \brief Stop the Trigger. */
91  void
92  stop ();
93  private:
94  void
95  thread_function ();
96  boost::signals2::signal <void() > callbacks_;
97 
98  double interval_;
99 
100  bool quit_;
101  bool running_;
102 
103  std::thread timer_thread_;
104  std::condition_variable condition_;
105  std::mutex condition_mutex_;
106  };
107 }
Timer class that invokes registered callback methods periodically.
Definition: time_trigger.h:56
void stop()
Stop the Trigger.
TimeTrigger(double interval_seconds=1.0)
Timer class that calls a callback method periodically.
void setInterval(double interval_seconds)
Resets the timer interval.
void start()
Start the Trigger.
boost::signals2::connection registerCallback(const callback_type &callback)
registers a callback
std::function< void()> callback_type
Definition: time_trigger.h:58
TimeTrigger(double interval_seconds, const callback_type &callback)
Timer class that calls a callback method periodically.
~TimeTrigger()
Destructor.
Defines all the PCL and non-PCL macros used.
#define PCL_EXPORTS
Definition: pcl_macros.h:323