-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working on virtual directories, adding EnableSharedFromThis
- Loading branch information
Showing
15 changed files
with
462 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.