diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2022-09-01 16:39:08 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-01 16:39:08 -0400 |
| commit | cc0b81350f6b681c794b4ac7c0f3b5fe73cb19eb (patch) | |
| tree | 7fd935748f4da5daa381f6cf4ef5d06c6adfc0a6 /source/core | |
| parent | f64d8748d4396a90d27adbdc17db3bac4a58d666 (diff) | |
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.
Diffstat (limited to 'source/core')
| -rw-r--r-- | source/core/slang-archive-file-system.cpp | 17 | ||||
| -rw-r--r-- | source/core/slang-archive-file-system.h | 22 | ||||
| -rw-r--r-- | source/core/slang-file-system.cpp | 64 | ||||
| -rw-r--r-- | source/core/slang-file-system.h | 24 | ||||
| -rw-r--r-- | source/core/slang-riff-file-system.cpp | 31 | ||||
| -rw-r--r-- | source/core/slang-riff-file-system.h | 16 | ||||
| -rw-r--r-- | source/core/slang-zip-file-system.cpp | 55 | ||||
| -rw-r--r-- | source/core/slang-zip-file-system.h | 2 |
8 files changed, 182 insertions, 49 deletions
diff --git a/source/core/slang-archive-file-system.cpp b/source/core/slang-archive-file-system.cpp index 0d998c849..98c995d23 100644 --- a/source/core/slang-archive-file-system.cpp +++ b/source/core/slang-archive-file-system.cpp @@ -11,6 +11,8 @@ #include "slang-riff-file-system.h" +#include "slang-destroyable.h" + // Compression systems #include "slang-deflate-compression-system.h" #include "slang-lz4-compression-system.h" @@ -130,9 +132,9 @@ SlangResult ImplicitDirectoryCollector::enumerate(FileSystemContentsCallBack cal return getDirectoryExists() ? SLANG_OK : SLANG_E_NOT_FOUND; } -SlangResult loadArchiveFileSystem(const void* data, size_t dataSizeInBytes, RefPtr<ArchiveFileSystem>& outFileSystem) +SlangResult loadArchiveFileSystem(const void* data, size_t dataSizeInBytes, ComPtr<ISlangFileSystemExt>& outFileSystem) { - RefPtr<ArchiveFileSystem> fileSystem; + ComPtr<ISlangMutableFileSystem> fileSystem; if (ZipFileSystem::isArchive(data, dataSizeInBytes)) { // It's a zip @@ -147,13 +149,20 @@ SlangResult loadArchiveFileSystem(const void* data, size_t dataSizeInBytes, RefP { return SLANG_FAIL; } - SLANG_RETURN_ON_FAIL(fileSystem->loadArchive(data, dataSizeInBytes)); + + auto archiveFileSystem = as<IArchiveFileSystem>(fileSystem); + if (!archiveFileSystem) + { + return SLANG_FAIL; + } + + SLANG_RETURN_ON_FAIL(archiveFileSystem->loadArchive(data, dataSizeInBytes)); outFileSystem = fileSystem; return SLANG_OK; } -SlangResult createArchiveFileSystem(SlangArchiveType type, RefPtr<ArchiveFileSystem>& outFileSystem) +SlangResult createArchiveFileSystem(SlangArchiveType type, ComPtr<ISlangMutableFileSystem>& outFileSystem) { switch (type) { diff --git a/source/core/slang-archive-file-system.h b/source/core/slang-archive-file-system.h index 6b4fe9e51..03571bdc1 100644 --- a/source/core/slang-archive-file-system.h +++ b/source/core/slang-archive-file-system.h @@ -8,20 +8,28 @@ #include "slang-compression-system.h" #include "slang-string-slice-pool.h" +#include "slang-com-object.h" namespace Slang { -class ArchiveFileSystem : public RefObject, public ISlangMutableFileSystem +class IArchiveFileSystem : public ISlangCastable { -public: + SLANG_COM_INTERFACE(0x5c565aac, 0xe834, 0x41fc, { 0x8b, 0xb, 0x7d, 0x4c, 0xf3, 0x8b, 0x89, 0x50 }); + /// Loads an archive. - virtual SlangResult loadArchive(const void* archive, size_t archiveSizeInBytes) = 0; + SLANG_NO_THROW virtual SlangResult SLANG_MCALL loadArchive(const void* archive, size_t archiveSizeInBytes) = 0; /// Get as an archive (that can be saved to disk) /// NOTE! If the blob is not owned, it's contents can be invalidated by any call to a method of the file system or loss of scope - virtual SlangResult storeArchive(bool blobOwnsContent, ISlangBlob** outBlob) = 0; + SLANG_NO_THROW virtual SlangResult SLANG_MCALL storeArchive(bool blobOwnsContent, ISlangBlob** outBlob) = 0; /// Set the compression - used for any subsequent items added - virtual void setCompressionStyle(const CompressionStyle& style) = 0; + SLANG_NO_THROW virtual void SLANG_MCALL setCompressionStyle(const CompressionStyle& style) = 0; +}; + +class ArchiveFileSystem : public ISlangMutableFileSystem, public ComBaseObject +{ +public: + }; /* Maps an UnownedStringSlice to an index. All substrings are held internally in a StringSlicePool, and so @@ -145,8 +153,8 @@ public: }; -SlangResult loadArchiveFileSystem(const void* data, size_t dataSizeInBytes, RefPtr<ArchiveFileSystem>& outFileSystem); -SlangResult createArchiveFileSystem(SlangArchiveType type, RefPtr<ArchiveFileSystem>& outFileSystem); +SlangResult loadArchiveFileSystem(const void* data, size_t dataSizeInBytes, ComPtr<ISlangFileSystemExt>& outFileSystem); +SlangResult createArchiveFileSystem(SlangArchiveType type, ComPtr<ISlangMutableFileSystem>& outFileSystem); } 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<ISlangFileSystem*>(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<ISlangFileSystemExt*>(this); } + return nullptr; +} - if (_canCast(FileSystemStyle::Ext, uuid)) +void* CacheFileSystem::getObject(const Guid& guid) +{ + if (guid == CacheFileSystem::getTypeGuid()) { - addReference(); - *outObject = static_cast<ISlangFileSystemExt*>(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<ISlangMutableFileSystem*>(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(); diff --git a/source/core/slang-file-system.h b/source/core/slang-file-system.h index c24d837b2..ea432ad30 100644 --- a/source/core/slang-file-system.h +++ b/source/core/slang-file-system.h @@ -30,6 +30,9 @@ public: SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE { return 1; } SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; } + // ISlangCastable + virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const Guid& guid) SLANG_OVERRIDE; + // ISlangFileSystem virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(char const* path, ISlangBlob** outBlob) SLANG_OVERRIDE; @@ -63,6 +66,7 @@ private: virtual ~OSFileSystem() {} ISlangUnknown* getInterface(const Guid& guid); + void* getObject(const Guid& guid); FileSystemStyle m_style; @@ -79,7 +83,7 @@ NOTE! That this behavior is the same as previously in that.... 1) calcRelativePath, just returns the path as processed by the Path:: methods 2) getUniqueIdentity behavior depends on the UniqueIdentityMode. */ -class CacheFileSystem: public ISlangFileSystemExt, public RefObject +class CacheFileSystem: public ISlangFileSystemExt, public ComBaseObject { public: SLANG_CLASS_GUID(0x2f4d1d03, 0xa0d1, 0x434b, { 0x87, 0x7a, 0x65, 0x5, 0xa4, 0xa0, 0x9a, 0x3b }) @@ -141,9 +145,10 @@ class CacheFileSystem: public ISlangFileSystemExt, public RefObject Dictionary<String, PathInfo*>& getUniqueMap() { return m_uniqueIdentityMap; } // ISlangUnknown - SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const& uuid, void** outObject) SLANG_OVERRIDE; - SLANG_REF_OBJECT_IUNKNOWN_ADD_REF - SLANG_REF_OBJECT_IUNKNOWN_RELEASE + SLANG_COM_BASE_IUNKNOWN_ALL + + // ISlangCastable + virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const Guid& guid) SLANG_OVERRIDE; // ISlangFileSystem virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(char const* path, ISlangBlob** outBlob) SLANG_OVERRIDE; @@ -177,6 +182,9 @@ class CacheFileSystem: public ISlangFileSystemExt, public RefObject protected: + void* getInterface(const Guid& guid); + void* getObject(const Guid& guid); + /// Given a path, works out a uniqueIdentity, based on the uniqueIdentityMode. /// outFileContents will be set if file had to be read to produce the uniqueIdentity (ie with Hash) /// If the file doesn't have to be read, then outFileContents will be nullptr, even if it is backed by a file. @@ -208,14 +216,17 @@ protected: OSPathKind m_osPathKind = OSPathKind::None; ///< OS path kind }; -class RelativeFileSystem : public ISlangMutableFileSystem, public RefObject +class RelativeFileSystem : public ISlangMutableFileSystem, public ComBaseObject { public: - SLANG_REF_OBJECT_IUNKNOWN_ALL + SLANG_COM_BASE_IUNKNOWN_ALL // ISlangFileSystem virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(char const* path, ISlangBlob** outBlob) SLANG_OVERRIDE; + // ISlangCastable + virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const Guid& guid) SLANG_OVERRIDE; + // ISlangFileSystemExt virtual SLANG_NO_THROW SlangResult SLANG_MCALL getFileUniqueIdentity(const char* path, ISlangBlob** outUniqueIdentity) SLANG_OVERRIDE; virtual SLANG_NO_THROW SlangResult SLANG_MCALL calcCombinedPath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut) SLANG_OVERRIDE; @@ -243,6 +254,7 @@ protected: SlangResult _getFixedPath(const char* path, String& outPath); ISlangUnknown* getInterface(const Guid& guid); + void* getObject(const Guid& guid); bool m_stripPath; diff --git a/source/core/slang-riff-file-system.cpp b/source/core/slang-riff-file-system.cpp index e916cf689..a8e0f2073 100644 --- a/source/core/slang-riff-file-system.cpp +++ b/source/core/slang-riff-file-system.cpp @@ -21,9 +21,36 @@ RiffFileSystem::RiffFileSystem(ICompressionSystem* compressionSystem): { } -ISlangMutableFileSystem* RiffFileSystem::getInterface(const Guid& guid) +void* RiffFileSystem::getInterface(const Guid& guid) { - return (guid == ISlangUnknown::getTypeGuid() || guid == ISlangFileSystem::getTypeGuid() || guid == ISlangFileSystemExt::getTypeGuid() || guid == ISlangMutableFileSystem::getTypeGuid()) ? static_cast<ISlangMutableFileSystem*>(this) : nullptr; + if ( guid == ISlangUnknown::getTypeGuid() || + guid == ISlangCastable::getTypeGuid() || + guid == ISlangFileSystem::getTypeGuid() || + guid == ISlangFileSystemExt::getTypeGuid() || + guid == ISlangMutableFileSystem::getTypeGuid()) + { + return static_cast<ISlangMutableFileSystem*>(this); + } + else if (guid == IArchiveFileSystem::getTypeGuid()) + { + return static_cast<IArchiveFileSystem*>(this); + } + return nullptr; +} + +void* RiffFileSystem::getObject(const Guid& guid) +{ + SLANG_UNUSED(guid); + return nullptr; +} + +void* RiffFileSystem::castAs(const Guid& guid) +{ + if (auto ptr = getInterface(guid)) + { + return ptr; + } + return getObject(guid); } SlangResult RiffFileSystem::_calcCanonicalPath(const char* path, StringBuilder& out) diff --git a/source/core/slang-riff-file-system.h b/source/core/slang-riff-file-system.h index 0f424b5ef..656b14325 100644 --- a/source/core/slang-riff-file-system.h +++ b/source/core/slang-riff-file-system.h @@ -33,12 +33,15 @@ struct RiffFileSystemBinary }; }; -class RiffFileSystem : public ArchiveFileSystem +class RiffFileSystem : public ISlangMutableFileSystem, public IArchiveFileSystem, public ComBaseObject { public: // ISlangUnknown - SLANG_REF_OBJECT_IUNKNOWN_ALL + SLANG_COM_BASE_IUNKNOWN_ALL + + // ISlangCastable + virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const Guid& guid) SLANG_OVERRIDE; // ISlangFileSystem virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(char const* path, ISlangBlob** outBlob) SLANG_OVERRIDE; @@ -59,9 +62,9 @@ public: virtual SLANG_NO_THROW SlangResult SLANG_MCALL createDirectory(const char* path) SLANG_OVERRIDE; // ArchiveFileSystem - virtual SlangResult loadArchive(const void* archive, size_t archiveSizeInBytes) SLANG_OVERRIDE; - virtual SlangResult storeArchive(bool blobOwnsContent, ISlangBlob** outBlob) SLANG_OVERRIDE; - virtual void setCompressionStyle(const CompressionStyle& style) SLANG_OVERRIDE { m_compressionStyle = style; } + virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadArchive(const void* archive, size_t archiveSizeInBytes) SLANG_OVERRIDE; + virtual SLANG_NO_THROW SlangResult SLANG_MCALL storeArchive(bool blobOwnsContent, ISlangBlob** outBlob) SLANG_OVERRIDE; + virtual SLANG_NO_THROW void SLANG_MCALL setCompressionStyle(const CompressionStyle& style) SLANG_OVERRIDE { m_compressionStyle = style; } RiffFileSystem(ICompressionSystem* compressionSystem); @@ -78,7 +81,8 @@ protected: ComPtr<ISlangBlob> m_contents; ///< Can be compressed or not }; - ISlangMutableFileSystem* getInterface(const Guid& guid); + void* getInterface(const Guid& guid); + void* getObject(const Guid& guid); SlangResult _calcCanonicalPath(const char* path, StringBuilder& out); Entry* _getEntryFromPath(const char* path, String* outPath = nullptr); diff --git a/source/core/slang-zip-file-system.cpp b/source/core/slang-zip-file-system.cpp index 636fd7d9e..d9c0a900e 100644 --- a/source/core/slang-zip-file-system.cpp +++ b/source/core/slang-zip-file-system.cpp @@ -19,12 +19,14 @@ namespace Slang { -class ZipFileSystemImpl : public ArchiveFileSystem +class ZipFileSystemImpl : public ISlangMutableFileSystem, public IArchiveFileSystem, public ComBaseObject { public: // ISlangUnknown - // override ref counting, as DefaultFileSystem is singleton - SLANG_REF_OBJECT_IUNKNOWN_ALL + SLANG_COM_BASE_IUNKNOWN_ALL + + // ISlangCastable + virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const Guid& guid) SLANG_OVERRIDE; // ISlangFileSystem virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadFile(char const* path, ISlangBlob** outBlob) SLANG_OVERRIDE; @@ -44,10 +46,10 @@ public: virtual SLANG_NO_THROW SlangResult SLANG_MCALL remove(const char* path) SLANG_OVERRIDE; virtual SLANG_NO_THROW SlangResult SLANG_MCALL createDirectory(const char* path) SLANG_OVERRIDE; - // ArchiveFileSystem - SlangResult loadArchive(const void* archive, size_t archiveSizeInBytes) SLANG_OVERRIDE; - virtual SlangResult storeArchive(bool blobOwnsContent, ISlangBlob** outBlob) SLANG_OVERRIDE; - virtual void setCompressionStyle(const CompressionStyle& style) SLANG_OVERRIDE; + // IArchiveFileSystem + virtual SLANG_NO_THROW SlangResult SLANG_MCALL loadArchive(const void* archive, size_t archiveSizeInBytes) SLANG_OVERRIDE; + virtual SLANG_NO_THROW SlangResult SLANG_MCALL storeArchive(bool blobOwnsContent, ISlangBlob** outBlob) SLANG_OVERRIDE; + virtual SLANG_NO_THROW void SLANG_MCALL setCompressionStyle(const CompressionStyle& style) SLANG_OVERRIDE; ZipFileSystemImpl(); ~ZipFileSystemImpl(); @@ -82,7 +84,8 @@ protected: /// Returns true if the named item is at the index UnownedStringSlice _getPathAtIndex(Index index); - ISlangMutableFileSystem* getInterface(const Guid& guid); + void* getInterface(const Guid& guid); + void* getObject(const Guid& guid); void _initReadWrite(mz_zip_archive& outWriter); @@ -101,9 +104,39 @@ protected: mz_zip_archive m_archive; }; -ISlangMutableFileSystem* ZipFileSystemImpl::getInterface(const Guid& guid) +void* ZipFileSystemImpl::getInterface(const Guid& guid) { - return (guid == ISlangUnknown::getTypeGuid() || guid == ISlangFileSystem::getTypeGuid() || guid == ISlangFileSystemExt::getTypeGuid() || guid == ISlangMutableFileSystem::getTypeGuid()) ? static_cast<ISlangMutableFileSystem*>(this) : nullptr; + if ( guid == ISlangUnknown::getTypeGuid() || + guid == ISlangCastable::getTypeGuid()) + { + return static_cast<ISlangMutableFileSystem*>(this); + } + else if (guid == ISlangFileSystem::getTypeGuid() || + guid == ISlangFileSystemExt::getTypeGuid() || + guid == ISlangMutableFileSystem::getTypeGuid()) + { + return static_cast<ISlangMutableFileSystem*>(this); + } + else if (guid == IArchiveFileSystem::getTypeGuid()) + { + return static_cast<IArchiveFileSystem*>(this); + } + return nullptr; +} + +void* ZipFileSystemImpl::getObject(const Guid& guid) +{ + SLANG_UNUSED(guid); + return nullptr; +} + +void* ZipFileSystemImpl::castAs(const Guid& guid) +{ + if (auto ptr = getInterface(guid)) + { + return ptr; + } + return getObject(guid); } // This is a very awkward hack to make it so we can get a read func, without having to implement all of the tracking etc. @@ -767,7 +800,7 @@ void ZipFileSystemImpl::setCompressionStyle(const CompressionStyle& style) } } -/* static */SlangResult ZipFileSystem::create(RefPtr<ArchiveFileSystem>& out) +/* static */SlangResult ZipFileSystem::create(ComPtr<ISlangMutableFileSystem>& out) { out = new ZipFileSystemImpl; return SLANG_OK; diff --git a/source/core/slang-zip-file-system.h b/source/core/slang-zip-file-system.h index 189060822..120d5003a 100644 --- a/source/core/slang-zip-file-system.h +++ b/source/core/slang-zip-file-system.h @@ -11,7 +11,7 @@ namespace Slang struct ZipFileSystem { /// Create an empty zip - static SlangResult create(RefPtr<ArchiveFileSystem>& out); + static SlangResult create(ComPtr<ISlangMutableFileSystem>& out); /// True if this appears to be a zip archive static bool isArchive(const void* data, size_t size); }; |
