From a119e6ca70ffab14f0a70908fa3eeb83b41bb5ab Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 14 Mar 2022 10:24:36 +0100 Subject: refactor: rename std to common --- src/common/memory.tpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/common/memory.tpp (limited to 'src/common/memory.tpp') diff --git a/src/common/memory.tpp b/src/common/memory.tpp new file mode 100644 index 0000000..ace2432 --- /dev/null +++ b/src/common/memory.tpp @@ -0,0 +1,76 @@ +#pragma once + +#include "memory.hpp" + +#include "utils.hpp" + +#include +#include + +namespace common +{ + +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(Target *target) : _target(target) +{ +} + +template +UniquePtr::UniquePtr(UniquePtr &&unique_ptr) noexcept + : _target(unique_ptr._target) +{ + unique_ptr._target = nullptr; +} + +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() +{ + delete _target; +} + +template +Target UniquePtr::operator*() const +{ + return *(_target); +} + +template +Target *UniquePtr::operator->() const +{ + return _target; +} + +template +UniquePtr make_unique(Args... args) +{ + return UniquePtr(new Target(args...)); +} + +} // namespace common -- cgit v1.2.3-18-g5258