-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalPtr.h
More file actions
51 lines (36 loc) · 2.02 KB
/
LocalPtr.h
File metadata and controls
51 lines (36 loc) · 2.02 KB
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
49
50
51
#pragma once
#include <cstddef>
#include <malloc.h>
#if _MSC_VER//[
// msvc
#define alloca _alloca
#elif __clang__//][
// clang
#else//][
// gcc
#endif//]
#define local_alloc(T, s) static_cast<T*>(alloca(s))
#define make_local(T, ...) local_ptr<T>(local_alloc(T, T::local_size()), ##__VA_ARGS__)
#define make_local_ext(T, ...) local_ptr<T>(local_alloc(T, T::local_size(__VA_ARGS__)), ##__VA_ARGS__)
template <class T>
class local_ptr final {
public:
~local_ptr() noexcept { reset(); }
constexpr local_ptr() noexcept :mp(nullptr){}
constexpr local_ptr(std::nullptr_t) noexcept :mp(nullptr){}
template <class... Args> explicit local_ptr(T* p, Args... args) :mp(p){ mp->local_init(args...); }
local_ptr(local_ptr<T>&& rhs) noexcept { mp = rhs.release(); }
local_ptr<T>& operator =(local_ptr<T>&& rhs) noexcept { mp = rhs.release(); return *this; }
T& operator *() const noexcept { return *get(); }
T* const operator ->() const noexcept { return mp; }
explicit operator bool() const noexcept { return (mp); }
T* const release() noexcept { auto p = mp; mp = nullptr; return p; }
void reset(T* p = nullptr) noexcept { auto d = mp; mp = p; if (d){ d->~T(); } }
void swap(local_ptr<T>& Swap) noexcept { auto p = mp; mp = Swap.mp; Swap.mp = p; }
T* const get() const noexcept { return mp; }
private:
local_ptr(const local_ptr<T>& rhs) noexcept = delete;
local_ptr<T>& operator =(const local_ptr<T>& rhs) noexcept = delete;
private:
T* mp;
};