Skip to content

Commit

Permalink
UnitTests Added tests for TRAP::Utils::Safe 11/24/2024 | 24w47b2
Browse files Browse the repository at this point in the history
  • Loading branch information
GamesTrap committed Nov 24, 2024
1 parent 3e87bf2 commit a91c097
Show file tree
Hide file tree
Showing 3 changed files with 404 additions and 19 deletions.
1 change: 1 addition & 0 deletions SITREPS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5420,3 +5420,4 @@ SITREP 11/23/2024|24w47b1
SITREP 11/24/2024|24w47b2
- Changed TRAP Engine version to 24w47b2(0.11.47)
- UnitTests Added tests for TRAP::Utils::LockFreeQueue ~<15 mins
- UnitTests Added tests for TRAP::Utils::Safe ~<30 mins
26 changes: 7 additions & 19 deletions TRAP/src/Utils/Concurrency/Safe.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,31 @@ namespace TRAP::Utils
};

template<typename LockType>
struct AccessTraits
struct AccessTraits final
{
static constexpr bool IsReadOnly = false;
};

template<typename MutexType>
struct AccessTraits<std::lock_guard<MutexType>>
struct AccessTraits<std::lock_guard<MutexType>> final
{
static constexpr bool IsReadOnly = false;
};

template<typename... MutexTypes>
struct AccessTraits<std::scoped_lock<MutexTypes...>>
struct AccessTraits<std::scoped_lock<MutexTypes...>> final
{
static constexpr bool IsReadOnly = false;
};

template<typename MutexType>
struct AccessTraits<std::unique_lock<MutexType>>
struct AccessTraits<std::unique_lock<MutexType>> final
{
static constexpr bool IsReadOnly = false;
};

template<typename MutexType>
struct AccessTraits<std::shared_lock<MutexType>>
struct AccessTraits<std::shared_lock<MutexType>> final
{
static constexpr bool IsReadOnly = true;
};
Expand All @@ -80,7 +80,7 @@ namespace TRAP::Utils
/// @tparam ValueType The type of the value to protect.
/// @tparam MutexType The type of the mutex.
template<typename ValueType, typename MutexType = INTERNAL::DefaultMutex>
class Safe
class Safe final
{
static_assert(!INTERNAL::SupportsAtomic<ValueType>,
"ValueType support std::atomic, use it instead of Safe!");
Expand Down Expand Up @@ -213,7 +213,7 @@ namespace TRAP::Utils
/// @tparam Mode Determines the access mode of the Access object.
/// Can be either AccessMode::ReadOnly or AccessMode::ReadWrite.
template<template<typename> class LockType, AccessMode Mode>
class Access
class Access final
{
//Make sure AccessMode is ReadOnly if a read-only lock is used.
static_assert(!(AccessTraits<LockType<RemoveRefMutexType>>::IsReadOnly && Mode == AccessMode::ReadWrite),
Expand Down Expand Up @@ -265,18 +265,6 @@ namespace TRAP::Utils
{
}

/// @brief Construct an Access object from another one.
/// @param otherAccess The Acces sobject to construct from.
/// @param otherLockArgs Other arguments needed to construct the lock object.
/// @note OtherLockType must implement release() like std::unique_lock does.
template<template<typename> class OtherLockType, AccessMode OtherMode, typename... OtherLockArgs>
explicit Access(Access<OtherLockType, OtherMode>& otherAccess, OtherLockArgs&&... otherLockArgs)
: Access(*otherAccess, *otherAccess.lock().release(), std::adopt_lock, std::forward<OtherLockArgs>(otherLockArgs)...)
{
static_assert(OtherMode == AccessMode::ReadWrite || OtherMode == Mode,
"Cannot construct a ReadWrite Access object from a ReadOnly one!");
}

/// @brief Const accessor to the value.
/// @return Const pointer to the protected value.
[[nodiscard]] constexpr ConstPointerType operator->() const noexcept
Expand Down
Loading

0 comments on commit a91c097

Please sign in to comment.