//------------------------------------------------------------------------------ /* This file is part of rippled: https://github.com/ripple/rippled Copyright (c) 2023 Ripple Labs Inc. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #ifndef RIPPLE_BASICS_SHAREDWEAKCACHEPOINTER_IPP_INCLUDED #define RIPPLE_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