-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathobserver_ptr.h
More file actions
51 lines (39 loc) · 1.32 KB
/
observer_ptr.h
File metadata and controls
51 lines (39 loc) · 1.32 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
#pragma once
#include <cstddef>
template <typename T> class ObserverPtr {
public:
ObserverPtr() noexcept : ptr_(nullptr) {}
ObserverPtr(std::nullptr_t) noexcept : ptr_(nullptr) {}
explicit ObserverPtr(T* ptr) noexcept : ptr_(ptr) {}
ObserverPtr(const ObserverPtr&) = default;
ObserverPtr& operator=(const ObserverPtr&) = default;
ObserverPtr(ObserverPtr&&) = default;
ObserverPtr& operator=(ObserverPtr&&) = default;
void reset(T* ptr = nullptr) noexcept { ptr_ = ptr; }
T* release() noexcept {
T* old = ptr_;
ptr_ = nullptr;
return old;
}
T* get() const noexcept { return ptr_; }
T& operator*() const noexcept { return *ptr_; }
T* operator->() const noexcept { return ptr_; }
explicit operator bool() const noexcept { return ptr_ != nullptr; }
void swap(ObserverPtr& other) noexcept {
T* tmp = ptr_;
ptr_ = other.ptr_;
other.ptr_ = tmp;
}
private:
T* ptr_;
};
template <typename T, typename U>
bool operator==(const ObserverPtr<T>& a, const ObserverPtr<U>& b) noexcept {
return a.get() == b.get();
}
template <typename T> bool operator==(const ObserverPtr<T>& a, std::nullptr_t) noexcept {
return !a;
}
template <typename T> ObserverPtr<T> make_observer(T* ptr) noexcept {
return ObserverPtr<T>(ptr);
}