From d3ff24cbf87224b23a0c99c74ed50744711909b2 Mon Sep 17 00:00:00 2001 From: Nathan James Date: Mon, 11 Jan 2021 20:12:53 +0000 Subject: [PATCH] [ADT] Add makeIntrusiveRefCnt helper function Works like std::make_unique but for IntrusiveRefCntPtr objects. See https://lists.llvm.org/pipermail/llvm-dev/2021-January/147729.html Reviewed By: dblaikie, MaskRay Differential Revision: https://reviews.llvm.org/D94440 --- llvm/include/llvm/ADT/IntrusiveRefCntPtr.h | 6 ++++++ llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h index dcd35253c5f2..ca4c40db48b9 100644 --- a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h +++ b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h @@ -297,6 +297,12 @@ template struct simplify_type> { } }; +/// Factory function for creating intrusive ref counted pointers. +template +IntrusiveRefCntPtr makeIntrusiveRefCnt(Args &&...A) { + return IntrusiveRefCntPtr(new T(std::forward(A)...)); +} + } // end namespace llvm #endif // LLVM_ADT_INTRUSIVEREFCNTPTR_H diff --git a/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp b/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp index f69239162e3a..5dbc1b53d3fe 100644 --- a/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp +++ b/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp @@ -53,6 +53,22 @@ TYPED_TEST(IntrusiveRefCntPtrTest, InteropsWithUniquePtr) { EXPECT_EQ(0, NumInstances); } +TYPED_TEST(IntrusiveRefCntPtrTest, MakeIntrusiveRefCnt) { + EXPECT_EQ(0, NumInstances); + { + auto S1 = makeIntrusiveRefCnt(); + auto S2 = makeIntrusiveRefCnt(); + EXPECT_EQ(2, NumInstances); + static_assert( + std::is_same>::value, + "Non-const type mismatch"); + static_assert( + std::is_same>::value, + "Const type mismatch"); + } + EXPECT_EQ(0, NumInstances); +} + struct InterceptRefCounted : public RefCountedBase { InterceptRefCounted(bool *Released, bool *Retained) : Released(Released), Retained(Retained) {}