Skip to content

Commit

Permalink
Fixed compiler errors 11/27/2024 | 24w48a1
Browse files Browse the repository at this point in the history
  • Loading branch information
GamesTrap committed Nov 26, 2024
1 parent 0cae71f commit 2c60051
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 47 deletions.
17 changes: 16 additions & 1 deletion TRAP/src/Utils/Concurrency/Safe.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ namespace TRAP::Utils

template<typename T>
concept SupportsAtomic = std::integral<T> || std::same_as<T, bool>;

#ifdef TRACY_ENABLE
template<typename Mutex>
concept IsMutexMarkable = requires(Mutex mtx, const tracy::SourceLocationData* srcLoc)
{
{ mtx.Mark(srcLoc) };
};
#else
template<typename Mutex>
concept IsMutexMarkable = false;
#endif
}

enum class AccessMode : u8
Expand Down Expand Up @@ -236,7 +247,11 @@ namespace TRAP::Utils
: Lock(mutex, std::forward<OtherLockArgs>(otherLockArgs)...), m_value(value)
{
ZoneScoped;
LockMark(mutex);

if constexpr(INTERNAL::IsMutexMarkable<MutexType>)
{
LockMark(mutex);
}
}

/// @brief Construct a read-only Access object from a const Safe object and any
Expand Down
107 changes: 61 additions & 46 deletions UnitTests/src/Utils/Concurrency/Safe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

#include "Utils/Concurrency/Safe.h"

namespace
{
using Mutex = TRAP::Utils::ContentionFreeSharedMutex<>;

template<typename Mtx>
using ReadLock = std::shared_lock<Mtx>;
template<typename Mtx>
using WriteLock = std::unique_lock<Mtx>;

template<typename T>
using ReadAccess = TRAP::Utils::Safe<T, Mutex>::template ReadAccess<ReadLock>;
template<typename T>
using WriteAccess = TRAP::Utils::Safe<T, Mutex>::template WriteAccess<WriteLock>;
}

TEST_CASE("TRAP::Utils::AccessTraits", "[utils][concurrency][accesstraits]")
{
STATIC_REQUIRE_FALSE(TRAP::Utils::AccessTraits<i32>::IsReadOnly);
Expand Down Expand Up @@ -162,12 +177,12 @@ TEST_CASE("TRAP::Utils::Safe::ReadAccess", "[utils][concurrency][safe][readacces

SECTION("Constructors")
{
TRAP::Utils::INTERNAL::DefaultMutex mtx{};
Mutex mtx{};

//Value, mutex constructor
{
const std::string str = "Hello World!";
TRAP::Utils::Safe<std::string>::ReadAccess<> access{str, mtx};
ReadAccess<std::string> access{str, mtx};
}

//Safe constructor
Expand All @@ -179,102 +194,102 @@ TEST_CASE("TRAP::Utils::Safe::ReadAccess", "[utils][concurrency][safe][readacces
//Copy constructor
// {
// const std::string str = "Hello World!";
// TRAP::Utils::Safe<std::string>::ReadAccess<> access{str, mtx};
// ReadAccess<std::string> access{str, mtx};

// const TRAP::Utils::Safe<std::string>::ReadAccess<> accessCopy(access); //Not allowed
// const ReadAccess<std::string> accessCopy(access); //Not allowed
// }

//Copy constructor (read->write access)
// {
// std::string str = "Hello World!";
// TRAP::Utils::Safe<std::string>::ReadAccess<> access{str, mtx};
// ReadAccess<std::string> access{str, mtx};

// const TRAP::Utils::Safe<std::string>::WriteAccess<> accessCopy(access); //Not allowed
// const WriteAccess<std::string> accessCopy(access); //Not allowed
// }

//Move constructor
{
const std::string str = "Hello World!";
TRAP::Utils::Safe<std::string>::ReadAccess<> access(str, mtx);
ReadAccess<std::string> access(str, mtx);

const TRAP::Utils::Safe<std::string>::ReadAccess<> accessMove(std::move(access));
const ReadAccess<std::string> accessMove(std::move(access));
}

//Move constructor (read->write access)
// {
// std::string str = "Hello World!";
// TRAP::Utils::Safe<std::string>::ReadAccess<> access(str, mtx);
// ReadAccess<std::string> access(str, mtx);

// const TRAP::Utils::Safe<std::string>::WriteAccess<> accessMove(std::move(access)); //Not allowed
// const WriteAccess<std::string> accessMove(std::move(access)); //Not allowed
// }
}

SECTION("Assignments")
{
TRAP::Utils::INTERNAL::DefaultMutex mtx{};
Mutex mtx{};

//Copy assignment operator
// {
// const std::string str = "Hello World!";
// TRAP::Utils::Safe<std::string>::ReadAccess<> access{str, mtx};
// ReadAccess<std::string> access{str, mtx};

// const TRAP::Utils::Safe<std::string>::ReadAccess<> accessCopy = access; //Deleted
// const ReadAccess<std::string> accessCopy = access; //Deleted
// }

//Copy assignment operator (read->write access)
// {
// const std::string str = "Hello World!";
// TRAP::Utils::Safe<std::string>::ReadAccess<> access{str, mtx};
// ReadAccess<std::string> access{str, mtx};

// const TRAP::Utils::Safe<std::string>::WriteAccess<> accessCopy = access; //Deleted
// const WriteAccess<std::string> accessCopy = access; //Deleted
// }

//Move assignment operator
{
const std::string str = "Hello World!";
TRAP::Utils::Safe<std::string>::ReadAccess<> access{str, mtx};
ReadAccess<std::string> access{str, mtx};

const TRAP::Utils::Safe<std::string>::ReadAccess<> accessMove = std::move(access);
const ReadAccess<std::string> accessMove = std::move(access);
}

//Move assignment operator (read->write access)
// {
// const std::string str = "Hello World!";
// TRAP::Utils::Safe<std::string>::ReadAccess<> access{str, mtx};
// ReadAccess<std::string> access{str, mtx};

// const TRAP::Utils::Safe<std::string>::WriteAccess<> accessMove = std::move(access);
// const WriteAccess<std::string> accessMove = std::move(access);
// }
}

SECTION("operator->()")
{
TRAP::Utils::INTERNAL::DefaultMutex mtx{};
Mutex mtx{};
const std::string str = "Hello World!";

{
const TRAP::Utils::Safe<std::string>::ReadAccess<> access{str, mtx};
const ReadAccess<std::string> access{str, mtx};

REQUIRE_FALSE(access->empty());
}
{
TRAP::Utils::Safe<std::string>::ReadAccess<> access{str, mtx};
ReadAccess<std::string> access{str, mtx};

REQUIRE_FALSE(access->empty());
}
}

SECTION("operator*()")
{
TRAP::Utils::INTERNAL::DefaultMutex mtx{};
Mutex mtx{};
const std::string str = "Hello World!";

{
const TRAP::Utils::Safe<std::string>::ReadAccess<> access{str, mtx};
const ReadAccess<std::string> access{str, mtx};

REQUIRE_FALSE((*access).empty());
}
{
TRAP::Utils::Safe<std::string>::ReadAccess<> access{str, mtx};
ReadAccess<std::string> access{str, mtx};

REQUIRE_FALSE((*access).empty());
}
Expand All @@ -292,12 +307,12 @@ TEST_CASE("TRAP::Utils::Safe::WriteAccess", "[utils][concurrency][safe][writeacc

SECTION("Constructors")
{
TRAP::Utils::INTERNAL::DefaultMutex mtx{};
Mutex mtx{};

//Value, mutex constructor
{
std::string str = "Hello World!";
TRAP::Utils::Safe<std::string>::WriteAccess<> access{str, mtx};
WriteAccess<std::string> access{str, mtx};
}

//Safe constructor
Expand All @@ -309,86 +324,86 @@ TEST_CASE("TRAP::Utils::Safe::WriteAccess", "[utils][concurrency][safe][writeacc
//Copy constructor
// {
// std::string str = "Hello World!";
// TRAP::Utils::Safe<std::string>::WriteAccess<> access{str, mtx};
// WriteAccess<std::string> access{str, mtx};

// const TRAP::Utils::Safe<std::string>::WriteAccess<> accessCopy(access); //Not allowed
// const WriteAccess<std::string> accessCopy(access); //Not allowed
// }

//Copy constructor (write->read access)
// {
// std::string str = "Hello World!";
// TRAP::Utils::Safe<std::string>::WriteAccess<> access{str, mtx};
// WriteAccess<std::string> access{str, mtx};

// const TRAP::Utils::Safe<std::string>::ReadAccess<> accessCopy(access); //Not allowed
// const ReadAccess<std::string> accessCopy(access); //Not allowed
// }

//Move constructor
{
std::string str = "Hello World!";
TRAP::Utils::Safe<std::string>::WriteAccess<> access(str, mtx);
WriteAccess<std::string> access(str, mtx);

const TRAP::Utils::Safe<std::string>::WriteAccess<> accessMove(std::move(access));
const WriteAccess<std::string> accessMove(std::move(access));
}

//Move constructor (write->read access)
// {
// std::string str = "Hello World!";
// TRAP::Utils::Safe<std::string>::WriteAccess<> access(str, mtx);
// WriteAccess<std::string> access(str, mtx);

// const TRAP::Utils::Safe<std::string>::ReadAccess<> accessMove(std::move(access)); //Not allowed
// const ReadAccess<std::string> accessMove(std::move(access)); //Not allowed
// }
}

SECTION("Assignments")
{
TRAP::Utils::INTERNAL::DefaultMutex mtx{};
Mutex mtx{};

//Copy assignment operator (write->read access)
// {
// std::string str = "Hello World!";
// TRAP::Utils::Safe<std::string>::WriteAccess<> access{str, mtx};
// WriteAccess<std::string> access{str, mtx};

// const TRAP::Utils::Safe<std::string>::ReadAccess<> accessCopy = access; //Deleted
// const ReadAccess<std::string> accessCopy = access; //Deleted
// }

//Move assignment operator (write->read access)
// {
// std::string str = "Hello World!";
// TRAP::Utils::Safe<std::string>::WriteAccess<> access{str, mtx};
// WriteAccess<std::string> access{str, mtx};

// const TRAP::Utils::Safe<std::string>::ReadAccess<> accessMove = std::move(access);
// const ReadAccess<std::string> accessMove = std::move(access);
// }
}

SECTION("operator->()")
{
TRAP::Utils::INTERNAL::DefaultMutex mtx{};
Mutex mtx{};
std::string str = "Hello World!";

{
const TRAP::Utils::Safe<std::string>::WriteAccess<> access{str, mtx};
const WriteAccess<std::string> access{str, mtx};

REQUIRE_FALSE(access->empty());
}
{
TRAP::Utils::Safe<std::string>::WriteAccess<> access{str, mtx};
WriteAccess<std::string> access{str, mtx};

REQUIRE_FALSE(access->empty());
}
}

SECTION("operator*()")
{
TRAP::Utils::INTERNAL::DefaultMutex mtx{};
Mutex mtx{};
std::string str = "Hello World!";

{
const TRAP::Utils::Safe<std::string>::WriteAccess<> access{str, mtx};
const WriteAccess<std::string> access{str, mtx};

REQUIRE_FALSE((*access).empty());
}
{
TRAP::Utils::Safe<std::string>::WriteAccess<> access{str, mtx};
WriteAccess<std::string> access{str, mtx};

REQUIRE_FALSE((*access).empty());
}
Expand Down

0 comments on commit 2c60051

Please sign in to comment.