/*** Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: AuIOHandle.cpp Date: 2023-7-28 Author: Reece ***/ #include #include "AuIOHandle.hpp" namespace Aurora::IO { AFileHandle::~AFileHandle() { if (this->uOSWriteHandle.HasValue() && this->uOSReadHandle.HasValue() && this->uOSReadHandle.Value() == this->uOSWriteHandle.Value()) { this->CloseHandle(this->uOSReadHandle.value()); AuResetMember(this->uOSReadHandle); AuResetMember(this->uOSWriteHandle); } if (this->uOSReadHandle) { this->CloseHandle(this->uOSReadHandle.value()); AuResetMember(this->uOSReadHandle); } if (this->uOSWriteHandle) { this->CloseHandle(this->uOSWriteHandle.value()); AuResetMember(this->uOSWriteHandle); } } bool AFileHandle::InitFromHandle(AuSPtr pHandle) { auto pSrc = AuStaticCast(pHandle); auto pDest = this; if (this->IsValid()) { return false; } pDest->pThat = pHandle; pDest->uOSReadHandle = pSrc->uOSReadHandle; pDest->uOSWriteHandle = pSrc->uOSWriteHandle; pDest->bIsAsync = pSrc->bIsAsync; return true; } bool AFileHandle::InitFromCopy(AuUInt64 uOSHandle) { if (this->IsValid()) { return false; } if (auto uOSWriteHandle = this->DupHandle(uOSHandle, true)) { this->uOSReadHandle = uOSWriteHandle; this->uOSWriteHandle = uOSWriteHandle; return true; } else { return {}; } } bool AFileHandle::InitFromMove(AuUInt64 uOSHandle) { if (this->IsValid()) { return false; } this->uOSReadHandle = uOSHandle; this->uOSWriteHandle = uOSHandle; return true; } bool AFileHandle::InitFromPair(AuUInt64 uOSReadHandle, AuUInt64 uOSWriteHandle) { if (this->IsValid()) { return false; } if (auto uOSReadHandle2 = this->DupHandle(uOSReadHandle, false)) { this->uOSReadHandle = uOSReadHandle2; } else { return {}; } if (auto uOSWriteHandle2 = this->DupHandle(uOSWriteHandle, true)) { this->uOSWriteHandle = uOSWriteHandle2; } else { this->CloseHandle(this->uOSReadHandle); AuResetMember(this->uOSReadHandle); return {}; } return true; } bool AFileHandle::InitFromPairMove(AuOptionalEx uOSReadHandle, AuOptionalEx uOSWriteHandle) { if (this->IsValid()) { return false; } this->uOSReadHandle = uOSReadHandle; this->uOSWriteHandle = uOSWriteHandle; return true; } AuUInt64 AFileHandle::GetOSHandle() { return this->uOSReadHandle.ValueOr(this->uOSWriteHandle.Value()); } AuUInt64 AFileHandle::GetOSReadHandle() { return this->uOSReadHandle.value(); } AuOptionalEx AFileHandle::GetOSReadHandleSafe() { return this->uOSReadHandle; } AuUInt64 AFileHandle::GetOSWriteHandle() { return this->uOSWriteHandle.Value(); } AuOptionalEx AFileHandle::GetOSWriteHandleSafe() { return this->uOSWriteHandle; } bool AFileHandle::IsValid() { return this->uOSReadHandle.HasValue() || this->uOSWriteHandle.HasValue(); } bool AFileHandle::HasUniqueWriteHandle() { return this->uOSWriteHandle.HasValue(); } bool AFileHandle::IsAsync() { return this->bIsAsync; } AuString AFileHandle::GetPath() { return this->path; } }