]> granicus.if.org Git - llvm/commitdiff
StringMap: Test and finish off supporting perfectly forwarded values in StringMap...
authorDavid Blaikie <dblaikie@gmail.com>
Fri, 14 Nov 2014 00:41:46 +0000 (00:41 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Fri, 14 Nov 2014 00:41:46 +0000 (00:41 +0000)
Followup to r221946.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221958 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/StringMap.h
unittests/ADT/StringMapTest.cpp

index 17d5c0d609198e7485cdb647f599c813e7836391..e1a08d9d7f9e5d65a6b8740e20b12be866daba3a 100644 (file)
@@ -170,9 +170,9 @@ public:
 
   /// Create - Create a StringMapEntry with normal malloc/free.
   template<typename InitType>
-  static StringMapEntry *Create(StringRef Key, InitType InitVal) {
+  static StringMapEntry *Create(StringRef Key, InitType &&InitVal) {
     MallocAllocator A;
-    return Create(Key, A, std::move(InitVal));
+    return Create(Key, A, std::forward<InitType>(InitVal));
   }
 
   static StringMapEntry *Create(StringRef Key) {
@@ -367,8 +367,9 @@ public:
   /// exists, return it.  Otherwise, default construct a value, insert it, and
   /// return.
   template <typename InitTy>
-  MapEntryTy &GetOrCreateValue(StringRef Key, InitTy Val) {
-    return *insert(std::make_pair(Key, std::move(Val))).first;
+  MapEntryTy &GetOrCreateValue(StringRef Key, InitTy &&Val) {
+    return *insert(std::pair<StringRef, ValueTy>(
+                       Key, std::forward<InitTy>(Val))).first;
   }
 
   MapEntryTy &GetOrCreateValue(StringRef Key) {
index 028375d7b4be48a51c7f84a8103aaeaa896c2c2a..af9b6115f850c59e20548b284115469810d87bf0 100644 (file)
@@ -256,9 +256,15 @@ TEST_F(StringMapTest, NonDefaultConstructable) {
   ASSERT_EQ(iter->second.i, 123);
 }
 
+struct Immovable {
+  Immovable() {}
+  Immovable(Immovable&&) LLVM_DELETED_FUNCTION; // will disable the other special members
+};
+
 struct MoveOnly {
   int i;
   MoveOnly(int i) : i(i) {}
+  MoveOnly(const Immovable&) : i(0) {}
   MoveOnly(MoveOnly &&RHS) : i(RHS.i) {}
   MoveOnly &operator=(MoveOnly &&RHS) {
     i = RHS.i;
@@ -270,7 +276,7 @@ private:
   MoveOnly &operator=(const MoveOnly &) LLVM_DELETED_FUNCTION;
 };
 
-TEST_F(StringMapTest, MoveOnlyKey) {
+TEST_F(StringMapTest, MoveOnly) {
   StringMap<MoveOnly> t;
   t.GetOrCreateValue("Test", MoveOnly(42));
   StringRef Key = "Test";
@@ -278,6 +284,14 @@ TEST_F(StringMapTest, MoveOnlyKey) {
       ->Destroy();
 }
 
+TEST_F(StringMapTest, CtorArg) {
+  StringMap<MoveOnly> t;
+  t.GetOrCreateValue("Test", Immovable());
+  StringRef Key = "Test";
+  StringMapEntry<MoveOnly>::Create(Key, Immovable())
+      ->Destroy();
+}
+
 TEST_F(StringMapTest, MoveConstruct) {
   StringMap<int> A;
   A.GetOrCreateValue("x", 42);