adding unstd base

This commit is contained in:
Kbz-8
2024-01-20 13:17:11 +01:00
parent a2b0a4f7e7
commit c82548df98
12 changed files with 664 additions and 16 deletions

77
includes/unstd/array.h Normal file
View File

@@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* array.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 09:31:30 by maldavid #+# #+# */
/* Updated: 2024/01/20 09:40:29 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __UNSTD_ARRAY__
#define __UNSTD_ARRAY__
#include <cstddef>
#include <cassert>
namespace unstd
{
template <typename T, std::size_t N>
class TArray
{
public:
TArray() {}
TArray(const TArray<T, N>& rhs)
{
for(std::size_t i = 0; i < N; i++)
_data[i] = rhs._data[i];
}
inline TArray& operator=(const TArray<T, N>& rhs)
{
for(std::size_t i = 0; i < N; i++)
_data[i] = rhs._data[i];
return *this;
}
inline bool empty() const
{
return N == 0;
}
inline std::size_t size() const
{
return N;
}
inline std::size_t max_size() const
{
return N;
}
inline T* data()
{
return _data;
}
inline T& at(std::size_t pos)
{
assert(pos < N && "TArray assertion failed : index out of range");
return _data[pos];
}
inline T& operator[](std::size_t pos)
{
return _data[pos];
}
~TArray() {}
private:
T _data[N];
};
}
#endif

View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ref_counter.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 12:54:09 by maldavid #+# #+# */
/* Updated: 2024/01/20 12:55:02 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __UNSTD_BITS_REFERENCE_COUNTER__
#define __UNSTD_BITS_REFERENCE_COUNTER__
namespace unstd
{
namespace bits
{
struct RefCount
{
int shared;
int weak;
RefCount(int s, int w) : shared(s), weak(w) {}
};
}
}
#endif

View File

@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* shared_ptr.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 10:09:02 by maldavid #+# #+# */
/* Updated: 2024/01/20 13:08:04 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __UNSTD_SHARED_PTR__
#define __UNSTD_SHARED_PTR__
#include <unstd/bits/ref_counter.h>
#include <cstddef>
#include <map>
namespace unstd
{
template <typename T>
class SharedPtr
{
template <typename WT>
friend class WeakPtr;
public:
explicit SharedPtr(T* ptr = NULL);
SharedPtr(const SharedPtr& rhs);
SharedPtr& operator=(const SharedPtr& rhs);
operator bool() const;
bool operator==(const SharedPtr& rhs);
bool operator==(T* ptr);
T& operator*();
T* operator->();
T* get();
void swap(SharedPtr& rhs);
void reset(T* ptr = NULL);
~SharedPtr();
private:
void safeRelease();
private:
static std::map<void*, bits::RefCount*> _refs;
T* _ptr;
bits::RefCount* _ref;
};
}
#include <unstd/shared_ptr.ipp>
#endif

View File

@@ -0,0 +1,136 @@
/* **************************************************************************** */
/* */
/* ::: :::::::: */
/* shared_ptr.ipp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 10:17:18 by maldavid #+# #+# */
/* Updated: 2024/01/20 10:17:18 by maldavid ### ########.fr */
/* */
/* **************************************************************************** */
namespace unstd
{
template <typename T>
SharedPtr<T>::SharedPtr(T* ptr) : _ptr(ptr)
{
typename std::map<void*, bits::RefCount*>::iterator it = _refs.find(static_cast<void*>(ptr));
if(it == _refs.end())
{
_ref = new bits::RefCount(1, 0);
_refs[static_cast<void*>(ptr)] = _ref;
}
else
{
_ref = it->second;
_ref->shared++;
}
}
template <typename T>
SharedPtr<T>::SharedPtr(const SharedPtr<T>& rhs) : _ptr(rhs._ptr), _ref(rhs._ref)
{
if(_ptr && _ref)
++_ref->shared;
}
template <typename T>
SharedPtr<T>& SharedPtr<T>::operator=(const SharedPtr<T>& rhs)
{
if(_ptr == rhs._ptr)
return *this;
safeRelease();
_ptr = rhs._ptr;
_ref = rhs._ref;
_ref->shared++;
return *this;
}
template <typename T>
SharedPtr<T>::operator bool() const
{
return _ptr != NULL;
}
template <typename T>
bool SharedPtr<T>::operator==(const SharedPtr<T>& rhs)
{
return _ptr == rhs._ptr;
}
template <typename T>
bool SharedPtr<T>::operator==(T* ptr)
{
return _ptr == ptr;
}
template <typename T>
T& SharedPtr<T>::operator*()
{
return *_ptr;
}
template <typename T>
T* SharedPtr<T>::operator->()
{
return _ptr;
}
template <typename T>
T* SharedPtr<T>::get()
{
return _ptr;
}
template <typename T>
void SharedPtr<T>::swap(SharedPtr<T>& rhs)
{
T* tmptr = _ptr;
bits::RefCount* tmpref = _ref;
_ptr = rhs._ptr;
_ref = rhs._ref;
rhs._ptr = tmptr;
rhs._ref = tmpref;
}
template <typename T>
void SharedPtr<T>::reset(T* ptr)
{
safeRelease();
_ptr = ptr;
typename std::map<void*, bits::RefCount*>::iterator it = _refs.find(static_cast<void*>(ptr));
if(it == _refs.end())
{
_ref = new bits::RefCount(1, 0);
_refs[static_cast<void*>(ptr)] = _ref;
}
else
{
_ref = it->second;
_ref->shared++;
}
}
template <typename T>
void SharedPtr<T>::safeRelease()
{
if(_ref == NULL)
return;
_ref->shared--;
if(_ref->shared <= 0)
{
delete _ptr;
_ptr = NULL;
delete _ref;
_ref = NULL;
}
}
template <typename T>
SharedPtr<T>::~SharedPtr()
{
safeRelease();
}
}

View File

@@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* unique_ptr.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 09:40:56 by maldavid #+# #+# */
/* Updated: 2024/01/20 10:06:54 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __UNSTD_UNIQUE_PTR__
#define __UNSTD_UNIQUE_PTR__
#include <cstddef>
namespace unstd
{
template <typename T>
class UniquePtr
{
public:
UniquePtr();
UniquePtr(UniquePtr& ptr);
explicit UniquePtr(T* ptr);
inline T* get();
inline void swap(const UniquePtr<T>& ptr);
inline void reset(T* ptr = NULL);
inline T* release();
inline operator bool() const;
inline UniquePtr& operator=(T* ptr);
inline T* operator->() const;
inline T& operator*() const;
~UniquePtr();
private:
UniquePtr(const UniquePtr& ptr);
inline UniquePtr& operator=(const UniquePtr& ptr);
private:
T* _ptr;
};
}
#include <unstd/unique_ptr.ipp>
#endif

View File

@@ -0,0 +1,103 @@
/* **************************************************************************** */
/* */
/* ::: :::::::: */
/* unique_ptr.tpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 09:53:45 by maldavid #+# #+# */
/* Updated: 2024/01/20 09:53:45 by maldavid ### ########.fr */
/* */
/* **************************************************************************** */
#include <cassert>
namespace unstd
{
template <typename T>
UniquePtr<T>::UniquePtr() : _ptr(NULL) {}
template <typename T>
UniquePtr<T>::UniquePtr(T* ptr) : _ptr(ptr) {}
template <typename T>
UniquePtr<T>::UniquePtr(UniquePtr<T>& rhs) : _ptr(rhs._ptr)
{
rhs._ptr = NULL;
}
template <typename T>
T* UniquePtr<T>::get()
{
return _ptr;
}
template <typename T>
void UniquePtr<T>::swap(const UniquePtr<T>& ptr)
{
T* temp = _ptr;
_ptr = ptr._ptr;
ptr._ptr = temp;
}
template <typename T>
void UniquePtr<T>::reset(T* ptr)
{
if(_ptr != NULL)
delete _ptr;
_ptr = ptr;
}
template <typename T>
T* UniquePtr<T>::release()
{
T* temp = _ptr;
_ptr = NULL;
return temp;
}
template <typename T>
UniquePtr<T>::operator bool() const
{
return _ptr != NULL;
}
template <typename T>
UniquePtr<T>& UniquePtr<T>::operator=(T* ptr)
{
assert(_ptr == NULL && "UniquePtr assertion failed : `operator=`, unique ptr already holds a pointer");
reset(ptr);
}
template <typename T>
T* UniquePtr<T>::operator->() const
{
return _ptr;
}
template <typename T>
T& UniquePtr<T>::operator*() const
{
return *_ptr;
}
template <typename T>
UniquePtr<T>::~UniquePtr()
{
if(_ptr != NULL)
delete _ptr;
}
template <typename T>
UniquePtr<T>::UniquePtr(const UniquePtr<T>& ptr)
{
(void)ptr;
}
template <typename T>
UniquePtr<T>& UniquePtr<T>::operator=(const UniquePtr& ptr)
{
(void)ptr;
return *this;
}
}

51
includes/unstd/weak_ptr.h Normal file
View File

@@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* weak_ptr.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 11:17:07 by maldavid #+# #+# */
/* Updated: 2024/01/20 13:16:14 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __UNSTD_WEAK_PTR__
#define __UNSTD_WEAK_PTR__
#include <unstd/bits/ref_counter.h>
#include <unstd/shared_ptr.h>
namespace unstd
{
template <typename T>
class WeakPtr
{
public:
WeakPtr();
WeakPtr(const WeakPtr& rhs);
WeakPtr(const SharedPtr<T>& shared);
WeakPtr& operator=(const WeakPtr& rhs);
WeakPtr& operator=(const SharedPtr<T>& shared);
void reset();
void swap(WeakPtr& rhs);
bool expired() const;
SharedPtr<T> lock() const;
~WeakPtr();
private:
void safeRelease();
private:
bits::RefCount* _ref;
T* _ptr;
};
}
#include <unstd/weak_ptr.h>
#endif

109
includes/unstd/weak_ptr.ipp Normal file
View File

@@ -0,0 +1,109 @@
/* **************************************************************************** */
/* */
/* ::: :::::::: */
/* weak_ptr.ipp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 12:58:44 by maldavid #+# #+# */
/* Updated: 2024/01/20 12:58:44 by maldavid ### ########.fr */
/* */
/* **************************************************************************** */
namespace unstd
{
template <typename T>
WeakPtr<T>::WeakPtr() : _ref(NULL), _ptr(NULL) {}
template <typename T>
WeakPtr<T>::WeakPtr(const WeakPtr<T>& rhs) : _ref(rhs._ref), _ptr(rhs._ptr)
{
if(_ptr && _ref)
_ref->weak++;
}
template <typename T>
WeakPtr<T>::WeakPtr(const SharedPtr<T>& shared) : _ref(shared._ref), _ptr(shared._ptr)
{
if(_ptr && _ref)
_ref->weak++;
}
template <typename T>
WeakPtr<T>& WeakPtr<T>::operator=(const WeakPtr<T>& rhs)
{
if(_ptr == rhs._ptr)
return *this;
safeRelease();
_ptr = rhs._ptr;
_ref = rhs._ref;
return *this;
}
template <typename T>
WeakPtr<T>& WeakPtr<T>::operator=(const SharedPtr<T>& shared)
{
if(_ptr == shared._ptr)
return *this;
safeRelease();
_ptr = shared._ptr;
_ref = shared._ref;
_ref->weak++;
return *this;
}
template <typename T>
void WeakPtr<T>::reset()
{
safeRelease();
_ptr = NULL;
_ref = NULL;
}
template <typename T>
void WeakPtr<T>::swap(WeakPtr<T>& rhs)
{
T* tmptr = _ptr;
bits::RefCount* tmpref = _ref;
_ptr = rhs._ptr;
_ref = rhs._ref;
rhs._ptr = tmptr;
rhs._ref = tmpref;
}
template <typename T>
bool WeakPtr<T>::expired() const
{
return (_ptr && _ref && _ref->shared > 0);
}
template <typename T>
SharedPtr<T> WeakPtr<T>::lock() const
{
return (expired() ? SharedPtr<T>() : SharedPtr<T>(_ptr));
}
template <typename T>
void WeakPtr<T>::safeRelease()
{
if(_ref == NULL)
return;
_ref->weak--;
if(_ref && _ref->shared <= 0)
{
delete _ptr;
_ptr = NULL;
delete _ref;
_ref = NULL;
}
}
template <typename T>
WeakPtr<T>::~WeakPtr()
{
if(_ptr && _ref && _ref->shared > 0)
safeRelease();
}
}