1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \----------------------------------------------------------------------/
10 * This file contains private API, this might break at any time between releases.
11 * You have been warned!
14 #ifndef ZYPP_NG_BASE_TIMER_H_INCLUDED
15 #define ZYPP_NG_BASE_TIMER_H_INCLUDED
17 #include <zypp/zyppng/base/zyppglobal.h>
18 #include <zypp/zyppng/base/Base>
19 #include <zypp/zyppng/base/Signals>
24 class EventDispatcher;
27 * \brief The Timer class provides repetitive and single-shot timers.
29 * Provides a high level interface for timers. To use it, create a Timer and
30 * connect a slot to its \sa sigExpired signal.
33 * zyppng::Timer::Ptr t1 = zyppng::Timer::create();
34 * t1->sigExpired().connect( sigc::mem_fun(this, &HandlerClass::timeout) );
38 * The timeout slot will now be called every second.
40 * \note The accuracy of the timer should be around 1ms , but also depends on the underlying hardware.
42 class Timer : public Base
44 ZYPP_DECLARE_PRIVATE(Timer)
45 friend class EventDispatcher;
49 using Ptr = std::shared_ptr<Timer>;
50 using WeakPtr = std::shared_ptr<Timer>;
53 * \brief Creates a new Timer object, the timer is not started at this point
55 static std::shared_ptr<Timer> create ();
59 * \brief Sets the timer to trigger only once, after it has expired once
60 * \sa start needs to be called again
62 void setSingleShot ( bool singleShot = true );
65 * \returns true if the timer is a single shot timer
67 bool singleShot () const;
70 * \returns The current monotonic system time in milliseconds
72 static uint64_t now ();
75 * \returns the monotonic system time when the timer started
77 uint64_t started () const;
80 * \returns the requested interval in milliseconds
82 uint64_t interval () const;
85 * \returns the remaining time until the timer expires in milliseconds
87 uint64_t remaining () const;
90 * \return the time that has elapsed since the last call to \a start in milliseconds
92 uint64_t elapsed () const;
95 * \returns the monotonic system time in ms when the timer is about to expire
97 uint64_t expires () const;
100 * \brief Advances the internal clock of the timer, if the timer expires the \a sigExpired signal is emitted
102 * \returns the monotonic system time in ms when the timer is about to expire
103 * \note There should not be any reason to call this manually, the \sa EventDispatcher is taking care of that
108 * \returns if the timer is currently active
110 bool isRunning ( ) const;
113 * Starts the timer, if the timer is already running this will restart the currently running timer
118 * \brief Starts the timer, if the timer is already running this will restart the currently running timer
119 * \param timeout the new timeout in ms
121 void start ( uint64_t timeout );
124 * \brief Stops the timer if its running. The \sa sigExpired signal will not emit until \sa start was called again
129 * \brief This signal is always emitted when the timer expires.
131 SignalProxy<void (Timer &t)> sigExpired ();