Skip to content

Commit cfc7eb8

Browse files
Merge pull request #1161 from arcaneframework/dev/gg-improve-reference-counter
Improve usage of class 'Ref'
2 parents 1c940a9 + 3bb02f8 commit cfc7eb8

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

arccore/src/base/arccore/base/Ref.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ class Ref
285285
return a.isNull();
286286
}
287287

288+
operator bool() const { return (!isNull()); }
289+
288290
public:
289291

290292
//! Instance associée ou `nullptr` si aucune
@@ -350,7 +352,7 @@ class Ref
350352
* sera détruit par l'opérateur 'operator delete' lorsqu'il n'y aura plus
351353
* de référence dessus.
352354
*/
353-
template <typename InstanceType> auto
355+
template <typename InstanceType> inline auto
354356
makeRef(InstanceType* t) -> Ref<InstanceType>
355357
{
356358
return Ref<InstanceType>::create(t);
@@ -381,6 +383,18 @@ makeRefFromInstance(InstanceType2* t)
381383
return Ref<InstanceType>::create(t);
382384
}
383385

386+
/*---------------------------------------------------------------------------*/
387+
/*---------------------------------------------------------------------------*/
388+
/*!
389+
* \brief Créé une instance de type \a TrueType avec les arguments \a Args
390+
* et retourne une référence dessus.
391+
*/
392+
template <typename TrueType, class... Args> inline Ref<TrueType>
393+
createRef(Args&&... args)
394+
{
395+
return makeRef<TrueType>(new TrueType(std::forward<Args>(args)...));
396+
}
397+
384398
/*---------------------------------------------------------------------------*/
385399
/*---------------------------------------------------------------------------*/
386400

arccore/src/base/tests/TestRef.cc

+25
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,30 @@ _doTest2()
216216
}
217217
}
218218

219+
void _doTest3()
220+
{
221+
Ref<TestBaseType> t0;
222+
{
223+
auto myx3 = createRef<TestRefOwn>(23, "abc");
224+
t0 = myx3;
225+
bool is_ok = false;
226+
if (myx3)
227+
is_ok = true;
228+
ASSERT_TRUE(is_ok);
229+
ASSERT_FALSE(!myx3);
230+
ASSERT_EQ(t0->pa(), 23);
231+
ASSERT_EQ(t0->pb(), "abc");
232+
}
233+
{
234+
Ref<TestBaseType> myx4;
235+
bool is_null = true;
236+
if (myx4)
237+
is_null = false;
238+
ASSERT_TRUE(is_null);
239+
ASSERT_TRUE(!myx4);
240+
}
241+
}
242+
219243
namespace Arccore
220244
{
221245
ARCCORE_DEFINE_REFERENCE_COUNTED_CLASS(MyTest::TestBaseType);
@@ -230,4 +254,5 @@ TEST(Ref, Misc)
230254
_doTest1<TestRefOwn,1>();
231255
_doTest1<TestRefSharedPtr,0>();
232256
_doTest2();
257+
_doTest3();
233258
}

arccore/src/base/tests/TestReferenceCounter.cc

+19-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ TEST(ReferenceCounter, Ref)
121121
}
122122
{
123123
StatInfo stat_info;
124-
_doTest1(makeRef(new Simple2(&stat_info)));
124+
_doTest1(createRef<Simple2>(&stat_info));
125125
ASSERT_TRUE(stat_info.checkValid(0)) << "Bad destroy3";
126126
}
127127
}
@@ -174,7 +174,7 @@ TEST(ReferenceCounter, RefWithDeleter)
174174
using namespace Test1;
175175
Ref<ITestClassWithDeleter> myx2;
176176
{
177-
Ref<ITestClassWithDeleter> myx1 = makeRef<ITestClassWithDeleter>(new TestClassWithDeleter());
177+
Ref<ITestClassWithDeleter> myx1 = makeRef<TestClassWithDeleter>(new TestClassWithDeleter());
178178
myx2 = myx1;
179179
}
180180
myx2.reset();
@@ -183,6 +183,23 @@ TEST(ReferenceCounter, RefWithDeleter)
183183
auto* ptr1 = new TestClassWithDeleter();
184184
Ref<ITestClassWithDeleter> x4 = Ref<ITestClassWithDeleter>::createWithHandle(ptr1,external_ref);
185185
}
186+
{
187+
Ref<ITestClassWithDeleter> myx3 = createRef<TestClassWithDeleter>();
188+
myx2 = myx3;
189+
bool is_ok = false;
190+
if (myx3)
191+
is_ok = true;
192+
ASSERT_TRUE(is_ok);
193+
ASSERT_FALSE(!myx3);
194+
}
195+
{
196+
Ref<ITestClassWithDeleter> myx4;
197+
bool is_null = true;
198+
if (myx4)
199+
is_null = false;
200+
ASSERT_TRUE(is_null);
201+
ASSERT_TRUE(!myx4);
202+
}
186203
}
187204
catch(const std::exception& ex){
188205
std::cerr << "Exception ex=" << ex.what() << "\n";

0 commit comments

Comments
 (0)