Cogs.Core
Public Member Functions | Public Attributes | List of all members
Cogs::UniqueValue< T > Struct Template Reference

A value that is cleared (zero-initialised) when it is moved from. More...

#include <UniqueValue.h>

Public Member Functions

 UniqueValue (const T &value)
 
 UniqueValue (const UniqueValue &)=delete
 
 UniqueValue (UniqueValue &&other) noexcept
 
UniqueValueoperator= (const UniqueValue &)=delete
 
UniqueValueoperator= (UniqueValue &&other) noexcept
 

Public Attributes

value = T(0)
 

Detailed Description

template<typename T>
struct Cogs::UniqueValue< T >

A value that is cleared (zero-initialised) when it is moved from.

Use this to give a primitive member move-transfer semantics — the value travels to the new owner and is reset in the old one, preventing duplicate action on the same resource.

Example (from Cogs.Core Texture): a texture may wrap an externally-provided GPU handle that it does not always own. A plain bool ownsHandle would be copied on move, causing both the moved-from and moved-to object to believe they own the handle and attempt to release it. UniqueValue<bool> solves this by clearing the flag in the moved-from object automatically:

struct Texture {
intptr_t externalHandle = 0;
UniqueValue<bool> ownsExternalTexture{ false };
Texture(Texture&&) = default; // ownsExternalTexture clears in moved-from
~Texture() {
if (ownsExternalTexture.value)
releaseHandle(externalHandle); // only called by the true owner
}
};
A value that is cleared (zero-initialised) when it is moved from.
Definition: UniqueValue.h:32

Definition at line 32 of file UniqueValue.h.

Constructor & Destructor Documentation

◆ UniqueValue() [1/2]

template<typename T >
Cogs::UniqueValue< T >::UniqueValue ( const T &  value)
inline

Definition at line 34 of file UniqueValue.h.

◆ UniqueValue() [2/2]

template<typename T >
Cogs::UniqueValue< T >::UniqueValue ( UniqueValue< T > &&  other)
inlinenoexcept

Definition at line 36 of file UniqueValue.h.

Member Function Documentation

◆ operator=()

template<typename T >
UniqueValue & Cogs::UniqueValue< T >::operator= ( UniqueValue< T > &&  other)
inlinenoexcept

Definition at line 39 of file UniqueValue.h.

Member Data Documentation

◆ value

template<typename T >
T Cogs::UniqueValue< T >::value = T(0)

Definition at line 41 of file UniqueValue.h.


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