summaryrefslogtreecommitdiff
path: root/source/core/slang-blob.h
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2023-06-08 17:26:33 -0400
committerGitHub <noreply@github.com>2023-06-08 17:26:33 -0400
commit3913091a021a8f4525f0050dfb83d1c2b8fc6f6b (patch)
tree32d7560aa9ca037db8c156776b83785cac7994f0 /source/core/slang-blob.h
parentc492288b4778b19bd66ac31edb076add096e757d (diff)
Improvements around StringBlob (#2921)
* #include an absolute path didn't work - because paths were taken to always be relative. * Small fixes and improvements around reflection tool. * Make PrettyWriter printing a class. * Improvements around handling StringBlob and storing stdlib source in ISlangBlob. * Fix some issues with comments around StringBlob. * Default initialize StringBlob fields.
Diffstat (limited to 'source/core/slang-blob.h')
-rw-r--r--source/core/slang-blob.h45
1 files changed, 23 insertions, 22 deletions
diff --git a/source/core/slang-blob.h b/source/core/slang-blob.h
index aa503a6e5..f439a4eae 100644
--- a/source/core/slang-blob.h
+++ b/source/core/slang-blob.h
@@ -31,11 +31,10 @@ protected:
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.
+/** A blob that uses a `StringRepresentation` for its storage.
-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.
+By design the StringBlob owns a unique reference to the StringRepresentation.
+This is because StringBlob, implements an interface which should work across threads.
*/
class StringBlob : public BlobBase
{
@@ -46,37 +45,39 @@ public:
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_string.getBuffer(); }
- SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_string.getLength(); }
+ SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE { return m_chars; }
+ SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_charsCount; }
- static ComPtr<ISlangBlob> create(const String& in) { return ComPtr<ISlangBlob>(new StringBlob(in)); }
+ /// Since in is not being moved will *always* create a new representation, unless the in is empty
+ static ComPtr<ISlangBlob> create(const String& in);
+ /// Create from a slice
+ static ComPtr<ISlangBlob> create(const UnownedStringSlice& slice);
/// Moves from in into the created blob.
/// NOTE! That will only use the representation from in, if it is *unique*
/// otherwise it will make a new copy.
- /// This is so that StringBlob won't hold a reference count via a string held externally.
- /// In contrast StringBlob::create *may* share the representation.
static ComPtr<ISlangBlob> moveCreate(String& in);
static ComPtr<ISlangBlob> moveCreate(String&& in);
-protected:
- /// A type that is only used to differentiate a constructor. Can construct with
- /// MoveUnique{}
- struct MoveUnique {};
-
- explicit StringBlob(String const& string)
- : m_string(string)
- {}
+ /// Dtor
+ ~StringBlob();
- StringBlob(MoveUnique, String& string);
- StringBlob() {}
+protected:
+ /// Use when rep *must* be unique (ie nullptr, and has a ref count of 1, that can be `taken` by the blob
+ void _uniqueInit(StringRepresentation* uniqueRep);
+ /// Init with a rep when can't be owned.
+ void _init(StringRepresentation* rep);
+ /// Init with a representation that has been moved.
+ void _moveInit(StringRepresentation* rep);
- /// Get the contained string
- SLANG_FORCE_INLINE const String& getString() const { return m_string; }
+ /// Checks that m_rep is either nullptr or has a ref count of 1 (ie it is owned by the blob)
+ SLANG_FORCE_INLINE void _checkRep() const { SLANG_ASSERT(m_rep == nullptr || m_rep->isUniquelyReferenced()); }
void* getObject(const Guid& guid);
- String m_string;
+ char* m_chars = nullptr; ///< Pointer to the contained data.
+ size_t m_charsCount = 0; ///< The amount of chars *not* including terminating 0
+ StringRepresentation* m_rep = nullptr; ///< Holds actual bytes. Can be nullptr if it's an empty string.
};
class ListBlob : public BlobBase