FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_callback.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_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... Types>
37{
38public:
39 virtual ~CallbackBase() = default;
40
41 virtual void call(Types... args) = 0;
42 virtual bool check(void* ptr) = 0;
43};
44
52template<class... Types>
53class CallbackFunctor : public fge::CallbackBase<Types...>
54{
55public:
56 using CallbackFunction = void (*)(Types... args);
57
63 explicit CallbackFunctor(fge::CallbackFunctor<Types...>::CallbackFunction func);
64 ~CallbackFunctor() override = default;
65
71 void call(Types... args) override;
78 inline bool check(void* ptr) override;
79
80protected:
81 fge::CallbackFunctor<Types...>::CallbackFunction g_function;
82};
83
91template<class... Types>
92class CallbackLambda : public fge::CallbackBase<Types...>
93{
94public:
101 template<typename TLambda>
102 explicit CallbackLambda(TLambda const& lambda);
103 ~CallbackLambda() override;
104
110 void call(Types... args) override;
117 inline bool check(void* ptr) override;
118
119protected:
120 void* g_lambda;
121 void (*g_executeLambda)(void*, Types...);
122 void (*g_deleteLambda)(void*);
123};
124
133template<class TObject, class... Types>
135{
136public:
137 using CallbackFunctionObject = void (TObject::*)(Types... args);
138
145 CallbackObjectFunctor(fge::CallbackObjectFunctor<TObject, Types...>::CallbackFunctionObject func, TObject* object);
146 ~CallbackObjectFunctor() override = default;
147
153 void call(Types... args) override;
160 inline bool check(void* ptr) override;
161
162protected:
163 fge::CallbackObjectFunctor<TObject, Types...>::CallbackFunctionObject g_functionObj;
164 TObject* g_object;
165};
166
167template<class... Types>
168using CalleeUniquePtr = std::unique_ptr<fge::CallbackBase<Types...>>;
169template<class... Types>
170using CalleeSharedPtr = std::shared_ptr<fge::CallbackBase<Types...>>;
171
187template<class... Types>
189{
190public:
191 using CalleePtr = CalleeUniquePtr<Types...>;
192
193 CallbackHandler() = default;
194 ~CallbackHandler() override = default;
195
200 fge::Subscription() {};
205
210 {
211 return *this;
212 };
217
221 inline void clear();
222
238 inline fge::CallbackBase<Types...>* add(CalleePtr&& callback, fge::Subscriber* subscriber = nullptr);
239
249 inline fge::CallbackFunctor<Types...>* addFunctor(typename fge::CallbackFunctor<Types...>::CallbackFunction func,
250 fge::Subscriber* subscriber = nullptr);
261 template<typename TLambda>
262 inline fge::CallbackLambda<Types...>* addLambda(TLambda const& lambda, fge::Subscriber* subscriber = nullptr);
274 template<class TObject>
275 inline fge::CallbackObjectFunctor<TObject, Types...>*
276 addObjectFunctor(typename fge::CallbackObjectFunctor<TObject, Types...>::CallbackFunctionObject func,
277 TObject* object,
278 Subscriber* subscriber = nullptr);
279
287 void delPtr(void* ptr);
295 void delSub(fge::Subscriber* subscriber);
303 void del(fge::CallbackBase<Types...>* callback);
304
310 void call(Types... args);
311
323 void hook(fge::CallbackHandler<Types...>& handler, fge::Subscriber* subscriber = nullptr);
324
325protected:
333 void onDetach(fge::Subscriber* subscriber) override;
334
335private:
336 struct CalleeData
337 {
338 inline CalleeData(CalleePtr&& f, fge::Subscriber* subscriber) :
339 _f(std::move(f)),
340 _subscriber(subscriber)
341 {}
342
343 CalleePtr _f;
344 fge::Subscriber* _subscriber = nullptr;
345 };
346 using CalleeList = std::vector<CalleeData>;
347
348 CalleeList g_callees;
349
350 mutable std::recursive_mutex g_mutex;
351};
352
361template<class TCalleePtr, class... Types>
363{
364 using CalleePtr = TCalleePtr;
365
372 [[nodiscard]] inline static CalleePtr newFunctor(typename fge::CallbackFunctor<Types...>::CallbackFunction func)
373 {
374 return CalleePtr{new fge::CallbackFunctor<Types...>(func)};
375 }
376
384 template<typename TLambda>
385 [[nodiscard]] inline static CalleePtr newLambda(TLambda const& lambda)
386 {
387 return CalleePtr{new fge::CallbackLambda<Types...>(lambda)};
388 }
389
398 template<class TObject>
399 [[nodiscard]] inline static CalleePtr
400 newObjectFunctor(typename fge::CallbackObjectFunctor<TObject, Types...>::CallbackFunctionObject func,
401 TObject* object)
402 {
403 return CalleePtr{new fge::CallbackObjectFunctor<TObject, Types...>(func, object)};
404 }
405};
406
407} // namespace fge
408
409#include "FastEngine/C_callback.inl"
410
411#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
void call(Types... args) override
Call the callback function with the given arguments.
Definition C_callback.inl:28
CallbackFunctor(fge::CallbackFunctor< Types... >::CallbackFunction func)
Constructor.
Definition C_callback.inl:23
This class is used to handle callbacks in a safe way.
Definition C_callback.hpp:189
void delPtr(void *ptr)
Remove a callback from the list.
Definition C_callback.inl:159
fge::CallbackHandler< Types... > & operator=(fge::CallbackHandler< Types... > &&n)=delete
Move operator prohibited.
fge::CallbackFunctor< Types... > * addFunctor(typename fge::CallbackFunctor< Types... >::CallbackFunction func, fge::Subscriber *subscriber=nullptr)
Helper method to add a callback functor.
Definition C_callback.inl:133
CallbackHandler(fge::CallbackHandler< Types... > const &n)
Copy constructor that does nothing.
Definition C_callback.hpp:199
void del(fge::CallbackBase< Types... > *callback)
Remove a callback from the list.
Definition C_callback.inl:199
fge::CallbackBase< Types... > * add(CalleePtr &&callback, fge::Subscriber *subscriber=nullptr)
Add a new callback to the list.
Definition C_callback.inl:110
fge::CallbackHandler< Types... > & operator=(fge::CallbackHandler< Types... > const &n)
Copy operator that does nothing.
Definition C_callback.hpp:209
void onDetach(fge::Subscriber *subscriber) override
This method is called when a subscriber is destroyed (destructor called)
Definition C_callback.inl:261
void call(Types... args)
Call all the callbacks with the given arguments.
Definition C_callback.inl:218
void hook(fge::CallbackHandler< Types... > &handler, fge::Subscriber *subscriber=nullptr)
Hook a callback handler to this handler.
Definition C_callback.inl:247
void delSub(fge::Subscriber *subscriber)
Remove a callback from the list.
Definition C_callback.inl:177
CallbackHandler(fge::CallbackHandler< Types... > &&n)=delete
Move constructor prohibited.
void clear()
Clear the list of callbacks.
Definition C_callback.inl:99
fge::CallbackObjectFunctor< TObject, Types... > * addObjectFunctor(typename fge::CallbackObjectFunctor< TObject, Types... >::CallbackFunctionObject func, TObject *object, Subscriber *subscriber=nullptr)
Helper method to add a callback object functor.
Definition C_callback.inl:149
fge::CallbackLambda< Types... > * addLambda(TLambda const &lambda, fge::Subscriber *subscriber=nullptr)
Helper method to add a callback lambda.
Definition C_callback.inl:141
Callback lambda (with/without capture)
Definition C_callback.hpp:93
bool check(void *ptr) override
Always return false.
Definition C_callback.inl:91
void call(Types... args) override
Call the callback function with the given arguments.
Definition C_callback.inl:86
CallbackLambda(TLambda const &lambda)
Constructor.
Definition C_callback.inl:64
Callback functor of a method with an object.
Definition C_callback.hpp:135
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:55
void call(Types... args) override
Call the callback method with the given arguments.
Definition C_callback.inl:49
CallbackObjectFunctor(fge::CallbackObjectFunctor< TObject, Types... >::CallbackFunctionObject func, TObject *object)
Constructor.
Definition C_callback.inl:41
This class is a useful utility to "link" multiple objects around.
Definition C_subscription.hpp:150
This class is a useful utility to "link" multiple objects around a specific use with automatic lifeti...
Definition C_subscription.hpp:48
This struct helper is used to create callbacks.
Definition C_callback.hpp:363
static CalleePtr newLambda(TLambda const &lambda)
Helper function to create a new callback lambda.
Definition C_callback.hpp:385
static CalleePtr newFunctor(typename fge::CallbackFunctor< Types... >::CallbackFunction func)
Helper function to create a new callback functor.
Definition C_callback.hpp:372
static CalleePtr newObjectFunctor(typename fge::CallbackObjectFunctor< TObject, Types... >::CallbackFunctionObject func, TObject *object)
Helper function to create a new callback object functor.
Definition C_callback.hpp:400