Arcane  v3.14.10.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
SmallVariant.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 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/* Type de base polymorphe. */
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
25ARCANE_BEGIN_NAMESPACE
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29/*!
30 * \brief Classe gérant un type polymorphe.
31 */
33{
34 private:
35
36 public:
37
38 enum eType
39 {
40 TUnknown = 0,
41 TReal= 1,
42 TInt32 = 2,
43 TInt64 = 3,
44 TBool = 4,
45 TString = 5
46 };
47
48 public:
49
50 static inline int convertFromReal(int,Real v)
51 {
52 if (v>Convert::toReal(INT_MAX))
53 return INT_MAX;
54 if (v<Convert::toReal(INT_MIN))
55 return INT_MIN;
56 return (int)(Convert::toDouble(v));
57 }
58 static inline unsigned int convertFromReal(unsigned int,Real v)
59 {
60 if (v>Convert::toReal(UINT_MAX))
61 return UINT_MAX;
62 if (v<0.0)
63 return 0;
64 return (unsigned int)(Convert::toDouble(v));
65 }
66 static inline long convertFromReal(long,Real v)
67 {
68 if (v>Convert::toReal(LONG_MAX))
69 return LONG_MAX;
70 if (v<Convert::toReal(LONG_MIN))
71 return LONG_MIN;
72 return (long)(Convert::toDouble(v));
73 }
74 static inline unsigned long convertFromReal(unsigned long,Real v)
75 {
76 if (v>Convert::toReal(ULONG_MAX))
77 return ULONG_MAX;
78 if (v<0.0)
79 return 0;
80 return (unsigned long)(Convert::toDouble(v));
81 }
82 static inline long long convertFromReal(long long,Real v)
83 {
84 if (v>Convert::toReal(LLONG_MAX))
85 return LLONG_MAX;
86 if (v<Convert::toReal(LLONG_MIN))
87 return LLONG_MIN;
88 return (long long)(Convert::toDouble(v));
89 }
90 static inline unsigned long long convertFromReal(unsigned long long,Real v)
91 {
92 if (v>Convert::toReal(ULLONG_MAX))
93 return ULLONG_MAX;
94 if (v<0.0)
95 return 0;
96 return (unsigned long long)(Convert::toDouble(v));
97 }
98
99 public:
100
102 : m_real_value(0.), m_int32_value(0), m_int64_value(0),
103 m_bool_value(false), m_sticky_type(TUnknown) {}
104 SmallVariant(Real v)
105 : m_real_value(v), m_int32_value(0), m_int64_value(0),
106 m_bool_value(false), m_sticky_type(TReal) {}
107 SmallVariant(Int32 v)
108 : m_real_value(0.), m_int32_value(v), m_int64_value(0),
109 m_bool_value(false), m_sticky_type(TInt32) {}
110 SmallVariant(Int64 v)
111 : m_real_value(0.), m_int32_value(0), m_int64_value(v),
112 m_bool_value(false), m_sticky_type(TInt64) {}
113 SmallVariant(bool v)
114 : m_real_value(0.), m_int32_value(0), m_int64_value(v),
115 m_bool_value(v), m_sticky_type(TBool) {}
116 SmallVariant(const String& v)
117 : m_real_value(0.), m_int32_value(0), m_int64_value(0),
118 m_bool_value(false), m_string_value(v), m_sticky_type(TString) {}
119
120 void setValue(Real v) { m_real_value = v; m_sticky_type = TReal; }
121 void setValue(Int32 v) { m_int32_value = v; m_sticky_type = TInt32; }
122 void setValue(Int64 v) { m_int64_value = v; m_sticky_type = TInt64; }
123 void setValue(const String& v) { m_string_value = v; m_sticky_type = TString; }
124 void setValue(bool v) { m_bool_value = v; m_sticky_type = TBool; }
125
126 void setValueAll(Real v)
127 {
128 m_sticky_type = TReal;
129 m_real_value = v;
130 m_int32_value = convertFromReal(m_int32_value,v);
131 m_int64_value = convertFromReal(m_int64_value,v);
132 m_bool_value = v!=0.;
133 m_string_value = String::fromNumber(m_real_value);
134 }
135 void setValueAll(Int32 v)
136 {
137 m_sticky_type = TInt32;
138 m_real_value = Convert::toReal(v);
139 m_int32_value = v;
140 m_int64_value = (Int64)(v);
141 m_bool_value = v!=0;
142 m_string_value = String::fromNumber(m_int32_value);
143 }
144 void setValueAll(Int64 v)
145 {
146 m_sticky_type = TInt64;
147 m_real_value = Convert::toReal(v);
148 m_int32_value = (Int32)(v);
149 m_int64_value = v;
150 m_bool_value = v!=0;
151 m_string_value = String::fromNumber(m_int64_value);
152 }
153 void setValueAll(bool v)
154 {
155 m_sticky_type = TBool;
156 m_real_value = Convert::toReal(v);
157 m_int32_value = v ? 1 : 0;
158 m_int64_value = v ? 1 : 0;
159 m_bool_value = v;
160 m_string_value = String::fromNumber(m_int64_value);
161 }
162
163 void value(bool& v) const { v = m_bool_value; }
164 void value(Real& v) const { v = m_real_value; }
165 void value(Int32& v) const { v = m_int32_value; }
166 void value(Int64& v) const { v = m_int64_value; }
167 void value(String& v) const { v = m_string_value; }
168
169 bool asBool() const { return m_bool_value; }
170 Real asReal() const { return m_real_value; }
171 Integer asInteger() const;
172 Int32 asInt32() const { return m_int32_value; }
173 Int64 asInt64() const { return m_int64_value; }
174 const String& asString() const { return m_string_value; }
175 eType type() const { return m_sticky_type; }
176
177 private:
178
179 Real m_real_value; //!< Valeur de type réelle
180 Int32 m_int32_value; //!< Valeur de type entier
181 Int64 m_int64_value; //!< Valeur de type entier naturel
182 bool m_bool_value; //!< Valeur de type entier booléenne
183 String m_string_value; //!< Valeur de type chaîne de caractère.
184 eType m_sticky_type; //!< Type garanti valide de la valeur.
185};
186
187/*---------------------------------------------------------------------------*/
188/*---------------------------------------------------------------------------*/
189
190template<class Type>
192{
193 public:
194 VariantGetterT() {}
195 virtual ~VariantGetterT() {}
196 static Type asType(const SmallVariant& v)
197 { Type t; v.value(t); return t; }
198};
199
200/*---------------------------------------------------------------------------*/
201/*---------------------------------------------------------------------------*/
202
203ARCANE_END_NAMESPACE
204
205/*---------------------------------------------------------------------------*/
206/*---------------------------------------------------------------------------*/
207
208#endif
209
Classe gérant un type polymorphe.
Chaîne de caractères unicode.