adding unstd base
This commit is contained in:
21
.gitignore
vendored
Normal file
21
.gitignore
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
*.swp
|
||||||
|
*.swx
|
||||||
|
*.o
|
||||||
|
*.a
|
||||||
|
*.so
|
||||||
|
*.out
|
||||||
|
*.dll
|
||||||
|
*.lib
|
||||||
|
*.exp
|
||||||
|
*.json
|
||||||
|
*.tmp
|
||||||
|
*.ilk
|
||||||
|
*.pdb
|
||||||
|
*.exe
|
||||||
|
*vgcore.*
|
||||||
|
*.gdb_history
|
||||||
|
.vs/
|
||||||
|
.xmake/
|
||||||
|
.cache/
|
||||||
|
objs/
|
||||||
|
ircserv
|
||||||
22
Makefile
22
Makefile
@@ -3,10 +3,10 @@
|
|||||||
# ::: :::::::: #
|
# ::: :::::::: #
|
||||||
# Makefile :+: :+: :+: #
|
# Makefile :+: :+: :+: #
|
||||||
# +:+ +:+ +:+ #
|
# +:+ +:+ +:+ #
|
||||||
# By: vvaas <vvaas@student.42.fr> +#+ +:+ +#+ #
|
# By: vavaas <vvaas@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/08/09 15:08:49 by vvaas #+# #+# #
|
# Created: 2023/08/09 15:08:49 by vavaas #+# #+# #
|
||||||
# Updated: 2023/11/29 16:17:31 by vvaas ### ########.fr #
|
# Updated: 2024/01/20 10:01:26 by maldavid ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@@ -17,29 +17,29 @@ SRCS = srcs/main.cpp \
|
|||||||
OBJ_DIR = objs
|
OBJ_DIR = objs
|
||||||
OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o))
|
OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o))
|
||||||
|
|
||||||
CC = c++
|
CXX = c++
|
||||||
CFLAGS = -std=c++98 -Wall -Wextra -Werror
|
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -I includes
|
||||||
|
|
||||||
DEBUG ?= false
|
DEBUG ?= false
|
||||||
MODE = "release"
|
MODE = "release"
|
||||||
|
|
||||||
ifeq ($(DEBUG), true)
|
ifeq ($(DEBUG), true)
|
||||||
CFLAGS += -g -D DEBUG
|
CXXFLAGS += -g -D DEBUG
|
||||||
MODE = "debug"
|
MODE = "debug"
|
||||||
endif
|
endif
|
||||||
|
|
||||||
RM = rm -rf
|
RM = rm -rf
|
||||||
|
|
||||||
$(OBJ_DIR)/%.o: %.cpp
|
$(OBJ_DIR)/%.o: %.cpp
|
||||||
@echo "\e[1;32m[compiling "$(MODE)" {"$(CC)"}...]\e[1;00m "$<
|
@printf "\e[1;32m[compiling "$(MODE)" {"$(CXX)"}...]\e[1;00m "$<"\n"
|
||||||
@$(CC) $(CFLAGS) $(COPTS) -c $< -o $@
|
@$(CXX) $(CXXFLAGS) $(COPTS) -c $< -o $@
|
||||||
|
|
||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
|
||||||
$(NAME): $(OBJ_DIR) $(OBJS)
|
$(NAME): $(OBJ_DIR) $(OBJS)
|
||||||
@echo "\e[1;32m[linking "$(MODE)" {"$(CC)"}...]\e[1;00m "$@
|
@printf "\e[1;32m[linking "$(MODE)" {"$(CXX)"}...]\e[1;00m "$@"\n"
|
||||||
@$(CC) -o $(NAME) $(OBJS)
|
@$(CXX) -o $(NAME) $(OBJS)
|
||||||
@echo "\e[1;32m[build finished]\e[1;00m"
|
@printf "\e[1;32m[build finished]\e[1;00m\n"
|
||||||
|
|
||||||
$(OBJ_DIR):
|
$(OBJ_DIR):
|
||||||
@mkdir -p $(sort $(addprefix $(OBJ_DIR)/, $(dir $(SRCS))))
|
@mkdir -p $(sort $(addprefix $(OBJ_DIR)/, $(dir $(SRCS))))
|
||||||
|
|||||||
@@ -1,2 +1,5 @@
|
|||||||
Si tu lis ça c'est que tu es mauvais et que je ne devrais pas faire IRC avec toi
|
"Vas sans craintes vavaas, je te pardonne"
|
||||||
|
- Malo Jan 20, 2024
|
||||||
|
|
||||||
|
"Si tu lis ça c'est que tu es mauvais et que je ne devrais pas faire IRC avec toi"
|
||||||
- Malo Nov 17, 2023
|
- Malo Nov 17, 2023
|
||||||
|
|||||||
77
includes/unstd/array.h
Normal file
77
includes/unstd/array.h
Normal 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
|
||||||
30
includes/unstd/bits/ref_counter.h
Normal file
30
includes/unstd/bits/ref_counter.h
Normal 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
|
||||||
54
includes/unstd/shared_ptr.h
Normal file
54
includes/unstd/shared_ptr.h
Normal 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
|
||||||
136
includes/unstd/shared_ptr.ipp
Normal file
136
includes/unstd/shared_ptr.ipp
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
55
includes/unstd/unique_ptr.h
Normal file
55
includes/unstd/unique_ptr.h
Normal 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
|
||||||
103
includes/unstd/unique_ptr.ipp
Normal file
103
includes/unstd/unique_ptr.ipp
Normal 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
51
includes/unstd/weak_ptr.h
Normal 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
109
includes/unstd/weak_ptr.ipp
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,11 +3,20 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* main.cpp :+: :+: :+: */
|
/* main.cpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: vvaas <vvaas@student.42.fr> +#+ +:+ +#+ */
|
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/11/24 10:40:01 by maldavid #+# #+# */
|
/* Created: 2024/01/20 09:27:04 by maldavid #+# #+# */
|
||||||
/* Updated: 2023/11/29 16:43:45 by vvaas ### ########.fr */
|
/* Updated: 2024/01/20 13:16:21 by maldavid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
// t'es mauvais malo
|
#include <unstd/unique_ptr.h>
|
||||||
|
#include <unstd/shared_ptr.h>
|
||||||
|
#include <unstd/weak_ptr.h>
|
||||||
|
|
||||||
|
int main(int ac, char** av)
|
||||||
|
{
|
||||||
|
(void)ac;
|
||||||
|
(void)av;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user