Arcane  v3.16.0.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
Convert.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/*---------------------------------------------------------------------------*/
8/* Convert.cc (C) 2000-2025 */
9/* */
10/* Fonctions pour convertir un type en un autre. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/Convert.h"
15
16#include "arcane/utils/ValueConvert.h"
17#include "arcane/utils/String.h"
18#include "arcane/utils/Array.h"
19#include "arcane/utils/PlatformUtils.h"
20#include "arcane/utils/FatalErrorException.h"
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24/*!
25 * \brief Fonctions pour convertir un type en un autre.
26 * \namespace Arcane::Convert
27 */
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32namespace Arcane
33{
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
38static char global_hexa[16] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9',
39 'a', 'b', 'c', 'd', 'e', 'f' };
40
41/*---------------------------------------------------------------------------*/
42/*---------------------------------------------------------------------------*/
43
44namespace
45{
47_toHexaString(Span<const std::byte> input)
48{
49 UniqueArray<Byte> out_buf;
50 Int64 len = input.size();
51 out_buf.resize((len*2)+1);
52 for( Int64 i=0; i<len; ++i ){
53 int v = std::to_integer<int>(input[i]);
54 out_buf[(i*2)] = global_hexa[v/16];
55 out_buf[(i*2)+1] = global_hexa[v%16];
56 }
57 out_buf[len*2] = '\0';
58 return String(out_buf);
59}
60}
61
62/*---------------------------------------------------------------------------*/
63/*---------------------------------------------------------------------------*/
64
67{
68 return _toHexaString(input);
69}
70
71/*---------------------------------------------------------------------------*/
72/*---------------------------------------------------------------------------*/
73
76{
77 return _toHexaString(asBytes(Span<const Byte>(input)));
78}
79
80/*---------------------------------------------------------------------------*/
81/*---------------------------------------------------------------------------*/
82
84toHexaString(Int64 input,Span<Byte> output)
85{
86 for (Integer i=0; i<8; ++i ){
87 Byte v = (Byte)(input % 256);
88 output[(i*2)] = global_hexa[v/16];
89 output[(i*2)+1] = global_hexa[v%16];
90 input = input / 256;
91 }
92}
93
94/*---------------------------------------------------------------------------*/
95/*---------------------------------------------------------------------------*/
96
98toHexaString(Real input)
99{
100 return toHexaString(ByteConstArrayView(sizeof(Real),(Byte*)&input));
101}
102
103/*---------------------------------------------------------------------------*/
104/*---------------------------------------------------------------------------*/
105
106namespace Convert
107{
108
109template<typename T> std::optional<T>
111{
112 T v;
113 if (s.empty())
114 return std::nullopt;
115 bool is_bad = builtInGetValue(v,s);
116 if (is_bad)
117 return std::nullopt;
118 return v;
119}
120
121template<typename T> std::optional<T>
123{
125 if (env_value.null())
126 return std::nullopt;
127 auto v = tryParse(env_value);
128 if (!v && throw_if_invalid)
129 ARCANE_FATAL("Invalid value '{0}' for environment variable {1}. Can not convert to type '{2}'",
130 env_value,s,typeToName(T{}));
131 return v;
132}
133
134template class ScalarType<Int32>;
135template class ScalarType<Int64>;
136template class ScalarType<Real>;
137
138}
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
142
143} // End namespace Arcane
144
145/*---------------------------------------------------------------------------*/
146/*---------------------------------------------------------------------------*/
147
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
void resize(Int64 s)
Change le nombre d'éléments du tableau à s.
static std::optional< T > tryParseFromEnvironment(StringView s, bool throw_if_invalid)
Convertit la valeur de la variable d'environnement s en le type T.
Definition Convert.cc:122
static std::optional< T > tryParse(StringView s)
Convertit s en le type T.
Definition Convert.cc:110
constexpr __host__ __device__ SizeType size() const noexcept
Retourne la taille du tableau.
Definition Span.h:212
Vue d'un tableau d'éléments de type T.
Definition Span.h:513
Vue sur une chaîne de caractères UTF-8.
Definition StringView.h:47
constexpr bool empty() const ARCCORE_NOEXCEPT
Vrai si la chaîne est nulle ou vide.
Definition StringView.h:105
Chaîne de caractères unicode.
bool null() const
Retourne true si la chaîne est nulle.
Definition String.cc:304
Vecteur 1D de données avec sémantique par valeur (style STL).
Fonctions pour convertir un type en un autre.
String toHexaString(ByteConstArrayView input)
Converti un tableau d'octet en sa représentation hexadécimale.
Definition Convert.cc:75
ARCCORE_BASE_EXPORT String getEnvironmentVariable(const String &name)
Variable d'environnement du nom name.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
std::int64_t Int64
Type entier signé sur 64 bits.
Int32 Integer
Type représentant un entier.
impl::SpanTypeFromSize< conststd::byte, SizeType >::SpanType asBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converti la vue en un tableau d'octets non modifiables.
Definition Span.h:884
double Real
Type représentant un réel.
ConstArrayView< Byte > ByteConstArrayView
Equivalent C d'un tableau à une dimension de caractères.
Definition UtilsTypes.h:563
unsigned char Byte
Type d'un octet.
Definition BaseTypes.h:43