diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2022-08-10 10:04:06 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-10 10:04:06 -0400 |
| commit | 1378fffd9da094beb41b2db89b96f556c23ab6cb (patch) | |
| tree | e0558c031bd4b1a013dc56b75c4caa3ff5442021 /source/core/slang-blob.h | |
| parent | 89083c4b50af8e48e70b25b63cc62aca21ab706c (diff) | |
Artifact and ICastable (#2351)
* #include an absolute path didn't work - because paths were taken to always be relative.
* WIP with hierarchical enums.
* Some small fixes and improvements around artifact desc related types.
* Improvements around hierarchical enum.
* Fixes to get Artifact types refactor to be able to execute tests.
* Attempt to better categorize PTX.
* Work around for potentially unused function warning.
* Typo fix.
* Simplify Artifact header.
* Small improvements around Artifact kind/payload/style.
* Added IDestroyable/ICastable
* Add IArtifactList.
* First impl of IArtifactUtil.
* Use the ICastable interface for IArtifactRepresentation.
* Added IArtifactRepresentation & IArtifactAssociated.
* Add SLANG_OVERRIDE to avoid gcc/clang warning.
* Fix calling convention issue on win32.
* Fix missing SLANG_OVERRIDE.
* First attempt at file abstraction around Artifact.
* Added creation of lock file.
* Move functionality for determining file paths to the IArtifactUtil.
Add casting to ICastable.
* Added some casting/finding mechanisms.
* Simplify IArtifact interface, and use Items for file reps.
* Fix problem with libraries on DXIL.
* Split out ArtifactRepresentation.
* Move ArtifactDesc functionality to ArtifactDescUtil. ArtifactInfoUtil becomes ArtifactDescUtil.
* Split implementations from the interfaces for Artifact.
* Use TypeTextUtil for target name outputting.
* Add artifact impls.
* Add ICastableList
* Added UnknownCastableAdapter
* Make ISlangSharedLibrary derive from ICastable, and remain backwards compatible with slang-llvm.
* Refactor Representation on Artifact.
* Make our ISlangBlobs also derive from ICastable.
Make ISlangBlob atomic ref counted.
* Fix typo.
Diffstat (limited to 'source/core/slang-blob.h')
| -rw-r--r-- | source/core/slang-blob.h | 98 |
1 files changed, 63 insertions, 35 deletions
diff --git a/source/core/slang-blob.h b/source/core/slang-blob.h index 58984471f..b1384bb2c 100644 --- a/source/core/slang-blob.h +++ b/source/core/slang-blob.h @@ -11,22 +11,31 @@ #include "../../slang-com-helper.h" #include "../../slang-com-ptr.h" +#include "../core/slang-com-object.h" + namespace Slang { /** Base class for simple blobs. */ -class BlobBase : public ISlangBlob, public RefObject +class BlobBase : public ISlangBlob, public ICastable, public ComBaseObject { public: // ISlangUnknown - SLANG_REF_OBJECT_IUNKNOWN_ALL + SLANG_COM_BASE_IUNKNOWN_ALL + + // ICastable + virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const SlangUUID& guid) SLANG_OVERRIDE; protected: ISlangUnknown* getInterface(const Guid& guid); + void* getObject(const Guid& guid); }; /** A blob that uses a `String` for its storage. NOTE! Returns length *WITHOUT* terminating 0, even though there is one. + +NOTE! Whilst BobBase is atomic ref counted, the contained string *is not*. +There is a reasonable argument that StringBlob should contain it's own copy of the string contents. */ class StringBlob : public BlobBase { @@ -35,14 +44,16 @@ public: SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE { return m_string.getBuffer(); } SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_string.getLength(); } - /// Get the contained string - SLANG_FORCE_INLINE const String& getString() const { return m_string; } + static ComPtr<ISlangBlob> create(const String& in) { return ComPtr<ISlangBlob>(new StringBlob(in)); } +protected: explicit StringBlob(String const& string) : m_string(string) {} -protected: + /// Get the contained string + SLANG_FORCE_INLINE const String& getString() const { return m_string; } + String m_string; }; @@ -56,21 +67,20 @@ public: SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE { return m_data.getBuffer(); } SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_data.getCount(); } - ListBlob() {} + static ComPtr<ISlangBlob> create(const List<uint8_t>& data) { return ComPtr<ISlangBlob>(new ListBlob(data)); } + + static ComPtr<ISlangBlob> moveCreate(List<uint8_t>& data) { return ComPtr<ISlangBlob>(new ListBlob(_Move(data))); } - ListBlob(const List<uint8_t>& data): m_data(data) {} +protected: + explicit ListBlob(const List<uint8_t>& data) : m_data(data) {} // Move ctor - ListBlob(List<uint8_t>&& data): m_data(data) {} + explicit ListBlob(List<uint8_t>&& data) : m_data(data) {} - static RefPtr<ListBlob> moveCreate(List<uint8_t>& data) { return new ListBlob(_Move(data)); } + void operator=(const ThisType& rhs) = delete; List<uint8_t> m_data; - -protected: - void operator=(const ThisType& rhs) = delete; }; - class ScopedAllocation { public: @@ -172,29 +182,35 @@ public: SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE { return m_data.getData(); } SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_data.getSizeInBytes(); } - // Ctor - // NOTE! Takes a copy of the input data - RawBlob(const void* data, size_t size) - { - memcpy(m_data.allocate(size), data, size); - } - /// Moves ownership of data and dataCount to the blob /// data must be a pointer returned by ::malloc. - static RefPtr<RawBlob> moveCreate(uint8_t* data, size_t dataCount) + static ComPtr<ISlangBlob> moveCreate(uint8_t* data, size_t dataCount) { RawBlob* blob = new RawBlob; blob->m_data.attach(data, dataCount); - return blob; + return ComPtr<ISlangBlob>(blob); } - static RefPtr<RawBlob> moveCreate(ScopedAllocation& alloc) + static ComPtr<ISlangBlob> moveCreate(ScopedAllocation& alloc) { RawBlob* blob = new RawBlob; blob->m_data.swap(alloc); - return blob; + return ComPtr<ISlangBlob>(blob); + } + + /// Create a blob that will retain (a copy of) raw data. + static inline ComPtr<ISlangBlob> create(void const* inData, size_t size) + { + return ComPtr<ISlangBlob>(new RawBlob(inData, size)); } protected: + // Ctor + // NOTE! Takes a copy of the input data + RawBlob(const void* data, size_t size) + { + memcpy(m_data.allocate(size), data, size); + } + RawBlob() = default; ScopedAllocation m_data; @@ -208,14 +224,19 @@ public: SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE { return m_data; } SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_dataSizeInBytes; } + static inline ComPtr<ISlangBlob> create(void const* inData, size_t size) + { + return ComPtr<ISlangBlob>(new UnownedRawBlob(inData, size)); + } + +protected: // Ctor - UnownedRawBlob(const void* data, size_t size): + UnownedRawBlob(const void* data, size_t size) : m_data(data), m_dataSizeInBytes(size) { } -protected: UnownedRawBlob() = default; const void* m_data; @@ -226,7 +247,7 @@ protected: The memory it references is *not* owned by the blob. This is useful when a Blob is useful to represent some global immutable chunk of memory. */ -class StaticBlob : public ISlangBlob +class StaticBlob : public ISlangBlob, public ICastable { public: @@ -235,6 +256,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; } + // ICastable + virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const SlangUUID& guid) SLANG_OVERRIDE; + // ISlangBlob SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE { return m_data; } SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_dataCount; } @@ -246,17 +270,13 @@ public: } protected: + ISlangUnknown* getInterface(const Guid& guid); + void* getObject(const Guid& guid); + const void* m_data; size_t m_dataCount; }; -/// Create a blob that will retain (a copy of) raw data. -/// -inline ComPtr<ISlangBlob> createRawBlob(void const* inData, size_t size) -{ - return ComPtr<ISlangBlob>(new RawBlob(inData, size)); -} - class ScopeRefObjectBlob : public BlobBase { public: @@ -264,14 +284,22 @@ public: SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE { return m_blob->getBufferPointer(); } SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_blob->getBufferSize(); } + static inline ComPtr<ISlangBlob> create(ISlangBlob* blob, RefObject* scope) + { + return ComPtr<ISlangBlob>(new ScopeRefObjectBlob(blob, scope)); + } + +protected: + // Ctor + ScopeRefObjectBlob(ISlangBlob* blob, RefObject* scope) : m_blob(blob), m_scope(scope) { } -protected: + RefPtr<RefObject> m_scope; ComPtr<ISlangBlob> m_blob; }; |
