[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
This commit is contained in:
Nathan James 2021-01-11 20:12:53 +00:00
parent d79642b3db
commit d3ff24cbf8
No known key found for this signature in database
GPG Key ID: CC007AFCDA90AA5F
2 changed files with 22 additions and 0 deletions

View File

@ -297,6 +297,12 @@ template <class T> struct simplify_type<const IntrusiveRefCntPtr<T>> {
}
};
/// Factory function for creating intrusive 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

View File

@ -53,6 +53,22 @@ TYPED_TEST(IntrusiveRefCntPtrTest, InteropsWithUniquePtr) {
EXPECT_EQ(0, NumInstances);
}
TYPED_TEST(IntrusiveRefCntPtrTest, MakeIntrusiveRefCnt) {
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) {}