[PATCH] D94440: [ADT] Add makeIntrusiveRefCnt helper function

Nathan James via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 11 11:48:38 PST 2021


njames93 created this revision.
njames93 added reviewers: dblaikie, MaskRay.
Herald added a subscriber: dexonsmith.
njames93 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Works like std::make_unique but for IntrusiveRefCntPtr objects.
See https://lists.llvm.org/pipermail/llvm-dev/2021-January/147729.html


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D94440

Files:
  llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
  llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp


Index: llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
===================================================================
--- llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
+++ llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
@@ -53,6 +53,22 @@
   EXPECT_EQ(0, NumInstances);
 }
 
+TYPED_TEST(IntrusiveRefCntPtrTest, MakeIntrusive) {
+  EXPECT_EQ(0, NumInstances);
+  {
+    auto S1 = makeIntrusiveRefCnt<TypeParam>();
+    auto S2 = makeIntrusiveRefCnt<const TypeParam>();
+    EXPECT_EQ(2, NumInstances);
+    static_assert(
+        std::is_same<decltype(S1), IntrusiveRefCntPtr<TypeParam>>::value,
+        "Non-const type mismatch");
+    static_assert(
+        std::is_same<decltype(S2), IntrusiveRefCntPtr<const TypeParam>>::value,
+        "Const type mismatch");
+  }
+  EXPECT_EQ(0, NumInstances);
+}
+
 struct InterceptRefCounted : public RefCountedBase<InterceptRefCounted> {
   InterceptRefCounted(bool *Released, bool *Retained)
     : Released(Released), Retained(Retained) {}
Index: llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
===================================================================
--- llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
+++ llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
@@ -297,6 +297,12 @@
   }
 };
 
+/// Factory function for creating smart ref counted pointers.
+template <typename T, typename... Args>
+IntrusiveRefCntPtr<T> makeIntrusiveRefCnt(Args &&...A) {
+  return IntrusiveRefCntPtr<T>(new T(std::forward<Args>(A)...));
+}
+
 } // end namespace llvm
 
 #endif // LLVM_ADT_INTRUSIVEREFCNTPTR_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94440.315892.patch
Type: text/x-patch
Size: 1533 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210111/766c2c04/attachment.bin>


More information about the llvm-commits mailing list