libbio
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
bio::LazyPointer< T > Class Template Reference

#include <LazyPointer.h>

Public Types

typedef T *(* Factory) ()
 

Public Member Functions

 LazyPointer ()
 
 LazyPointer (Factory factory)
 
 LazyPointer (T *materialized)
 
 LazyPointer (const LazyPointer &other)
 
LazyPointeroperator= (const LazyPointer &other)
 
 ~LazyPointer ()
 
Toperator-> () const
 
Toperator* () const
 
TGet () const
 
TGetIfMaterialized () const
 
bool IsMaterialized () const
 
void SetFactory (Factory factory)
 
Factory GetFactory () const
 
void ResetWith (T *materialized)
 
TRelease ()
 

Detailed Description

template<typename T>
class bio::LazyPointer< T >

LazyPointer<T> is an owning smart pointer that defers the allocation of its T until the first access through operator-> or operator* (or an explicit Get()). Two construction modes:

  1. Eager: pass a T* directly. The pointer is stored as-is; no factory is involved. Behaves like a unique_ptr — operator-> just returns the stored pointer, never allocates.
  2. Lazy: pass a Factory (function pointer returning a T*). The stored pointer starts NULL; the first dereference invokes the factory and caches the result. Subsequent accesses return the cached pointer. The dtor delete[s] whatever the factory produced.

The dtor only delete[s] if the pointer was actually materialised, so a lazy LazyPointer that was never dereferenced costs nothing at destruction.

Two explicit peek/control accessors exist to deliberately NOT materialise:

And two write-side controls:

Copy semantics: factory propagates, materialised pointer does NOT. This matches the existing Wave copy-ctor contract for mSymmetry — a clone gets a fresh empty cache and re-materialises on demand.

NOT THREAD SAFE. Wave's mSymmetry is documented as a per-instance cache and existing Wave methods aren't thread-safe either, so we keep the same guarantees.

Design note: T may be forward-declared at the LazyPointer<T> declaration site, but the destructor's delete mPtr requires the full type at the point where ~LazyPointer<T> is implicitly instantiated (typically in the .cpp that defines the owning class's dtor). This is the same constraint std::unique_ptr<T> imposes; obey it the same way.

Member Typedef Documentation

◆ Factory

template<typename T >
typedef T *(* bio::LazyPointer< T >::Factory) ()

Constructor & Destructor Documentation

◆ LazyPointer() [1/4]

template<typename T >
bio::LazyPointer< T >::LazyPointer ( )
inline

◆ LazyPointer() [2/4]

template<typename T >
bio::LazyPointer< T >::LazyPointer ( Factory  factory)
inlineexplicit

◆ LazyPointer() [3/4]

template<typename T >
bio::LazyPointer< T >::LazyPointer ( T materialized)
inlineexplicit

◆ LazyPointer() [4/4]

template<typename T >
bio::LazyPointer< T >::LazyPointer ( const LazyPointer< T > &  other)
inline

◆ ~LazyPointer()

template<typename T >
bio::LazyPointer< T >::~LazyPointer ( )
inline

Member Function Documentation

◆ Get()

template<typename T >
T * bio::LazyPointer< T >::Get ( ) const
inline

Materialising explicit access. Same semantics as operator-> but usable in expression contexts that need a T* directly.

◆ GetFactory()

template<typename T >
Factory bio::LazyPointer< T >::GetFactory ( ) const
inline

◆ GetIfMaterialized()

template<typename T >
T * bio::LazyPointer< T >::GetIfMaterialized ( ) const
inline

Peek without materialising. Returns the currently-cached T* (NULL if no allocation has happened yet). Use this in dtors / null-checks where firing the factory would be wrong.

◆ IsMaterialized()

template<typename T >
bool bio::LazyPointer< T >::IsMaterialized ( ) const
inline

◆ operator*()

template<typename T >
T & bio::LazyPointer< T >::operator* ( ) const
inline

◆ operator->()

template<typename T >
T * bio::LazyPointer< T >::operator-> ( ) const
inline

Materialising access. If no T has been allocated yet AND a factory is set, the factory fires and the result is cached. Returns NULL iff both the cache is empty and no factory is set.

◆ operator=()

template<typename T >
LazyPointer & bio::LazyPointer< T >::operator= ( const LazyPointer< T > &  other)
inline

◆ Release()

template<typename T >
T * bio::LazyPointer< T >::Release ( )
inline

Transfer ownership of the cached pointer to the caller. The factory is unchanged so the next access will re-materialise. Returns NULL if no T was cached.

◆ ResetWith()

template<typename T >
void bio::LazyPointer< T >::ResetWith ( T materialized)
inline

Replace the cached pointer. The previous cached pointer is delete'd if any. The factory is left in place (used on the next reset-to- NULL access if one happens). Pass NULL to clear.

◆ SetFactory()

template<typename T >
void bio::LazyPointer< T >::SetFactory ( Factory  factory)
inline

The documentation for this class was generated from the following file: