exprTraits.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 Class
27  Foam::exprTypeTraits
28 
29 Description
30  Simple type identifiers for polymorphic expression values.
31  The definitions are similar to std::integral_constant in that they
32  provide value, value_type (and name).
33 
34 SourceFiles
35  exprTraits.C
36 
37 Namespace
38  Foam::expressions
39 
40 Description
41  A namespace for expression-related classes/traits etc.
42 
43 Namespace
44  Foam::expressions::Detail
45 
46 Description
47  A namespace for implementation details related to expressions.
48 
49 \*---------------------------------------------------------------------------*/
50 
51 #ifndef Foam_expressions_exprTraits_H
52 #define Foam_expressions_exprTraits_H
53 
54 // Regular field types
55 #include "label.H"
56 #include "scalar.H"
57 //TBD: #include "complex.H"
58 #include "vector.H"
59 #include "sphericalTensor.H"
60 #include "symmTensor.H"
61 #include "tensor.H"
62 #include "word.H"
63 
64 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 
66 namespace Foam
67 {
68 namespace expressions
69 {
70 
71 //- An enumeration of known and expected expression value types.
72 // Do not rely on the enumeration values for any direct coding.
73 //
74 // \note NONE used when initializing types, whereas INVALID is used
75 // for unsupported types (never as a stored type).
76 // This avoids false positives when testing.
77 //
78 // Except NONE and INVALID, the enumerations will mostly not be used
79 // directly, but through exprTypeTraits :: value
80 
81 enum class valueTypeCode : unsigned char
82 {
83  NONE = 0,
84  INVALID,
85 
86  // Rank 0 types
87  type_bool,
88  type_label,
89  type_scalar,
90 //TBD: type_complex, //!< Type is 'complex'
91 
92  // Rank 1 types
93  type_vector,
94 
95  // Rank 2 types
99 };
100 
101 
102 // Global Functions
103 
104 //- From string to valueTypeCode (if any)
106 (
107  const word& dataTypeName,
108  // Fallback for unknown
110 );
111 
112 
113 // Some implementation detail
114 namespace Detail
115 {
116 
117 //- The number of components associated with given valueTypeCode
119 
120 //- The vector-space rank associated with given valueTypeCode
122 
123 } // End namespace Detail
124 
125 
126 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
127 
128 } // End namespace expressions
129 
130 
131 /*---------------------------------------------------------------------------*\
132  Class exprTypeTraits Declaration
133 \*---------------------------------------------------------------------------*/
134 
135 // Generic enumerated traits is INVALID (unknown)
136 template<class Type>
137 struct exprTypeTraits
138 {
139  // The value type
140  typedef Type value_type;
141  // The type name (similar to pTraits typeName)
142  static constexpr const char* const name = "";
143  // The enumeration number associated with the type
144  static constexpr
147  // The rank of the type
149  // The number of components
151 };
152 
153 
154 #undef defineExprTypeTraits
155 #define defineExprTypeTraits(Type, Name, Rank, NumCmpts) \
156  \
157  template<> \
158  struct exprTypeTraits<Type> \
159  { \
160  typedef Type value_type; \
161  static constexpr const char* const name = #Name ; \
162  static constexpr \
163  ::Foam::expressions::valueTypeCode value = \
164  ::Foam::expressions::valueTypeCode::type_##Name ; \
165  static constexpr ::Foam::direction rank = Rank ; \
166  static constexpr ::Foam::direction nComponents = NumCmpts ; \
167  };
168 
169 
170 // Define with name to match pTraits::typeName, with rank/nComponents
171 defineExprTypeTraits(bool, bool, 0, 1);
172 defineExprTypeTraits(::Foam::label, label, 0, 1);
173 defineExprTypeTraits(::Foam::scalar, scalar, 0, 1);
174 //TBD: defineExprTypeTraits(::Foam::complex, complex, 0, 2);
180 #undef defineExprTypeTraits
182 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
184 //- A word representation of a valueTypeCode.
185 //- Empty for expressions::valueTypeCode::INVALID
186 word name(const expressions::valueTypeCode typeCode);
187 
188 
189 //- A word representation of a valueTypeCode.
190 //- Empty for expressions::valueTypeCode::INVALID
191 template<>
192 struct nameOp<expressions::valueTypeCode>
193 {
194  word operator()(const expressions::valueTypeCode typeCode) const
195  {
196  return Foam::name(typeCode);
197  }
198 };
199 
200 // No IOstream Operators for valueTypeCode at the moment (Nov 2021)
201 
202 } // End namespace Foam
203 
204 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
205 
206 #endif
207 
208 // ************************************************************************* //
uint8_t direction
Definition: direction.H:46
Tensor< scalar > tensor
Definition: symmTensor.H:57
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
Extract name (as a word) from an object, typically using its name() method.
Definition: word.H:340
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
static constexpr ::Foam::direction rank
Definition: exprTraits.H:154
valueTypeCode valueTypeCodeOf(const word &dataTypeName, const expressions::valueTypeCode deflt=expressions::valueTypeCode::INVALID)
From string to valueTypeCode (if any)
Definition: exprTraits.C:100
Vector< scalar > vector
Definition: vector.H:57
static constexpr ::Foam::expressions::valueTypeCode value
Definition: exprTraits.H:151
word operator()(const T &obj) const
Definition: word.H:342
const direction noexcept
Definition: Scalar.H:258
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
static constexpr const char *const name
Definition: exprTraits.H:148
::Foam::direction rank(const expressions::valueTypeCode) noexcept
The vector-space rank associated with given valueTypeCode.
Definition: exprTraits.C:70
#define defineExprTypeTraits(Type, Name, Rank, NumCmpts)
Definition: exprTraits.H:161
static constexpr ::Foam::direction nComponents
Definition: exprTraits.H:156
Tensor of scalars, i.e. Tensor<scalar>.
Namespace for OpenFOAM.