Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>

DEFINE_LOG_CHANNEL(Temp, All)

static constexpr const FAnsiChar* LoggerPattern = "%^[%T] [%t] [%-23!n] %8l:%$ %v";

void FLogManager::Initialize()
Expand Down
144 changes: 144 additions & 0 deletions Engine/Source/Runtime/Core/Public/Core/Delegates/Delegate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// RavenStorm Copyright @ 2025-2025

#pragma once

#include <functional>
#include <type_traits>

#include "Core/CoreDefinitions.hpp"
#include "Core/Memory/SmartPointers.hpp"

template <typename TReturnType, typename... TParameters>
class TDelegate
{
public:
using FFunctionType = std::function<TReturnType(TParameters...)>;

public:
TDelegate() = default;
~TDelegate() = default;

DEFAULT_COPY_MOVEABLE(TDelegate)

public:
[[nodiscard]] bool8 IsBound() const noexcept
{
return Function != nullptr;
}

void Bind(FFunctionType&& BoundFunction)
{
Function = std::move(BoundFunction);
}

template <typename TOwner>
void BindRaw(TOwner* Instance, TReturnType (TOwner::*OwnerFunction)(TParameters...))
{
Function = [Instance, OwnerFunction](TParameters... Parameters) -> TReturnType
{
return (Instance->*OwnerFunction)(std::forward<TParameters>(Parameters)...);
};
}

template <typename TOwner>
void BindRaw(TOwner* Owner, TReturnType (TOwner::*OwnerFunction)(TParameters...) const)
{
Function = [Owner, OwnerFunction](TParameters... Parameters) -> TReturnType
{
return (Owner->*OwnerFunction)(std::forward<TParameters>(Parameters)...);
};
}

template <typename TOwner>
void BindShared(TSharedPtr<TOwner> SharedOwner, TReturnType (TOwner::*MemberFunction)(TParameters...))
{
Function = [SharedOwner, MemberFunction](TParameters... Parameters) -> TReturnType
{
return (SharedOwner.get()->*MemberFunction)(std::forward<TParameters>(Parameters)...);
};
}

template <typename TOwner>
void BindShared(const TSharedPtr<TOwner> SharedOwner, TReturnType (TOwner::*MemberFunction)(TParameters...) const)
{
Function = [SharedOwner, MemberFunction](TParameters... Parameters) -> TReturnType
{
return (SharedOwner.get()->*MemberFunction)(std::forward<TParameters>(Parameters)...);
};
}

template <typename TOwner>
void BindWeak(TWeakPtr<TOwner> WeakOwner, TReturnType (TOwner::*MemberFunction)(TParameters...))
{
Function = [WeakOwner, MemberFunction](TParameters... Parameters) -> TReturnType
{
if (auto Owner = WeakOwner.lock())
{
return (Owner.get()->*MemberFunction)(std::forward<TParameters>(Parameters)...);
}
if constexpr (!std::is_same_v<TReturnType, void>)
{
return TReturnType{};
}
else
{
return;
}
};
}

template <typename TOwner>
void BindWeak(const TWeakPtr<TOwner> WeakOwner, TReturnType (TOwner::*MemberFunction)(TParameters...) const)
{
Function = [WeakOwner, MemberFunction](TParameters... Parameters) -> TReturnType
{
if (auto Owner = WeakOwner.lock())
{
return (Owner.get()->*MemberFunction)(std::forward<TParameters>(Parameters)...);
}
if constexpr (!std::is_same_v<TReturnType, void>)
{
return TReturnType{};
}
else
{
return;
}
};
}

void Unbind()
{
Function = nullptr;
}

template <typename... TOtherParameters>
requires (sizeof...(TOtherParameters) == sizeof...(TParameters) && (std::convertible_to<TOtherParameters&&, TParameters> && ...))
TReturnType Execute(TOtherParameters&&... Parameters)
{
if constexpr (std::is_same_v<TReturnType, void>)
{
Function.operator()(std::forward<TOtherParameters>(Parameters)...);
return;
}
return Function.operator()(std::forward<TOtherParameters>(Parameters)...);
}

template <typename... TOtherParameters>
requires (sizeof...(TOtherParameters) == sizeof...(TParameters) && (std::convertible_to<TOtherParameters&&, TParameters> && ...))
TReturnType ExecuteIfBound(TOtherParameters&&... Parameters) requires std::is_same_v<void, TReturnType> || std::is_default_constructible_v<TReturnType>
{
if (!IsBound())
{
if constexpr (std::is_same_v<TReturnType, void>)
{
return;
}
return TReturnType();
}
return Execute(std::forward<TOtherParameters>(Parameters)...);
}

private:
FFunctionType Function = nullptr;
};
179 changes: 179 additions & 0 deletions Engine/Source/Runtime/Core/Public/Core/Delegates/MulticastDelegate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// RavenStorm Copyright @ 2025-2025

#pragma once

#include <functional>
#include <ranges>
#include <type_traits>

#include "Core/CoreDefinitions.hpp"
#include "Core/Containers/Map.hpp"
#include "Core/Logging/LogManager.hpp"
#include "Core/Memory/SmartPointers.hpp"

#define CV_INVALID_DELEGATE_HANDLE FORWARD(0)

struct FDelegateHandle
{
public:
constexpr FDelegateHandle() = default;

constexpr FDelegateHandle(const uint64 InId)
: Id(InId)
{
}

constexpr ~FDelegateHandle() = default;

DEFAULT_COPY_MOVEABLE_PREFIX(FDelegateHandle, constexpr)

public:
[[nodiscard]] constexpr bool8 IsValid() const
{
return Id != CV_INVALID_DELEGATE_HANDLE;
}

public:
[[nodiscard]] constexpr uint64 GetId() const { return Id; }

public:
constexpr operator uint64() const
{
return GetId();
}

constexpr operator bool8() const
{
return IsValid();
}

private:
uint64 Id = CV_INVALID_DELEGATE_HANDLE;
};

template <typename... TParameters>
class TMulticastDelegate
{
public:
using TFuncType = std::function<void(TParameters...)>;

public:
TMulticastDelegate() = default;
~TMulticastDelegate() = default;

NON_COPY_MOVEABLE(TMulticastDelegate)

public:
[[nodiscard]] bool8 IsBound() const noexcept
{
return !Functions.IsEmpty();
}

[[nodiscard]] FDelegateHandle Bind(TFuncType&& Function)
{
const FDelegateHandle Handle(GetNextHandleId());
Functions.Emplace(Handle.GetId(), std::move(Function));
return Handle;
}

template <typename TOwner>
[[nodiscard]] FDelegateHandle BindRaw(TOwner* Owner, void (TOwner::*Function)(TParameters...))
{
return Bind([Owner, Function](TParameters... Args)
{
(Owner->*Function)(std::forward<TParameters>(Args)...);
});
}

template <typename TOwner>
[[nodiscard]] FDelegateHandle BindRaw(const TOwner* Owner, void (TOwner::*Function)(TParameters...) const)
{
return Bind([Owner, Function](TParameters... Args)
{
(Owner->*Function)(std::forward<TParameters>(Args)...);
});
}

template <typename TOwner>
[[nodiscard]] FDelegateHandle BindShared(TSharedPtr<TOwner> Owner, void (TOwner::*MemberFunction)(TParameters...))
{
return Bind([Owner, MemberFunction](TParameters... Parameters)
{
(Owner.get()->*MemberFunction)(std::forward<TParameters>(Parameters)...);
});
}

template <typename TOwner>
[[nodiscard]] FDelegateHandle BindShared(const TSharedPtr<TOwner> Owner, void (TOwner::*MemberFunction)(TParameters...) const)
{
return Bind([Owner, MemberFunction](TParameters... Parameters)
{
(Owner.get()->*MemberFunction)(std::forward<TParameters>(Parameters)...);
});
}

template <typename TOwner>
[[nodiscard]] FDelegateHandle BindWeak(TWeakPtr<TOwner> WeakOwner, void (TOwner::*MemberFunction)(TParameters...))
{
return Bind([WeakOwner, MemberFunction](TParameters... Parameters)
{
if (auto Owner = WeakOwner.lock())
{
(Owner.get()->*MemberFunction)(std::forward<TParameters>(Parameters)...);
}
});
}

template <typename TOwner>
[[nodiscard]] FDelegateHandle BindWeak(const TWeakPtr<TOwner> WeakOwner, void (TOwner::*MemberFunction)(TParameters...) const)
{
return Bind([WeakOwner, MemberFunction](TParameters... Parameters)
{
if (auto Owner = WeakOwner.lock())
{
(Owner.get()->*MemberFunction)(std::forward<TParameters>(Parameters)...);
}
});
}

bool8 Unbind(const FDelegateHandle& Handle)
{
if (!Functions.Contains(Handle.GetId()))
{
CVLOG(LogTemp, Warning, "There is no handle with id '{}' registered on this delegate", Handle.GetId());
return false;
}
Functions.Remove(Handle.GetId());
return true;
}

void Clear()
{
Functions.Clear();
}

template <typename... TOtherParameters>
requires (sizeof...(TOtherParameters) == sizeof...(TParameters) && (std::convertible_to<TOtherParameters&&, TParameters> && ...))
void Broadcast(TOtherParameters&&... Parameters)
{
for (TFuncType& Function : Functions | std::views::values)
{
Function.operator()(std::forward<TOtherParameters>(Parameters)...);
}
}

private:
[[nodiscard]] uint64 GetNextHandleId()
{
++NextHandleId;
if (NextHandleId == CV_INVALID_DELEGATE_HANDLE)
{
++NextHandleId;
}
return NextHandleId;
}

private:
TMap<uint64, TFuncType> Functions;
uint64 NextHandleId = 0;
};
Loading