-
Notifications
You must be signed in to change notification settings - Fork 0
/
CopyablePtr.hpp
67 lines (55 loc) · 1.57 KB
/
CopyablePtr.hpp
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
#ifndef ____NICE__COPYABLEPTR____
#define ____NICE__COPYABLEPTR____
#include <memory>
namespace nicehero {
/*
A copyable unique_ptr
*/
template <class T>
class CopyablePtr {
public:
/** If value can be default-constructed, why not?
Then we don't have to move it in */
CopyablePtr() = default;
/// Move a value in.
explicit CopyablePtr(T&& t) = delete;
explicit CopyablePtr(T* t) : value(t) {}
/// copy is move
CopyablePtr(const CopyablePtr& other) : value(std::move(other.value)) {}
/// move is also move
CopyablePtr(CopyablePtr&& other) : value(std::move(other.value)) {}
const T& operator*() const {
return (*value);
}
T& operator*() {
return (*value);
}
T* get() {
return value.get();
}
const T* operator->() const {
return value.get();
}
T* operator->() {
return value.get();
}
/// move the value out (sugar for std::move(*CopyablePtr))
T&& move() {
return std::move(value);
}
// If you want these you're probably doing it wrong, though they'd be
// easy enough to implement
CopyablePtr& operator=(CopyablePtr const&) = delete;
CopyablePtr& operator=(CopyablePtr&&) = delete;
private:
mutable std::unique_ptr<T> value;
};
/// Make a CopyablePtr from the argument. Because the name "makeCopyablePtr"
/// is already quite transparent in its intent, this will work for lvalues as
/// if you had wrapped them in std::move.
template <class T, class... _Types>
CopyablePtr<T> make_copyable(_Types&&... _Args) {
return CopyablePtr<T>(new T(std::forward<_Types>(_Args)...));
}
} // namespace folly
#endif