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
-
Definition | A struct declaring handle type, release function and invalid handle value. |
Example:
... definition in header
struct SC_COMPILER_EXPORT FileDescriptorDefinition
{
using Handle = int;
static Result releaseHandle(Handle& handle);
static constexpr Handle Invalid = -1;
};
... derive from it
struct SC_COMPILER_EXPORT FileDescriptor : public UniqueHandle<detail::FileDescriptorDefinition>
{
using UniqueHandle::UniqueHandle;
...declaration in .cpp file
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
SC::Result SC::detail::FileDescriptorDefinition::releaseHandle(Handle& handle)
{
if (::close(handle) != 0)
{
return Result::Error(
"FileDescriptorDefinition::releaseHandle - close failed");
}
return Result(true);
}
...usage
#include <fcntl.h>
{
SC_TRY(filePath.path.assign(
"someFile.txt"));
const int flags = O_RDWR | O_CREAT | O_TRUNC;
const int access = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
const int nativeFd = ::open(filePath.path.buffer, flags, access);
SC_TRY(myDescriptor.assign(nativeFd));
SC_TRY(otherDescriptor.close());
otherDescriptor.detach();
if (otherDescriptor.isValid())
{
}
}