From cc0b81350f6b681c794b4ac7c0f3b5fe73cb19eb Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Thu, 1 Sep 2022 16:39:08 -0400 Subject: Make ISlangFileSystem derive from ICastable (#2386) * #include an absolute path didn't work - because paths were taken to always be relative. * Make ISlangFileSystem derive from ICastable. * Make ArchiveFileSystem into an interface Make file systems atomically reference counted. * Small fix. * Some small fixes to work around issues of ICastable on ISlangFileSystem * Use ISlangFileSystem derived type instead of IArchiveFileSystem. Can always get other interface with castAs. * Some small fixes around change of interface returned from archive type functions. * Remove CacheFileSystem member from linkage. Can access easily from m_fileSystemExt if necessary with as cast. * Fix RiffFileSystem casting issue. * Add a check around CacheFileSystem. --- source/core/slang-file-system.cpp | 64 +++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 12 deletions(-) (limited to 'source/core/slang-file-system.cpp') diff --git a/source/core/slang-file-system.cpp b/source/core/slang-file-system.cpp index 208c4d834..283dab712 100644 --- a/source/core/slang-file-system.cpp +++ b/source/core/slang-file-system.cpp @@ -7,14 +7,14 @@ namespace Slang { -// Allocate static const storage for the various interface IDs that the Slang API needs to expose - SLANG_FORCE_INLINE static SlangResult _checkExt(FileSystemStyle style) { return Index(style) >= Index(FileSystemStyle::Ext) ? SLANG_OK : SLANG_E_NOT_IMPLEMENTED; } SLANG_FORCE_INLINE static SlangResult _checkMutable(FileSystemStyle style) { return Index(style) >= Index(FileSystemStyle::Mutable) ? SLANG_OK : SLANG_E_NOT_IMPLEMENTED; } SLANG_FORCE_INLINE static bool _canCast(FileSystemStyle style, const Guid& guid) { - if (guid == ISlangUnknown::getTypeGuid() || guid == ISlangFileSystem::getTypeGuid()) + if (guid == ISlangUnknown::getTypeGuid() || + guid == ISlangCastable::getTypeGuid() || + guid == ISlangFileSystem::getTypeGuid()) { return true; } @@ -81,11 +81,26 @@ static SlangResult _calcCombinedPath(SlangPathType fromPathType, const char* fro /* static */OSFileSystem OSFileSystem::g_ext(FileSystemStyle::Ext); /* static */OSFileSystem OSFileSystem::g_mutable(FileSystemStyle::Mutable); +void* OSFileSystem::castAs(const Guid& guid) +{ + if (auto ptr = getInterface(guid)) + { + return ptr; + } + return getObject(guid); +} + ISlangUnknown* OSFileSystem::getInterface(const Guid& guid) { return _canCast(m_style, guid) ? static_cast(this) : nullptr; } +void* OSFileSystem::getObject(const Guid& guid) +{ + SLANG_UNUSED(guid); + return nullptr; +} + static String _fixPathDelimiters(const char* pathIn) { #if SLANG_WINDOWS_FAMILY @@ -245,21 +260,31 @@ SlangResult OSFileSystem::createDirectory(const char* path) } } -SLANG_NO_THROW SlangResult SLANG_MCALL CacheFileSystem::queryInterface(SlangUUID const& uuid, void** outObject) +void* CacheFileSystem::castAs(const Guid& guid) +{ + if (auto ptr = getInterface(guid)) + { + return ptr; + } + return getObject(guid); +} + +void* CacheFileSystem::getInterface(const Guid& guid) { - if (uuid == CacheFileSystem::getTypeGuid()) + if (_canCast(FileSystemStyle::Ext, guid)) { - *outObject = this; - return SLANG_OK; + return static_cast(this); } + return nullptr; +} - if (_canCast(FileSystemStyle::Ext, uuid)) +void* CacheFileSystem::getObject(const Guid& guid) +{ + if (guid == CacheFileSystem::getTypeGuid()) { - addReference(); - *outObject = static_cast(this); - return SLANG_OK; + return this; } - return SLANG_E_NO_INTERFACE; + return nullptr; } CacheFileSystem::CacheFileSystem(ISlangFileSystem* fileSystem, UniqueIdentityMode uniqueIdentityMode, PathStyle pathStyle) @@ -770,6 +795,21 @@ ISlangUnknown* RelativeFileSystem::getInterface(const Guid& guid) return _canCast(m_style, guid) ? static_cast(this) : nullptr; } +void* RelativeFileSystem::getObject(const Guid& guid) +{ + SLANG_UNUSED(guid); + return nullptr; +} + +void* RelativeFileSystem::castAs(const Guid& guid) +{ + if (auto ptr = getInterface(guid)) + { + return ptr; + } + return getObject(guid); +} + SlangResult RelativeFileSystem::_calcCombinedPathInner(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** outPath) { ISlangFileSystemExt* fileSystem = _getExt(); -- cgit v1.2.3