Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
10License
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/>.
26\*---------------------------------------------------------------------------*/
27
28#include "exprValue.H"
29
30// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
31
33(
34 const expressions::valueTypeCode valTypeCode
35) noexcept
36{
37 return
38 (
41 );
42}
43
44
46(
47 const expressions::valueTypeCode valTypeCode
48) noexcept
49{
50 return
51 (
54 );
55}
56
57
59(
61) noexcept
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
76template<class Type>
78{
79 return
80 (
82 );
83}
84
85
86// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
87
88inline void Foam::expressions::exprValue::fill_zero()
89{
90 // Preserve the type
91 const auto oldType(typeCode_);
93 typeCode_ = oldType;
94}
95
96
97// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
100{
101 clear(); // zero-initialized (type is NONE)
102}
103
104
106{
107 deepCopy(rhs);
108}
109
110
111template<class Type>
112inline 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
142inline Foam::direction
147
148
149inline Foam::direction
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
175template<class Type>
176inline 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
197template<class Type>
198inline 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;
210}
211
212
213// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
214
215template<class Type>
216inline 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// ************************************************************************* //
exprValue()
Default construct (zero-initialized) as 'none'.
Definition exprValueI.H:92
bool set(const Type &val)
Assign from type. Returns false and sets to 'none' for unsupported types.
Definition exprValueI.H:169
bool is_integral() const noexcept
True if the value type is an integral (bool or label).
Definition exprValueI.H:129
const Type & get() const
Return the value. Return pTraits zero if the types mismatch.
Definition exprValueI.H:191
word listCompoundName() const
The name for the compound token (for a List of values). Eg, List<scalar>, List<vector> ....
Definition exprValueI.H:156
bool good() const noexcept
True if the value type is not none/invalid.
Definition exprValueI.H:123
static bool supportedType()
True if the specified type is supported.
Definition exprValueI.H:70
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
word valueTypeName() const
The name for the value type. Similar to pTraits typeName.
Definition exprValueI.H:150
direction nComponents() const noexcept
The number of components associated with the value type.
Definition exprValueI.H:136
direction rank() const noexcept
The vector-space rank associated with the value type.
Definition exprValueI.H:143
static bool good(const expressions::valueTypeCode) noexcept
True if valueTypeCode is not none/invalid.
Definition exprValueI.H:26
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
A class for handling words, derived from Foam::string.
Definition word.H:66
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
auto & name
surface1 clear()
::Foam::direction nComponents(const expressions::valueTypeCode) noexcept
The number of components associated with given valueTypeCode.
Definition exprTraits.C:40
::Foam::direction rank(const expressions::valueTypeCode) noexcept
The vector-space rank associated with given valueTypeCode.
Definition exprTraits.C:70
valueTypeCode
An enumeration of known and expected expression value types.
Definition exprTraits.H:82
@ NONE
No type, or default initialized type.
Definition exprTraits.H:83
@ INVALID
Invalid/unknown/error type.
Definition exprTraits.H:84
const Type * isA(const U &obj)
Attempt dynamic_cast to Type.
Definition typeInfo.H:87
errorManip< error > abort(error &err)
Definition errorManip.H:139
uint8_t direction
Definition direction.H:49
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
const direction noexcept
Definition scalarImpl.H:265
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
static constexpr::Foam::expressions::valueTypeCode value
Definition exprTraits.H:151