diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2018-06-22 13:09:01 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-06-22 13:09:01 -0400 |
| commit | d0c9571be3a2167a9f019310aca8f7cd326972c0 (patch) | |
| tree | 52aa2f14ec2c9d8d42bc6fcbd381ed9799c19533 /source/slang/slang.cpp | |
| parent | e66d66b88e1c6ef8499708952fcbe3ba873f6e4c (diff) | |
Expose macros/functionality for defining interfaces (#604)
* Added Result definitions to the slang.h
* Removed slang-result.h and added slang-com-helper.h
* Move slang-com-ptr.h to be publically available.
* Add SLANG_IUNKNOWN macros to simplify implementing interfaces.
Use the SLANG_IUNKNOWN macros to in slang.c
* Removed slang-defines.h added outstanding defines to slang.h
Diffstat (limited to 'source/slang/slang.cpp')
| -rw-r--r-- | source/slang/slang.cpp | 103 |
1 files changed, 30 insertions, 73 deletions
diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 24dff88a8..ced2beb14 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -1,4 +1,4 @@ -#include "../../slang.h" +#include "../../slang.h" #include "../core/slang-io.h" #include "parameter-binding.h" @@ -211,46 +211,20 @@ static const Guid IID_ISlangBlob = SLANG_UUID_ISlangBlob; class BlobBase : public ISlangBlob { public: - BlobBase() {} - virtual ~BlobBase() {} - - uint32_t referenceCount = 0; + uint32_t m_refCount = 0; // ISlangUnknown + SLANG_IUNKNOWN_ALL - SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const& uuid, void** outObject) SLANG_OVERRIDE - { - if(uuid == IID_IComUnknown) - { - *(ISlangUnknown**)outObject = this; - addRef(); - return SLANG_OK; - } - else if(uuid == IID_ISlangBlob) - { - *(ISlangBlob**)outObject = this; - addRef(); - return SLANG_OK; - } - - return SLANG_FAIL; - } - - SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE - { - referenceCount++; - return 0; - } + /// Need virtual dtor, because BlobBase is derived from and release impl used is the one in the base class (that doesn't know the derived type) + /// Alternatively could be implemented by always using SLANG_IUNKNOWN_RELEASE in derived types - this would make derived types slightly smaller/faster + virtual ~BlobBase() {} - SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE +protected: + SLANG_FORCE_INLINE ISlangUnknown* getInterface(const Guid& guid) { - if(--referenceCount == 0) - { - delete this; - } - return 0; + return (guid == IID_ISlangUnknown || guid == IID_ISlangBlob) ? static_cast<ISlangBlob*>(this) : nullptr; } - }; /** A blob that uses a `String` for its storage. @@ -258,23 +232,15 @@ public: class StringBlob : public BlobBase { public: - String string; - + // ISlangBlob + SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE { return m_string.Buffer(); } + SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_string.Length(); } + explicit StringBlob(String const& string) - : string(string) + : m_string(string) {} - - // ISlangBlob - - SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE - { - return string.Buffer(); - } - - SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE - { - return string.Length(); - } +protected: + String m_string; }; ComPtr<ISlangBlob> createStringBlob(String const& string) @@ -287,41 +253,32 @@ ComPtr<ISlangBlob> createStringBlob(String const& string) class RawBlob : public BlobBase { public: - void* data; - size_t size; - - ~RawBlob() - { - free(data); - } - // 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_size; } - SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE + // Ctor + RawBlob(const void* data, size_t size): + m_size(size) { - return data; + m_data = malloc(size); + memcpy(m_data, data, size); } - - SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE + ~RawBlob() { - return size; + free(m_data); } -}; +protected: + void* m_data; + size_t m_size; +}; ComPtr<ISlangBlob> createRawBlob(void const* inData, size_t size) { - void* dataCopy = malloc(size); - memcpy(dataCopy, inData, size); - - RawBlob* rawBlob = new RawBlob(); - rawBlob->data = dataCopy; - rawBlob->size = size; - - return ComPtr<ISlangBlob>(rawBlob); + return ComPtr<ISlangBlob>(new RawBlob(inData, size)); } - SlangResult CompileRequest::loadFile(String const& path, ISlangBlob** outBlob) { // If there is a used-defined filesystem, then use that to load files. |
