Skip to content

Commit 549f8eb

Browse files
committed
Add CheckedUniquePtr
1 parent 504c0e8 commit 549f8eb

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

CMakeLists.shared

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ set(FLASHMQ_HEADERS
9191
${RELPATH}checkedsharedptr.h
9292
${RELPATH}sharedmutexowned.h
9393
${RELPATH}reentrantmap.h
94+
${RELPATH}checkeduniqueptr.h
9495
)
9596

9697
set(FLASHMQ_IMPLS

checkeduniqueptr.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#ifndef CHECKEDUNIQUEPTR_H
2+
#define CHECKEDUNIQUEPTR_H
3+
4+
#include <stdexcept>
5+
#include <memory>
6+
7+
class CheckedUniquePtrNull : private std::exception
8+
{
9+
public:
10+
virtual const char* what() const noexcept override
11+
{
12+
return "CheckedUniquePtrNull pointer null";
13+
}
14+
};
15+
16+
template<typename T>
17+
class CheckedUniquePtr
18+
{
19+
std::unique_ptr<T> d;
20+
21+
public:
22+
CheckedUniquePtr() = default;
23+
24+
template<typename... Args>
25+
CheckedUniquePtr(Args... args) :
26+
d(args...)
27+
{
28+
29+
}
30+
31+
CheckedUniquePtr(std::unique_ptr<T> &&other)
32+
{
33+
d = std::move(other);
34+
}
35+
36+
T &operator*()
37+
{
38+
if (!d)
39+
throw CheckedUniquePtrNull();
40+
41+
return *d;
42+
}
43+
44+
T *operator->()
45+
{
46+
if (!d)
47+
throw CheckedUniquePtrNull();
48+
49+
return d.get();
50+
}
51+
52+
void reset()
53+
{
54+
d.reset();
55+
}
56+
57+
operator bool() const
58+
{
59+
return static_cast<bool>(d);
60+
}
61+
};
62+
63+
#endif // CHECKEDUNIQUEPTR_H

0 commit comments

Comments
 (0)