pTraits.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) 2011 OpenFOAM Foundation
9  Copyright (C) 2020-2025 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::pTraits
29 
30 Description
31  A traits class, which is primarily used for primitives and vector-space.
32 
33  All primitives need a specialised version of this class. The
34  specialised versions will normally also require a conversion
35  method.
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef Foam_pTraits_H
40 #define Foam_pTraits_H
41 
42 #include "direction.H"
43 #include <type_traits> // For std::integral_constant, std::void_t
44 #include <utility> // For declval
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 /*---------------------------------------------------------------------------*\
52  Class zero_one Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 //- Represents 0/1 range or concept. Used for tagged dispatch or clamping
56 class zero_one {};
57 
58 
59 /*---------------------------------------------------------------------------*\
60  Class pTraits Declaration
61 \*---------------------------------------------------------------------------*/
62 
63 // The base implementation is a pass-through to the base class.
64 // Accordingly it inherits all static methods (eg, typeName etc).
65 template<class Base>
66 class pTraits
67 :
68  public Base
69 {
70 public:
71 
72  // Constructors
73 
74  //- Copy construct from base class
75  explicit pTraits(const Base& obj)
76  :
77  Base(obj)
78  {}
79 
80  //- Construct from Istream
81  explicit pTraits(Istream& is)
82  :
83  Base(is)
84  {}
85 };
86 
87 
88 /*---------------------------------------------------------------------------*\
89  Robuster generalised pTraits
90 \*---------------------------------------------------------------------------*/
91 
92 //- The vector-space rank: default is 0.
93 template<class T, class = void>
94 struct pTraits_rank : std::integral_constant<Foam::direction, 0> {};
95 
96 //- Rank of VectorSpace,
97 //- using the pTraits \c rank static member.
98 template<class T>
99 struct pTraits_rank
100 <
101  T,
102  std::void_t<decltype(pTraits<std::remove_cv_t<T>>::rank)>
103 >
104 :
105  std::integral_constant<Foam::direction, pTraits<std::remove_cv_t<T>>::rank>
106 {};
107 
108 
109 //- The underlying component data type: default is pass-through.
110 template<class T, class = void>
111 struct pTraits_cmptType { typedef T type; };
112 
113 //- The underlying component data type for vector-space (or complex).
114 // Enabled when pTraits<T>:zero has a defined type (ie, exists),
115 // such as for arithmetic primitives, vector-space etc. where the concept
116 // of a component type also makes sense.
117 template<class T>
118 struct pTraits_cmptType
119 <
120  T,
121  std::void_t<decltype(pTraits<std::remove_cv_t<T>>::zero)>
122 >
123 {
124  using type = typename pTraits<std::remove_cv_t<T>>::cmptType;
125 };
127 
128 //- The vector-space number of components: default is 1.
129 template<class T, class = void>
130 struct pTraits_nComponents : std::integral_constant<Foam::direction, 1> {};
131 
132 //- Number of VectorSpace components,
133 //- using the pTraits \c nComponents static member.
134 template<class T>
135 struct pTraits_nComponents
136 <
137  T,
138  std::void_t<decltype(std::remove_cv_t<T>::nComponents)>
139 >
140 :
141  std::integral_constant
142  <
143  Foam::direction,
144  pTraits<std::remove_cv_t<T>>::nComponents
145  >
146 {};
148 
149 //- Test for pTraits zero : default is false
150 template<class T, class = void>
151 struct pTraits_has_zero : std::false_type {};
152 
153 //- Test for pTraits zero
154 template<class T>
155 struct pTraits_has_zero
156 <
157  T,
158  std::void_t<decltype(pTraits<std::remove_cv_t<T>>::zero)>
159 >
160 :
161  std::true_type
162 {};
163 
164 
165 /*---------------------------------------------------------------------------*\
166  VectorSpace Traits
167 \*---------------------------------------------------------------------------*/
168 
169 //- Test for VectorSpace : default is false
170 template<class T, class = void>
171 struct is_vectorspace : std::false_type {};
172 
173 //- Test for VectorSpace : test for T::rank != 0 static member directly
174 // Do not need pTraits layer since rank is defined via VectorSpace etc
175 template<class T>
176 struct is_vectorspace
177 <
178  T,
179  std::void_t<decltype(std::remove_cv_t<T>::rank)>
180 >
181 :
182  std::bool_constant<std::remove_cv_t<T>::rank != 0>
183 {};
184 
185 
186 //- Test for VectorSpace and multiple components (eg, not sphericalTensor)
187 //- which means it is not rotationally invariant
188 template<class T, class = void>
189 struct is_rotational_vectorspace : std::false_type {};
190 
191 //- Test for VectorSpace and multiple components (eg, not sphericalTensor)
192 // Do not need pTraits layer since rank and nComponents are both
193 // defined via VectorSpace
194 template<class T>
196 <
197  T,
198  std::void_t
199  <
200  decltype(std::remove_cv_t<T>::rank),
201  decltype(std::remove_cv_t<T>::nComponents)
202  >
203 >
204 :
205  std::bool_constant
206  <
207  (std::remove_cv_t<T>::rank != 0)
208  && (std::remove_cv_t<T>::nComponents > 1)
209  >
210 {};
211 
213 //- The is_vectorspace value of Type
214 template<class T>
215 inline constexpr bool is_vectorspace_v
217 
218 //- The is_rotational_vectorspace value of Type
219 template<class T>
220 inline constexpr bool is_rotational_vectorspace_v
222 
223 
224 /*---------------------------------------------------------------------------*\
225  Container Traits
226 \*---------------------------------------------------------------------------*/
227 
228 //- Test for containers with begin/end range iterators.
229 template<class T, class = void>
230 struct is_range : std::false_type {};
231 
232 //- Test for list containers with begin/end range iterators.
233 //- \attention May yield a false positive with HashTable, HashSet etc
234 template<class T>
235 struct is_range
236 <
237  T,
238  std::void_t
239  <
240  decltype(std::declval<T>().begin()),
241  decltype(std::declval<T>().end())
242  >
243 >
244 :
245  std::true_type
246 {};
247 
248 
249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
250 
251 } // End namespace Foam
252 
253 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
254 
255 #endif
256 
257 // ************************************************************************* //
constexpr bool is_rotational_vectorspace_v
The is_rotational_vectorspace value of Type.
Definition: pTraits.H:251
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
The vector-space number of components: default is 1.
Definition: pTraits.H:140
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:61
Test for VectorSpace and multiple components (eg, not sphericalTensor) which means it is not rotation...
Definition: pTraits.H:212
The vector-space rank: default is 0.
Definition: pTraits.H:95
constexpr bool is_vectorspace_v
The is_vectorspace value of Type.
Definition: pTraits.H:244
pTraits(const Base &obj)
Copy construct from base class.
Definition: pTraits.H:72
Test for VectorSpace : default is false.
Definition: pTraits.H:189
The underlying component data type: default is pass-through.
Definition: pTraits.H:116
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Represents 0/1 range or concept. Used for tagged dispatch or clamping.
Definition: pTraits.H:51
Direction is an 8-bit unsigned integer type used to represent Cartesian directions, components etc.
Test for containers with begin/end range iterators.
Definition: pTraits.H:262
Test for pTraits zero : default is false.
Definition: pTraits.H:165
Namespace for OpenFOAM.