Skip to content

Commit

Permalink
working on virtual directories, adding EnableSharedFromThis
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Apr 9, 2024
1 parent 4b9d92f commit 6213f9a
Show file tree
Hide file tree
Showing 15 changed files with 462 additions and 35 deletions.
8 changes: 4 additions & 4 deletions Akel/Runtime/Includes/Core/File.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is a part of Akel
// Authors : @kbz_8
// Created : 18/03/2024
// Updated : 19/03/2024
// Updated : 09/04/2024

#ifndef __AK_CORE_FILE__
#define __AK_CORE_FILE__
Expand All @@ -14,10 +14,10 @@ namespace Ak
class AK_CORE_API File
{
public:
File() = default;
File(const std::filesystem::path& filepaht);
File(const std::filesystem::path& filepath, std::uint32_t flags);

bool Open(const std::filesystem::path& filepath, std::uint32_t flags);
bool Open(std::uint32_t flags);
void Close();

template <typename T>
Expand All @@ -33,8 +33,8 @@ namespace Ak
~File() = default;

private:
std::filesystem::path m_filepath;
std::fstream m_stream;
std::filesystem::path m_filepath;
};
}

Expand Down
4 changes: 3 additions & 1 deletion Akel/Runtime/Includes/Core/Logs.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// This file is a part of Akel
// Authors : @kbz_8
// Created : 31/01/2024
// Updated : 04/03/2024
// Updated : 21/03/2024

#pragma once

#ifndef __AK_CORE_LOGS__
#define __AK_CORE_LOGS__
Expand Down
37 changes: 31 additions & 6 deletions Akel/Runtime/Includes/Core/Memory/SharedPtr.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// This file is a part of Akel
// Authors : @kbz_8
// Created : 04/03/2024
// Updated : 04/03/2024
// Updated : 09/04/2024

#pragma once

#ifndef __AK_CORE_SHARED_PTR__
#define __AK_CORE_SHARED_PTR__
Expand All @@ -10,14 +12,18 @@

namespace Ak
{
template <typename T>
class WeakPtr;

template <typename T>
class SharedPtr
{
public:
explicit SharedPtr(T* ptr = nullptr) noexcept;
SharedPtr(std::nullptr_t) = default;
SharedPtr(std::nullptr_t) noexcept : m_ref(nullptr), m_ptr(nullptr) {}
SharedPtr(const SharedPtr& ptr) noexcept;
SharedPtr(SharedPtr&& ptr) noexcept;
explicit SharedPtr(const WeakPtr<T>& weak) noexcept;

inline SharedPtr operator=(SharedPtr ptr) noexcept;
inline SharedPtr operator=(T* ptr) noexcept;
Expand All @@ -34,12 +40,13 @@ namespace Ak
inline void Swap(const SharedPtr& ptr) noexcept;

~SharedPtr() noexcept;

private:
struct RefCounter
{
T* ptr;
std::uint32_t count;
T* ptr = nullptr;
std::uint32_t count = 0;
std::uint32_t weaks = 0;
};

private:
Expand All @@ -48,7 +55,25 @@ namespace Ak
};

template <typename T, typename ... Args>
inline SharedPtr<T> MakeShared(Args&& ... args) noexcept;
inline std::enable_if_t<!std::is_array<T>::value, SharedPtr<T>> MakeShared(Args&& ... args) noexcept;

template <typename T>
class EnableSharedFromThis
{
public:
EnableSharedFromThis() = default;

inline SharedPtr<T> SharedFromThis();
inline SharedPtr<const T> SharedFromThis() const;

inline WeakPtr<T> WeakFromThis();
inline WeakPtr<const T> WeakFromThis() const;

~EnableSharedFromThis() = default;

private:
mutable WeakPtr<T> m_weak_this;
};
}

#include <Core/Memory/SharedPtr.inl>
Expand Down
46 changes: 43 additions & 3 deletions Akel/Runtime/Includes/Core/Memory/SharedPtr.inl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// This file is a part of Akel
// Authors : @kbz_8
// Created : 04/03/2024
// Updated : 04/03/2024
// Updated : 09/04/2024

#pragma once
#include <Core/Memory/SharedPtr.h>

#include <Core/Memory/MemoryManager.h>
#include <utility>

namespace Ak
{
Expand All @@ -33,6 +32,13 @@ namespace Ak
ptr.m_ref = nullptr;
}

template <typename T>
SharedPtr<T>::SharedPtr(const WeakPtr<T>& weak) noexcept : m_ptr(weak.m_ptr), m_ref(weak.m_ref)
{
if(m_ref)
m_ref->count++;
}

template <typename T>
SharedPtr<T> SharedPtr<T>::operator=(SharedPtr ptr) noexcept
{
Expand Down Expand Up @@ -85,8 +91,11 @@ namespace Ak
m_ptr = nullptr;
if(m_ref)
m_ref->count--;
if(m_ref->count == 0)
if(m_ref->count <= 0 && m_ref->weaks <= 0)
{
MemFree(m_ref->ptr);
m_ref = nullptr;
}
}

template <typename T>
Expand All @@ -101,4 +110,35 @@ namespace Ak
if(m_ptr)
Reset();
}

template <typename T, typename ... Args>
std::enable_if_t<!std::is_array<T>::value, SharedPtr<T>> MakeShared(Args&& ... args) noexcept
{
// TODO : do memory contiguous pointer and ref counter allocations
return SharedPtr<T>(MemAlloc<T>(std::forward<Args>(args)...));
}

template <typename T>
SharedPtr<T> EnableSharedFromThis<T>::SharedFromThis()
{
return SharedPtr<T>(m_weak_this);
}

template <typename T>
SharedPtr<const T> EnableSharedFromThis<T>::SharedFromThis() const
{
return SharedPtr<const T>(m_weak_this);
}

template <typename T>
WeakPtr<T> EnableSharedFromThis<T>::WeakFromThis()
{
return m_weak_this;
}

template <typename T>
WeakPtr<const T> EnableSharedFromThis<T>::WeakFromThis() const
{
return m_weak_this;
}
}
4 changes: 3 additions & 1 deletion Akel/Runtime/Includes/Core/Memory/UniquePtr.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// This file is a part of Akel
// Authors : @kbz_8
// Created : 04/03/2024
// Updated : 04/03/2024
// Updated : 21/03/2024

#pragma once

#ifndef __AK_CORE_UNIQUE_PTR__
#define __AK_CORE_UNIQUE_PTR__
Expand Down
52 changes: 52 additions & 0 deletions Akel/Runtime/Includes/Core/Memory/WeakPtr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// This file is a part of Akel
// Authors : @kbz_8
// Created : 21/03/2024
// Updated : 22/03/2024

#ifndef __AK_CORE_WEAK_PTR__
#define __AK_CORE_WEAK_PTR__

#include <Core/PreCompiled.h>

namespace Ak
{
template <typename T>
class WeakPtr
{
friend SharedPtr<T>;

public:
constexpr WeakPtr() noexcept = default;
WeakPtr(const WeakPtr& rhs) noexcept;
WeakPtr(WeakPtr&& rhs) noexcept;
explicit WeakPtr(const SharedPtr<T>& shared) noexcept;

inline void Reset() noexcept;
inline void Swap(WeakPtr& rhs) noexcept;

inline std::size_t UseCount() const noexcept;
inline bool Expired() const noexcept;
inline SharedPtr<T> Lock() const noexcept;

~WeakPtr();

private:
struct RefCounter
{
T* ptr = nullptr;
std::uint32_t count = 0;
std::uint32_t weaks = 0;
};

private:
inline void DecrementWeakCount() noexcept;

private:
RefCounter* m_ref = nullptr;
T* m_ptr = nullptr;
};
}

#include <Core/Memory/WeakPtr.h>

#endif
83 changes: 83 additions & 0 deletions Akel/Runtime/Includes/Core/Memory/WeakPtr.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// This file is a part of Akel
// Authors : @kbz_8
// Created : 22/03/2024
// Updated : 22/03/2024

#pragma once
#include <Core/Memory/WeakPtr.h>

namespace Ak
{
template <typename T>
WeakPtr<T>::WeakPtr(const WeakPtr<T>& rhs) noexcept : m_ref(rhs.m_ref), m_ptr(rhs.m_ptr)
{
if(m_ref)
m_ref->weaks++;
}

template <typename T>
WeakPtr<T>::WeakPtr(WeakPtr<T>&& rhs) noexcept
{
Swap(rhs);
rhs.Reset();
}

template <typename T>
WeakPtr<T>::WeakPtr(const SharedPtr<T>& shared) noexcept : m_ref(shared.m_ref), m_ptr(shared.m_ptr)
{
if(m_ref)
m_ref->weaks++;
}

template <typename T>
void WeakPtr<T>::Reset() noexcept
{
DecrementWeakCount();
m_ref = nullptr;
m_ptr = nullptr;
}

template <typename T>
void WeakPtr<T>::Swap(WeakPtr<T>& rhs) noexcept
{
std::swap(m_ref, rhs.m_ref);
std::swap(m_ptr, rhs.m_ptr);
}

template <typename T>
std::size_t WeakPtr<T>::UseCount() const noexcept
{
return (m_ref ? m_ref->weaks : 0);
}

template <typename T>
bool WeakPtr<T>::Expired() const noexcept
{
return !m_ref || m_ref->count <= 0;;
}

template <typename T>
SharedPtr<T> WeakPtr<T>::Lock() const noexcept
{
return (Expired() ? SharedPtr<T>(nullptr) : SharedPtr<T>(*this));
}

template <typename T>
void WeakPtr<T>::DecrementWeakCount() noexcept
{
if(m_ref == nullptr)
return;
m_ref->weaks--;
if(m_ref->count <= 0 && m_ref->weaks <= 0)
{
MemFree(m_ref);
m_ref = nullptr;
}
}

template <typename T>
WeakPtr<T>::~WeakPtr<T>()
{
DecrementWeakCount();
}
}
4 changes: 3 additions & 1 deletion Akel/Runtime/Includes/Core/PreCompiled.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is a part of Akel
// Authors : @kbz_8
// Created : 31/01/2024
// Updated : 20/03/2024
// Updated : 23/03/2024

#ifndef __AK_CORE_PRE_COMPILED_HEADER__
#define __AK_CORE_PRE_COMPILED_HEADER__
Expand Down Expand Up @@ -32,6 +32,7 @@
#include <thread>
#include <optional>
#include <unordered_set>
#include <variant>
#include <unordered_map>

#if !__has_include(<filesystem>)
Expand All @@ -49,5 +50,6 @@
#include <Core/Memory/MemoryManager.h>
#include <Core/Memory/UniquePtr.h>
#include <Core/Memory/SharedPtr.h>
#include <Core/Memory/WeakPtr.h>

#endif
Loading

0 comments on commit 6213f9a

Please sign in to comment.