HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
memory_utils.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <memory>
10#include <new>
11#include <utility>
12
13namespace hf {
14namespace utils {
15
38template <typename T, typename... Args>
39std::unique_ptr<T> make_unique_nothrow(Args&&... args) {
40 T* raw_ptr = new (std::nothrow) T(std::forward<Args>(args)...);
41 if (!raw_ptr) {
42 return nullptr;
43 }
44 return std::unique_ptr<T>(raw_ptr);
45}
46
66template <typename T>
67std::unique_ptr<T[]> make_unique_array_nothrow(size_t size) {
68 T* raw_ptr = new (std::nothrow) T[size];
69 if (!raw_ptr) {
70 return nullptr;
71 }
72 return std::unique_ptr<T[]>(raw_ptr);
73}
74
75} // namespace utils
76} // namespace hf
std::unique_ptr< T > make_unique_nothrow(Args &&... args)
Creates a unique_ptr using nothrow new for exception-free design.
Definition memory_utils.h:39
std::unique_ptr< T[]> make_unique_array_nothrow(size_t size)
Creates a unique_ptr for arrays using nothrow new.
Definition memory_utils.h:67
Definition StmTypes.h:70