FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_subscription.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_SUBSCRIPTION_HPP_INCLUDED
18#define _FGE_C_SUBSCRIPTION_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include <cstdint>
22#include <unordered_map>
23#include <unordered_set>
24
25namespace fge
26{
27
28class Subscriber;
29
47class FGE_API Subscription
48{
49public:
50 using SubscriberCount = unsigned int;
51
52 Subscription() = default;
53
55 Subscription([[maybe_unused]] fge::Subscription const& r) {};
56 Subscription(fge::Subscription&& r) noexcept;
57
61 virtual inline ~Subscription() { this->detachAll(); }
62
64 fge::Subscription& operator=([[maybe_unused]] fge::Subscription const& r) { return *this; };
65 fge::Subscription& operator=(fge::Subscription&& r) noexcept;
66
67protected:
75 virtual void onDetach(fge::Subscriber* subscriber) = 0;
76
80 void detachAll();
81
90 bool detach(fge::Subscriber* subscriber);
101 fge::Subscription::SubscriberCount detachOnce(fge::Subscriber* subscriber);
102
112 fge::Subscription::SubscriberCount attach(fge::Subscriber* subscriber);
113
120 fge::Subscription::SubscriberCount getCount(fge::Subscriber* subscriber) const;
121
122private:
123 using SubscriptionDataType = std::unordered_map<fge::Subscriber*, fge::Subscription::SubscriberCount>;
124
133 void detachSilent(fge::Subscriber* subscriber);
134
135 fge::Subscription::SubscriptionDataType g_subData;
136 friend class fge::Subscriber;
137};
138
149class FGE_API Subscriber
150{
151public:
152 Subscriber() = default;
153
155 Subscriber([[maybe_unused]] fge::Subscriber const& n) {};
157 Subscriber(fge::Subscriber&& n) noexcept = delete;
158
162 virtual inline ~Subscriber() { this->detachAll(); };
163
165 fge::Subscriber& operator=([[maybe_unused]] fge::Subscriber const& n) { return *this; };
168
172 void detachAll();
178 void detach(fge::Subscription* subscription);
179
180protected:
186 virtual void onDetach([[maybe_unused]] fge::Subscription* subscription) {}
187
188private:
189 using SubscriberDataType = std::unordered_set<fge::Subscription*>;
190
199 void detachSilent(fge::Subscription* subscription);
205 void attachSilent(fge::Subscription* subscription);
206
207 fge::Subscriber::SubscriberDataType g_subData;
208 friend class fge::Subscription;
209};
210
211} // namespace fge
212
213#endif // _FGE_C_SUBSCRIPTION_HPP_INCLUDED
This class is a useful utility to "link" multiple objects around.
Definition C_subscription.hpp:150
fge::Subscriber & operator=(fge::Subscriber &&n) noexcept=delete
Subscriber(fge::Subscriber const &n)
Definition C_subscription.hpp:155
fge::Subscriber & operator=(fge::Subscriber const &n)
Definition C_subscription.hpp:165
virtual void onDetach(fge::Subscription *subscription)
Callback called when a subscription is detached.
Definition C_subscription.hpp:186
void detachAll()
Detach from all subscriptions.
void detach(fge::Subscription *subscription)
Detach from a specific subscription.
Subscriber(fge::Subscriber &&n) noexcept=delete
virtual ~Subscriber()
When the object is destroyed, it will detach from all subscriptions.
Definition C_subscription.hpp:162
This class is a useful utility to "link" multiple objects around a specific use with automatic lifeti...
Definition C_subscription.hpp:48
Subscription(fge::Subscription const &r)
Definition C_subscription.hpp:55
virtual ~Subscription()
When the object is destroyed, it will detach all subscribers.
Definition C_subscription.hpp:61
fge::Subscription::SubscriberCount detachOnce(fge::Subscriber *subscriber)
Detach only once a specific subscriber.
fge::Subscription::SubscriberCount attach(fge::Subscriber *subscriber)
Attach a specific subscriber.
virtual void onDetach(fge::Subscriber *subscriber)=0
Callback called when a subscriber is detached.
void detachAll()
Detach all subscribers.
fge::Subscription::SubscriberCount getCount(fge::Subscriber *subscriber) const
Get the SubscriberCount of a specific subscriber.
bool detach(fge::Subscriber *subscriber)
Detach a specific subscriber.
fge::Subscription & operator=(fge::Subscription const &r)
Definition C_subscription.hpp:64