MUSX Document Model
Loading...
Searching...
No Matches
musx::dom::DeferredReference< T > Class Template Reference

Wraps a reference to an existing object or owns a temporary value if needed. More...

#include <MusxInstance.h>

Public Member Functions

 DeferredReference () noexcept=default
 Constructs an empty DeferredReference with no bound reference.
 
 DeferredReference (const T &ref) noexcept
 Constructs a non-owning DeferredReference bound to an existing object.
 
 DeferredReference (const T *ptr) noexcept
 Constructs a non-owning DeferredReference bound to a pointer.
 
const T & emplace (OwnedT &&value) noexcept(std::is_nothrow_move_constructible_v< OwnedT >)
 Moves a value into owned storage and binds to it.
 
 operator bool () const noexcept
 Checks whether this DeferredReference currently references a valid object.
 
const T & get () const noexcept
 Gets a const reference to the referenced or owned object.
 
const T * operator-> () const noexcept
 Provides pointer-like access to the referenced or owned object.
 
const T & operator* () const noexcept
 Dereferences to the referenced or owned object.
 

Detailed Description

template<class T>
class musx::dom::DeferredReference< T >

Wraps a reference to an existing object or owns a temporary value if needed.

This utility allows a function parameter to accept either a non-owning reference (bound implicitly from a const T& or const T*) or an owned value created later via emplace(). It avoids copies and enables lazy materialization.

Example usage:

int func(DeferredReference<const Foo> foo = {})
{
if (!foo)
foo.emplace(createFoo()); // owns the result
return foo->bar();
}
Foo f;
func(f); // non-owning, no copy
func(); // omitted (empty), function will emplace()
func(DeferredReference{&f}); // explicit pointer form
Wraps a reference to an existing object or owns a temporary value if needed.
Definition MusxInstance.h:179
DeferredReference() noexcept=default
Constructs an empty DeferredReference with no bound reference.
Template Parameters
TThe public reference type. May be const-qualified (e.g., const Foo). Ownership is always stored as a mutable std::remove_const_t<T> internally.

Constructor & Destructor Documentation

◆ DeferredReference() [1/2]

template<class T >
musx::dom::DeferredReference< T >::DeferredReference ( const T &  ref)
inlinenoexcept

Constructs a non-owning DeferredReference bound to an existing object.

Parameters
refThe object to bind. No copy is made.

◆ DeferredReference() [2/2]

template<class T >
musx::dom::DeferredReference< T >::DeferredReference ( const T *  ptr)
inlineexplicitnoexcept

Constructs a non-owning DeferredReference bound to a pointer.

Parameters
ptrPointer to the object. No copy is made.

Member Function Documentation

◆ emplace()

template<class T >
const T & musx::dom::DeferredReference< T >::emplace ( OwnedT &&  value)
inlinenoexcept

Moves a value into owned storage and binds to it.

This replaces any previous binding (owned or non-owned) and ensures that the DeferredReference has a valid internal reference thereafter.

Parameters
valueThe object to move into internal storage.
Returns
A const reference to the stored object.

◆ get()

template<class T >
const T & musx::dom::DeferredReference< T >::get ( ) const
inlinenoexcept

Gets a const reference to the referenced or owned object.

Warning
Undefined behavior if called when !(*this).