From: David Blaikie Date: Thu, 8 May 2014 21:52:23 +0000 (+0000) Subject: StringMap support for move-only values. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=b0439e6d6a7c3cd93589b712b732334803506184;p=oota-llvm.git StringMap support for move-only values. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208359 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/StringMap.h b/include/llvm/ADT/StringMap.h index a966977ea57..3a0006f5a1c 100644 --- a/include/llvm/ADT/StringMap.h +++ b/include/llvm/ADT/StringMap.h @@ -109,8 +109,8 @@ public: explicit StringMapEntry(unsigned strLen) : StringMapEntryBase(strLen), second() {} - StringMapEntry(unsigned strLen, const ValueTy &V) - : StringMapEntryBase(strLen), second(V) {} + StringMapEntry(unsigned strLen, ValueTy V) + : StringMapEntryBase(strLen), second(std::move(V)) {} StringRef getKey() const { return StringRef(getKeyData(), getKeyLength()); @@ -146,7 +146,7 @@ public: static_cast(Allocator.Allocate(AllocSize,Alignment)); // Default construct the value. - new (NewItem) StringMapEntry(KeyLength, InitVal); + new (NewItem) StringMapEntry(KeyLength, std::move(InitVal)); // Copy the string information. char *StrBuffer = const_cast(NewItem->getKeyData()); @@ -166,7 +166,7 @@ public: static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd, InitType InitVal) { MallocAllocator A; - return Create(KeyStart, KeyEnd, A, InitVal); + return Create(KeyStart, KeyEnd, A, std::move(InitVal)); } static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd) { @@ -348,7 +348,7 @@ public: return *static_cast(Bucket); MapEntryTy *NewItem = - MapEntryTy::Create(Key.begin(), Key.end(), Allocator, Val); + MapEntryTy::Create(Key.begin(), Key.end(), Allocator, std::move(Val)); if (Bucket == getTombstoneVal()) --NumTombstones; diff --git a/unittests/ADT/StringMapTest.cpp b/unittests/ADT/StringMapTest.cpp index b6d41bcc8e5..42a03886180 100644 --- a/unittests/ADT/StringMapTest.cpp +++ b/unittests/ADT/StringMapTest.cpp @@ -218,4 +218,20 @@ TEST_F(StringMapTest, NonDefaultConstructable) { ASSERT_EQ(iter->second.i, 123); } +struct MoveOnly { + int i; + MoveOnly(int i) : i(i) {} + MoveOnly(MoveOnly&&) = default; + MoveOnly(const MoveOnly&) = delete; + MoveOnly &operator=(MoveOnly&&) = default; + MoveOnly &operator=(const MoveOnly&) = delete; +}; + +TEST_F(StringMapTest, MoveOnlyKey) { + StringMap t; + t.GetOrCreateValue("Test", MoveOnly(42)); + StringRef Key = "Test"; + StringMapEntry::Create(Key.begin(), Key.end(), MoveOnly(42))->Destroy(); +} + } // end anonymous namespace