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/ForLoopRanges.h"
32#include "arccore/base/ParallelLoopOptions.h"
33
34#include <iostream>
35#include <cstring>
36#include <sstream>
37#include <cstdarg>
38
39#ifndef ARCCORE_OS_WIN32
40#include <unistd.h>
41#endif
42
43/*---------------------------------------------------------------------------*/
44/*---------------------------------------------------------------------------*/
45/*!
46 * \namespace Arccore
47 *
48 * \brief Espace de nom de %Arccore
49 *
50 * Toutes les classes et types utilisés dans \b Arccore sont dans ce
51 * namespace.
52 */
53/*---------------------------------------------------------------------------*/
54/*---------------------------------------------------------------------------*/
55
56namespace Arcane
57{
58/*---------------------------------------------------------------------------*/
59/*---------------------------------------------------------------------------*/
60
61namespace
62{
63#ifdef ARCCORE_CHECK
64static bool global_arccore_is_check = true;
65#else
66static bool global_arccore_is_check = false;
67#endif
68}
69
70extern "C++" ARCCORE_BASE_EXPORT
72{
73 return global_arccore_is_check;
74}
75
76extern "C++" ARCCORE_BASE_EXPORT
77void arccoreSetCheck(bool v)
78{
79 global_arccore_is_check = v;
80}
81
82/*---------------------------------------------------------------------------*/
83/*---------------------------------------------------------------------------*/
84
85namespace
86{
87bool global_pause_on_error = false;
88}
89
90extern "C++" ARCCORE_BASE_EXPORT void
92{
93 global_pause_on_error = v;
94}
95
96extern "C++" ARCCORE_BASE_EXPORT void
97arccoreDebugPause(const char* msg)
98{
99 if (global_pause_on_error){
100 std::ostringstream ostr;
101 String host_name(Platform::getHostName());
102 ostr << "** FATAL: Debug mode activated. Execution paused\n"
103 << "** FATAL: message:" << msg << "\n"
104 << "** FATAL: To find the location of the error, start\n"
105 << "** FATAL: start the debugger using the process number\n"
106 << "** FATAL: (pid=" << Platform::getProcessId() << ",host=" << host_name << ").\n";
107 std::cerr << ostr.str();
108#ifndef ARCCORE_OS_WIN32
109 ::pause();
110#endif
111 }
112}
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
116
117extern "C++" ARCCORE_BASE_EXPORT void
118arccoreRangeError(Int64 i,Int64 min_value_inclusive,Int64 max_value_exclusive)
119{
120 arccoreDebugPause("arccoreRangeError");
121 throw IndexOutOfRangeException(A_FUNCINFO,String(),i,min_value_inclusive,max_value_exclusive);
122}
123
124/*---------------------------------------------------------------------------*/
125/*---------------------------------------------------------------------------*/
126
127extern "C++" ARCCORE_BASE_EXPORT void
128arccoreRangeError(Int32 i,Int32 max_size)
129{
130 arccoreDebugPause("arccoreRangeError");
131 throw IndexOutOfRangeException(A_FUNCINFO,String(),i,0,max_size);
132}
133
134/*---------------------------------------------------------------------------*/
135/*---------------------------------------------------------------------------*/
136
137extern "C++" ARCCORE_BASE_EXPORT void
139{
140 arccoreDebugPause("arccoreRangeError");
141 throw IndexOutOfRangeException(A_FUNCINFO,String(),i,0,max_size);
142}
143
144/*---------------------------------------------------------------------------*/
145/*---------------------------------------------------------------------------*/
146
147extern "C++" ARCCORE_BASE_EXPORT void
149{
150 std::cerr << "** FATAL: null pointer.\n";
151 std::cerr << "** FATAL: Trying to dereference a null pointer.\n";
152 arccoreDebugPause("arcaneNullPointerPtr");
153 throw FatalErrorException(A_FUNCINFO,"null pointer");
154}
155
156/*---------------------------------------------------------------------------*/
157/*---------------------------------------------------------------------------*/
158
159extern "C++" ARCCORE_BASE_EXPORT void
160arccoreThrowNullPointerError(const char* ptr_name,const char* text)
161{
162 throw FatalErrorException(A_FUNCINFO,text ? text : ptr_name);
163}
164
165/*---------------------------------------------------------------------------*/
166/*---------------------------------------------------------------------------*/
167
168// Cette fonction peut être appelée souvent et certaines fois
169// dans des conditions d'exceptions. Pour cette raison, il ne
170// faut pas qu'elle fasse d'allocations.
171namespace
172{
173void _printFuncName(std::ostream& o,const char* name)
174{
175 const char* par_pos = std::strchr(name,'(');
176 if (!par_pos){
177 o << name;
178 return;
179 }
180
181 // Recherche quelque chose du type namespace::class_name::func_name
182 // et essaye de ne conserver que class_name::func_name
183 ptrdiff_t len = par_pos - name;
184 ptrdiff_t last_scope = 0;
185 ptrdiff_t last_scope2 = 0;
186 for( ptrdiff_t i=0; i<len; ++i ){
187 if (name[i]==':' && name[i+1]==':'){
188 last_scope2 = last_scope;
189 last_scope = i;
190 }
191 }
192 if (last_scope2!=0)
193 last_scope2+=2;
194 ptrdiff_t true_pos = last_scope2;
195 ptrdiff_t true_len = len - true_pos;
196 o.write(&name[true_pos],true_len);
197 o << "()";
198}
199}
200
201/*---------------------------------------------------------------------------*/
202/*---------------------------------------------------------------------------*/
203
204extern "C++" ARCCORE_BASE_EXPORT std::ostream&
205operator<<(std::ostream& o,const TraceInfo& t)
206{
207 if (t.printSignature())
208 o << t.name() << ":" << t.line();
209 else{
210 _printFuncName(o,t.name());
211 }
212 return o;
213}
214
215/*---------------------------------------------------------------------------*/
216/*---------------------------------------------------------------------------*/
217
218/*---------------------------------------------------------------------------*/
219/*---------------------------------------------------------------------------*/
220
221namespace
222{
223/// Fonction appelée lorsqu'une assertion échoue.
224typedef void (*fDoAssert)(const char*,const char*,const char*,size_t);
225/// Fonction appelée pour indiquer s'il faut afficher l'information de débug
226typedef bool (*fCheckDebug)(unsigned int);
227
228fDoAssert g_do_assert_func = 0;
229}
230
231/*---------------------------------------------------------------------------*/
232/*---------------------------------------------------------------------------*/
233/*!
234 * Affichage d'une assertion ayant échouée.
235 */
236extern "C++" ARCCORE_BASE_EXPORT void
237_doAssert(const char* text,const char* file,const char* func,int line)
238{
239 if (g_do_assert_func)
240 (*g_do_assert_func)(text,file,func,line);
241 else{
242 std::ostringstream ostr;
243 ostr << text << ':' << file << ':' << func << ':' << line << ": ";
244 throw FatalErrorException("Assert",ostr.str());
245 }
246}
247
248/*---------------------------------------------------------------------------*/
249/*---------------------------------------------------------------------------*/
250
251extern "C++" ARCCORE_BASE_EXPORT void
252arccorePrintf(const char* format,...)
253{
254 // \n écrit en meme temps pour éviter des écritures intermédiares parasites
255 char buffer[4096];
256 va_list ap;
257 va_start(ap,format);
258 vsnprintf(buffer,4095,format,ap);
259 va_end(ap);
260 std::cerr << buffer << "\n";
261 std::cout << "*E* " << buffer << "\n";
262}
263
264/*---------------------------------------------------------------------------*/
265/*---------------------------------------------------------------------------*/
266
267} // End namespace Arccore
268
269/*---------------------------------------------------------------------------*/
270/*---------------------------------------------------------------------------*/
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.