From 01ce0af940bd69c94a2fac8b65219262845cca98 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 14 Feb 2022 18:18:38 +0100 Subject: refactor: clean sewage --- src/utils/memory.tpp | 65 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 10 deletions(-) (limited to 'src/utils/memory.tpp') diff --git a/src/utils/memory.tpp b/src/utils/memory.tpp index 276b0b4..6261c81 100644 --- a/src/utils/memory.tpp +++ b/src/utils/memory.tpp @@ -1,8 +1,9 @@ -#include "memory.hpp" +#pragma once -#include "Arduino.h" #include "general.hpp" +#include + template memType *malloc_s(unsigned int size) { @@ -18,31 +19,75 @@ memType *malloc_s(unsigned int size) } template -unique_ptr::unique_ptr(Target *target) +UniquePtr::UniquePtr() = default; + +template +UniquePtr::UniquePtr(Target *target) : _target(target) +{ +} + +template +UniquePtr::UniquePtr(const UniquePtr &unique_ptr) + : _target(new Target(*(unique_ptr._target))) { - this->_target = target; } template -unique_ptr::~unique_ptr() +UniquePtr::UniquePtr(UniquePtr &&unique_ptr) noexcept + : _target(unique_ptr._target) { - delete this->_target; + unique_ptr._target = nullptr; +} + +template +UniquePtr &UniquePtr::operator=(const UniquePtr &unique_ptr) +{ + if (&unique_ptr != this) + { + delete _target; + _target = nullptr; + _target = new Target(*(unique_ptr._target)); + } + + return *this; +} + +template +UniquePtr &UniquePtr::operator=(UniquePtr &&unique_ptr) noexcept +{ + if (&unique_ptr != this) + { + delete _target; + _target = unique_ptr._target; + unique_ptr._target = nullptr; + } + + return *this; +} + +template +UniquePtr::~UniquePtr() +{ + if (this->_target != nullptr) + { + delete this->_target; + } } template -Target unique_ptr::operator*() const +Target UniquePtr::operator*() const { return *(this->_target); } template -Target *unique_ptr::operator->() const +Target *UniquePtr::operator->() const { return this->_target; } template -unique_ptr make_unique(Args... args) +UniquePtr make_unique(Args... args) { - return unique_ptr(new Target(args...)); + return UniquePtr(new Target(args...)); } -- cgit v1.2.3-18-g5258