Sane C++ Libraries
C++ Platform Abstraction Libraries
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. More...
 
CloseReturnType assign (const Handle &externalHandle)
 Copy assigns another UniqueHandle to this object, eventually closing existing handle. More...
 
UniqueHandleoperator= (UniqueHandle &&other)
 
bool isValid () const
 Check if current handle is valid. More...
 
void detach ()
 Detaches (sets to invalid) current handle, without closing it. More...
 
CloseReturnType get (Handle &outHandle, CloseReturnType invalidReturnType) const
 Extracts the native operating system handle out. More...
 
CloseReturnType close ()
 Closes the handle by calling its OS specific close function. More...
 

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
static constexpr Result Error(const char(&msg)[numChars])
Constructs an Error from a pointer to an ASCII string literal.
Definition: Result.h:23
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)
inline

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)
inline

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 ( )
inline

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
inline

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
inline

Check if current handle is valid.

Returns
true if handle is valid

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