Alien-libpanda
view release on metacpan or search on metacpan
src/panda/refcnt.h view on Meta::CPAN
weak_iptr(const iptr<U>& src) : storage(src ? refcnt_weak(src.get()) : nullptr), object(src ? src.get() : nullptr) {}
template <typename U, typename = enable_if_convertible_t<U*, T*>>
weak_iptr(const weak_iptr<U>& src) : storage(src.storage), object(src.object) {}
template <class U>
weak_iptr& operator=(const weak_iptr<U>& o) {
storage = o.storage;
object = o.object;
return *this;
}
template <class U>
weak_iptr& operator=(const iptr<U>& src) {
storage = src ? refcnt_weak(src.get()) : nullptr;
object = src ? src.get() : nullptr;
return *this;
}
template <class U>
weak_iptr& operator=(weak_iptr<U>&& o) {
storage = std::move(o.storage);
object = std::move(o.object);
return *this;
}
iptr<T> lock() const {
return expired() ? nullptr : object;
}
bool expired() const {
return !operator bool();
}
explicit operator bool() const {
return storage && storage->valid;
}
size_t use_count() const {
return *this ? refcnt_get(object) : 0;
}
size_t weak_count() const {
if (!storage) return 0;
if (storage->valid) return storage.use_count() - 1; // object itself refers to weak storage, ignore this reference in count
return storage.use_count();
}
void reset() noexcept {
storage.reset();
object = nullptr;
}
void swap(weak_iptr& oth) noexcept {
storage.swap(oth.storage);
std::swap(object, oth.object);
}
private:
iptr<weak_storage> storage;
T* object; // it is cache, it never invalidates itself, use storage->object to check validity
};
template <class T>
void swap(weak_iptr<T>& a, weak_iptr<T>& b) noexcept { a.swap(b); }
template <class T> struct _weak_t;
template <class T> struct _weak_t<iptr<T>> {
using type = weak_iptr<T>;
};
template <typename T>
using weak = typename _weak_t<T>::type;
}
( run in 1.455 second using v1.01-cache-2.11-cpan-140bd7fdf52 )