From 47cc49a962f125269942576830fa66ccb1f547e7 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Thu, 14 Apr 2022 22:17:40 +0200 Subject: [PATCH] Add `Delegate::BindUnique` --- Source/Engine/Core/Delegate.h | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/Source/Engine/Core/Delegate.h b/Source/Engine/Core/Delegate.h index bd448165f..57d685031 100644 --- a/Source/Engine/Core/Delegate.h +++ b/Source/Engine/Core/Delegate.h @@ -405,6 +405,59 @@ public: } } + /// + /// Binds a static function (if not binded yet). + /// + template + void BindUnique() + { + FunctionType f; + f.template Bind(); + BindUnique(f); + } + + /// + /// Binds a member function (if not binded yet). + /// + /// The object instance. + template + void BindUnique(T* callee) + { + FunctionType f; + f.template Bind(callee); + BindUnique(f); + } + + /// + /// Binds a function (if not binded yet). + /// + /// The method. + void BindUnique(Signature method) + { + FunctionType f(method); + BindUnique(f); + } + + /// + /// Binds a function (if not binded yet). + /// + /// The function to bind. + void BindUnique(const FunctionType& f) + { + const intptr size = Platform::AtomicRead(&_size); + FunctionType* bindings = (FunctionType*)Platform::AtomicRead(&_ptr); + if (bindings) + { + // Skip if already binded + for (intptr i = 0; i < size; i++) + { + if (Platform::AtomicRead((intptr volatile*)&bindings[i]._callee) == (intptr)f._callee && Platform::AtomicRead((intptr volatile*)&bindings[i]._function) == (intptr)f._function) + return; + } + } + Bind(f); + } + /// /// Unbinds a static function. ///