WeakPtr: weak-pointer design lessons from Chromium
This directory implements a Chromium-style WeakPtr weak-pointer component and thoroughly unpacks a piece of modern C++ design: a pointer that never touches ownership, yet lets you safely observe whether the object is still alive. It is the sister series to the OnceCallback series — the industrial-grade answer to the hand-rolled cancellation token from 01-4 is WeakPtr.
Complete tutorial (full/)
Aimed at readers starting from zero: it opens with weak-reference concepts and prerequisite knowledge, then gradually leads up to a complete component implementation.
Prerequisites (7 articles):
- WeakPtr prerequisite (0): weak references and the lifetime puzzle
- WeakPtr prerequisite (I): intrusive reference counting and scoped_refptr
- WeakPtr prerequisite (II): std::atomic and memory_order
- WeakPtr prerequisite (III): sequences, SEQUENCE_CHECKER, and DCHECK/CHECK
- WeakPtr prerequisite (IV): applying concepts and requires
- WeakPtr prerequisite (V): template friend and uintptr_t type erasure
- WeakPtr prerequisite (VI): TRIVIAL_ABI and trivial relocatability
Hands-on practice (6 articles):
- WeakPtr hands-on (I): motivation and API design
- WeakPtr hands-on (II): the core skeleton and control block
- WeakPtr hands-on (III): WeakPtrFactory and the last-member idiom
- WeakPtr hands-on (IV): sequence affinity and lazy binding
- WeakPtr hands-on (V): integrating with callbacks to close the OnceCallback loop
- WeakPtr hands-on (VI): tests and performance comparison
Advanced design guide (hands_on/)
Aimed at readers who already have experience with C++ templates and concurrency: a fast-paced walkthrough of the design motivation, the implementation strategy, and the test verification: