forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone_ptr.h
65 lines (56 loc) · 1.55 KB
/
clone_ptr.h
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
#ifndef CATA_SRC_CLONE_PTR_H
#define CATA_SRC_CLONE_PTR_H
#include <memory>
namespace cata
{
template<typename T>
class clone_ptr
{
public:
clone_ptr() = default;
clone_ptr( std::nullptr_t ) {}
clone_ptr( const clone_ptr &other ) : p_( other.p_ ? other.p_->clone() : nullptr ) {}
clone_ptr( clone_ptr && ) = default;
clone_ptr &operator=( const clone_ptr &other ) {
p_ = other.p_ ? other.p_->clone() : nullptr;
return *this;
}
clone_ptr &operator=( clone_ptr && ) = default;
// implicit conversion from unique_ptr
template<typename U>
clone_ptr( std::unique_ptr<U> p ) : p_( std::move( p ) ) {}
T &operator*() {
return *p_;
}
const T &operator*() const {
return *p_;
}
T *operator->() {
return p_.get();
}
const T *operator->() const {
return p_.get();
}
T *get() {
return p_.get();
}
const T *get() const {
return p_.get();
}
explicit operator bool() const {
return !!*this;
}
bool operator!() const {
return !p_;
}
friend bool operator==( const clone_ptr &l, const clone_ptr &r ) {
return l.p_ == r.p_;
}
friend bool operator!=( const clone_ptr &l, const clone_ptr &r ) {
return l.p_ != r.p_;
}
private:
std::unique_ptr<T> p_;
};
} // namespace cata
#endif // CATA_SRC_CLONE_PTR_H