blob: 6cf5117aaddc6d54454bd7f5c51f0c15d643121f (
plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#pragma once
#include "Base.h"
#include <cassert>
#include <memory>
#include <type_traits>
namespace cru {
template <typename T>
class SelfResolvable;
template <typename T>
class ObjectResolver {
friend SelfResolvable<T>;
template <typename U>
friend class ObjectResolver;
private:
explicit ObjectResolver(std::shared_ptr<bool> destroyed, T* ptr)
: destroyed_(std::move(destroyed)), ptr_(ptr) {}
public:
CRU_DEFAULT_COPY(ObjectResolver)
CRU_DEFAULT_MOVE(ObjectResolver)
template <typename U>
ObjectResolver(const ObjectResolver<U>& other)
requires(std::is_convertible_v<U*, T*>)
: destroyed_(other.destroyed_), ptr_(static_cast<T*>(other.ptr_)) {}
template <typename U>
ObjectResolver(ObjectResolver<U>&& other)
requires(std::is_convertible_v<U*, T*>)
: destroyed_(std::move(other.destroyed_)),
ptr_(static_cast<T*>(other.ptr_)) {}
template <typename U>
ObjectResolver& operator=(const ObjectResolver<U>& other)
requires(std::is_convertible_v<U*, T*>)
{
if (this != &other) {
this->destroyed_ = other.destroyed_;
this->ptr_ = static_cast<T*>(other.ptr_);
}
return *this;
}
template <typename U>
ObjectResolver& operator=(ObjectResolver<U>&& other)
requires(std::is_convertible_v<U*, T*>)
{
if (this != &other) {
this->destroyed_ = std::move(other.destroyed_);
this->ptr_ = static_cast<T*>(other.ptr_);
}
return *this;
}
bool IsValid() const { return this->destroyed_ != nullptr; }
T* Resolve() const {
assert(IsValid());
return *destroyed_ ? nullptr : ptr_;
}
/**
* @remarks So this class can be used as a functor.
*/
T* operator()() const { return Resolve(); }
template <typename U>
operator ObjectResolver<U>() const
requires(std::is_convertible_v<T*, U*>)
{
return ObjectResolver<U>(*this);
}
private:
std::shared_ptr<bool> destroyed_;
T* ptr_;
};
/**
* Currently the implementation is NOT thread-safe.
*/
template <typename T>
class SelfResolvable {
public:
SelfResolvable() : destroyed_(new bool(false)) {}
CRU_DELETE_COPY(SelfResolvable)
virtual ~SelfResolvable() { *destroyed_ = true; }
ObjectResolver<T> CreateResolver() {
return ObjectResolver<T>(destroyed_, static_cast<T*>(this));
}
private:
std::shared_ptr<bool> destroyed_;
};
} // namespace cru
|