|
libbio
|
#include <CowPtr.h>
Public Types | |
| typedef T | Type |
Public Member Functions | |
| CowPtr () | |
| CowPtr (T *owned, bool immutable=false) | |
| bool | IsBorrowed () const |
| CowPtr (const CowPtr &other) | |
| CowPtr & | operator= (const CowPtr &other) |
| ~CowPtr () | |
| const T * | View () const |
| bool | IsShared () const |
| bool | IsNull () const |
| template<typename CLONE > | |
| T * | Moo (CLONE cloneFn) |
| T * | Mutable () |
| void | Reset (T *owned) |
Static Public Member Functions | |
| static CowPtr | Borrow (T *borrowed) |
CowPtr stores ONE pointer, mSlot, which is one of three things:
|
inline |
Null / empty (mirrors a lazily-allocated mContents == NULL).
Adopt an owned payload (refcount 1). Pass immutable=true to seed a process-lifetime per-type prototype that always detaches on first write.
| owned | the payload to own (may be NULL for empty). |
| immutable | whether this is an immutable shared prototype. |
SHARE on copy (the win): owned bumps the refcount; borrowed copies the raw tagged pointer (no refcount), exactly like copying a bare T*.
|
inline |
NON-virtual on purpose (see class doc). Drops one reference (owned only).
Wrap a NON-owning reference with NO heap block: this shares the payload by raw pointer copy (no refcount), NEVER deletes it and NEVER detaches on write — exactly the semantics of the bare T it replaces. This is the borrowed-Bond case (self-bond, Metallic, Manage): clone-shares the raw reference, zero allocations.
| borrowed | the non-owned reference (may be NULL for empty). |
|
inline |
|
inline |
|
inline |
Moo() — Make Owned Object. The write path: detach-on-write. If the payload is shared or an immutable prototype, deep-copy it (via cloneFn, which runs at the leaf type) into a fresh private block before returning a mutable, uniquely-owned pointer. The caller MUST hold the host's exclusive lock around this (see class doc / CowRefCount.h). Borrowed references never copy-on-write: the real owner mutates in place and sharers of a borrowed reference intentionally observe each other's writes. (Yes, the cow says Moo when you make it your own. The name is a unique token by design — a one-line regex un-does it if it ever stops sparking joy.)
| CLONE | a callable T* (const T*) that deep-copies the payload. |
| cloneFn | the typed deep-copier. |
|
inline |
UNIQUE-OWNER mutable access: returns the payload WITHOUT detaching.
For owners that guarantee they never share this payload — e.g. a LinearMotif< X* >, whose pointer content is type-gated OUT of the clone-sharing path (a raw byte share would alias owned pointees and double-free), so its block is always uniquely owned (refcount 1). Such an owner mutates its payload in place with no copy-on-write overhead.
The debug assert documents and enforces the contract: it MUST NOT be called on a shared payload (that would mutate every sharer's view). Use Moo(cloner) instead whenever sharing is possible.
SHARE on assign. inc-new-before-dec-old makes self-assignment safe. Borrowed slots carry no refcount and are never Released.
First lazy allocation of a unique OWNED payload (NULL -> owned). Drops any prior reference first (owned or borrowed).
| owned | the payload to own. |
VIEW (read) path: never detaches, never bumps the refcount. Safe for unlimited concurrent readers of a shared payload.