From c82548df98283f9724723e6963587a905d9f2a39 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Sat, 20 Jan 2024 13:17:11 +0100 Subject: [PATCH] adding unstd base --- .gitignore | 21 +++++ Makefile | 22 ++--- README.md | 5 +- includes/unstd/array.h | 77 +++++++++++++++++ includes/unstd/bits/ref_counter.h | 30 +++++++ includes/unstd/shared_ptr.h | 54 ++++++++++++ includes/unstd/shared_ptr.ipp | 136 ++++++++++++++++++++++++++++++ includes/unstd/unique_ptr.h | 55 ++++++++++++ includes/unstd/unique_ptr.ipp | 103 ++++++++++++++++++++++ includes/unstd/weak_ptr.h | 51 +++++++++++ includes/unstd/weak_ptr.ipp | 109 ++++++++++++++++++++++++ srcs/main.cpp | 17 +++- 12 files changed, 664 insertions(+), 16 deletions(-) create mode 100644 .gitignore create mode 100644 includes/unstd/array.h create mode 100644 includes/unstd/bits/ref_counter.h create mode 100644 includes/unstd/shared_ptr.h create mode 100644 includes/unstd/shared_ptr.ipp create mode 100644 includes/unstd/unique_ptr.h create mode 100644 includes/unstd/unique_ptr.ipp create mode 100644 includes/unstd/weak_ptr.h create mode 100644 includes/unstd/weak_ptr.ipp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d38a3b5 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/Makefile b/Makefile index 2ee54cf..ac776b4 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ # ::: :::::::: # # Makefile :+: :+: :+: # # +:+ +:+ +:+ # -# By: vvaas +#+ +:+ +#+ # +# By: vavaas +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # -# Created: 2023/08/09 15:08:49 by vvaas #+# #+# # -# Updated: 2023/11/29 16:17:31 by vvaas ### ########.fr # +# Created: 2023/08/09 15:08:49 by vavaas #+# #+# # +# Updated: 2024/01/20 10:01:26 by maldavid ### ########.fr # # # # **************************************************************************** # @@ -17,29 +17,29 @@ SRCS = srcs/main.cpp \ OBJ_DIR = objs OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o)) -CC = c++ -CFLAGS = -std=c++98 -Wall -Wextra -Werror +CXX = c++ +CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -I includes DEBUG ?= false MODE = "release" ifeq ($(DEBUG), true) - CFLAGS += -g -D DEBUG + CXXFLAGS += -g -D DEBUG MODE = "debug" endif RM = rm -rf $(OBJ_DIR)/%.o: %.cpp - @echo "\e[1;32m[compiling "$(MODE)" {"$(CC)"}...]\e[1;00m "$< - @$(CC) $(CFLAGS) $(COPTS) -c $< -o $@ + @printf "\e[1;32m[compiling "$(MODE)" {"$(CXX)"}...]\e[1;00m "$<"\n" + @$(CXX) $(CXXFLAGS) $(COPTS) -c $< -o $@ all: $(NAME) $(NAME): $(OBJ_DIR) $(OBJS) - @echo "\e[1;32m[linking "$(MODE)" {"$(CC)"}...]\e[1;00m "$@ - @$(CC) -o $(NAME) $(OBJS) - @echo "\e[1;32m[build finished]\e[1;00m" + @printf "\e[1;32m[linking "$(MODE)" {"$(CXX)"}...]\e[1;00m "$@"\n" + @$(CXX) -o $(NAME) $(OBJS) + @printf "\e[1;32m[build finished]\e[1;00m\n" $(OBJ_DIR): @mkdir -p $(sort $(addprefix $(OBJ_DIR)/, $(dir $(SRCS)))) diff --git a/README.md b/README.md index 2f6ba1b..a3eb81d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/includes/unstd/array.h b/includes/unstd/array.h new file mode 100644 index 0000000..e00551a --- /dev/null +++ b/includes/unstd/array.h @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* array.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include + +namespace unstd +{ + template + class TArray + { + public: + TArray() {} + TArray(const TArray& rhs) + { + for(std::size_t i = 0; i < N; i++) + _data[i] = rhs._data[i]; + } + + inline TArray& operator=(const TArray& 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 diff --git a/includes/unstd/bits/ref_counter.h b/includes/unstd/bits/ref_counter.h new file mode 100644 index 0000000..de3f0ea --- /dev/null +++ b/includes/unstd/bits/ref_counter.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ref_counter.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 diff --git a/includes/unstd/shared_ptr.h b/includes/unstd/shared_ptr.h new file mode 100644 index 0000000..3622017 --- /dev/null +++ b/includes/unstd/shared_ptr.h @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* shared_ptr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include +#include + +namespace unstd +{ + template + class SharedPtr + { + template + 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 _refs; + T* _ptr; + bits::RefCount* _ref; + }; +} + +#include + +#endif diff --git a/includes/unstd/shared_ptr.ipp b/includes/unstd/shared_ptr.ipp new file mode 100644 index 0000000..011489b --- /dev/null +++ b/includes/unstd/shared_ptr.ipp @@ -0,0 +1,136 @@ +/* **************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* shared_ptr.ipp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/20 10:17:18 by maldavid #+# #+# */ +/* Updated: 2024/01/20 10:17:18 by maldavid ### ########.fr */ +/* */ +/* **************************************************************************** */ + +namespace unstd +{ + template + SharedPtr::SharedPtr(T* ptr) : _ptr(ptr) + { + typename std::map::iterator it = _refs.find(static_cast(ptr)); + if(it == _refs.end()) + { + _ref = new bits::RefCount(1, 0); + _refs[static_cast(ptr)] = _ref; + } + else + { + _ref = it->second; + _ref->shared++; + } + } + + template + SharedPtr::SharedPtr(const SharedPtr& rhs) : _ptr(rhs._ptr), _ref(rhs._ref) + { + if(_ptr && _ref) + ++_ref->shared; + } + + template + SharedPtr& SharedPtr::operator=(const SharedPtr& rhs) + { + if(_ptr == rhs._ptr) + return *this; + + safeRelease(); + _ptr = rhs._ptr; + _ref = rhs._ref; + _ref->shared++; + return *this; + } + + template + SharedPtr::operator bool() const + { + return _ptr != NULL; + } + + template + bool SharedPtr::operator==(const SharedPtr& rhs) + { + return _ptr == rhs._ptr; + } + + template + bool SharedPtr::operator==(T* ptr) + { + return _ptr == ptr; + } + + template + T& SharedPtr::operator*() + { + return *_ptr; + } + + template + T* SharedPtr::operator->() + { + return _ptr; + } + + template + T* SharedPtr::get() + { + return _ptr; + } + + template + void SharedPtr::swap(SharedPtr& rhs) + { + T* tmptr = _ptr; + bits::RefCount* tmpref = _ref; + _ptr = rhs._ptr; + _ref = rhs._ref; + rhs._ptr = tmptr; + rhs._ref = tmpref; + } + + template + void SharedPtr::reset(T* ptr) + { + safeRelease(); + _ptr = ptr; + typename std::map::iterator it = _refs.find(static_cast(ptr)); + if(it == _refs.end()) + { + _ref = new bits::RefCount(1, 0); + _refs[static_cast(ptr)] = _ref; + } + else + { + _ref = it->second; + _ref->shared++; + } + } + + template + void SharedPtr::safeRelease() + { + if(_ref == NULL) + return; + _ref->shared--; + if(_ref->shared <= 0) + { + delete _ptr; + _ptr = NULL; + delete _ref; + _ref = NULL; + } + } + + template + SharedPtr::~SharedPtr() + { + safeRelease(); + } +} diff --git a/includes/unstd/unique_ptr.h b/includes/unstd/unique_ptr.h new file mode 100644 index 0000000..4d94e46 --- /dev/null +++ b/includes/unstd/unique_ptr.h @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* unique_ptr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +namespace unstd +{ + template + class UniquePtr + { + public: + UniquePtr(); + UniquePtr(UniquePtr& ptr); + explicit UniquePtr(T* ptr); + + inline T* get(); + + inline void swap(const UniquePtr& 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 + +#endif diff --git a/includes/unstd/unique_ptr.ipp b/includes/unstd/unique_ptr.ipp new file mode 100644 index 0000000..95b948c --- /dev/null +++ b/includes/unstd/unique_ptr.ipp @@ -0,0 +1,103 @@ +/* **************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* unique_ptr.tpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/20 09:53:45 by maldavid #+# #+# */ +/* Updated: 2024/01/20 09:53:45 by maldavid ### ########.fr */ +/* */ +/* **************************************************************************** */ + +#include + +namespace unstd +{ + template + UniquePtr::UniquePtr() : _ptr(NULL) {} + + template + UniquePtr::UniquePtr(T* ptr) : _ptr(ptr) {} + + template + UniquePtr::UniquePtr(UniquePtr& rhs) : _ptr(rhs._ptr) + { + rhs._ptr = NULL; + } + + template + T* UniquePtr::get() + { + return _ptr; + } + + template + void UniquePtr::swap(const UniquePtr& ptr) + { + T* temp = _ptr; + _ptr = ptr._ptr; + ptr._ptr = temp; + } + + template + void UniquePtr::reset(T* ptr) + { + if(_ptr != NULL) + delete _ptr; + _ptr = ptr; + } + + template + T* UniquePtr::release() + { + T* temp = _ptr; + _ptr = NULL; + return temp; + } + + template + UniquePtr::operator bool() const + { + return _ptr != NULL; + } + + template + UniquePtr& UniquePtr::operator=(T* ptr) + { + assert(_ptr == NULL && "UniquePtr assertion failed : `operator=`, unique ptr already holds a pointer"); + reset(ptr); + } + + template + T* UniquePtr::operator->() const + { + return _ptr; + } + + template + T& UniquePtr::operator*() const + { + return *_ptr; + } + + template + UniquePtr::~UniquePtr() + { + if(_ptr != NULL) + delete _ptr; + } + + template + UniquePtr::UniquePtr(const UniquePtr& ptr) + { + (void)ptr; + } + + template + UniquePtr& UniquePtr::operator=(const UniquePtr& ptr) + { + (void)ptr; + return *this; + } +} diff --git a/includes/unstd/weak_ptr.h b/includes/unstd/weak_ptr.h new file mode 100644 index 0000000..44bc6a1 --- /dev/null +++ b/includes/unstd/weak_ptr.h @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* weak_ptr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include + +namespace unstd +{ + template + class WeakPtr + { + public: + WeakPtr(); + WeakPtr(const WeakPtr& rhs); + WeakPtr(const SharedPtr& shared); + + WeakPtr& operator=(const WeakPtr& rhs); + WeakPtr& operator=(const SharedPtr& shared); + + void reset(); + void swap(WeakPtr& rhs); + bool expired() const; + + SharedPtr lock() const; + + ~WeakPtr(); + + private: + void safeRelease(); + + private: + bits::RefCount* _ref; + T* _ptr; + }; +} + +#include + +#endif diff --git a/includes/unstd/weak_ptr.ipp b/includes/unstd/weak_ptr.ipp new file mode 100644 index 0000000..0cb05b6 --- /dev/null +++ b/includes/unstd/weak_ptr.ipp @@ -0,0 +1,109 @@ +/* **************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* weak_ptr.ipp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/20 12:58:44 by maldavid #+# #+# */ +/* Updated: 2024/01/20 12:58:44 by maldavid ### ########.fr */ +/* */ +/* **************************************************************************** */ + +namespace unstd +{ + template + WeakPtr::WeakPtr() : _ref(NULL), _ptr(NULL) {} + + template + WeakPtr::WeakPtr(const WeakPtr& rhs) : _ref(rhs._ref), _ptr(rhs._ptr) + { + if(_ptr && _ref) + _ref->weak++; + } + + template + WeakPtr::WeakPtr(const SharedPtr& shared) : _ref(shared._ref), _ptr(shared._ptr) + { + if(_ptr && _ref) + _ref->weak++; + } + + template + WeakPtr& WeakPtr::operator=(const WeakPtr& rhs) + { + if(_ptr == rhs._ptr) + return *this; + + safeRelease(); + _ptr = rhs._ptr; + _ref = rhs._ref; + return *this; + } + + template + WeakPtr& WeakPtr::operator=(const SharedPtr& shared) + { + if(_ptr == shared._ptr) + return *this; + + safeRelease(); + _ptr = shared._ptr; + _ref = shared._ref; + _ref->weak++; + return *this; + } + + template + void WeakPtr::reset() + { + safeRelease(); + _ptr = NULL; + _ref = NULL; + } + + template + void WeakPtr::swap(WeakPtr& rhs) + { + T* tmptr = _ptr; + bits::RefCount* tmpref = _ref; + _ptr = rhs._ptr; + _ref = rhs._ref; + rhs._ptr = tmptr; + rhs._ref = tmpref; + } + + template + bool WeakPtr::expired() const + { + return (_ptr && _ref && _ref->shared > 0); + } + + template + SharedPtr WeakPtr::lock() const + { + return (expired() ? SharedPtr() : SharedPtr(_ptr)); + } + + template + void WeakPtr::safeRelease() + { + if(_ref == NULL) + return; + _ref->weak--; + if(_ref && _ref->shared <= 0) + { + delete _ptr; + _ptr = NULL; + delete _ref; + _ref = NULL; + } + } + + template + WeakPtr::~WeakPtr() + { + if(_ptr && _ref && _ref->shared > 0) + safeRelease(); + } +} diff --git a/srcs/main.cpp b/srcs/main.cpp index 5149611..06befe5 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -3,11 +3,20 @@ /* ::: :::::::: */ /* main.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: vvaas +#+ +:+ +#+ */ +/* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/24 10:40:01 by maldavid #+# #+# */ -/* Updated: 2023/11/29 16:43:45 by vvaas ### ########.fr */ +/* Created: 2024/01/20 09:27:04 by maldavid #+# #+# */ +/* Updated: 2024/01/20 13:16:21 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ -// t'es mauvais malo +#include +#include +#include + +int main(int ac, char** av) +{ + (void)ac; + (void)av; + return 0; +}