#ifndef XRPL_BASICS_SHAREDWEAKCACHEPOINTER_IPP_INCLUDED #define XRPL_BASICS_SHAREDWEAKCACHEPOINTER_IPP_INCLUDED #include namespace ripple { template SharedWeakCachePointer::SharedWeakCachePointer( SharedWeakCachePointer const& rhs) = default; template template requires std::convertible_to SharedWeakCachePointer::SharedWeakCachePointer( std::shared_ptr const& rhs) : combo_{rhs} { } template SharedWeakCachePointer::SharedWeakCachePointer( SharedWeakCachePointer&& rhs) = default; template template requires std::convertible_to SharedWeakCachePointer::SharedWeakCachePointer(std::shared_ptr&& rhs) : combo_{std::move(rhs)} { } template SharedWeakCachePointer& SharedWeakCachePointer::operator=(SharedWeakCachePointer const& rhs) = default; template template requires std::convertible_to SharedWeakCachePointer& SharedWeakCachePointer::operator=(std::shared_ptr const& rhs) { combo_ = rhs; return *this; } template template requires std::convertible_to SharedWeakCachePointer& SharedWeakCachePointer::operator=(std::shared_ptr&& rhs) { combo_ = std::move(rhs); return *this; } template SharedWeakCachePointer::~SharedWeakCachePointer() = default; // Return a strong pointer if this is already a strong pointer (i.e. don't // lock the weak pointer. Use the `lock` method if that's what's needed) template std::shared_ptr const& SharedWeakCachePointer::getStrong() const { static std::shared_ptr const empty; if (auto p = std::get_if>(&combo_)) return *p; return empty; } template SharedWeakCachePointer::operator bool() const noexcept { return !!std::get_if>(&combo_); } template void SharedWeakCachePointer::reset() { combo_ = std::shared_ptr{}; } template T* SharedWeakCachePointer::get() const { return std::get_if>(&combo_).get(); } template std::size_t SharedWeakCachePointer::use_count() const { if (auto p = std::get_if>(&combo_)) return p->use_count(); return 0; } template bool SharedWeakCachePointer::expired() const { if (auto p = std::get_if>(&combo_)) return p->expired(); return !std::get_if>(&combo_); } template std::shared_ptr SharedWeakCachePointer::lock() const { if (auto p = std::get_if>(&combo_)) return *p; if (auto p = std::get_if>(&combo_)) return p->lock(); return {}; } template bool SharedWeakCachePointer::isStrong() const { if (auto p = std::get_if>(&combo_)) return !!p->get(); return false; } template bool SharedWeakCachePointer::isWeak() const { return !isStrong(); } template bool SharedWeakCachePointer::convertToStrong() { if (isStrong()) return true; if (auto p = std::get_if>(&combo_)) { if (auto s = p->lock()) { combo_ = std::move(s); return true; } } return false; } template bool SharedWeakCachePointer::convertToWeak() { if (isWeak()) return true; if (auto p = std::get_if>(&combo_)) { combo_ = std::weak_ptr(*p); return true; } return false; } } // namespace ripple #endif