neoGFX
Cross-platform C++ app/game engine
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 
43 #include <neolib/core/lifetime.hpp>
44 #include <neolib/task/event.hpp>
47 
48 namespace neolib
49 {
50  class NEOLIB_EXPORT timer : private noncopyable, public lifetime<>
51  {
52  // types
53  public:
54  struct already_waiting : std::logic_error { already_waiting() : std::logic_error("neolib::timer::already_waiting") {} };
55  struct already_enabled : std::logic_error { already_enabled() : std::logic_error("neolib::timer::already_enabled") {} };
56  struct already_disabled : std::logic_error { already_disabled() : std::logic_error("neolib::timer::already_disabled") {} };
57  // construction
58  public:
59  timer(i_async_task& aTask, uint32_t aDuration_ms, bool aInitialWait = true);
60  timer(i_async_task& aTask, const i_lifetime& aContext, uint32_t aDuration_ms, bool aInitialWait = true);
61  timer(const timer& aOther);
62  timer& operator=(const timer& aOther);
63  virtual ~timer();
64  // operations
65  public:
67  void enable(bool aWait = true);
68  void disable();
69  bool enabled() const;
70  bool disabled() const;
71  void again();
72  void again_if();
73  void cancel();
74  void reset();
75  bool waiting() const;
76  uint32_t duration() const;
77  void set_duration(uint32_t aDuration_ms, bool aEffectiveImmediately = false);
78  uint32_t duration_ms() const;
79  public:
80  void set_debug(bool aDebug);
81  // implementation
82  protected:
83  void unsubscribe();
84  private:
86  void handler();
87  virtual void ready() = 0;
88  // attributes
89  private:
90  i_async_task& iTask;
91  destroying_flag iTaskDestroying;
92  destroyed_flag iTaskDestroyed;
93  optional_destroyed_flag iContextDestroyed;
94  sink iSink;
95  ref_ptr<i_timer_object> iTimerObject;
96  ref_ptr<i_timer_subscriber> iTimerSubscriber;
97  uint32_t iDuration_ms;
98  bool iEnabled;
99  bool iWaiting;
100  bool iInReady;
101 #if !defined(NDEBUG) || defined(DEBUG_TIMER_OBJECTS)
102  bool iDebug = false;
103 #endif
104  };
105 
106  class NEOLIB_EXPORT callback_timer : public timer
107  {
108  public:
109  callback_timer(i_async_task& aTask, std::function<void(callback_timer&)> aCallback, uint32_t aDuration_ms, bool aInitialWait = true);
110  callback_timer(i_async_task& aTask, const i_lifetime& aContext, std::function<void(callback_timer&)> aCallback, uint32_t aDuration_ms, bool aInitialWait = true);
111  template <typename Context>
112  callback_timer(i_async_task& aTask, const Context& aContext, std::function<void(callback_timer&)> aCallback, uint32_t aDuration_ms, bool aInitialWait = true) :
113  callback_timer{ aTask, dynamic_cast<const i_lifetime&>(aContext), aCallback, aDuration_ms, aInitialWait } {}
115  private:
116  virtual void ready();
117  private:
118  std::function<void(callback_timer&)> iCallback;
119  };
120 }
neolib::timer::enable
void enable(bool aWait=true)
neolib::callback_timer
Definition: timer.hpp:107
neolib::timer::again
void again()
neolib::timer::already_disabled::already_disabled
already_disabled()
Definition: timer.hpp:56
neolib::timer::already_enabled::already_enabled
already_enabled()
Definition: timer.hpp:55
neolib::timer::timer
timer(const timer &aOther)
neolib::timer::enabled
bool enabled() const
neolib::lifetime_flag< lifetime_state::Destroying >
neolib::timer::~timer
virtual ~timer()
neolib::timer::duration_ms
uint32_t duration_ms() const
neolib::i_async_task
Definition: i_async_task.hpp:88
neolib::timer
Definition: timer.hpp:51
neolib::timer::operator=
timer & operator=(const timer &aOther)
event.hpp
neolib::timer::again_if
void again_if()
neolib::timer::already_enabled
Definition: timer.hpp:55
neolib::optional_destroyed_flag
std::optional< destroyed_flag > optional_destroyed_flag
Definition: lifetime.hpp:74
noncopyable.hpp
lifetime.hpp
neolib::timer::set_duration
void set_duration(uint32_t aDuration_ms, bool aEffectiveImmediately=false)
neolib::callback_timer::callback_timer
callback_timer(i_async_task &aTask, const i_lifetime &aContext, std::function< void(callback_timer &)> aCallback, uint32_t aDuration_ms, bool aInitialWait=true)
neolib::timer::disabled
bool disabled() const
neolib::timer::set_debug
void set_debug(bool aDebug)
neolib::i_lifetime
Definition: i_lifetime.hpp:67
neolib::timer::cancel
void cancel()
neolib::callback_timer::~callback_timer
~callback_timer()
neolib::callback_timer::callback_timer
callback_timer(i_async_task &aTask, const Context &aContext, std::function< void(callback_timer &)> aCallback, uint32_t aDuration_ms, bool aInitialWait=true)
Definition: timer.hpp:112
neolib
Definition: application.hpp:46
neolib::timer::duration
uint32_t duration() const
neolib::timer::owner_task
i_async_task & owner_task() const
i_async_task.hpp
neolib::timer::disable
void disable()
neolib::timer::already_waiting::already_waiting
already_waiting()
Definition: timer.hpp:54
neolib::timer::already_waiting
Definition: timer.hpp:54
neolib::timer::already_disabled
Definition: timer.hpp:56
neolib::sink
Definition: event.hpp:828
neolib::timer::waiting
bool waiting() const
neolib::callback_timer::callback_timer
callback_timer(i_async_task &aTask, std::function< void(callback_timer &)> aCallback, uint32_t aDuration_ms, bool aInitialWait=true)
neolib::timer::timer
timer(i_async_task &aTask, uint32_t aDuration_ms, bool aInitialWait=true)
neolib::noncopyable
Definition: noncopyable.hpp:41
neolib::timer::timer
timer(i_async_task &aTask, const i_lifetime &aContext, uint32_t aDuration_ms, bool aInitialWait=true)
neolib::lifetime
Definition: lifetime.hpp:78
neolib::timer::unsubscribe
void unsubscribe()
neolib::timer::reset
void reset()
neolib::ref_ptr
Definition: reference_counted.hpp:181
neolib::i_timer_object
Definition: i_timer_object.hpp:66
neolib.hpp
i_timer_object.hpp
neolib::timer_object
Definition: timer_object.hpp:49