Arcane  v4.1.0.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
ArccoreGlobal.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 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/* ArccoreGlobal.cc (C) 2000-2025 */
9/* */
10/* Déclarations générales de Arccore. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/base/ArccoreGlobal.h"
15#include "arccore/base/TraceInfo.h"
16#include "arccore/base/PlatformUtils.h"
17#include "arccore/base/String.h"
18#include "arccore/base/IndexOutOfRangeException.h"
19#include "arccore/base/FatalErrorException.h"
20#include "arccore/base/Ref.h"
21
22// Nécessaire pour les exports de symboles
24#include "arccore/base/Float16.h"
25#include "arccore/base/BFloat16.h"
26#include "arccore/base/Float128.h"
27#include "arccore/base/Int128.h"
28#include "arccore/base/IRangeFunctor.h"
29#include "arccore/base/CheckedConvert.h"
30#include "arccore/base/ForLoopRunInfo.h"
31#include "arccore/base/ParallelLoopOptions.h"
32
33#include <iostream>
34#include <cstring>
35#include <sstream>
36#include <cstdarg>
37
38#ifndef ARCCORE_OS_WIN32
39#include <unistd.h>
40#endif
41
42/*---------------------------------------------------------------------------*/
43/*---------------------------------------------------------------------------*/
44/*!
45 * \namespace Arccore
46 *
47 * \brief Espace de nom de %Arccore
48 *
49 * Toutes les classes et types utilisés dans \b Arccore sont dans ce
50 * namespace.
51 */
52/*---------------------------------------------------------------------------*/
53/*---------------------------------------------------------------------------*/
54
55namespace Arcane
56{
57/*---------------------------------------------------------------------------*/
58/*---------------------------------------------------------------------------*/
59
60namespace
61{
62#ifdef ARCCORE_CHECK
63static bool global_arccore_is_check = true;
64#else
65static bool global_arccore_is_check = false;
66#endif
67}
68
69extern "C++" ARCCORE_BASE_EXPORT
71{
72 return global_arccore_is_check;
73}
74
75extern "C++" ARCCORE_BASE_EXPORT
76void arccoreSetCheck(bool v)
77{
78 global_arccore_is_check = v;
79}
80
81/*---------------------------------------------------------------------------*/
82/*---------------------------------------------------------------------------*/
83
84namespace
85{
86bool global_pause_on_error = false;
87}
88
89extern "C++" ARCCORE_BASE_EXPORT void
91{
92 global_pause_on_error = v;
93}
94
95extern "C++" ARCCORE_BASE_EXPORT void
96arccoreDebugPause(const char* msg)
97{
98 if (global_pause_on_error){
99 std::ostringstream ostr;
100 String host_name(Platform::getHostName());
101 ostr << "** FATAL: Debug mode activated. Execution paused\n"
102 << "** FATAL: message:" << msg << "\n"
103 << "** FATAL: To find the location of the error, start\n"
104 << "** FATAL: start the debugger using the process number\n"
105 << "** FATAL: (pid=" << Platform::getProcessId() << ",host=" << host_name << ").\n";
106 std::cerr << ostr.str();
107#ifndef ARCCORE_OS_WIN32
108 ::pause();
109#endif
110 }
111}
112
113/*---------------------------------------------------------------------------*/
114/*---------------------------------------------------------------------------*/
115
116extern "C++" ARCCORE_BASE_EXPORT void
117arccoreRangeError(Int64 i,Int64 min_value_inclusive,Int64 max_value_exclusive)
118{
119 arccoreDebugPause("arccoreRangeError");
120 throw IndexOutOfRangeException(A_FUNCINFO,String(),i,min_value_inclusive,max_value_exclusive);
121}
122
123/*---------------------------------------------------------------------------*/
124/*---------------------------------------------------------------------------*/
125
126extern "C++" ARCCORE_BASE_EXPORT void
127arccoreRangeError(Int32 i,Int32 max_size)
128{
129 arccoreDebugPause("arccoreRangeError");
130 throw IndexOutOfRangeException(A_FUNCINFO,String(),i,0,max_size);
131}
132
133/*---------------------------------------------------------------------------*/
134/*---------------------------------------------------------------------------*/
135
136extern "C++" ARCCORE_BASE_EXPORT void
138{
139 arccoreDebugPause("arccoreRangeError");
140 throw IndexOutOfRangeException(A_FUNCINFO,String(),i,0,max_size);
141}
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145
146extern "C++" ARCCORE_BASE_EXPORT void
148{
149 std::cerr << "** FATAL: null pointer.\n";
150 std::cerr << "** FATAL: Trying to dereference a null pointer.\n";
151 arccoreDebugPause("arcaneNullPointerPtr");
152 throw FatalErrorException(A_FUNCINFO,"null pointer");
153}
154
155/*---------------------------------------------------------------------------*/
156/*---------------------------------------------------------------------------*/
157
158extern "C++" ARCCORE_BASE_EXPORT void
159arccoreThrowNullPointerError(const char* ptr_name,const char* text)
160{
161 throw FatalErrorException(A_FUNCINFO,text ? text : ptr_name);
162}
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166
167// Cette fonction peut être appelée souvent et certaines fois
168// dans des conditions d'exceptions. Pour cette raison, il ne
169// faut pas qu'elle fasse d'allocations.
170namespace
171{
172void _printFuncName(std::ostream& o,const char* name)
173{
174 const char* par_pos = std::strchr(name,'(');
175 if (!par_pos){
176 o << name;
177 return;
178 }
179
180 // Recherche quelque chose du type namespace::class_name::func_name
181 // et essaye de ne conserver que class_name::func_name
182 ptrdiff_t len = par_pos - name;
183 ptrdiff_t last_scope = 0;
184 ptrdiff_t last_scope2 = 0;
185 for( ptrdiff_t i=0; i<len; ++i ){
186 if (name[i]==':' && name[i+1]==':'){
187 last_scope2 = last_scope;
188 last_scope = i;
189 }
190 }
191 if (last_scope2!=0)
192 last_scope2+=2;
193 ptrdiff_t true_pos = last_scope2;
194 ptrdiff_t true_len = len - true_pos;
195 o.write(&name[true_pos],true_len);
196 o << "()";
197}
198}
199
200/*---------------------------------------------------------------------------*/
201/*---------------------------------------------------------------------------*/
202
203extern "C++" ARCCORE_BASE_EXPORT std::ostream&
204operator<<(std::ostream& o,const TraceInfo& t)
205{
206 if (t.printSignature())
207 o << t.name() << ":" << t.line();
208 else{
209 _printFuncName(o,t.name());
210 }
211 return o;
212}
213
214/*---------------------------------------------------------------------------*/
215/*---------------------------------------------------------------------------*/
216
217/*---------------------------------------------------------------------------*/
218/*---------------------------------------------------------------------------*/
219
220namespace
221{
222/// Fonction appelée lorsqu'une assertion échoue.
223typedef void (*fDoAssert)(const char*,const char*,const char*,size_t);
224/// Fonction appelée pour indiquer s'il faut afficher l'information de débug
225typedef bool (*fCheckDebug)(unsigned int);
226
227fDoAssert g_do_assert_func = 0;
228}
229
230/*---------------------------------------------------------------------------*/
231/*---------------------------------------------------------------------------*/
232/*!
233 * Affichage d'une assertion ayant échouée.
234 */
235extern "C++" ARCCORE_BASE_EXPORT void
236_doAssert(const char* text,const char* file,const char* func,int line)
237{
238 if (g_do_assert_func)
239 (*g_do_assert_func)(text,file,func,line);
240 else{
241 std::ostringstream ostr;
242 ostr << text << ':' << file << ':' << func << ':' << line << ": ";
243 throw FatalErrorException("Assert",ostr.str());
244 }
245}
246
247/*---------------------------------------------------------------------------*/
248/*---------------------------------------------------------------------------*/
249
250extern "C++" ARCCORE_BASE_EXPORT void
251arccorePrintf(const char* format,...)
252{
253 // \n écrit en meme temps pour éviter des écritures intermédiares parasites
254 char buffer[4096];
255 va_list ap;
256 va_start(ap,format);
257 vsnprintf(buffer,4095,format,ap);
258 va_end(ap);
259 std::cerr << buffer << "\n";
260 std::cout << "*E* " << buffer << "\n";
261}
262
263/*---------------------------------------------------------------------------*/
264/*---------------------------------------------------------------------------*/
265
266} // End namespace Arccore
267
268/*---------------------------------------------------------------------------*/
269/*---------------------------------------------------------------------------*/
Gestion des références à une classe C++.
Exception lorsqu'une erreur fatale est survenue.
Exception lorsqu'une valeur n'est pas dans un intervalle donné.
Chaîne de caractères unicode.
Integer len(const char *s)
Retourne la longueur de la chaîne s.
ARCCORE_BASE_EXPORT String getHostName()
Nom de la machine sur lequel tourne le processus.
ARCCORE_BASE_EXPORT int getProcessId()
Numéro du processus.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
ARCCORE_BASE_EXPORT void arccoreNullPointerError()
Signalue l'utilisation d'un pointeur nul.
std::int64_t Int64
Type entier signé sur 64 bits.
ARCCORE_BASE_EXPORT void arccorePrintf(const char *format,...)
Encapsulation de la fonction C printf.
void(* fDoAssert)(const char *, const char *, const char *, size_t)
Fonction appelée lorsqu'une assertion échoue.
Definition Misc.cc:222
ARCCORE_BASE_EXPORT void arccoreDebugPause(const char *msg)
Passe en mode pause ou lance une erreur fatale.
bool(* fCheckDebug)(unsigned int)
Fonction appelée pour indiquer s'il faut afficher l'information de débug.
Definition Misc.cc:224
ARCCORE_BASE_EXPORT void arccoreRangeError(Int64 i, Int64 min_value_inclusive, Int64 max_value_exclusive)
Signale qu'une valeur n'est pas dans l'intervalle souhaité.
ARCCORE_BASE_EXPORT void arccoreThrowNullPointerError(const char *ptr_name, const char *text)
Signalee l'utilisation d'un pointeur nul en envoyant une exception.
ARCCORE_BASE_EXPORT bool arccoreIsCheck()
Vrai si on est en mode vérification.
void _doAssert(const char *text, const char *file, const char *func, size_t line)
Definition Misc.cc:235
ARCCORE_BASE_EXPORT void arccoreSetPauseOnError(bool v)
Indique si on l'appel à arccoreDebugPause() effectue une pause.
ARCCORE_BASE_EXPORT void arccoreSetCheck(bool v)
Active ou désactive le mode vérification.
std::ostream & operator<<(std::ostream &ostr, eItemKind item_kind)
Opérateur de sortie sur un flot.