Arcane  4.2.3.0
User documentation
Loading...
Searching...
No Matches
MeshHandle.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 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/*---------------------------------------------------------------------------*/
8/* MeshHandle.cc (C) 2000-2026 */
9/* */
10/* Handle on a mesh. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/core/MeshHandle.h"
15
16#include "IParallelMng.h"
17#include "arcane/utils/UserDataList.h"
18#include "arcane/utils/FatalErrorException.h"
19#include "arcane/utils/Observable.h"
20#include "arcane/utils/ValueConvert.h"
21
22#include "arcane/core/ISubDomain.h"
23#include "arcane/core/IMesh.h"
24#include "arcane/core/IMeshBase.h"
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29namespace Arcane
30{
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
37{
38 public:
39
40 MeshHandleRef()
41 : m_is_null(true)
42 {}
43 MeshHandleRef(ISubDomain* sd, const String& name);
44 ~MeshHandleRef();
45
46 public:
47
48 const String& meshName() const { return m_mesh_name; }
49 bool isNull() const { return m_is_null; }
50 IMesh* mesh() const { return m_mesh_ptr; }
51 IMeshBase* meshBase() const { return m_mesh_base_ptr; }
52 ISubDomain* subDomain() const { return m_sub_domain; }
53 IMeshMng* meshMng() const { return m_mesh_mng; }
54 ITraceMng* traceMng() const { return m_trace_mng; }
55 IVariableMng* variableMng() const { return m_variable_mng; }
56 IUserDataList* userDataList() const { return m_user_data_list; }
57 Observable* onDestroyObservable() const { return m_on_destroy_observable; }
58 bool isDoFatalInMeshMethod() const { return m_do_fatal_in_mesh_method; }
59
60 public:
61
62 void _destroyMesh();
63 void _setMesh(IMesh* mesh);
64
65 public:
66
67 void addRef()
68 {
69 ++m_nb_ref;
70 }
71 void removeRef()
72 {
73 Int32 v = std::atomic_fetch_add(&m_nb_ref, -1);
74 if (v == 1)
75 delete this;
76 }
77
78 private:
79
80 String m_mesh_name;
81 IMesh* m_mesh_ptr = nullptr;
82 IMeshBase* m_mesh_base_ptr = nullptr;
83 Ref<IParallelMng> m_parallel_mng_ref;
84 // For now we need it, but it should be removed
85 ISubDomain* m_sub_domain = nullptr;
86 IUserDataList* m_user_data_list = nullptr;
87 IMeshMng* m_mesh_mng = nullptr;
88 ITraceMng* m_trace_mng = nullptr;
89 IVariableMng* m_variable_mng = nullptr;
90 Observable* m_on_destroy_observable = nullptr;
91 bool m_is_null = true;
92 bool m_do_fatal_in_mesh_method = false;
93 std::atomic<Int32> m_nb_ref = 0;
94};
95
96/*---------------------------------------------------------------------------*/
97/*---------------------------------------------------------------------------*/
98
99// TODO make this constructor private to IMeshMng to avoid
100// possible duplicates for MeshHandles associated with the same mesh name.
101MeshHandle::MeshHandleRef::
102MeshHandleRef(ISubDomain* sd, const String& name)
103: m_mesh_name(name)
104, m_sub_domain(sd)
105, m_is_null(name.null())
106{
107 if (!m_is_null)
108 m_user_data_list = new UserDataList();
110 m_trace_mng = sd->traceMng();
111 m_mesh_mng = sd->meshMng();
112 m_variable_mng = sd->variableMng();
113 m_on_destroy_observable = new Observable();
114 if (auto v = Convert::Type<Int32>::tryParseFromEnvironment("ARCANE_DO_FATAL_IN_MESHHANDLE", true))
115 m_do_fatal_in_mesh_method = v.value() != 0;
116}
117
118/*---------------------------------------------------------------------------*/
119/*---------------------------------------------------------------------------*/
120
121MeshHandle::MeshHandleRef::
122~MeshHandleRef()
123{
124 delete m_user_data_list;
125 delete m_on_destroy_observable;
126}
127
128/*---------------------------------------------------------------------------*/
129/*---------------------------------------------------------------------------*/
130
131void MeshHandle::MeshHandleRef::
132_setMesh(IMesh* mesh)
133{
134 m_mesh_ptr = mesh;
135 if (mesh)
136 // We need to keep a reference for the parallel mng because it may have
137 // been creating during mesh building and no longer have reference after.
138 m_parallel_mng_ref = makeRef(mesh->parallelMng());
139 else
140 m_parallel_mng_ref.reset();
141}
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145
146void MeshHandle::MeshHandleRef::
147_destroyMesh()
148{
149 // TODO: protect against multiple calls
150 IMesh* mesh = m_mesh_ptr;
151 if (!mesh)
152 return;
153 m_on_destroy_observable->notifyAllObservers();
154 m_user_data_list->clear();
155 // Be careful not to set to null until the end of this routine because the
156 // users of \a m_user_data might need this MeshHandle.
157 m_mesh_ptr = nullptr;
158 delete mesh;
159}
160
161/*---------------------------------------------------------------------------*/
162/*---------------------------------------------------------------------------*/
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166
167MeshHandle::
168MeshHandle(ISubDomain* sd, const String& name)
169: m_ref(new MeshHandleRef(sd, name))
170{
171}
172
173/*---------------------------------------------------------------------------*/
174/*---------------------------------------------------------------------------*/
175
176MeshHandle::
177MeshHandle()
178: m_ref(new MeshHandleRef())
179{
180}
181
182MeshHandle::
183MeshHandle(const MeshHandle& x)
184: m_ref(x.m_ref)
185{
186}
187
188MeshHandle::
189MeshHandle(MeshHandle&& x) noexcept
190: m_ref(std::move(x.m_ref))
191{
192}
193
194MeshHandle& MeshHandle::
195operator=(const MeshHandle& x)
196{
197 if (&x != this)
198 m_ref = x.m_ref;
199 return (*this);
200}
201
202MeshHandle& MeshHandle::
203operator=(MeshHandle&& x) noexcept
204{
205 m_ref = std::move(x.m_ref);
206 return (*this);
207}
208
209MeshHandle::
210~MeshHandle()
211{
212 // The destructor needs to be here because MeshHandleRef is not known
213 // in the class definition.
214}
215
216/*---------------------------------------------------------------------------*/
217/*---------------------------------------------------------------------------*/
218
219/*---------------------------------------------------------------------------*/
220/*---------------------------------------------------------------------------*/
221
223meshMng() const
224{
225 return m_ref->meshMng();
226}
227
228/*---------------------------------------------------------------------------*/
229/*---------------------------------------------------------------------------*/
230
232hasMesh() const
233{
234 IMesh* m = m_ref->mesh();
235 return m;
236}
237
238/*---------------------------------------------------------------------------*/
239/*---------------------------------------------------------------------------*/
240
242mesh() const
243{
244 IMesh* m = m_ref->mesh();
245 if (m)
246 return m;
247 // Eventually, make a fatal error if the mesh is null. For reasons of
248 // compatibility with the existing code, we return 'nullptr'.
249 bool do_fatal = m_ref->isDoFatalInMeshMethod();
250 if (do_fatal)
251 ARCANE_FATAL("Invalid call for null mesh. Call MeshHandle::hasMesh() before to make sure mesh is valid");
252 return nullptr;
253}
254
255/*---------------------------------------------------------------------------*/
256/*---------------------------------------------------------------------------*/
257
259meshOrNull() const
260{
261 return m_ref->mesh();
262}
263
264/*---------------------------------------------------------------------------*/
265/*---------------------------------------------------------------------------*/
266
268traceMng() const
269{
270 return m_ref->traceMng();
271}
272
273/*---------------------------------------------------------------------------*/
274/*---------------------------------------------------------------------------*/
275
277variableMng() const
278{
279 return m_ref->variableMng();
280}
281
282/*---------------------------------------------------------------------------*/
283/*---------------------------------------------------------------------------*/
284
286application() const
287{
288 return m_ref->subDomain()->application();
289}
290
291/*---------------------------------------------------------------------------*/
292/*---------------------------------------------------------------------------*/
293
296{
297 return m_ref->onDestroyObservable();
298}
299
301{
302 return m_ref->userDataList();
303}
304
305String MeshHandle::meshName() const
306{
307 return m_ref->meshName();
308}
309
310//! Indicates if the handle is null (it does not reference any existing mesh or not)
312{
313 return m_ref->isNull();
314}
315
316//! \internal
317void MeshHandle::
318_setMesh(IMesh* mesh)
319{
320 m_ref->_setMesh(mesh);
321}
322
323//! \internal
324void MeshHandle::
325_destroyMesh()
326{
327 m_ref->_destroyMesh();
328}
329
330//! \internal
331IMesh* MeshHandle::
332_internalMeshOrNull() const
333{
334 return m_ref->mesh();
335}
336
338subDomain() const
339{
340 return m_ref->subDomain();
341}
342
343/*---------------------------------------------------------------------------*/
344/*---------------------------------------------------------------------------*/
345
346/*---------------------------------------------------------------------------*/
347/*---------------------------------------------------------------------------*/
348
353
354/*---------------------------------------------------------------------------*/
355/*---------------------------------------------------------------------------*/
356
359{
360 if (mesh)
361 m_handle = mesh->handle();
362}
363
364/*---------------------------------------------------------------------------*/
365/*---------------------------------------------------------------------------*/
366
367} // End namespace Arcane
368
369/*---------------------------------------------------------------------------*/
370/*---------------------------------------------------------------------------*/
#define ARCANE_CHECK_POINTER(ptr)
Macro returning the pointer ptr if it is not null or throwing an exception if it is null.
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Template class for converting a type.
Application interface.
virtual ITraceMng * traceMng() const =0
Trace manager.
Mesh manager interface.
Definition IMeshMng.h:41
virtual IParallelMng * parallelMng()=0
Parallelism manager.
Interface of the subdomain manager.
Definition ISubDomain.h:78
virtual IMeshMng * meshMng() const =0
Returns the mesh manager.
virtual IVariableMng * variableMng()=0
Returns the variable manager.
Interface of a list that manages user data.
Variable manager interface.
IMesh * mesh() const
Associated mesh. Can be null if the mesh has not yet been created.
Definition MeshHandle.h:176
const MeshHandle & handle() const
Associated handle.
Definition MeshHandle.h:182
MeshHandleOrMesh(const MeshHandle &handle)
Constructs an instance from a MeshHandle.
Handle on a mesh.
Definition MeshHandle.h:48
ITraceMng * traceMng() const
Associated trace manager. nullptr if isNull() is true.
ISubDomain * subDomain() const
Associated sub-domain. Null if isNull() is true.
bool hasMesh() const
Indicates if the associated mesh has already been created (i.e.: mesh() is valid).
IMesh * meshOrNull() const
Returns the mesh associated with this instance.
bool isNull() const
Indicates if the handle is null (it does not reference any existing mesh or not).
IMesh * mesh() const
Associated mesh.
IApplication * application() const
Associated application. nullptr if isNull() is true.
IUserDataList * meshUserDataList() const
Associated user data.
IVariableMng * variableMng() const
Associated variable manager. nullptr if isNull() is true.
IMeshMng * meshMng() const
Associated mesh manager. nullptr if isNull() is true.
IObservable * onDestroyObservable() const
Observable to be notified of destruction.
Reference to an instance.
Thread-safe implementation of a reference counter.
Manages a list of user data.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
std::int32_t Int32
Signed integer type of 32 bits.