exprValueI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2021-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "exprValue.H"
29 
30 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
31 
33 (
34  const expressions::valueTypeCode valTypeCode
35 ) noexcept
36 {
37  return
38  (
39  valTypeCode != expressions::valueTypeCode::NONE
40  && valTypeCode != expressions::valueTypeCode::INVALID
41  );
42 }
43 
44 
46 (
47  const expressions::valueTypeCode valTypeCode
48 ) noexcept
49 {
50  return
51  (
52  valTypeCode == expressions::valueTypeCode::type_bool
53  || valTypeCode == expressions::valueTypeCode::type_label
54  );
55 }
56 
57 
59 (
60  const expressions::valueTypeCode valTypeCode
62 {
63  return expressions::Detail::nComponents(valTypeCode);
64 }
65 
66 
68 (
69  const expressions::valueTypeCode valTypeCode
71 {
72  return expressions::Detail::rank(valTypeCode);
73 }
74 
75 
76 template<class Type>
78 {
79  return
80  (
82  );
83 }
84 
85 
86 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
87 
88 inline void Foam::expressions::exprValue::fill_zero()
89 {
90  // Preserve the type
91  const auto oldType(typeCode_);
92  clear();
93  typeCode_ = oldType;
94 }
95 
96 
97 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
98 
100 {
101  clear(); // zero-initialized (type is NONE)
102 }
103 
104 
106 {
107  deepCopy(rhs);
108 }
109 
110 
111 template<class Type>
112 inline Foam::expressions::exprValue::exprValue(const Type& val)
113 {
114  clear(); // type: none, value: zero
115 
116  set<Type>(val);
117 
118  if (!good())
119  {
121  << "Cannot construct for unsupported type: "
122  << typeid(Type).name() << nl
124  }
125 }
126 
127 
128 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
131 {
132  return exprValue::good(typeCode_);
133 }
134 
135 
137 {
138  return exprValue::is_integral(typeCode_);
139 }
140 
141 
142 inline Foam::direction
144 {
145  return expressions::Detail::nComponents(typeCode_);
146 }
147 
148 
149 inline Foam::direction
151 {
152  return expressions::Detail::rank(typeCode_);
153 }
154 
155 
158 {
159  return Foam::name(typeCode_);
160 }
161 
162 
164 {
165  return
166  (
167  good()
168  // List<scalar>, List<vector> ...
169  ? word("List<" + Foam::name(typeCode_) + '>')
170  : word()
171  );
172 }
173 
174 
175 template<class Type>
176 inline bool Foam::expressions::exprValue::set(const Type& val)
177 {
178  const auto whichCode(exprTypeTraits<Type>::value);
179 
180  if
181  (
184  )
185  {
186  clear();
188  return false;
189  }
190 
191  data_.set<Type>(val);
192  typeCode_ = whichCode;
193  return true;
194 }
195 
196 
197 template<class Type>
198 inline const Type& Foam::expressions::exprValue::get() const
199 {
200  const Type* dataPtr = this->isA<Type>();
201 
202  if (dataPtr)
203  {
204  return *dataPtr;
205  }
206  else
207  {
208  return pTraits<Type>::zero;
209  }
210 }
211 
212 
213 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
214 
215 template<class Type>
216 inline const Type* Foam::expressions::exprValue::isA() const noexcept
217 {
218  // NOTE: typeCode_ is *never* set to INVALID,
219  // so no false positives for unsupported types
220  return
221  (
222  (typeCode_ == exprTypeTraits<Type>::value)
223  ? data_.get<Type>()
224  : nullptr
225  );
226 }
227 
228 
229 // ************************************************************************* //
const Type * isA() const noexcept
Return non-null pointer to the data element (if types match). Can also be tested as a bool...
Definition: exprValueI.H:209
bool good() const noexcept
True if the value type is not none/invalid.
Definition: exprValueI.H:123
uint8_t direction
Definition: direction.H:46
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
const Type & get() const
Return the value. Return pTraits zero if the types mismatch.
Definition: exprValueI.H:191
direction nComponents() const noexcept
The number of components associated with the value type.
Definition: exprValueI.H:136
A polymorphic typed union of simple primitive and VectorSpace types. It uses a &#39;fatter&#39; representatio...
Definition: exprValue.H:164
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
No type, or default initialized type.
::Foam::direction nComponents(const expressions::valueTypeCode) noexcept
The number of components associated with given valueTypeCode.
Definition: exprTraits.C:40
bool set(const Type &val)
Assign from type. Returns false and sets to &#39;none&#39; for unsupported types.
Definition: exprValueI.H:169
static bool supportedType()
True if the specified type is supported.
Definition: exprValueI.H:70
Invalid/unknown/error type.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
valueTypeCode
An enumeration of known and expected expression value types.
Definition: exprTraits.H:81
direction rank() const noexcept
The vector-space rank associated with the value type.
Definition: exprValueI.H:143
bool is_integral() const noexcept
True if the value type is an integral (bool or label)
Definition: exprValueI.H:129
patchWriters clear()
errorManip< error > abort(error &err)
Definition: errorManip.H:139
const direction noexcept
Definition: Scalar.H:258
word valueTypeName() const
The name for the value type. Similar to pTraits typeName.
Definition: exprValueI.H:150
Simple type identifiers for polymorphic expression values. The definitions are similar to std::integr...
Definition: exprTraits.H:143
word listCompoundName() const
The name for the compound token (for a List of values). Eg, List<scalar>, List<vector> ...
Definition: exprValueI.H:156
::Foam::direction rank(const expressions::valueTypeCode) noexcept
The vector-space rank associated with given valueTypeCode.
Definition: exprTraits.C:70
exprValue()
Default construct (zero-initialized) as &#39;none&#39;.
Definition: exprValueI.H:92