1#ifndef FREE_TENSOR_REF_H
2#define FREE_TENSOR_REF_H
14template <
class T>
class EnableSelf;
24template <
class T>
class Ref {
25 template <
class U>
friend class Ref;
26 template <
class U>
friend class Weak;
29 std::shared_ptr<T> ptr_;
32 Ref(std::shared_ptr<T> &&ptr) : ptr_(
std::move(ptr)) { updateSelf(); }
38 if constexpr (std::is_base_of_v<EnableSelfBase, T>) {
39 if (ptr_ !=
nullptr) {
41 std::static_pointer_cast<EnableSelf<typename T::Self>>(ptr_)
50 if (!self.isValid()) {
67 Ref(T *ptr) : ptr_(ptr) { updateSelf(); }
72 template <std::derived_from<T> U>
73 Ref(
const Ref<U> &other) : ptr_(
std::static_pointer_cast<T>(other.ptr_)) {}
76 ptr_ = std::static_pointer_cast<T>(other.ptr_);
85 ret.ptr_ = std::static_pointer_cast<U>(ptr_);
89 bool isValid()
const {
return ptr_ !=
nullptr; }
112 template <
class... Args>
static Ref make(Args &&...args) {
114 std::forward<Args>(args)...));
118 return lhs.ptr_ ==
rhs.ptr_;
121 return lhs.ptr_ <=>
rhs.ptr_;
126 std::weak_ptr<T> ptr_;
127 bool notNull_ =
false;
133 template <std::derived_from<T> U>
155 template <
class U>
friend class Ref;
163 auto ret = self_.
lock();
164 if (!ret.isValid()) {
166 "BUG: This class is not managed by Ref. Are you trying to get "
167 "the Ref in a constructor even before a Ref is constructed?");
180 return hash_(ref.
get());
Definition: allocator.h:66
T Self
Definition: ref.h:160
Ref< T > self() const
Definition: ref.h:162
static Ref make(T &&x)
Definition: ref.h:106
static Ref make()
Definition: ref.h:105
static Ref make(const T &x)
Definition: ref.h:109
T & operator*() const
Definition: ref.h:91
Ref(T *ptr)
Definition: ref.h:67
friend bool operator==(const Ref &lhs, const Ref &rhs)
Definition: ref.h:117
bool isValid() const
Definition: ref.h:89
T * operator->() const
Definition: ref.h:96
Ref< U > as() const
Definition: ref.h:83
T Object
Definition: ref.h:58
static Ref make(Args &&...args)
Definition: ref.h:112
Ref & operator=(const Ref &)=default
Ref & operator=(Ref &&)=default
friend auto operator<=>(const Ref &lhs, const Ref &rhs)
Definition: ref.h:120
Ref(const Ref< U > &other)
Definition: ref.h:73
Ref & operator=(const Ref< U > &other)
Definition: ref.h:75
friend class Ref
Definition: ref.h:25
Ref(std::nullptr_t)
Definition: ref.h:61
T * get() const
Definition: ref.h:101
Weak()
Definition: ref.h:130
Ref< T > lock() const
Definition: ref.h:142
Weak(std::nullptr_t)
Definition: ref.h:131
bool isValid() const
Definition: ref.h:140
Weak(const Ref< U > &ref)
Definition: ref.h:134
#define ASSERT(expr)
Definition: except.h:152
#define ERROR(msg)
Definition: except.h:141
Definition: allocator.h:9
auto && lhs
Definition: const_fold.cc:70
auto auto && rhs
Definition: const_fold.cc:70
hash< T * > hash_
Definition: ref.h:178
size_t operator()(const freetensor::Ref< T > &ref) const
Definition: ref.h:179