summaryrefslogtreecommitdiffstats
path: root/source/core
diff options
context:
space:
mode:
Diffstat (limited to 'source/core')
-rw-r--r--source/core/slang-blob.cpp29
-rw-r--r--source/core/slang-blob.h20
2 files changed, 43 insertions, 6 deletions
diff --git a/source/core/slang-blob.cpp b/source/core/slang-blob.cpp
index 6cf5214cc..a4acb98cb 100644
--- a/source/core/slang-blob.cpp
+++ b/source/core/slang-blob.cpp
@@ -35,6 +35,25 @@ void* BlobBase::castAs(const SlangUUID& guid)
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! StringBlob !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
+StringBlob::StringBlob(MoveUnique, String& in)
+{
+ auto rep = in.getStringRepresentation();
+ if (rep && !rep->isUniquelyReferenced())
+ {
+ // Make a new unique copy
+ m_string = in.getUnownedSlice();
+
+ // Move out of in
+ String tmp;
+ tmp.swapWith(in);
+ }
+ else
+ {
+ // Must either not have a rep or be unique
+ m_string.swapWith(in);
+ }
+}
+
void* StringBlob::castAs(const SlangUUID& guid)
{
if (auto intf = getInterface(guid))
@@ -59,6 +78,16 @@ void* StringBlob::getObject(const Guid& guid)
return nullptr;
}
+/* static */ComPtr<ISlangBlob> StringBlob::moveCreate(String& in)
+{
+ return ComPtr<ISlangBlob>(new StringBlob(MoveUnique{}, in));
+}
+
+/* static */ComPtr<ISlangBlob> StringBlob::moveCreate(String&& in)
+{
+ return ComPtr<ISlangBlob>(new StringBlob(MoveUnique{}, in));
+}
+
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RawBlob !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
void* RawBlob::castAs(const SlangUUID& guid)
diff --git a/source/core/slang-blob.h b/source/core/slang-blob.h
index 1acf279ef..e528f55a8 100644
--- a/source/core/slang-blob.h
+++ b/source/core/slang-blob.h
@@ -50,17 +50,25 @@ public:
SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_string.getLength(); }
static ComPtr<ISlangBlob> create(const String& in) { return ComPtr<ISlangBlob>(new StringBlob(in)); }
- static ComPtr<ISlangBlob> moveCreate(String& in)
- {
- auto blob = new StringBlob;
- blob->m_string.swapWith(in);
- return ComPtr<ISlangBlob>(blob);
- }
+
+ /// 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)
{}
+
+ StringBlob(MoveUnique, String& string);
StringBlob() {}
/// Get the contained string