diff --git a/arccore/src/base/arccore/base/Ref.h b/arccore/src/base/arccore/base/Ref.h index 305824bbdf..6d12c30700 100644 --- a/arccore/src/base/arccore/base/Ref.h +++ b/arccore/src/base/arccore/base/Ref.h @@ -285,6 +285,8 @@ class Ref return a.isNull(); } + operator bool() const { return (!isNull()); } + public: //! Instance associée ou `nullptr` si aucune @@ -350,7 +352,7 @@ class Ref * sera détruit par l'opérateur 'operator delete' lorsqu'il n'y aura plus * de référence dessus. */ -template auto +template inline auto makeRef(InstanceType* t) -> Ref { return Ref::create(t); @@ -381,6 +383,18 @@ makeRefFromInstance(InstanceType2* t) return Ref::create(t); } +/*---------------------------------------------------------------------------*/ +/*---------------------------------------------------------------------------*/ +/*! + * \brief Créé une instance de type \a TrueType avec les arguments \a Args + * et retourne une référence dessus. + */ +template inline Ref +createRef(Args&&... args) +{ + return makeRef(new TrueType(std::forward(args)...)); +} + /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ diff --git a/arccore/src/base/tests/TestRef.cc b/arccore/src/base/tests/TestRef.cc index 16e05b9ed3..33117b0a5d 100644 --- a/arccore/src/base/tests/TestRef.cc +++ b/arccore/src/base/tests/TestRef.cc @@ -216,6 +216,30 @@ _doTest2() } } +void _doTest3() +{ + Ref t0; + { + auto myx3 = createRef(23, "abc"); + t0 = myx3; + bool is_ok = false; + if (myx3) + is_ok = true; + ASSERT_TRUE(is_ok); + ASSERT_FALSE(!myx3); + ASSERT_EQ(t0->pa(), 23); + ASSERT_EQ(t0->pb(), "abc"); + } + { + Ref myx4; + bool is_null = true; + if (myx4) + is_null = false; + ASSERT_TRUE(is_null); + ASSERT_TRUE(!myx4); + } +} + namespace Arccore { ARCCORE_DEFINE_REFERENCE_COUNTED_CLASS(MyTest::TestBaseType); @@ -230,4 +254,5 @@ TEST(Ref, Misc) _doTest1(); _doTest1(); _doTest2(); + _doTest3(); } diff --git a/arccore/src/base/tests/TestReferenceCounter.cc b/arccore/src/base/tests/TestReferenceCounter.cc index f8f62b0713..b7c4585341 100644 --- a/arccore/src/base/tests/TestReferenceCounter.cc +++ b/arccore/src/base/tests/TestReferenceCounter.cc @@ -121,7 +121,7 @@ TEST(ReferenceCounter, Ref) } { StatInfo stat_info; - _doTest1(makeRef(new Simple2(&stat_info))); + _doTest1(createRef(&stat_info)); ASSERT_TRUE(stat_info.checkValid(0)) << "Bad destroy3"; } } @@ -174,7 +174,7 @@ TEST(ReferenceCounter, RefWithDeleter) using namespace Test1; Ref myx2; { - Ref myx1 = makeRef(new TestClassWithDeleter()); + Ref myx1 = makeRef(new TestClassWithDeleter()); myx2 = myx1; } myx2.reset(); @@ -183,6 +183,23 @@ TEST(ReferenceCounter, RefWithDeleter) auto* ptr1 = new TestClassWithDeleter(); Ref x4 = Ref::createWithHandle(ptr1,external_ref); } + { + Ref myx3 = createRef(); + myx2 = myx3; + bool is_ok = false; + if (myx3) + is_ok = true; + ASSERT_TRUE(is_ok); + ASSERT_FALSE(!myx3); + } + { + Ref myx4; + bool is_null = true; + if (myx4) + is_null = false; + ASSERT_TRUE(is_null); + ASSERT_TRUE(!myx4); + } } catch(const std::exception& ex){ std::cerr << "Exception ex=" << ex.what() << "\n";