Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::UniqueHandle< Definition > Struct Template Reference

Move only handle that has a special tag value flagging its invalid state. More...

#include <UniqueHandle.h>

Public Types

using Handle = typename Definition::Handle
 
using CloseReturnType = typename TypeTraits::ReturnType<decltype(Definition::releaseHandle)>::type
 

Public Member Functions

 UniqueHandle (const UniqueHandle &v)=delete
 
UniqueHandleoperator= (const UniqueHandle &other)=delete
 
 UniqueHandle (UniqueHandle &&v)
 
 UniqueHandle (const Handle &externalHandle)
 
CloseReturnType assign (UniqueHandle &&other)
 Move assigns another UniqueHandle to this object, eventually closing existing handle.
 
CloseReturnType assign (const Handle &externalHandle)
 Copy assigns another UniqueHandle to this object, eventually closing existing handle.
 
UniqueHandleoperator= (UniqueHandle &&other)
 
bool isValid () const
 Check if current handle is valid.
 
void detach ()
 Detaches (sets to invalid) current handle, without closing it.
 
CloseReturnType get (Handle &outHandle, CloseReturnType invalidReturnType) const
 Extracts the native operating system handle out.
 
CloseReturnType close ()
 Closes the handle by calling its OS specific close function.
 

Static Public Attributes

static constexpr Handle Invalid = Definition::Invalid
 

Protected Attributes

Handle handle = Invalid
 

Detailed Description

template<typename Definition>
struct SC::UniqueHandle< Definition >

Move only handle that has a special tag value flagging its invalid state.


Typically used to wrap Operating System specific handles.

Template Parameters
DefinitionA struct declaring handle type, release function and invalid handle value.

Example:

// ... definition in header
struct PosixFileDescriptorDefinition
{
using Handle = int; // fd
static Result releaseHandle(Handle& handle);
static constexpr Handle Invalid = -1; // invalid fd
};
using PosixFileDescriptor = UniqueHandle<PosixFileDescriptorDefinition>;
// ...declaration in .cpp file
Result PosixFileDescriptorDefinition::releaseHandle(Handle& handle)
{
if (::close(handle) != 0)
{
return Result::Error("releaseHandle - close failed");
}
return Result(true);
}
// ... usage
PosixFileDescriptor myDescriptor;
const int nativeFd = ::open(filePath.getNullTerminatedNative(), flags, access);
// Assign the native handle to UniqueHandle (will release the existing one, if any)
myDescriptor.assign(nativeFd);
// UniqueHandle can only be moved, but not copied
PosixFileDescriptor otherDescriptor = move(myDescriptor);
// PosixFileDescriptor otherDescriptor = myDescriptor; // <- Doesn't compile
// Explicitly close (or it will be automatically released on scope close / destructor)
otherDescriptor.close()
// If detach() is called, the handle will be made invalid without releasing it
otherDescriptor.detach()
// Check handle for validity
if(otherDescriptor.isValid())
{
// ... do something
}
constexpr T && move(T &value)
Converts an lvalue to an rvalue reference.
Definition Compiler.h:269
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:12
static constexpr Result Error(const char(&msg)[numChars])
Constructs an Error from a pointer to an ASCII string literal.
Definition Result.h:24
Move only handle that has a special tag value flagging its invalid state.
Definition UniqueHandle.h:67
CloseReturnType close()
Closes the handle by calling its OS specific close function.
Definition UniqueHandle.h:140

Member Function Documentation

◆ assign() [1/2]

template<typename Definition >
CloseReturnType SC::UniqueHandle< Definition >::assign ( const Handle & externalHandle)
inlinenodiscard

Copy assigns another UniqueHandle to this object, eventually closing existing handle.

Parameters
externalHandleThe handle to be copy assigned
Returns
Returns invalid result if close failed

◆ assign() [2/2]

template<typename Definition >
CloseReturnType SC::UniqueHandle< Definition >::assign ( UniqueHandle< Definition > && other)
inlinenodiscard

Move assigns another UniqueHandle to this object, eventually closing existing handle.

Parameters
otherThe handle to be move-assigned
Returns
Returns invalid result if close failed

◆ close()

template<typename Definition >
CloseReturnType SC::UniqueHandle< Definition >::close ( )
inlinenodiscard

Closes the handle by calling its OS specific close function.

Returns
true if the handle was closed correctly

◆ detach()

template<typename Definition >
void SC::UniqueHandle< Definition >::detach ( )
inline

Detaches (sets to invalid) current handle, without closing it.

◆ get()

template<typename Definition >
CloseReturnType SC::UniqueHandle< Definition >::get ( Handle & outHandle,
CloseReturnType invalidReturnType ) const
inlinenodiscard

Extracts the native operating system handle out.

Parameters
outHandleOutput native OS handle
invalidReturnTypethe value to be returned if this function fails
Returns
invalidReturnType if isValid() == false

◆ isValid()

template<typename Definition >
bool SC::UniqueHandle< Definition >::isValid ( ) const
inlinenodiscard

Check if current handle is valid.

Returns
true if handle is valid

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