summaryrefslogtreecommitdiff
path: root/src/utils/memory.tpp
blob: 276b0b425cd132df8b20e5d4bd4a06ab92d3530a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "memory.hpp"

#include "Arduino.h"
#include "general.hpp"

template <typename memType>
memType *malloc_s(unsigned int size)
{
	auto *mem = malloc(size);

	if (mem == nullptr)
	{
		Serial.println("Error: Memory allocation failed");
		while (true) {}
	}

	return static_cast<memType *>(mem);
}

template <class Target>
unique_ptr<Target>::unique_ptr(Target *target)
{
	this->_target = target;
}

template <class Target>
unique_ptr<Target>::~unique_ptr()
{
	delete this->_target;
}

template <class Target>
Target unique_ptr<Target>::operator*() const
{
	return *(this->_target);
}

template <class Target>
Target *unique_ptr<Target>::operator->() const
{
	return this->_target;
}

template <class Target, typename... Args>
unique_ptr<Target> make_unique(Args... args)
{
	return unique_ptr<Target>(new Target(args...));
}