[+] Copyable IOHandles

This commit is contained in:
Reece Wilson 2023-12-18 05:32:38 +00:00
parent dc23f14192
commit 207b8f6f46
4 changed files with 101 additions and 84 deletions

View File

@ -15,6 +15,8 @@ namespace Aurora::IO
// Note: A handle is never disposable to prevent IO fd use after close
// You must ensure RAII and/or shared ownership release to dispose of the IO handle.
// This class is only intended to be an handle view; therefore, it is not possible to close the handle.
//
// You should use the AuIOHandle or AuIO::IOHandle aliases for construction
struct IIOHandle
{
struct HandleCreate
@ -101,38 +103,39 @@ namespace Aurora::IO
virtual bool InitFromMove(AuUInt64 uOSHandle) = 0;
virtual bool InitFromPair(AuUInt64 uOSReadHandle, AuUInt64 uOSWriteHandle) = 0;
virtual bool InitFromPair(AuOptional<AuUInt64> uOSReadHandle,
AuOptional<AuUInt64> uOSWriteHandle) = 0;
virtual bool InitFromPairMove(AuOptionalEx<AuUInt64> uOSReadHandle,
AuOptionalEx<AuUInt64> uOSWriteHandle) = 0;
virtual bool InitFromPairMove(AuOptional<AuUInt64> uOSReadHandle,
AuOptional<AuUInt64> uOSWriteHandle) = 0;
virtual bool InitFromStreamEnum(EStandardStream eStream) = 0;
virtual AuUInt64 GetOSHandle() = 0;
virtual AuUInt64 GetOSHandle() const = 0;
virtual AuOptionalEx<AuUInt64> GetOSHandleSafe() = 0;
virtual AuOptional<AuUInt64> GetOSHandleSafe() const = 0;
virtual AuUInt64 GetOSReadHandle() = 0;
virtual AuUInt64 GetOSReadHandle() const = 0;
virtual AuOptionalEx<AuUInt64> GetOSReadHandleSafe() = 0;
virtual AuOptional<AuUInt64> GetOSReadHandleSafe() const = 0;
virtual AuUInt64 GetOSWriteHandle() = 0;
virtual AuUInt64 GetOSWriteHandle() const = 0;
virtual AuOptionalEx<AuUInt64> GetOSWriteHandleSafe() = 0;
virtual AuOptional<AuUInt64> GetOSWriteHandleSafe() const = 0;
virtual bool IsValid() = 0;
virtual bool IsValid() const = 0;
virtual bool HasUniqueWriteHandle() = 0;
virtual bool HasUniqueWriteHandle() const = 0;
virtual bool IsAsync() = 0;
virtual bool IsAsync() const = 0;
virtual AuString GetPath() = 0;
virtual AuString GetPath() const = 0;
virtual bool IsFile() = 0;
virtual bool IsFile() const = 0;
virtual bool IsTTY() = 0;
virtual bool IsTTY() const = 0;
virtual bool IsPipe() = 0;
virtual bool IsPipe() const = 0;
virtual bool SectionLock(AuUInt64 uOffset,
AuUInt64 uLength,
@ -142,7 +145,7 @@ namespace Aurora::IO
AuUInt64 uLength) = 0;
};
AUKN_SHARED_SOO_NC(IOHandle, IIOHandle, 256);
AUKN_SHARED_SOO_CC(IOHandle, IIOHandle, 256);
AUKN_SYM bool IsHandleTTY(AuUInt uHandle);

View File

@ -31,12 +31,14 @@
#endif
#include <uuid.h>
#define AUKN_SHARED_API(name, type, ...) AU_SHARED_API_EX(AUKN_SYM, name, type, ## __VA_ARGS__)
#define AUKN_SHARED_SOO(name, type, size, ...) AUKN_SHARED_API(name, type, ## __VA_ARGS__) AUROXTL_INTERFACE_SOO_HDR_EX(AUKN_SYM, name, type, size)
#define AUKN_SHARED_SOO_NC(name, type, size, ...) AUKN_SHARED_API(name, type, ## __VA_ARGS__) AUROXTL_INTERFACE_SOO_HDR_EX_NC(AUKN_SYM, name, type, size)
#define AUKN_SHARED_SOO_NCM(name, type, size, ...) AUKN_SHARED_API(name, type, ## __VA_ARGS__) AUROXTL_INTERFACE_SOO_HDR_EX_NCM(AUKN_SYM, name, type, size)
#define AUKN_SHARED_SOO2(name, type, size, ctrs, ...) AUKN_SHARED_API(name, type, ## __VA_ARGS__) AUROXTL_INTERFACE_SOO_HDR_EX(AUKN_SYM, name, type, size, AU_STRIP_BRACKETS(ctrs))
#define AUKN_SHARED_SOO2_NC(name, type, size, ctrs, ...) AUKN_SHARED_API(name, type, ## __VA_ARGS__) AUROXTL_INTERFACE_SOO_HDR_EX_NC(AUKN_SYM, name, type, size, AU_STRIP_BRACKETS(ctrs))
#define AUKN_SHARED_API(name, type, ...) AU_SHARED_API_EX(AUKN_SYM, name, type, ## __VA_ARGS__)
#define AUKN_SHARED_SOO(name, type, size, ...) AUKN_SHARED_API(name, type, ## __VA_ARGS__) AUROXTL_INTERFACE_SOO_HDR_EX(AUKN_SYM, name, type, size)
#define AUKN_SHARED_SOO_NC(name, type, size, ...) AUKN_SHARED_API(name, type, ## __VA_ARGS__) AUROXTL_INTERFACE_SOO_HDR_EX_NC(AUKN_SYM, name, type, size)
#define AUKN_SHARED_SOO_CC(name, type, size, ...) AUKN_SHARED_API(name, type, ## __VA_ARGS__) AUROXTL_INTERFACE_SOO_HDR_EX_CC(AUKN_SYM, name, type, size)
#define AUKN_SHARED_SOO_CCNM(name, type, size, ...) AUKN_SHARED_API(name, type, ## __VA_ARGS__) AUROXTL_INTERFACE_SOO_HDR_EX_CCNM(AUKN_SYM, name, type, size)
#define AUKN_SHARED_SOO_NCM(name, type, size, ...) AUKN_SHARED_API(name, type, ## __VA_ARGS__) AUROXTL_INTERFACE_SOO_HDR_EX_NCM(AUKN_SYM, name, type, size)
#define AUKN_SHARED_SOO2(name, type, size, ctrs, ...) AUKN_SHARED_API(name, type, ## __VA_ARGS__) AUROXTL_INTERFACE_SOO_HDR_EX(AUKN_SYM, name, type, size, AU_STRIP_BRACKETS(ctrs))
#define AUKN_SHARED_SOO2_NC(name, type, size, ctrs, ...) AUKN_SHARED_API(name, type, ## __VA_ARGS__) AUROXTL_INTERFACE_SOO_HDR_EX_NC(AUKN_SYM, name, type, size, AU_STRIP_BRACKETS(ctrs))
#define AUKN_SHARED_SOO2_NCM(name, type, size, ctrs, ...) AUKN_SHARED_API(name, type, ## __VA_ARGS__) AUROXTL_INTERFACE_SOO_HDR_EX_NCM(AUKN_SYM, name, type, size, AU_STRIP_BRACKETS(ctrs))
#if defined(_AURORA_RUNTIME_BUILD_API_INTERFACES)

View File

@ -13,8 +13,8 @@ namespace Aurora::IO
{
AFileHandle::~AFileHandle()
{
if (this->uOSWriteHandle.HasValue() && this->uOSReadHandle.HasValue() &&
this->uOSReadHandle.Value() == this->uOSWriteHandle.Value())
if (this->uOSWriteHandle.has_value() && this->uOSReadHandle.has_value() &&
this->uOSReadHandle.value() == this->uOSWriteHandle.value())
{
this->CloseHandle(this->uOSReadHandle.value());
AuResetMember(this->uOSReadHandle);
@ -82,39 +82,45 @@ namespace Aurora::IO
return true;
}
bool AFileHandle::InitFromPair(AuUInt64 uOSReadHandle,
AuUInt64 uOSWriteHandle)
bool AFileHandle::InitFromPair(AuOptional<AuUInt64> optOSReadHandle,
AuOptional<AuUInt64> optOSWriteHandle)
{
if (this->IsValid())
{
return false;
}
if (auto uOSReadHandle2 = this->DupHandle(uOSReadHandle, false))
if (optOSReadHandle)
{
this->uOSReadHandle = uOSReadHandle2;
}
else
{
return {};
if (auto uOSReadHandle2 = this->DupHandle(optOSReadHandle.value(), false))
{
this->uOSReadHandle = uOSReadHandle2;
}
else
{
return {};
}
}
if (auto uOSWriteHandle2 = this->DupHandle(uOSWriteHandle, true))
if (optOSWriteHandle)
{
this->uOSWriteHandle = uOSWriteHandle2;
}
else
{
this->CloseHandle(this->uOSReadHandle);
AuResetMember(this->uOSReadHandle);
return {};
if (auto uOSWriteHandle2 = this->DupHandle(optOSWriteHandle.value(), true))
{
this->uOSWriteHandle = uOSWriteHandle2;
}
else
{
this->CloseHandle(this->uOSReadHandle.value());
AuResetMember(this->uOSReadHandle);
return {};
}
}
return true;
}
bool AFileHandle::InitFromPairMove(AuOptionalEx<AuUInt64> uOSReadHandle,
AuOptionalEx<AuUInt64> uOSWriteHandle)
bool AFileHandle::InitFromPairMove(AuOptional<AuUInt64> uOSReadHandle,
AuOptional<AuUInt64> uOSWriteHandle)
{
if (this->IsValid())
{
@ -144,7 +150,7 @@ namespace Aurora::IO
}
}
bool AFileHandle::IsFile()
bool AFileHandle::IsFile() const
{
bool bIsFile {};
@ -167,7 +173,7 @@ namespace Aurora::IO
return bIsFile;
}
bool AFileHandle::IsTTY()
bool AFileHandle::IsTTY() const
{
bool bIsTTY {};
@ -190,7 +196,7 @@ namespace Aurora::IO
return bIsTTY;
}
bool AFileHandle::IsPipe()
bool AFileHandle::IsPipe() const
{
bool bIsPipe {};
@ -213,7 +219,7 @@ namespace Aurora::IO
return bIsPipe;
}
AuOptionalEx<AuUInt64> AFileHandle::GetOSHandleSafe()
AuOptional<AuUInt64> AFileHandle::GetOSHandleSafe() const
{
if (auto write = this->uOSWriteHandle)
{
@ -228,48 +234,48 @@ namespace Aurora::IO
return {};
}
AuUInt64 AFileHandle::GetOSHandle()
AuUInt64 AFileHandle::GetOSHandle() const
{
return this->uOSReadHandle.ValueOr(this->uOSWriteHandle.Value());
return this->uOSReadHandle.value_or(this->uOSWriteHandle.value());
}
AuUInt64 AFileHandle::GetOSReadHandle()
AuUInt64 AFileHandle::GetOSReadHandle() const
{
return this->uOSReadHandle.value();
}
AuOptionalEx<AuUInt64> AFileHandle::GetOSReadHandleSafe()
AuOptional<AuUInt64> AFileHandle::GetOSReadHandleSafe() const
{
return this->uOSReadHandle;
}
AuUInt64 AFileHandle::GetOSWriteHandle()
AuUInt64 AFileHandle::GetOSWriteHandle() const
{
return this->uOSWriteHandle.Value();
return this->uOSWriteHandle.value();
}
AuOptionalEx<AuUInt64> AFileHandle::GetOSWriteHandleSafe()
AuOptional<AuUInt64> AFileHandle::GetOSWriteHandleSafe() const
{
return this->uOSWriteHandle;
}
bool AFileHandle::IsValid()
bool AFileHandle::IsValid() const
{
return this->uOSReadHandle.HasValue() ||
this->uOSWriteHandle.HasValue();
return this->uOSReadHandle ||
this->uOSWriteHandle;
}
bool AFileHandle::HasUniqueWriteHandle()
bool AFileHandle::HasUniqueWriteHandle() const
{
return this->uOSWriteHandle.HasValue();
return bool(this->uOSWriteHandle);
}
bool AFileHandle::IsAsync()
bool AFileHandle::IsAsync() const
{
return this->bIsAsync;
}
AuString AFileHandle::GetPath()
AuString AFileHandle::GetPath() const
{
return this->path;
}
@ -297,5 +303,11 @@ namespace Aurora::IO
AuSafeDelete<AFileHandle *>(pIOHandle);
}
AUKN_SYM void IOHandleCopy(IIOHandle *dest, const IIOHandle *source)
{
AuResetMember(*AuStaticCast<AFileHandle>(dest));
dest->InitFromPair(source->GetOSReadHandleSafe(), source->GetOSWriteHandleSafe());
}
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, IOHandle, AFileHandle)
}

View File

@ -16,7 +16,7 @@ namespace Aurora::IO
{
struct AFileHandle : IIOHandle
{
virtual ~AFileHandle();
~AFileHandle();
bool InitFromHandle(AuSPtr<IIOHandle> pHandle) override;
@ -24,39 +24,39 @@ namespace Aurora::IO
bool InitFromMove(AuUInt64 uOSHandle) override;
bool InitFromPair(AuUInt64 uOSReadHandle,
AuUInt64 uOSWriteHandle) override;
bool InitFromPair(AuOptional<AuUInt64> uOSReadHandle,
AuOptional<AuUInt64> uOSWriteHandle) override;
bool InitFromPairMove(AuOptionalEx<AuUInt64> uOSReadHandle,
AuOptionalEx<AuUInt64> uOSWriteHandle) override;
bool InitFromPairMove(AuOptional<AuUInt64> uOSReadHandle,
AuOptional<AuUInt64> uOSWriteHandle) override;
bool InitFromStreamEnum(EStandardStream eStream) override;
bool InitFromPath(HandleCreate create) override;
AuUInt64 GetOSHandle() override;
AuUInt64 GetOSHandle() const override;
AuOptionalEx<AuUInt64> GetOSHandleSafe() override;
AuOptional<AuUInt64> GetOSHandleSafe() const override;
AuUInt64 GetOSReadHandle() override;
AuUInt64 GetOSReadHandle() const override;
AuOptionalEx<AuUInt64> GetOSReadHandleSafe() override;
AuOptional<AuUInt64> GetOSReadHandleSafe() const override;
AuUInt64 GetOSWriteHandle() override;
AuUInt64 GetOSWriteHandle() const override;
AuOptionalEx<AuUInt64> GetOSWriteHandleSafe() override;
AuOptional<AuUInt64> GetOSWriteHandleSafe() const override;
bool IsValid() override;
bool IsValid() const override;
bool HasUniqueWriteHandle() override;
bool HasUniqueWriteHandle()const override;
bool IsAsync() override;
bool IsAsync() const override;
AuString GetPath() override;
AuString GetPath() const override;
bool IsFile() override;
bool IsTTY() override;
bool IsPipe() override;
bool IsFile() const override;
bool IsTTY() const override;
bool IsPipe() const override;
void InitStdIn(bool bSharing = false);
void InitStdOut(bool bError = false, bool bSharing = false);
@ -68,16 +68,16 @@ namespace Aurora::IO
bool SectionUnlock(AuUInt64 uOffset,
AuUInt64 uLength) override;
AuOptionalEx<AuUInt64> uOSWriteHandle;
AuOptionalEx<AuUInt64> uOSReadHandle;
AuOptional<AuUInt64> uOSWriteHandle;
AuOptional<AuUInt64> uOSReadHandle;
AuSPtr<IIOHandle> pThat;
bool bIsAsync {};
AuString path;
IPC::IPCPipeImpl *pIPCPipe {};
bool bDirectIO {};
AuOptionalEx<bool> optIsFile;
AuOptionalEx<bool> optIsPipe;
AuOptionalEx<bool> optIsTTY;
mutable AuOptional<bool> optIsFile;
mutable AuOptional<bool> optIsPipe;
mutable AuOptional<bool> optIsTTY;
protected:
// Implement me: