File tree Expand file tree Collapse file tree 2 files changed +64
-0
lines changed
Expand file tree Collapse file tree 2 files changed +64
-0
lines changed Original file line number Diff line number Diff 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
9697set(FLASHMQ_IMPLS
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments