From bcdce9633dc351d3bc7f347a165348b8fab87cd9 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 15 Feb 2022 12:33:52 +0100 Subject: refactor: reorganize files & improve classes --- src/std/memory.tpp | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/std/memory.tpp (limited to 'src/std/memory.tpp') diff --git a/src/std/memory.tpp b/src/std/memory.tpp new file mode 100644 index 0000000..fbfcd32 --- /dev/null +++ b/src/std/memory.tpp @@ -0,0 +1,94 @@ +#pragma once + +#include "utils.hpp" + +#include +#include + +template +memType *malloc_s(unsigned int size) +{ + auto *mem = malloc(size); + + if (mem == nullptr) + { + Serial.println("Error: Memory allocation failed"); + while (true) {} + } + + return static_cast(mem); +} + +template +UniquePtr::UniquePtr() = default; + +template +UniquePtr::UniquePtr(Target *target) : _target(target) +{ +} + +template +UniquePtr::UniquePtr(const UniquePtr &unique_ptr) + : _target(new Target(*(unique_ptr._target))) +{ +} + +template +UniquePtr::UniquePtr(UniquePtr &&unique_ptr) noexcept + : _target(unique_ptr._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 UniquePtr::operator*() const +{ + return *(this->_target); +} + +template +Target *UniquePtr::operator->() const +{ + return this->_target; +} + +template +UniquePtr make_unique(Args... args) +{ + return UniquePtr(new Target(args...)); +} -- cgit v1.2.3-18-g5258