FastEngine 0.9.5
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_callback.hpp
1/*
2 * Copyright 2026 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_CALLBACKHANDLER_HPP_INCLUDED
18#define _FGE_C_CALLBACKHANDLER_HPP_INCLUDED
19
20#include "FastEngine/C_subscription.hpp"
21#include <memory>
22#include <mutex>
23#include <vector>
24
25namespace fge
26{
27
35template<class TReturn, class... Types>
37{
38public:
39 virtual ~CallbackBase() = default;
40
41 virtual TReturn call(Types... args) = 0;
42 virtual bool check(void* ptr) = 0;
43};
44
52template<class TReturn, class... Types>
53class CallbackFunctor : public fge::CallbackBase<TReturn, Types...>
54{
55public:
56 using CallbackFunction = TReturn (*)(Types... args);
57
63 explicit CallbackFunctor(CallbackFunction func);
64 ~CallbackFunctor() override = default;
65
71 TReturn call(Types... args) override;
78 inline bool check(void* ptr) override;
79
80protected:
81 CallbackFunction g_function;
82};
83
91template<class TReturn, class... Types>
92class CallbackLambda : public fge::CallbackBase<TReturn, Types...>
93{
94public:
101 template<typename TLambda>
102 explicit CallbackLambda(TLambda const& lambda);
103 ~CallbackLambda() override;
104
110 TReturn call(Types... args) override;
117 inline bool check(void* ptr) override;
118
119protected:
120 void* g_lambda;
121 TReturn (*g_executeLambda)(void*, Types...);
122 void (*g_deleteLambda)(void*);
123};
124
133template<class TReturn, class TObject, class... Types>
134class CallbackObjectFunctor : public fge::CallbackBase<TReturn, Types...>
135{
136public:
137 using CallbackFunctionObject = TReturn (TObject::*)(Types... args);
138
145 CallbackObjectFunctor(CallbackFunctionObject func, TObject* object);
146 ~CallbackObjectFunctor() override = default;
147
153 TReturn call(Types... args) override;
160 inline bool check(void* ptr) override;
161
162protected:
163 CallbackFunctionObject g_functionObj;
164 TObject* g_object;
165};
166
167template<class TReturn, class... Types>
168using CalleeUniquePtr = std::unique_ptr<fge::CallbackBase<TReturn, Types...>>;
169template<class TReturn, class... Types>
170using CalleeSharedPtr = std::shared_ptr<fge::CallbackBase<TReturn, Types...>>;
171
180template<class TReturn, class TCalleePtr, class... Types>
182{
183 using CalleePtr = TCalleePtr;
184
191 [[nodiscard]] inline static CalleePtr
192 newFunctor(typename fge::CallbackFunctor<TReturn, Types...>::CallbackFunction func)
193 {
194 return CalleePtr{new fge::CallbackFunctor<TReturn, Types...>(func)};
195 }
196
204 template<typename TLambda>
205 [[nodiscard]] inline static CalleePtr newLambda(TLambda const& lambda)
206 {
207 return CalleePtr{new fge::CallbackLambda<TReturn, Types...>(lambda)};
208 }
209
218 template<class TObject>
219 [[nodiscard]] inline static CalleePtr
220 newObjectFunctor(typename fge::CallbackObjectFunctor<TReturn, TObject, Types...>::CallbackFunctionObject func,
221 TObject* object)
222 {
223 return CalleePtr{new fge::CallbackObjectFunctor<TReturn, TObject, Types...>(func, object)};
224 }
225};
226
242template<class... Types>
243class CallbackHandler : public fge::Subscription
244{
245public:
246 using CalleePtr = CalleeUniquePtr<void, Types...>;
247 using StaticHelpers = CallbackStaticHelpers<void, CalleePtr, Types...>;
248
249 CallbackHandler() = default;
250 ~CallbackHandler() override = default;
251
256 fge::Subscription() {};
257
261
266 {
267 return *this;
268 };
269
273
277 inline void clear();
278
294 inline fge::CallbackBase<void, Types...>* add(CalleePtr&& callback, fge::Subscriber* subscriber = nullptr);
295
305 inline fge::CallbackFunctor<void, Types...>*
306 addFunctor(typename fge::CallbackFunctor<void, Types...>::CallbackFunction func,
307 fge::Subscriber* subscriber = nullptr);
318 template<typename TLambda>
319 inline fge::CallbackLambda<void, Types...>* addLambda(TLambda const& lambda, fge::Subscriber* subscriber = nullptr);
331 template<class TObject>
332 inline fge::CallbackObjectFunctor<void, TObject, Types...>*
333 addObjectFunctor(typename fge::CallbackObjectFunctor<void, TObject, Types...>::CallbackFunctionObject func,
334 TObject* object,
335 Subscriber* subscriber = nullptr);
336
344 void delPtr(void* ptr);
352 void delSub(fge::Subscriber* subscriber);
361
367 void call(Types... args);
368
380 void hook(fge::CallbackHandler<Types...>& handler, fge::Subscriber* subscriber = nullptr);
381
382protected:
390 void onDetach(fge::Subscriber* subscriber) override;
391
392private:
393 struct CalleeData
394 {
395 inline CalleeData(CalleePtr&& f, fge::Subscriber* subscriber) :
396 _f(std::move(f)),
397 _subscriber(subscriber)
398 {}
399
400 CalleePtr _f;
401 fge::Subscriber* _subscriber = nullptr;
402 bool _markedForDeletion = false;
403 };
404 using CalleeList = std::vector<CalleeData>;
405
406 CalleeList g_callees;
407
408 mutable std::recursive_mutex g_mutex;
409};
410
420template<class... Types>
421class UniqueCallbackHandler : public fge::UniqueSubscription
422{
423public:
424 using CalleePtr = CalleeUniquePtr<void, Types...>;
425 using StaticHelpers = CallbackStaticHelpers<void, CalleePtr, Types...>;
426
427 UniqueCallbackHandler() = default;
428 ~UniqueCallbackHandler() override = default;
429
434 fge::UniqueSubscription()
435 {}
436
440
445 {
446 return *this;
447 };
448
452
453 inline void clear();
454
464 inline fge::CallbackBase<void, Types...>* set(CalleePtr&& callback, fge::Subscriber* subscriber = nullptr);
465
475 inline fge::CallbackFunctor<void, Types...>*
476 setFunctor(typename fge::CallbackFunctor<void, Types...>::CallbackFunction func,
477 fge::Subscriber* subscriber = nullptr);
488 template<typename TLambda>
489 inline fge::CallbackLambda<void, Types...>* setLambda(TLambda const& lambda, fge::Subscriber* subscriber = nullptr);
501 template<class TObject>
502 inline fge::CallbackObjectFunctor<void, TObject, Types...>*
503 setObjectFunctor(typename fge::CallbackObjectFunctor<void, TObject, Types...>::CallbackFunctionObject func,
504 TObject* object,
505 Subscriber* subscriber = nullptr);
506
512 void delPtr(void* ptr);
518 void delSub(fge::Subscriber* subscriber);
524 void del(fge::CallbackBase<Types...>* callback);
525
531 void call(Types... args);
532
533protected:
541 void onDetach(fge::Subscriber* subscriber) override;
542
543private:
544 struct CalleeData
545 {
546 inline CalleeData(CalleePtr&& f, fge::Subscriber* subscriber) :
547 _f(std::move(f)),
548 _subscriber(subscriber)
549 {}
550
551 CalleePtr _f;
552 fge::Subscriber* _subscriber = nullptr;
553 };
554
555 CalleeData g_callee{nullptr, nullptr};
556
557 mutable std::recursive_mutex g_mutex;
558};
559
560} // namespace fge
561
562#include "FastEngine/C_callback.inl"
563
564#endif // _FGE_C_CALLBACKHANDLER_HPP_INCLUDED
Base class for callbacks.
Definition C_callback.hpp:37
Callback functor.
Definition C_callback.hpp:54
bool check(void *ptr) override
Check if the given pointer is the same as the one used to construct the functor.
Definition C_callback.inl:33
TReturn call(Types... args) override
Call the callback function with the given arguments.
Definition C_callback.inl:28
CallbackFunctor(CallbackFunction func)
Constructor.
Definition C_callback.inl:23
This class is used to handle callbacks in a safe way.
Definition C_callback.hpp:244
void delPtr(void *ptr)
Remove a callback from the list.
Definition C_callback.inl:161
void del(fge::CallbackBase< void, Types... > *callback)
Remove a callback from the list.
Definition C_callback.inl:201
fge::CallbackHandler< Types... > & operator=(fge::CallbackHandler< Types... > &&n)=delete
Move operator prohibited.
CallbackHandler(fge::CallbackHandler< Types... > const &n)
Copy constructor that does nothing.
Definition C_callback.hpp:255
fge::CallbackBase< void, Types... > * add(CalleePtr &&callback, fge::Subscriber *subscriber=nullptr)
Add a new callback to the list.
Definition C_callback.inl:108
fge::CallbackObjectFunctor< void, TObject, Types... > * addObjectFunctor(typename fge::CallbackObjectFunctor< void, TObject, Types... >::CallbackFunctionObject func, TObject *object, Subscriber *subscriber=nullptr)
Helper method to add a callback object functor.
Definition C_callback.inl:151
fge::CallbackHandler< Types... > & operator=(fge::CallbackHandler< Types... > const &n)
Copy operator that does nothing.
Definition C_callback.hpp:265
void onDetach(fge::Subscriber *subscriber) override
This method is called when a subscriber is destroyed (destructor called).
Definition C_callback.inl:263
fge::CallbackFunctor< void, Types... > * addFunctor(typename fge::CallbackFunctor< void, Types... >::CallbackFunction func, fge::Subscriber *subscriber=nullptr)
Helper method to add a callback functor.
Definition C_callback.inl:135
fge::CallbackLambda< void, Types... > * addLambda(TLambda const &lambda, fge::Subscriber *subscriber=nullptr)
Helper method to add a callback lambda.
Definition C_callback.inl:143
void call(Types... args)
Call all the callbacks with the given arguments.
Definition C_callback.inl:220
void hook(fge::CallbackHandler< Types... > &handler, fge::Subscriber *subscriber=nullptr)
Hook a callback handler to this handler.
Definition C_callback.inl:249
void delSub(fge::Subscriber *subscriber)
Remove a callback from the list.
Definition C_callback.inl:179
CallbackHandler(fge::CallbackHandler< Types... > &&n)=delete
Move constructor prohibited.
void clear()
Clear the list of callbacks.
Definition C_callback.inl:97
Callback lambda (with/without capture).
Definition C_callback.hpp:93
CallbackLambda(TLambda const &lambda)
Constructor.
Definition C_callback.inl:62
bool check(void *ptr) override
Always return false.
Definition C_callback.inl:89
TReturn call(Types... args) override
Call the callback function with the given arguments.
Definition C_callback.inl:84
Callback functor of a method with an object.
Definition C_callback.hpp:135
TReturn call(Types... args) override
Call the callback method with the given arguments.
Definition C_callback.inl:47
CallbackObjectFunctor(CallbackFunctionObject func, TObject *object)
Constructor.
Definition C_callback.inl:41
bool check(void *ptr) override
Check if the given object pointer is the same as the one used to construct the functor.
Definition C_callback.inl:53
This class is a useful utility to "link" multiple objects around.
Definition C_subscription.hpp:210
This class is a useful utility to "link" multiple objects around a specific use with automatic lifeti...
Definition C_subscription.hpp:125
This class is used for cases where only one callback is needed.
Definition C_callback.hpp:422
fge::CallbackHandler< Types... > & operator=(fge::CallbackHandler< Types... > &&n)=delete
Move operator prohibited.
fge::CallbackFunctor< void, Types... > * setFunctor(typename fge::CallbackFunctor< void, Types... >::CallbackFunction func, fge::Subscriber *subscriber=nullptr)
Helper method to set a callback functor.
Definition C_callback.inl:308
UniqueCallbackHandler(fge::UniqueCallbackHandler< Types... > const &n)
Copy constructor that does nothing.
Definition C_callback.hpp:433
void delSub(fge::Subscriber *subscriber)
Remove the callback if the subscriber is the same as the one used to construct the callback.
Definition C_callback.inl:346
void delPtr(void *ptr)
Remove the callback if the function/object pointer is the same as the one used to construct the callb...
Definition C_callback.inl:334
fge::CallbackLambda< void, Types... > * setLambda(TLambda const &lambda, fge::Subscriber *subscriber=nullptr)
Helper method to set a callback lambda.
Definition C_callback.inl:316
fge::CallbackObjectFunctor< void, TObject, Types... > * setObjectFunctor(typename fge::CallbackObjectFunctor< void, TObject, Types... >::CallbackFunctionObject func, TObject *object, Subscriber *subscriber=nullptr)
Helper method to set a callback object functor.
Definition C_callback.inl:324
void call(Types... args)
Call the callback with the given arguments.
Definition C_callback.inl:371
void onDetach(fge::Subscriber *subscriber) override
This method is called when a subscriber is destroyed (destructor called).
Definition C_callback.inl:391
fge::CallbackHandler< Types... > & operator=(fge::CallbackHandler< Types... > const &n)
Copy operator that does nothing.
Definition C_callback.hpp:444
void del(fge::CallbackBase< Types... > *callback)
Remove the callback if the callback pointer is the same as the one used to construct the callback.
Definition C_callback.inl:358
UniqueCallbackHandler(fge::CallbackHandler< Types... > &&n)=delete
Move constructor prohibited.
fge::CallbackBase< void, Types... > * set(CalleePtr &&callback, fge::Subscriber *subscriber=nullptr)
Set a new callback to the handler, replacing the previous one if it exists.
Definition C_callback.inl:291
This class allow same functionality as Subscription but only allow one subscriber at a time.
Definition C_subscription.hpp:167
This struct helper is used to create callbacks.
Definition C_callback.hpp:182
static CalleePtr newFunctor(typename fge::CallbackFunctor< TReturn, Types... >::CallbackFunction func)
Helper function to create a new callback functor.
Definition C_callback.hpp:192
static CalleePtr newObjectFunctor(typename fge::CallbackObjectFunctor< TReturn, TObject, Types... >::CallbackFunctionObject func, TObject *object)
Helper function to create a new callback object functor.
Definition C_callback.hpp:220
static CalleePtr newLambda(TLambda const &lambda)
Helper function to create a new callback lambda.
Definition C_callback.hpp:205