neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
lifetime.hpp
Go to the documentation of this file.
1// lifetime.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#include <atomic>
41
42namespace neolib
43{
44 template <lifetime_state RequiredState>
46 {
47 public:
48 lifetime_flag(const i_lifetime& aSubject);
49 template <typename Subject>
50 lifetime_flag(const Subject& aSubject, std::enable_if_t<std::is_base_of_v<i_lifetime, Subject>, sfinae> = {}) :
51 lifetime_flag{ static_cast<const i_lifetime&>(aSubject), } {}
52 template <typename Subject>
53 lifetime_flag(const Subject& aSubject, std::enable_if_t<!std::is_base_of_v<i_lifetime, Subject>, sfinae> = {}) :
54 lifetime_flag{ dynamic_cast<const i_lifetime&>(aSubject), } {}
58 public:
61 public:
62 bool is_creating() const final;
63 bool is_alive() const final;
64 bool is_destroying() const final;
65 bool is_destroyed() const final;
66 operator bool() const final;
67 public:
68 bool debug() const override;
69 void set_debug(bool aDebug = true) override;
70 private:
71 std::shared_ptr<std::atomic<lifetime_state>> iState;
72 bool iDebug;
73 };
74
79
80 template <typename Base = i_lifetime>
81 class lifetime : public Base
82 {
83 public:
84 using typename Base::not_creating;
85 using typename Base::already_destroyed;
86 public:
88 public:
90 virtual ~lifetime();
91 public:
93 std::shared_ptr<std::atomic<lifetime_state>> object_state_ptr() const final;
94 bool is_creating() const final;
95 bool is_alive() const final;
96 bool is_destroying() const final;
97 bool is_destroyed() const final;
98 void set_alive() override;
99 void set_destroying() override;
100 void set_destroyed() override;
101 private:
102 std::shared_ptr<std::atomic<lifetime_state>> iState;
103 };
104
105 template <typename Base>
106 inline lifetime<Base>::lifetime(lifetime_state aState) : iState{ std::make_shared<std::atomic<lifetime_state>>(aState) }
107 {
108 }
109
110 template <typename Base>
112 {
113 if (!is_destroyed())
114 {
115 set_destroying();
116 set_destroyed();
117 }
118 }
119
120 template <typename Base>
122 {
123 return *iState;
124 }
125
126 template <typename Base>
127 inline std::shared_ptr<std::atomic<lifetime_state>> lifetime<Base>::object_state_ptr() const
128 {
129 return iState;
130 }
131
132 template <typename Base>
133 inline bool lifetime<Base>::is_creating() const
134 {
135 return *iState == lifetime_state::Creating;
136 }
137
138 template <typename Base>
139 inline bool lifetime<Base>::is_alive() const
140 {
141 return *iState == lifetime_state::Alive;
142 }
143
144 template <typename Base>
146 {
147 return *iState == lifetime_state::Destroying;
148 }
149
150 template <typename Base>
152 {
153 return *iState == lifetime_state::Destroyed;
154 }
155
156 template <typename Base>
158 {
159 if (!is_creating())
160 throw not_creating();
161 *iState = lifetime_state::Alive;
162 }
163
164 template <typename Base>
166 {
167 if (!is_destroying())
168 {
169 if (is_destroyed())
170 throw already_destroyed();
172 }
173 }
174
175 template <typename Base>
177 {
178 if (!is_destroyed())
179 {
180 if (*iState == lifetime_state::Creating || *iState == lifetime_state::Alive)
181 set_destroying();
183 }
184 }
185}
186
lifetime_flag(const Subject &aSubject, std::enable_if_t< std::is_base_of_v< i_lifetime, Subject >, sfinae >={})
Definition lifetime.hpp:50
bool debug() const override
bool is_destroying() const final
bool is_creating() const final
lifetime_flag(lifetime_flag &&aOther)
lifetime_flag(const Subject &aSubject, std::enable_if_t<!std::is_base_of_v< i_lifetime, Subject >, sfinae >={})
Definition lifetime.hpp:53
bool is_destroyed() const final
lifetime_flag(const i_lifetime &aSubject)
lifetime_flag & operator=(const lifetime_flag &aOther)
lifetime_flag & operator=(lifetime_flag &&aOther)
bool is_alive() const final
void set_debug(bool aDebug=true) override
lifetime_flag(const lifetime_flag &aOther)
void set_destroying() override
Definition lifetime.hpp:165
lifetime_state object_state() const final
Definition lifetime.hpp:121
bool is_creating() const final
Definition lifetime.hpp:133
neolib::destroyed_flag destroyed_flag
Definition lifetime.hpp:87
lifetime(lifetime_state aState=lifetime_state::Alive)
Definition lifetime.hpp:106
void set_destroyed() override
Definition lifetime.hpp:176
bool is_destroying() const final
Definition lifetime.hpp:145
bool is_alive() const final
Definition lifetime.hpp:139
virtual ~lifetime()
Definition lifetime.hpp:111
void set_alive() override
Definition lifetime.hpp:157
bool is_destroyed() const final
Definition lifetime.hpp:151
std::shared_ptr< std::atomic< lifetime_state > > object_state_ptr() const final
Definition lifetime.hpp:127
std::optional< destroyed_flag > optional_destroyed_flag
Definition lifetime.hpp:78
std::optional< destroying_flag > optional_destroying_flag
Definition lifetime.hpp:76
Definition plf_hive.h:79