From d0c9571be3a2167a9f019310aca8f7cd326972c0 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Fri, 22 Jun 2018 13:09:01 -0400 Subject: 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 --- source/slang/slang.cpp | 103 ++++++++++++++----------------------------------- 1 file changed, 30 insertions(+), 73 deletions(-) (limited to 'source/slang/slang.cpp') 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(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 createStringBlob(String const& string) @@ -287,41 +253,32 @@ ComPtr 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 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(rawBlob); + return ComPtr(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. -- cgit v1.2.3