Arcane  v3.15.0.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
Convert.h
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/* Convert.h (C) 2000-2025 */
9/* */
10/* Fonctions pour convertir un type en un autre. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_CONVERT_H
13#define ARCANE_UTILS_CONVERT_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18#include "arccore/base/StringView.h"
19
20#include <optional>
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane::Convert
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
31inline double
33{
34#ifdef ARCANE_REAL_USE_APFLOAT
35 return ap2double(r.ap);
36#else
37 return static_cast<double>(r);
38#endif
39}
40
41/*---------------------------------------------------------------------------*/
42/*---------------------------------------------------------------------------*/
44inline Integer
46{
47 return static_cast<Integer>(toDouble(r));
48}
49
50/*---------------------------------------------------------------------------*/
51/*---------------------------------------------------------------------------*/
53inline Int64
55{
56 return static_cast<Int64>(toDouble(r));
57}
58
59/*---------------------------------------------------------------------------*/
60/*---------------------------------------------------------------------------*/
62inline Int32
64{
65 return static_cast<Int32>(toDouble(r));
66}
67
68/*---------------------------------------------------------------------------*/
69/*---------------------------------------------------------------------------*/
70
72inline bool
73toBool(Real r)
74{
75 return static_cast<bool>(toDouble(r));
76}
77
79inline Real
80toReal(Real r)
81{
82 return r;
83}
85inline Real
87{
88 return static_cast<Real>(r);
89}
91inline Real
92toReal(unsigned int r)
93{
94 return static_cast<Real>(r);
95}
97inline Real
98toReal(long r)
99{
100 return static_cast<Real>(r);
101}
103inline Real
104toReal(unsigned long r)
105{
106 return static_cast<Real>(r);
107}
108
110inline Real
111toReal(long long r)
112{
113#ifdef ARCANE_REAL_USE_APFLOAT
114 return static_cast<Real>(static_cast<long>(r));
115#else
116 return static_cast<Real>(r);
117#endif
118}
120inline Real
121toReal(unsigned long long r)
122{
123#ifdef ARCANE_REAL_USE_APFLOAT
124 return static_cast<Real>(static_cast<unsigned long>(r));
125#else
126 return static_cast<Real>(r);
127#endif
128}
129
130/*---------------------------------------------------------------------------*/
131/*---------------------------------------------------------------------------*/
138extern ARCANE_UTILS_EXPORT String
140
141/*---------------------------------------------------------------------------*/
142/*---------------------------------------------------------------------------*/
149extern ARCANE_UTILS_EXPORT String
151
152/*---------------------------------------------------------------------------*/
153/*---------------------------------------------------------------------------*/
160extern ARCANE_UTILS_EXPORT String
161toHexaString(Real input);
162
163/*---------------------------------------------------------------------------*/
164/*---------------------------------------------------------------------------*/
172extern ARCANE_UTILS_EXPORT void
173toHexaString(Int64 input, Span<Byte> output);
174
175/*---------------------------------------------------------------------------*/
176/*---------------------------------------------------------------------------*/
177
178template <typename T>
179class Type;
180
181template <typename T>
183{
184 public:
185
187 ARCANE_UTILS_EXPORT static std::optional<T> tryParse(StringView s);
188
194 static std::optional<T>
196 {
197 return (s.empty()) ? default_value : tryParse(s);
198 }
199
208 ARCANE_UTILS_EXPORT static std::optional<T>
210};
211
213template <> class Type<Int64> : public ScalarType<Int64>
214{};
215template <> class Type<Int32> : public ScalarType<Int32>
216{};
217template <> class Type<Real> : public ScalarType<Real>
218{};
219
220/*---------------------------------------------------------------------------*/
221/*---------------------------------------------------------------------------*/
222
223} // namespace Arcane::Convert
224
225/*---------------------------------------------------------------------------*/
226/*---------------------------------------------------------------------------*/
227
228#endif
229
Déclarations des types utilisés dans Arcane.
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 > tryParseIfNotEmpty(StringView s, const T &default_value)
Convertit s en le type T.
Definition Convert.h:195
static std::optional< T > tryParse(StringView s)
Convertit s en le type T.
Definition Convert.cc:110
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:149
Vue constante d'un tableau de type T.
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.
Fonctions pour convertir un type en un autre.
Int64 toInt64(Real r)
Converti un Real en Int64.
Definition Convert.h:54
Integer toInteger(Real r)
Converti un Real en Integer.
Definition Convert.h:45
bool toBool(Real r)
Converti un Real en Integer.
Definition Convert.h:73
Real toReal(Real r)
Converti r en un Real.
Definition Convert.h:80
String toHexaString(ByteConstArrayView input)
Converti un tableau d'octet en sa représentation hexadécimale.
Definition Convert.cc:75
double toDouble(Real r)
Converti un Real en double.
Definition Convert.h:32
Int32 toInt32(Real r)
Converti un Real en Int32.
Definition Convert.h:63