summaryrefslogtreecommitdiff
path: root/AK/RefCountForwarder.h
AgeCommit message (Collapse)Author
2021-12-09AK: Add RefCountForwarder<T>Andreas Kling
This is a convenience template that implements reference count forwarding. This means that an object forwards ref() and unref() to another object. We can use this when two ref-counted objects need to keep each other alive. This situation poses two problems: - Using 2x RefPtr would cause a ref cycle and leak both objects. - Using 2x WeakPtr would allow one of them to be destroyed early. With RefCountForwarder, only one of the objects has a ref count. The object with the ref count points to the forwarding object by using a non-counting smart pointer (OwnPtr or NonnullOwnPtr). Thus, both objects are kept alive by the same ref count, and they can safely point to each other without worrying about disjoint lifetimes.