diff options
author | HampusM <hampus@hampusmat.com> | 2022-02-14 18:18:38 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-02-14 18:18:38 +0100 |
commit | 01ce0af940bd69c94a2fac8b65219262845cca98 (patch) | |
tree | 97c443782ce1cfba90b205183c8aab1e3edb0bb3 /src/utils/memory.tpp | |
parent | cb7a167c7dee2fa1a19bd09ede3bae8b140e79da (diff) |
refactor: clean sewage
Diffstat (limited to 'src/utils/memory.tpp')
-rw-r--r-- | src/utils/memory.tpp | 65 |
1 files changed, 55 insertions, 10 deletions
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 <Arduino.h> + template <typename memType> memType *malloc_s(unsigned int size) { @@ -18,31 +19,75 @@ memType *malloc_s(unsigned int size) } template <class Target> -unique_ptr<Target>::unique_ptr(Target *target) +UniquePtr<Target>::UniquePtr() = default; + +template <class Target> +UniquePtr<Target>::UniquePtr(Target *target) : _target(target) +{ +} + +template <class Target> +UniquePtr<Target>::UniquePtr(const UniquePtr &unique_ptr) + : _target(new Target(*(unique_ptr._target))) { - this->_target = target; } template <class Target> -unique_ptr<Target>::~unique_ptr() +UniquePtr<Target>::UniquePtr(UniquePtr &&unique_ptr) noexcept + : _target(unique_ptr._target) { - delete this->_target; + unique_ptr._target = nullptr; +} + +template <class Target> +UniquePtr<Target> &UniquePtr<Target>::operator=(const UniquePtr &unique_ptr) +{ + if (&unique_ptr != this) + { + delete _target; + _target = nullptr; + _target = new Target(*(unique_ptr._target)); + } + + return *this; +} + +template <class Target> +UniquePtr<Target> &UniquePtr<Target>::operator=(UniquePtr &&unique_ptr) noexcept +{ + if (&unique_ptr != this) + { + delete _target; + _target = unique_ptr._target; + unique_ptr._target = nullptr; + } + + return *this; +} + +template <class Target> +UniquePtr<Target>::~UniquePtr() +{ + if (this->_target != nullptr) + { + delete this->_target; + } } template <class Target> -Target unique_ptr<Target>::operator*() const +Target UniquePtr<Target>::operator*() const { return *(this->_target); } template <class Target> -Target *unique_ptr<Target>::operator->() const +Target *UniquePtr<Target>::operator->() const { return this->_target; } template <class Target, typename... Args> -unique_ptr<Target> make_unique(Args... args) +UniquePtr<Target> make_unique(Args... args) { - return unique_ptr<Target>(new Target(args...)); + return UniquePtr<Target>(new Target(args...)); } |