neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
i_timer_object.hpp
Go to the documentation of this file.
1// i_timer_object.hpp
2/*
3 * Copyright (c) 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#if !defined(NDEBUG) || defined(DEBUG_TIMER_OBJECTS)
40#include <iostream>
41#endif
44
45namespace neolib
46{
47 class i_timer_object;
48
50 {
51 public:
53 public:
54 virtual ~i_timer_subscriber() = default;
55 public:
56 virtual void timer_expired(i_timer_object& aTimerObject) = 0;
57 virtual bool attached() const = 0;
58 virtual void detach() = 0;
59 public:
60 friend bool operator<(const i_timer_subscriber& aLeft, const i_timer_subscriber& aRight)
61 {
62 return &aLeft < &aRight;
63 }
64 };
65
67 {
68 public:
70 public:
71 struct subscriber_not_found : std::logic_error { subscriber_not_found() : std::logic_error{ "i_timer_object::subscriber_not_found" } {} };
72 private:
73 class subscriber_wrapper : public lifetime<reference_counted<i_timer_subscriber>>
74 {
75 public:
76 subscriber_wrapper(i_timer_object& aTimerObject, std::function<void()> aCallback) :
77 iTimerObject{ &aTimerObject }, iCallback { aCallback }
78 {
79 }
80 ~subscriber_wrapper()
81 {
82#if !defined(NDEBUG) || defined(DEBUG_TIMER_OBJECTS)
83 if (iTimerObject != nullptr)
84 {
85 if (iTimerObject->debug())
86 {
87 std::cerr << "i_timer_object::subscriber_wrapper::~subscriber_wrapper()" << std::endl;
88 }
89 }
90#endif
91 }
92 protected:
93 void timer_expired(i_timer_object&) override
94 {
95#if !defined(NDEBUG) || defined(DEBUG_TIMER_OBJECTS)
96 if (attached())
97 {
98 if (iTimerObject->debug())
99 {
100 std::cerr << "i_timer_object::subscriber_wrapper::timer_expired(...)" << std::endl;
101 }
102 }
103#endif
104 iCallback();
105 }
106 bool attached() const override
107 {
108 return iTimerObject != nullptr;
109 }
110 void detach() override
111 {
112 if (attached())
113 {
114 if (iTimerObject->debug())
115 {
116 std::cerr << "i_timer_object::subscriber_wrapper::detach()" << std::endl;
117 }
118 iTimerObject = nullptr;
119 }
120 }
121 private:
122 i_timer_object* iTimerObject;
123 std::function<void()> iCallback;
124 };
125 public:
126 virtual ~i_timer_object() = default;
127 public:
128 virtual void expires_at(const std::chrono::steady_clock::time_point& aDeadline) = 0;
129 virtual void async_wait(i_timer_subscriber& aSubscriber) = 0;
130 virtual void unsubscribe(i_timer_subscriber& aSubscriber) = 0;
131 virtual void cancel() = 0;
132 public:
133 virtual bool poll() = 0;
134 public:
135 virtual bool debug() const = 0;
136 virtual void set_debug(bool aDebug) = 0;
137 public:
138 template <typename Duration>
139 void expires_from_now(const Duration& aDuration)
140 {
141 expires_at(std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::nanoseconds>(aDuration));
142 }
143 i_timer_subscriber& async_wait(const std::function<void()>& aSubscriber)
144 {
145 auto subscriber = make_ref<subscriber_wrapper>(*this, [aSubscriber]() { aSubscriber(); });
146 async_wait(*subscriber);
147 return *subscriber;
148 }
149 i_timer_subscriber& async_wait(const std::function<void(i_timer_object&)>& aSubscriber)
150 {
151 auto subscriber = make_ref<subscriber_wrapper>(*this, [this, aSubscriber]() { aSubscriber(*this); });
152 async_wait(*subscriber);
153 return *subscriber;
154 }
155 };
156}
virtual void cancel()=0
virtual bool poll()=0
i_timer_subscriber & async_wait(const std::function< void()> &aSubscriber)
i_timer_object abstract_type
virtual void unsubscribe(i_timer_subscriber &aSubscriber)=0
virtual void set_debug(bool aDebug)=0
i_timer_subscriber & async_wait(const std::function< void(i_timer_object &)> &aSubscriber)
virtual void expires_at(const std::chrono::steady_clock::time_point &aDeadline)=0
virtual bool debug() const =0
virtual ~i_timer_object()=default
void expires_from_now(const Duration &aDuration)
virtual void async_wait(i_timer_subscriber &aSubscriber)=0
virtual void detach()=0
virtual ~i_timer_subscriber()=default
virtual void timer_expired(i_timer_object &aTimerObject)=0
i_timer_subscriber abstract_type
virtual bool attached() const =0
friend bool operator<(const i_timer_subscriber &aLeft, const i_timer_subscriber &aRight)