stdFoam.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) 2017-2022 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 Description
27  Includes some standard C++ headers, defines global macros and templates
28  used in multiple places by OpenFOAM.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #ifndef Foam_stdFoam_H
33 #define Foam_stdFoam_H
34 
35 #include <algorithm>
36 #include <initializer_list>
37 #include <memory>
38 #include <utility>
39 
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41 
42 // Compile-time warning for use of deprecated methods (compiler-dependent).
43 // Use within the class declaration.
44 
45 #if (__cplusplus >= 201402L)
46 # define FOAM_DEPRECATED(since) [[deprecated("Since " #since)]]
47 # define FOAM_DEPRECATED_FOR(since, replacement) [[deprecated("Since " #since "; use " #replacement)]]
48 #elif defined(__GNUC__)
49 # define FOAM_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
50 # define FOAM_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
51 #else
52 # define FOAM_DEPRECATED(since)
53 # define FOAM_DEPRECATED_FOR(since, replacement)
54 #endif
55 
56 // Compile-time warning about unused result
57 // FUTURE: check __has_cpp_attribute(nodiscard) and define with [[nodiscard]]
58 #if defined(__GNUC__)
59 # define FOAM_NODISCARD __attribute__((warn_unused_result))
60 #else
61 # define FOAM_NODISCARD
62 #endif
63 
64 
65 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
66 
67 //- Namespace for OpenFOAM
68 namespace Foam
69 {
70 
71 //- Implementation details for various OpenFOAM classes
72 namespace Detail {}
73 
74 //- Additional OpenFOAM modules
75 namespace Module {}
76 
77 // Standard items
78 
79 //- Allow use of std::unique_ptr directly as Foam::unique_ptr
80 using std::unique_ptr;
81 
82 
83 //- A functor that returns its argument unchanged (cf. C++20 std::identity)
84 //- Should \em never be specialized.
85 struct identityOp
86 {
87  using is_transparent = void;
88 
89  template<class T>
90  constexpr T&& operator()(T&& val) const noexcept
91  {
92  return std::forward<T>(val);
93  }
94 };
95 
96 
97 //- Swap non-array types as per std::swap, but in Foam namespace.
98 // \sa http://www.cplusplus.com/reference/utility/swap/
99 //
100 // \note For complex structures, it is usual to provide a swap() member
101 // function and specialize Swap()
102 template<class T>
103 void Swap(T& a, T& b)
104 {
105  std::swap(a, b);
106 }
107 
108 
109 //- Swap array types as per std::swap example, but in Foam namespace.
110 // \sa http://www.cplusplus.com/reference/utility/swap/
111 template<class T, size_t N>
112 void Swap(T (&a)[N], T (&b)[N])
113 {
114  for (size_t i = 0; i < N; ++i)
115  {
116  Foam::Swap(a[i], b[i]);
117  }
118 }
119 
120 } // End namespace Foam
121 
123 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
124 
125 //- Namespace for std templates that are are part of future C++ standards
126 //- or that are in a state of change.
127 //
128 // SeeAlso
129 // - http://en.cppreference.com/w/cpp/iterator/begin
130 // - http://en.cppreference.com/w/cpp/iterator/end
131 // - http://en.cppreference.com/w/cpp/iterator/rbegin
132 // - http://en.cppreference.com/w/cpp/iterator/rend
133 
134 namespace stdFoam
135 {
136 
137 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
138 
139 // Forward iteration
140 
141 //- Return iterator to the beginning of the container \a c.
142 // Definition as per std::begin C++17
143 template<class C>
144 constexpr auto begin(C& c) -> decltype(c.begin())
145 {
146  return c.begin();
147 }
148 
149 //- Return const_iterator to the beginning of the container \a c.
150 // Definition as per std::begin C++17
151 template<class C>
152 constexpr auto begin(const C& c) -> decltype(c.begin())
153 {
154  return c.begin();
155 }
156 
157 //- Return const_iterator to the beginning of the container \a c.
158 // Definition as per std::cbegin C++17
159 template<class C>
160 constexpr auto cbegin(const C& c) -> decltype(c.begin())
161 {
162  return c.begin();
163 }
164 
165 //- Return iterator to the end of the container \a c.
166 // Definition as per std::end C++17
167 template<class C>
168 constexpr auto end(C& c) -> decltype(c.end())
169 {
170  return c.end();
171 }
172 
173 //- Return const_iterator to the end of the container \a c.
174 // Definition as per std::end C++17
175 template<class C>
176 constexpr auto end(const C& c) -> decltype(c.end())
177 {
178  return c.end();
179 }
180 
181 //- Return const_iterator to the end of the container \a c.
182 // Definition as per std::cend C++17
183 template<class C>
184 constexpr auto cend(const C& c) -> decltype(c.end())
185 {
186  return c.end();
187 }
188 
189 
190 // Reverse iteration
191 
192 //- Return reverse_iterator to the reverse-begin of container \a c.
193 // Definition as per std::rbegin C++17
194 template<class C>
195 constexpr auto rbegin(C& c) -> decltype(c.rbegin())
196 {
197  return c.rbegin();
198 }
199 
200 //- Return const_reverse_iterator to the reverse-begin of container \a c.
201 // Definition as per std::rbegin C++17
202 template<class C>
203 constexpr auto rbegin(const C& c) -> decltype(c.rbegin())
204 {
205  return c.rbegin();
206 }
207 
208 //- Return const_reverse_iterator to the reverse-begin of container \a c.
209 // Definition as per std::crbegin C++17
210 template<class C>
211 constexpr auto crbegin(const C& c) -> decltype(c.rbegin())
212 {
213  return c.rbegin();
214 }
216 //- Return reverse_iterator to reverse-end of container \a c.
217 // Definition as per std::rend C++17
218 template<class C>
219 constexpr auto rend(C& c) -> decltype(c.rend())
220 {
221  return c.rend();
222 }
223 
224 //- Return const_reverse_iterator to reverse-end of container \a c.
225 // Definition as per std::rend C++17
226 template<class C>
227 constexpr auto rend(const C& c) -> decltype(c.rend())
228 {
229  return c.rend();
230 }
231 
232 //- Return const_reverse_iterator to reverse-end of container \a c.
233 // Definition as per std::crend C++17
234 template<class C>
235 constexpr auto crend(const C& c) -> decltype(c.rend())
236 {
237  return c.rend();
238 }
239 
240 //- Return the lesser of the parameters.
241 // Definition as per std::min C++14
242 template<class T>
243 constexpr inline const T& min(const T& a, const T& b)
244 {
245  return (b < a) ? b : a;
246 }
247 
248 //- Return the greater of the parameters.
249 // Definition as per std::max C++14
250 template<class T>
251 constexpr inline const T& max(const T& a, const T& b)
252 {
253  return (a < b) ? b : a;
254 }
255 
256 } // End namespace stdFoam
257 
258 
259 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
260 
261 //- Iterate across all elements in the \a container object.
262 // \par Usage
263 // \code
264 // forAllIters(container, iter)
265 // {
266 // statements;
267 // }
268 // \endcode
269 // \sa forAllConstIters, forAllIter, forAllConstIters
270 #define forAllIters(container,iter) \
271  for \
272  ( \
273  auto iter = stdFoam::begin(container); \
274  iter != stdFoam::end(container); \
275  ++iter \
276  )
277 
278 
279 //- Iterate across all elements of the \a container object with const access.
280 // \par Usage
281 // \code
282 // forAllConstIters(container, iter)
283 // {
284 // statements;
285 // }
286 // \endcode
287 // \sa forAllIters, forAllIter, forAllConstIter
288 #define forAllConstIters(container,iter) \
289  for \
290  ( \
291  auto iter = stdFoam::cbegin(container); \
292  iter != stdFoam::cend(container); \
293  ++iter \
294  )
296 
297 //- Reverse iterate across elements in the \a container object of type
298 // \a Container.
299 // \par Usage
300 // \code
301 // forAllReverseIters(container, iter)
302 // {
303 // statements;
304 // }
305 // \endcode
306 // \sa forAllConstReverseIters
307 #define forAllReverseIters(container,iter) \
308  for \
309  ( \
310  auto iter = stdFoam::rbegin(container); \
311  iter != stdFoam::rend(container); \
312  ++iter \
313  )
314 
315 
316 //- Reverse iterate across elements of \a container object with const access.
317 // \par Usage
318 // \code
319 // forAllReverseConstIters(container, iter)
320 // {
321 // statements;
322 // }
323 // \endcode
324 // \sa forAllReverseIters
325 #define forAllConstReverseIters(container,iter) \
326  for \
327  ( \
328  auto iter = stdFoam::crbegin(container); \
329  iter != stdFoam::crend(container); \
330  ++iter \
331  )
332 
333 
334 //- Loop across all elements in \a list
335 // \par Usage
336 // \code
337 // forAll(anyList, i)
338 // {
339 // statements;
340 // }
341 // \endcode
342 // \sa forAllReverse
343 #define forAll(list, i) \
344  for (Foam::label i=0; i<(list).size(); ++i)
345 
346 
347 //- Reverse loop across all elements in \a list
348 // \par Usage
349 // \code
350 // forAllReverse(anyList, i)
351 // {
352 // statements;
353 // }
354 // \endcode
355 // \sa forAll
356 #define forAllReverse(list, i) \
357  for (Foam::label i=(list).size()-1; i>=0; --i)
358 
359 
360 // Compatibility macros for pre C++11
361 
362 //- Iterate across all elements in the \a container object
363 // of type \a Container.
364 // \par Usage
365 // \code
366 // forAllIter(ContainerType, container, iter)
367 // {
368 // statements;
369 // }
370 // \endcode
371 // \sa forAllConstIter
372 #define forAllIter(Container,container,iter) \
373  for \
374  ( \
375  Container::iterator iter = (container).begin(); \
376  iter != (container).end(); \
377  ++iter \
378  )
379 
380 
381 //- Iterate across all elements in the \a container object
382 // of type \a Container with const access.
383 // \par Usage
384 // \code
385 // forAllConstIter(ContainerType, container, iter)
386 // {
387 // statements;
388 // }
389 // \endcode
390 // \sa forAllIter
391 #define forAllConstIter(Container,container,iter) \
392  for \
393  ( \
394  Container::const_iterator iter = (container).cbegin(); \
395  iter != (container).cend(); \
396  ++iter \
397  )
398 
399 
400 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
401 
402 #endif
403 
404 // ************************************************************************* //
Graphite solid properties.
Definition: C.H:46
constexpr auto crend(const C &c) -> decltype(c.rend())
Return const_reverse_iterator to reverse-end of container c.
Definition: stdFoam.H:284
constexpr auto crbegin(const C &c) -> decltype(c.rbegin())
Return const_reverse_iterator to the reverse-begin of container c.
Definition: stdFoam.H:251
constexpr const T & max(const T &a, const T &b)
Return the greater of the parameters.
Definition: stdFoam.H:306
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:215
void is_transparent
Definition: stdFoam.H:91
const direction noexcept
Definition: Scalar.H:258
A functor that returns its argument unchanged (cf. C++20 std::identity) Should never be specialized...
Definition: stdFoam.H:89
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:193
volScalarField & C
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:182
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const volScalarField & T
const Vector< label > N(dict.get< Vector< label >>("N"))
constexpr auto rbegin(C &c) -> decltype(c.rbegin())
Return reverse_iterator to the reverse-begin of container c.
Definition: stdFoam.H:229
constexpr const T & min(const T &a, const T &b)
Return the lesser of the parameters.
Definition: stdFoam.H:295
const dimensionedScalar c
Speed of light in a vacuum.
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:647
constexpr auto rend(C &c) -> decltype(c.rend())
Return reverse_iterator to reverse-end of container c.
Definition: stdFoam.H:262
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:160
Namespace for std templates that are are part of future C++ standards or that are in a state of chang...
Definition: stdFoam.H:147
constexpr T && operator()(T &&val) const noexcept
Definition: stdFoam.H:94
Namespace for OpenFOAM.