fw4spl
Timer.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWTHREAD_TIMER_HPP__
8 #define __FWTHREAD_TIMER_HPP__
9 
10 #include "fwThread/config.hpp"
11 #include "fwThread/Worker.hpp"
12 
13 #include <fwCore/mt/types.hpp>
14 
15 #include <functional>
16 
17 namespace fwThread
18 {
19 
27 class FWTHREAD_CLASS_API Timer : public ::fwCore::BaseObject
28 {
29 public:
33  typedef std::shared_ptr< Timer > sptr;
34  typedef std::function< void () > FunctionType;
35  typedef std::chrono::duration<double> TimeDurationType;
38  FWTHREAD_API ~Timer();
39 
41  FWTHREAD_API virtual void start() = 0;
42 
44  FWTHREAD_API virtual void stop()
45  {
46  }
47 
49  FWTHREAD_API virtual void setDuration(TimeDurationType duration) = 0;
50 
52  template< typename F >
53  void setFunction(F f)
54  {
55  ::fwCore::mt::ScopedLock lock(m_mutex);
56  m_function = f;
57  updatedFunction();
58  }
59 
61  virtual bool isOneShot() const = 0;
62 
64  virtual void setOneShot(bool oneShot) = 0;
65 
67  virtual bool isRunning() const = 0;
68 
69 protected:
70 
75  FWTHREAD_API Timer();
76 
78  Timer( const Timer& );
79 
81  Timer& operator=( const Timer& );
82 
84  FWTHREAD_API virtual void updatedFunction()
85  {
86  }
87 
89  FunctionType m_function;
90 
91  mutable ::fwCore::mt::Mutex m_mutex;
92 };
93 
94 } //namespace fwThread
95 
96 #endif //__FWTHREAD_TIMER_HPP__
97 
Base class for all FW4SPL&#39;s classes.
Definition: BaseObject.hpp:22
void setFunction(F f)
Sets the function to be triggered when time duration expires.
Definition: Timer.hpp:53
FunctionType m_function
Function object to execute each time the timer expires.
Definition: Timer.hpp:89
virtual FWTHREAD_API void updatedFunction()
This method is triggered when Timer&#39;s function is changed.
Definition: Timer.hpp:84
The Timer class provides single-shot or repetitive timers. A Timer triggers a function once after a d...
Definition: Timer.hpp:27
virtual FWTHREAD_API void stop()
Stops the timer and cancel all pending operations.
Definition: Timer.hpp:44
This namespace fwThread provides few tools to execute asynchronous tasks on different threads...