Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TestRef.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7#include <gtest/gtest.h>
8
9#include "arccore/base/Ref.h"
11
12#include <string>
13
14using namespace Arccore;
15
16namespace MyTest
17{
18
20class TestRefOwn;
21class TestBaseType;
22}
23
25
26namespace MyTest
27{
28int global_nb_create = 0;
29int global_nb_destroy = 0;
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
36{
37 public:
39 public:
40 TestBaseType(int a,const std::string& b) : m_a(a), m_b(b)
41 {
42 std::cout << "CREATE ME this=" << this << "\n";
43 ++global_nb_create;
44 }
45 TestBaseType(const TestBaseType& x) : m_a(x.m_a), m_b(x.m_b)
46 {
47 std::cout << "CREATE ME (copy) this=" << this << "\n";
48 }
49 virtual ~TestBaseType()
50 {
51 std::cout << "DELETE ME this=" << this << "\n";
52 ++global_nb_destroy;
53 }
54 public:
55 int pa() const { return m_a; }
56 const std::string& pb() const { return m_b; }
57 void print(const std::string& x) const
58 {
59 std::cout << x << " A=" << pa() << " B=" << pb()
60 << " this=" << this << "\n";
61 }
62 private:
63 int m_a;
64 std::string m_b;
65};
66
67/*---------------------------------------------------------------------------*/
68/*---------------------------------------------------------------------------*/
69
71{
72 public:
73 TestBaseTypeNoRef(int a,const std::string& b) : m_a(a), m_b(b)
74 {
75 std::cout << "CREATE ME this=" << this << "\n";
76 ++global_nb_create;
77 }
78 virtual ~TestBaseTypeNoRef()
79 {
80 std::cout << "DELETE ME this=" << this << "\n";
81 ++global_nb_destroy;
82 }
83 public:
84 int pa() const { return m_a; }
85 const std::string& pb() const { return m_b; }
86 void print(const std::string& x) const
87 {
88 std::cout << x << " A=" << pa() << " B=" << pb()
89 << " this=" << this << "\n";
90 }
91 private:
92 int m_a;
93 std::string m_b;
94};
95
96/*---------------------------------------------------------------------------*/
97/*---------------------------------------------------------------------------*/
98
100: public TestBaseType
101{
102 public:
103 typedef TestBaseType BaseType;
104 public:
105 TestRefOwn(int a,const std::string& b) : TestBaseType(a,b){}
106 ~TestRefOwn() override
107 {
108 std::cout << "DELETE ME (TestRefOwn) this=" << this << "\n";
109 }
110};
111
112/*---------------------------------------------------------------------------*/
113/*---------------------------------------------------------------------------*/
114
117: public TestBaseTypeNoRef
118{
119 public:
121 public:
122 TestRefSharedPtr(int a,const std::string& b) : TestBaseTypeNoRef(a,b){}
123};
124
127{
128 ARCCORE_INTERNAL_DEFINE_REFERENCE_COUNTED_INCLASS_METHODS();
129};
130
131}
132
133using namespace MyTest;
134
135/*---------------------------------------------------------------------------*/
136/*---------------------------------------------------------------------------*/
137
138template<typename ClassType,int id> void
139_doTest1Helper()
140{
141 std::cout << "** ** BEGIN_TEST\n";
142
143 typedef Ref<ClassType> RefType;
144 typedef typename ClassType::BaseType BaseType;
145 int a = 3;
146 std::string b = "XYZ";
147
148 {
149 RefType ref0(makeRef(new ClassType(a,b)));
150 ref0->print("X0");
151 }
152
153 RefType ref1(RefType::template createRef<ClassType>(a,b));
154 ref1->print("X1");
155 {
156 RefType ref2(ref1);
157 ref2->print("X2");
158 Ref<BaseType> ref3(ref1);
159 ref3->print("X3");
160 }
161 if constexpr (id == 1){
162 ClassType* ct = ref1.get();
163 auto z = makeRefFromInstance<TestBaseType>(ct);
164 }
165 {
166 RefType null_ref_type;
167 ASSERT_EQ(null_ref_type.get(),nullptr);
168 if constexpr (id==0){
169 ClassType* ct2 = null_ref_type._release();
170 ASSERT_EQ(ct2,nullptr);
171 }
172 }
173 {
174 ClassType* ct = nullptr;
175 RefType null_ref_type(makeRef(ct));
176 ASSERT_EQ(null_ref_type.get(),nullptr);
177 if constexpr (id==0){
178 ClassType* ct2 = null_ref_type._release();
179 ASSERT_EQ(ct2,nullptr);
180 }
181 }
182 std::cout << "DoTestRelease\n";
183 {
184 RefType ref_ct(makeRef(new ClassType(a,b)));
185 ASSERT_NE(ref_ct.get(),nullptr);
186 ASSERT_EQ(ref_ct->pa(),a);
187 ASSERT_EQ(ref_ct->pb(),b);
188 if constexpr (id==0){
189 ClassType* ct2 = ref_ct._release();
190 ASSERT_NE(ct2,nullptr);
191 ASSERT_EQ(ct2->pa(),a);
192 ASSERT_EQ(ct2->pb(),b);
193 delete ct2;
194 }
195 }
196 std::cout << "** ** END_TEST\n";
197}
198
199template<typename ClassType,int id> void
200_doTest1()
201{
202 global_nb_create = global_nb_destroy = 0;
203 _doTest1Helper<ClassType,id>();
204 ASSERT_EQ(global_nb_create,3);
205 ASSERT_EQ(global_nb_create,global_nb_destroy);
206}
207
208void
209_doTest2()
210{
211 TestRefOwn* x1 = new TestRefOwn(1,"ZXB");
212 delete x1;
213
214 {
215 auto x2 = makeRef<TestRefMacroInternal>(new TestRefMacroInternal());
216 }
217}
218
219void _doTest3()
220{
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 {
235 bool is_null = true;
236 if (myx4)
237 is_null = false;
238 ASSERT_TRUE(is_null);
239 ASSERT_TRUE(!myx4);
240 }
241}
242
243namespace Arccore
244{
246}
247
248/*---------------------------------------------------------------------------*/
249/*---------------------------------------------------------------------------*/
250
251// Teste si le compteur de référence détruit bien l'instance.
252TEST(Ref, Misc)
253{
254 _doTest1<TestRefOwn,1>();
255 _doTest1<TestRefSharedPtr,0>();
256 _doTest2();
257 _doTest3();
258}
#define ASSERT_FALSE(condition)
Vérifie que condition est faux.
Definition Assertion.h:138
#define ASSERT_TRUE(condition)
Vérifie que condition est vrai.
Definition Assertion.h:126
#define ARCCORE_DECLARE_REFERENCE_COUNTED_CLASS(class_name)
Macro pour déclarer qu'une classe utilise un compteur de référence.
#define ARCCORE_DEFINE_REFERENCE_COUNTED_CLASS(class_name)
Macro pour définir les méthodes et types une classe qui utilise un compteur de référence.
Gestion des références à une classe C++.
Référence à une instance.
Implémentation thread-safe d'un compteur de référence.
Classe de test utilisant un shared_ptr.
Definition TestRef.cc:118
Espace de nom de Arccore.
Definition ArcaneTypes.h:24
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Créé une référence sur un pointeur.
Structure servant à tagger les interfaces/classes qui utilisent un compteur de référence interne.