File indexing completed on 2024-05-05 17:50:04

0001 /*
0002    Copyright (C) 2014 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #ifndef TIMER_H
0025 #define TIMER_H
0026 
0027 #include "types.h"
0028 
0029 class EventDispatcher;
0030 class EventDispatcherPrivate;
0031 class ICompletionListener;
0032 
0033 class DFERRY_EXPORT Timer
0034 {
0035 public:
0036     Timer(EventDispatcher *dispatcher);
0037     ~Timer();
0038     // can't allow copying because Timers are remembered by pointer in EventDispatcher
0039     // (and since this isn't a value class, copying makes little sense)
0040     Timer(const Timer &) = delete;
0041     void operator=(const Timer &) = delete;
0042 
0043     void start(int msec); // convenience: setInterval(msec) and setRunning(true)
0044     void stop(); // convenience: setRunning(false)
0045     void setRunning(bool);
0046     bool isRunning() const;
0047 
0048     void setInterval(int msec);
0049     int interval() const;
0050 
0051     void setRepeating(bool);
0052     bool isRepeating() const;
0053 
0054     int remainingTime() const;
0055 
0056     void setCompletionListener(ICompletionListener *client);
0057     ICompletionListener *completionClient() const;
0058 
0059     EventDispatcher *eventDispatcher() const;
0060 
0061 private:
0062     friend class EventDispatcherPrivate;
0063     void trigger();
0064     EventDispatcher *m_eventDispatcher; // TODO make a per-thread event dispatcher implicit?
0065     ICompletionListener *m_completionListener;
0066     bool *m_reentrancyGuard;
0067     int m_interval;
0068     bool m_isRunning : 1;
0069     bool m_isRepeating : 1;
0070     uint32 m_reserved : sizeof(uint32) - 2;
0071     inline uint64 tag() { return (m_nextDueTime << 10) + m_serial; }
0072     uint64 m_nextDueTime : 54;
0073     uint32 m_serial : 10;
0074 };
0075 
0076 #endif // TIMER_H