Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
SmallVariant.h
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/* SmallVariant.h (C) 2000-2017 */
9/* */
10/* Polymorphic base type. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_DATATYPE_SMALLVARIANT_H
14#define ARCANE_DATATYPE_SMALLVARIANT_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/utils/String.h"
19#include "arcane/utils/Convert.h"
20#include <climits>
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31/*!
32 * \brief Class managing a polymorphic type.
33 */
34class SmallVariant
35{
36 private:
37 public:
38
39 enum eType
40 {
41 TUnknown = 0,
42 TReal = 1,
43 TInt32 = 2,
44 TInt64 = 3,
45 TBool = 4,
46 TString = 5
47 };
48
49 public:
50
51 static inline int convertFromReal(int, Real v)
52 {
53 if (v > Convert::toReal(INT_MAX))
54 return INT_MAX;
55 if (v < Convert::toReal(INT_MIN))
56 return INT_MIN;
57 return (int)(Convert::toDouble(v));
58 }
59 static inline unsigned int convertFromReal(unsigned int, Real v)
60 {
61 if (v > Convert::toReal(UINT_MAX))
62 return UINT_MAX;
63 if (v < 0.0)
64 return 0;
65 return (unsigned int)(Convert::toDouble(v));
66 }
67 static inline long convertFromReal(long, Real v)
68 {
69 if (v > Convert::toReal(LONG_MAX))
70 return LONG_MAX;
71 if (v < Convert::toReal(LONG_MIN))
72 return LONG_MIN;
73 return (long)(Convert::toDouble(v));
74 }
75 static inline unsigned long convertFromReal(unsigned long, Real v)
76 {
77 if (v > Convert::toReal(ULONG_MAX))
78 return ULONG_MAX;
79 if (v < 0.0)
80 return 0;
81 return (unsigned long)(Convert::toDouble(v));
82 }
83 static inline long long convertFromReal(long long, Real v)
84 {
85 if (v > Convert::toReal(LLONG_MAX))
86 return LLONG_MAX;
87 if (v < Convert::toReal(LLONG_MIN))
88 return LLONG_MIN;
89 return (long long)(Convert::toDouble(v));
90 }
91 static inline unsigned long long convertFromReal(unsigned long long, Real v)
92 {
93 if (v > Convert::toReal(ULLONG_MAX))
94 return ULLONG_MAX;
95 if (v < 0.0)
96 return 0;
97 return (unsigned long long)(Convert::toDouble(v));
98 }
99
100 public:
101
102 SmallVariant()
103 : m_real_value(0.)
104 , m_int32_value(0)
105 , m_int64_value(0)
106 , m_bool_value(false)
107 , m_sticky_type(TUnknown)
108 {}
109 SmallVariant(Real v)
110 : m_real_value(v)
111 , m_int32_value(0)
112 , m_int64_value(0)
113 , m_bool_value(false)
114 , m_sticky_type(TReal)
115 {}
116 SmallVariant(Int32 v)
117 : m_real_value(0.)
118 , m_int32_value(v)
119 , m_int64_value(0)
120 , m_bool_value(false)
121 , m_sticky_type(TInt32)
122 {}
123 SmallVariant(Int64 v)
124 : m_real_value(0.)
125 , m_int32_value(0)
126 , m_int64_value(v)
127 , m_bool_value(false)
128 , m_sticky_type(TInt64)
129 {}
130 SmallVariant(bool v)
131 : m_real_value(0.)
132 , m_int32_value(0)
133 , m_int64_value(v)
134 , m_bool_value(v)
135 , m_sticky_type(TBool)
136 {}
137 SmallVariant(const String& v)
138 : m_real_value(0.)
139 , m_int32_value(0)
140 , m_int64_value(0)
141 , m_bool_value(false)
142 , m_string_value(v)
143 , m_sticky_type(TString)
144 {}
145
146 void setValue(Real v)
147 {
148 m_real_value = v;
149 m_sticky_type = TReal;
150 }
151 void setValue(Int32 v)
152 {
153 m_int32_value = v;
154 m_sticky_type = TInt32;
155 }
156 void setValue(Int64 v)
157 {
158 m_int64_value = v;
159 m_sticky_type = TInt64;
160 }
161 void setValue(const String& v)
162 {
163 m_string_value = v;
164 m_sticky_type = TString;
165 }
166 void setValue(bool v)
167 {
168 m_bool_value = v;
169 m_sticky_type = TBool;
170 }
171
172 void setValueAll(Real v)
173 {
174 m_sticky_type = TReal;
175 m_real_value = v;
176 m_int32_value = convertFromReal(m_int32_value, v);
177 m_int64_value = convertFromReal(m_int64_value, v);
178 m_bool_value = v != 0.;
179 m_string_value = String::fromNumber(m_real_value);
180 }
181 void setValueAll(Int32 v)
182 {
183 m_sticky_type = TInt32;
184 m_real_value = Convert::toReal(v);
185 m_int32_value = v;
186 m_int64_value = (Int64)(v);
187 m_bool_value = v != 0;
188 m_string_value = String::fromNumber(m_int32_value);
189 }
190 void setValueAll(Int64 v)
191 {
192 m_sticky_type = TInt64;
193 m_real_value = Convert::toReal(v);
194 m_int32_value = (Int32)(v);
195 m_int64_value = v;
196 m_bool_value = v != 0;
197 m_string_value = String::fromNumber(m_int64_value);
198 }
199 void setValueAll(bool v)
200 {
201 m_sticky_type = TBool;
202 m_real_value = Convert::toReal(v);
203 m_int32_value = v ? 1 : 0;
204 m_int64_value = v ? 1 : 0;
205 m_bool_value = v;
206 m_string_value = String::fromNumber(m_int64_value);
207 }
208
209 void value(bool& v) const { v = m_bool_value; }
210 void value(Real& v) const { v = m_real_value; }
211 void value(Int32& v) const { v = m_int32_value; }
212 void value(Int64& v) const { v = m_int64_value; }
213 void value(String& v) const { v = m_string_value; }
214
215 bool asBool() const { return m_bool_value; }
216 Real asReal() const { return m_real_value; }
217 Integer asInteger() const;
218 Int32 asInt32() const { return m_int32_value; }
219 Int64 asInt64() const { return m_int64_value; }
220 const String& asString() const { return m_string_value; }
221 eType type() const { return m_sticky_type; }
222
223 private:
224
225 Real m_real_value; //!< Real type value
226 Int32 m_int32_value; //!< Integer type value
227 Int64 m_int64_value; //!< Natural integer type value
228 bool m_bool_value; //!< Boolean type value
229 String m_string_value; //!< Character string type value.
230 eType m_sticky_type; //!< Guaranteed valid type of the value.
231};
232
233/*---------------------------------------------------------------------------*/
234/*---------------------------------------------------------------------------*/
235
236template <class Type>
237class VariantGetterT
238{
239 public:
240
241 VariantGetterT() {}
242 virtual ~VariantGetterT() {}
243 static Type asType(const SmallVariant& v)
244 {
245 Type t;
246 v.value(t);
247 return t;
248 }
249};
250
251/*---------------------------------------------------------------------------*/
252/*---------------------------------------------------------------------------*/
253
254} // namespace Arcane
255
256/*---------------------------------------------------------------------------*/
257/*---------------------------------------------------------------------------*/
258
259#endif
Class managing a polymorphic type.
Real toReal(Real r)
Converts r to a Real.
double toDouble(Real r)
Converts a Real to double.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
std::int32_t Int32
Signed integer type of 32 bits.