neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
timer.hpp
Go to the documentation of this file.
1// timer.hpp
2/*
3 * Copyright (c) 2007, 2020 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <stdexcept>
40#include <boost/bind.hpp>
41
44#include <neolib/task/event.hpp>
47
48namespace neolib
49{
50 class NEOLIB_EXPORT timer : private noncopyable, public lifetime<>
51 {
52 // types
53 public:
54 typedef std::chrono::duration<double> duration_type;
55 // exceptions
56 public:
57 struct already_waiting : std::logic_error { already_waiting() : std::logic_error("neolib::timer::already_waiting") {} };
58 struct already_enabled : std::logic_error { already_enabled() : std::logic_error("neolib::timer::already_enabled") {} };
59 struct already_disabled : std::logic_error { already_disabled() : std::logic_error("neolib::timer::already_disabled") {} };
60 // construction
61 public:
62 timer(i_async_task& aTask, const duration_type& aDuration_s, bool aInitialWait = true);
63 timer(i_async_task& aTask, const i_lifetime& aContext, const duration_type& aDuration_s, bool aInitialWait = true);
64 template <typename Context>
65 timer(i_async_task& aTask, const Context& aContext, const duration_type& aDuration_s, bool aInitialWait = true) :
66 timer{ aTask, dynamic_cast<const i_lifetime&>(aContext), aDuration_s, aInitialWait } {}
67 timer(const timer& aOther);
68 timer& operator=(const timer& aOther);
69 virtual ~timer();
70 // operations
71 public:
73 void enable(bool aWait = true);
74 void disable();
75 bool enabled() const;
76 bool disabled() const;
77 void again();
78 void again_if();
79 void cancel();
80 void reset();
81 bool waiting() const;
82 const duration_type& duration() const;
83 void set_duration(const duration_type& aDuration_s, bool aEffectiveImmediately = false);
84 public:
85 void set_debug(bool aDebug);
86 // implementation
87 protected:
89 private:
91 void handler();
92 virtual void ready() = 0;
93 // attributes
94 private:
95 i_async_task& iTask;
96 destroying_flag iTaskDestroying;
97 destroyed_flag iTaskDestroyed;
98 optional_destroyed_flag iContextDestroyed;
99 sink iSink;
100 ref_ptr<i_timer_object> iTimerObject;
101 ref_ptr<i_timer_subscriber> iTimerSubscriber;
102 duration_type iDuration_s;
103 bool iEnabled;
104 bool iWaiting;
105 bool iInReady;
106#if !defined(NDEBUG) || defined(DEBUG_TIMER_OBJECTS)
107 bool iDebug = false;
108#endif
109 };
110
111 class NEOLIB_EXPORT callback_timer : public timer
112 {
113 public:
114 callback_timer(i_async_task& aTask, std::function<void(callback_timer&)> aCallback, const duration_type& aDuration_s, bool aInitialWait = true);
115 callback_timer(i_async_task& aTask, const i_lifetime& aContext, std::function<void(callback_timer&)> aCallback, const duration_type& aDuration_s, bool aInitialWait = true);
116 template <typename Context>
117 callback_timer(i_async_task& aTask, const Context& aContext, std::function<void(callback_timer&)> aCallback, const duration_type& aDuration_s, bool aInitialWait = true) :
118 callback_timer{ aTask, dynamic_cast<const i_lifetime&>(aContext), aCallback, aDuration_s, aInitialWait } {}
120 private:
121 virtual void ready();
122 private:
123 std::function<void(callback_timer&)> iCallback;
124 };
125}
callback_timer(i_async_task &aTask, std::function< void(callback_timer &)> aCallback, const duration_type &aDuration_s, bool aInitialWait=true)
callback_timer(i_async_task &aTask, const Context &aContext, std::function< void(callback_timer &)> aCallback, const duration_type &aDuration_s, bool aInitialWait=true)
Definition timer.hpp:117
callback_timer(i_async_task &aTask, const i_lifetime &aContext, std::function< void(callback_timer &)> aCallback, const duration_type &aDuration_s, bool aInitialWait=true)
timer & operator=(const timer &aOther)
const duration_type & duration() const
void set_duration(const duration_type &aDuration_s, bool aEffectiveImmediately=false)
void set_debug(bool aDebug)
timer(const timer &aOther)
timer(i_async_task &aTask, const Context &aContext, const duration_type &aDuration_s, bool aInitialWait=true)
Definition timer.hpp:65
void enable(bool aWait=true)
void unsubscribe()
timer(i_async_task &aTask, const i_lifetime &aContext, const duration_type &aDuration_s, bool aInitialWait=true)
bool enabled() const
i_async_task & owner_task() const
bool disabled() const
bool waiting() const
timer(i_async_task &aTask, const duration_type &aDuration_s, bool aInitialWait=true)
std::chrono::duration< double > duration_type
Definition timer.hpp:54
virtual ~timer()
std::optional< destroyed_flag > optional_destroyed_flag
Definition lifetime.hpp:78