/* * Copyright (C) 2026 Josh Holtrop * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the “Software”), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */ #pragma once #include #include /** * Reference-counting pointer. */ template class rcp { template friend class rcp; private: T * ptr = nullptr; public: rcp() = default; rcp(T * p) : ptr(p) { if (p) { p->rcp_inc(); } } rcp(const rcp & other) : ptr(other.ptr) { if (ptr) { ptr->rcp_inc(); } } rcp & operator=(const rcp & other) { if (ptr != other.ptr) { T * old_ptr = ptr; ptr = other.ptr; if (ptr) { ptr->rcp_inc(); } if (old_ptr) { old_ptr->rcp_dec(); } } return *this; } rcp(rcp && other) noexcept : ptr(other.ptr) { other.ptr = nullptr; } rcp & operator=(rcp && other) noexcept { if (ptr != other.ptr) { if (ptr) { ptr->rcp_dec(); } ptr = other.ptr; other.ptr = nullptr; } return *this; } ~rcp() { if (ptr) { ptr->rcp_dec(); } } /* Upcasting constructor: allows rcp -> rcp */ template rcp(const rcp & other) : ptr(static_cast(other.ptr)) { static_assert(std::is_base_of::value, "rcp: implicit cast must be an upcast"); if (ptr) { ptr->rcp_inc(); } } template rcp(rcp && other) : ptr(static_cast(other.ptr)) { static_assert(std::is_base_of::value, "rcp: implicit cast must be an upcast"); other.ptr = nullptr; } void reset() { if (ptr) { ptr->rcp_dec(); ptr = nullptr; } } T * get_raw() const { return ptr; } T * operator->() const { return ptr; } T & operator*() const { return *ptr; } explicit operator bool() const { return ptr != nullptr; } template bool operator==(const rcp & other) const { return static_cast(ptr) == static_cast(other.ptr); } template bool operator!=(const rcp & other) const { return static_cast(ptr) != static_cast(other.ptr); } bool operator==(std::nullptr_t) const { return ptr == nullptr; } bool operator!=(std::nullptr_t) const { return ptr != nullptr; } template static rcp create(Args&&... args) { return T::create(std::forward(args)...); } template friend rcp rcp_dynamic_cast(rcp && other); }; template rcp rcp_dynamic_cast(const rcp & other) { return rcp(dynamic_cast(other.get_raw())); } template rcp rcp_dynamic_cast(rcp && other) { T * p = dynamic_cast(other.ptr); if (p) { other.ptr = nullptr; rcp result; result.ptr = p; return result; } return rcp(); } #define rcp_managed_root(classname) \ public: \ void rcp_inc() const \ { \ ref_count.fetch_add(1, std::memory_order_relaxed); \ } \ void rcp_dec() const \ { \ if (ref_count.fetch_sub(1, std::memory_order_acq_rel) == 1) \ { \ delete this; \ } \ } \ private: \ mutable std::atomic ref_count{0}; \ rcp_managed(classname) #define rcp_managed(classname) \ public: \ rcp get_rcp() \ { \ return rcp(this); \ } \ template \ static rcp create(Args&&... args) \ { \ return (new classname(std::forward(args)...))->get_rcp(); \ } \ private: