File tree 3 files changed +59
-3
lines changed
3 files changed +59
-3
lines changed Original file line number Diff line number Diff line change @@ -285,6 +285,8 @@ class Ref
285
285
return a.isNull ();
286
286
}
287
287
288
+ operator bool () const { return (!isNull ()); }
289
+
288
290
public:
289
291
290
292
// ! Instance associée ou `nullptr` si aucune
@@ -350,7 +352,7 @@ class Ref
350
352
* sera détruit par l'opérateur 'operator delete' lorsqu'il n'y aura plus
351
353
* de référence dessus.
352
354
*/
353
- template <typename InstanceType> auto
355
+ template <typename InstanceType> inline auto
354
356
makeRef (InstanceType* t) -> Ref<InstanceType>
355
357
{
356
358
return Ref<InstanceType>::create (t);
@@ -381,6 +383,18 @@ makeRefFromInstance(InstanceType2* t)
381
383
return Ref<InstanceType>::create (t);
382
384
}
383
385
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
+
384
398
/* ---------------------------------------------------------------------------*/
385
399
/* ---------------------------------------------------------------------------*/
386
400
Original file line number Diff line number Diff line change @@ -216,6 +216,30 @@ _doTest2()
216
216
}
217
217
}
218
218
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
+
219
243
namespace Arccore
220
244
{
221
245
ARCCORE_DEFINE_REFERENCE_COUNTED_CLASS (MyTest::TestBaseType);
@@ -230,4 +254,5 @@ TEST(Ref, Misc)
230
254
_doTest1<TestRefOwn,1 >();
231
255
_doTest1<TestRefSharedPtr,0 >();
232
256
_doTest2 ();
257
+ _doTest3 ();
233
258
}
Original file line number Diff line number Diff line change @@ -121,7 +121,7 @@ TEST(ReferenceCounter, Ref)
121
121
}
122
122
{
123
123
StatInfo stat_info;
124
- _doTest1 (makeRef ( new Simple2 (&stat_info) ));
124
+ _doTest1 (createRef< Simple2> (&stat_info));
125
125
ASSERT_TRUE (stat_info.checkValid (0 )) << " Bad destroy3" ;
126
126
}
127
127
}
@@ -174,7 +174,7 @@ TEST(ReferenceCounter, RefWithDeleter)
174
174
using namespace Test1 ;
175
175
Ref<ITestClassWithDeleter> myx2;
176
176
{
177
- Ref<ITestClassWithDeleter> myx1 = makeRef<ITestClassWithDeleter >(new TestClassWithDeleter ());
177
+ Ref<ITestClassWithDeleter> myx1 = makeRef<TestClassWithDeleter >(new TestClassWithDeleter ());
178
178
myx2 = myx1;
179
179
}
180
180
myx2.reset ();
@@ -183,6 +183,23 @@ TEST(ReferenceCounter, RefWithDeleter)
183
183
auto * ptr1 = new TestClassWithDeleter ();
184
184
Ref<ITestClassWithDeleter> x4 = Ref<ITestClassWithDeleter>::createWithHandle (ptr1,external_ref);
185
185
}
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
+ }
186
203
}
187
204
catch (const std::exception & ex){
188
205
std::cerr << " Exception ex=" << ex.what () << " \n " ;
You can’t perform that action at this time.
0 commit comments