Skip to content

Commit

Permalink
feat(header): added pointer library header
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliot MCNAB authored and Eliot MCNAB committed Nov 27, 2023
1 parent 9b1c997 commit 711997c
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions pointer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <ostream>

#ifndef POINTER_HPP
# define POINTER_HPP

namespace mini {
template<typename T>
class UniquePtr {
public:
explicit UniquePtr(T *ptr);
UniquePtr(const UniquePtr& ptr);
~UniquePtr();
UniquePtr<T>& operator=(T& other);

T* getPtr(void) const;

T& operator*(void);
T* operator->(void);
operator T*(void) const;

private:
UniquePtr();
UniquePtr(UniquePtr<T>& other);
UniquePtr<T>& operator=(UniquePtr& other);

T* const _ptr;
};

// ============================[ CONTRUCTOR ]============================ //

template<typename T>
UniquePtr<T>::UniquePtr(T *ptr) : _ptr(ptr) {}

template<typename T>
UniquePtr<T>& UniquePtr<T>::operator=(T& other) {
~UniquePtr();
_ptr = other;
}

template<typename T>
UniquePtr<T>::~UniquePtr(void) {
delete _ptr;
}

// =============================[ ACCESSORS ]============================ //

template<typename T>
T* UniquePtr<T>::getPtr(void) const {
return (_ptr);
}

// =============================[ OPERATORS ]============================ //

template<typename T>
T& UniquePtr<T>::operator*(void) {
return (*_ptr);
}

template<typename T>
T* UniquePtr<T>::operator->(void) {
return (_ptr);
}

template<typename T>
UniquePtr<T>::operator T*(void) const {
return (_ptr);
}

template<typename T>
std::ostream& operator<<(std::ostream& os, UniquePtr<T>& uniquePtr) {
os << std::hex << uniquePtr.getPtr();
return (os);
}

// =============================[ FUNCTIONS ]============================ //

template<typename T>
UniquePtr<T> make_unique() {
return (UniquePtr<T>(new T()));
}
}

#endif

0 comments on commit 711997c

Please sign in to comment.