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-2023 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 (C++17)
44 #include <utility> // For declval
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace stdFoam
49 {
50 
51 //- Map a sequence of any types to \c void as per C++17 \c std::void_t
52 template<class... >
53 using void_t = void;
54 
55 } // End namespace stdFoam
56 
57 
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 
60 namespace Foam
61 {
62 
63 /*---------------------------------------------------------------------------*\
64  Class zero_one Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 //- Represents 0/1 range or concept. Used for tagged dispatch or clamping
68 class zero_one {};
69 
70 
71 /*---------------------------------------------------------------------------*\
72  Class pTraits Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 // The base implementation is a pass-through to the base class.
76 // Accordingly it inherits all static methods (eg, typeName etc).
77 template<class Base>
78 class pTraits
79 :
80  public Base
81 {
82 public:
83 
84  // Constructors
85 
86  //- Copy construct from base class
87  explicit pTraits(const Base& obj)
88  :
89  Base(obj)
90  {}
91 
92  //- Construct from Istream
93  explicit pTraits(Istream& is)
94  :
95  Base(is)
96  {}
97 };
98 
99 
100 /*---------------------------------------------------------------------------*\
101  VectorSpace Traits
102 \*---------------------------------------------------------------------------*/
103 
104 //- Test for VectorSpace : default is false
105 template<class T, class = void>
106 struct is_vectorspace : std::false_type {};
107 
108 //- Test for VectorSpace : test for T::rank != 0 static member directly
109 // Do not need pTraits layer since rank is defined via VectorSpace etc
110 template<class T>
111 struct is_vectorspace<T, stdFoam::void_t<decltype(T::rank)>>
112 :
113  std::integral_constant<bool, (T::rank != 0)>
114 {};
115 
116 
117 //- The vector-space rank: default is 0.
118 template<class T, class = void>
119 struct pTraits_rank : std::integral_constant<Foam::direction, 0> {};
120 
121 //- Rank of VectorSpace,
122 //- using the pTraits \c rank static member.
123 template<class T>
124 struct pTraits_rank
125 <
126  T,
127  stdFoam::void_t<decltype(pTraits<T>::rank)>
128 >
129 :
130  std::integral_constant<Foam::direction, pTraits<T>::rank>
131 {};
132 
133 
134 //- The underlying component data type: default is pass-through.
135 template<class T, class = void>
136 struct pTraits_cmptType { typedef T type; };
137 
138 //- The underlying component data type for vector-space (or complex).
139 // Enabled when pTraits<T>:zero has a defined type (ie, exists),
140 // such as for arithmetic primitives, vector-space etc. where the concept
141 // of a component type also makes sense.
142 template<class T>
143 struct pTraits_cmptType<T, stdFoam::void_t<decltype(pTraits<T>::zero)>>
144 {
145  typedef typename pTraits<T>::cmptType type;
146 };
147 
149 //- The vector-space number of components: default is 1.
150 template<class T, class = void>
151 struct pTraits_nComponents : std::integral_constant<Foam::direction, 1> {};
152 
153 //- Number of VectorSpace components,
154 //- using the pTraits \c nComponents static member.
155 template<class T>
156 struct pTraits_nComponents
157 <
158  T,
159  stdFoam::void_t<decltype(pTraits<T>::nComponents)>
160 >
161 :
162  std::integral_constant<Foam::direction, pTraits<T>::nComponents>
163 {};
164 
165 
166 //- Test for pTraits zero : default is false
167 template<class T, class = void>
168 struct pTraits_has_zero : std::false_type {};
169 
170 //- Test for pTraits zero
171 template<class T>
172 struct pTraits_has_zero
173 <
174  T,
175  stdFoam::void_t<decltype(pTraits<T>::zero)>
176 >
177 :
178  std::true_type
179 {};
180 
181 
182 /*---------------------------------------------------------------------------*\
183  Container Traits
184 \*---------------------------------------------------------------------------*/
185 
186 //- Test for containers with begin/end range iterators.
187 template<class T, class = void>
188 struct is_range : std::false_type {};
190 //- Test for list containers with begin/end range iterators.
191 //- \attention May yield a false positive with HashTable, HashSet etc
192 template<class T>
193 struct is_range
194 <
195  T,
196  stdFoam::void_t
197  <
198  decltype(std::declval<T>().begin()),
199  decltype(std::declval<T>().end())
200  >
201 >
202 :
203  std::true_type
204 {};
205 
206 
207 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
208 
209 } // End namespace Foam
210 
211 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
212 
213 #endif
214 
215 // ************************************************************************* //
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:168
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
The vector-space rank: default is 0.
Definition: pTraits.H:127
pTraits(const Base &obj)
Copy construct from base class.
Definition: pTraits.H:86
Test for VectorSpace : default is false.
Definition: pTraits.H:109
The underlying component data type: default is pass-through.
Definition: pTraits.H:148
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:65
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:213
void void_t
Map a sequence of any types to void as per C++17 std::void_t.
Definition: pTraits.H:48
Namespace for std templates that are are part of future C++ standards or that are in a state of chang...
Definition: stdFoam.H:155
Test for pTraits zero : default is false.
Definition: pTraits.H:189
Namespace for OpenFOAM.