This repository was archived by the owner on Apr 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResGuard.hpp
More file actions
144 lines (119 loc) · 3.41 KB
/
ResGuard.hpp
File metadata and controls
144 lines (119 loc) · 3.41 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef RESGUARD_HPP
#define RESGUARD_HPP
#include <memory>
#include <type_traits>
#include <utility>
#include <stdexcept>
#include <cstddef>
namespace safe
{
template <class T>
class [[nodiscard]] unique_resource
{
static_assert(!std::is_pointer_v<T>, "raw pointer types are not allowed");
T value_;
bool valid_{true};
using deleter_type = void (*)(T&) noexcept;
deleter_type deleter_{nullptr};
void release() noexcept
{
if (valid_ && deleter_)
{
deleter_(value_);
valid_ = false;
}
}
public:
unique_resource() = delete;
unique_resource(std::nullptr_t) = delete;
template <class U>
explicit unique_resource(U&& res, deleter_type d) noexcept(std::is_nothrow_constructible_v<T, U&&>)
: value_{std::forward<U>(res)}, deleter_{d}
{
if (!d) std::terminate();
}
~unique_resource() noexcept
{
release();
}
unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible_v<T>)
: value_{std::move(other.value_)},
valid_{std::exchange(other.valid_, false)},
deleter_{other.deleter_}
{
}
unique_resource& operator=(unique_resource&& other) noexcept(std::is_nothrow_move_assign_v<T>)
{
if (this != &other)
{
release();
value_ = std::move(other.value_);
valid_ = std::exchange(other.valid_, false);
deleter_ = other.deleter_;
}
return *this;
}
unique_resource(const unique_resource&) = delete;
unique_resource& operator=(const unique_resource&) = delete;
T& get() noexcept
{
return value_;
}
const T& get() const noexcept
{
return value_;
}
T* operator->() noexcept
{
return &value_;
}
const T* operator->() const noexcept
{
return &value_;
}
explicit operator bool() const noexcept
{
return valid_;
}
void reset() noexcept
{
release();
}
T release_ownership() noexcept
{
valid_ = false;
return std::move(value_);
}
void swap(unique_resource& other) noexcept(std::is_nothrow_swappable_v<T>)
{
using std::swap;
swap(value_, other.value_);
swap(valid_, other.valid_);
swap(deleter_, other.deleter_);
}
};
template <class T, class D>
[[nodiscard]] auto make_unique_resource(T&& res, D&& deleter) noexcept(noexcept(deleter(std::declval<T&>())))
{
using decayed_t = std::decay_t<T>;
using deleter_fn = void (*)(decayed_t&) noexcept;
deleter_fn fn = +[](decayed_t& v) noexcept
{
try
{
std::forward<D>(deleter)(v);
}
catch (...)
{
std::terminate();
}
};
return unique_resource<decayed_t>{std::forward<T>(res), fn};
}
template <class T>
void swap(unique_resource<T>& a, unique_resource<T>& b) noexcept(noexcept(a.swap(b)))
{
a.swap(b);
}
}
#endif