Loading...
Searching...
No Matches
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-2025 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/>.
25
26Description
27 Includes some common 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 <iterator> // for std::begin, std::end, ...
38#include <memory>
39#include <numeric> // for std::iota, std::reduce, ...
40#include <type_traits>
41#include <utility>
43// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44
45// Compile-time warning for use of deprecated methods (compiler-dependent).
46// Use within the class declaration.
47
48#define FOAM_DEPRECATED(since) [[deprecated("Since " #since)]]
49#define FOAM_DEPRECATED_FOR(since, replacement) [[deprecated("Since " #since "; use " #replacement)]]
50
51#ifdef FOAM_COMPILE_STRICT
52# define FOAM_DEPRECATED_STRICT(since, replacement) [[deprecated("Since " #since "; use " #replacement)]]
53# if (FOAM_COMPILE_STRICT > 1)
54# define FOAM_DEPRECATED_STRICTER(since, replacement) [[deprecated("Since " #since "; use " #replacement)]]
55# endif
56#endif
57
58#ifndef FOAM_DEPRECATED_STRICT
59#define FOAM_DEPRECATED_STRICT(since, replacement)
60#endif
61#ifndef FOAM_DEPRECATED_STRICTER
62#define FOAM_DEPRECATED_STRICTER(since, replacement)
63#endif
65// Branch prediction helpers. With C++20 can use [[likely]], [[unlikely]]
66#if defined(__GNUC__) || defined(__clang__)
67# define FOAM_UNLIKELY(cond) __builtin_expect(!!(cond),0)
68# define FOAM_LIKELY(cond) __builtin_expect(!!(cond),1)
69#else
70# define FOAM_UNLIKELY(cond) (cond)
71# define FOAM_LIKELY(cond) (cond)
72#endif
73
74// Shadow macro for __GNUC__, excluding compilers masquerading as gcc
75#undef FOAM_REAL_GNUC
76#if defined(__GNUC__) && !defined(__llvm__)
77# define FOAM_REAL_GNUC __GNUC__
78#endif
79
80// Suppress false positives from -Wdangling-reference (gcc >= 14)
81#if (FOAM_REAL_GNUC >= 14)
82# define FOAM_NO_DANGLING_REFERENCE [[gnu::no_dangling]]
83#endif
84
85#ifndef FOAM_NO_DANGLING_REFERENCE
86#define FOAM_NO_DANGLING_REFERENCE
87#endif
88
89
90// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
91
92//- Namespace for OpenFOAM
93namespace Foam
94{
95
96//- Implementation details for various OpenFOAM classes
97namespace Detail {}
98
99//- Additional OpenFOAM modules
100namespace Module {}
101
102
103//- A functor that returns its argument unchanged (cf. C++20 std::identity)
104//- Should \em never be specialized.
105struct identityOp
106{
107 using is_transparent = void;
108
109 template<class T>
110 constexpr T&& operator()(T&& val) const noexcept
111 {
112 return std::forward<T>(val);
113 }
114
115 // Allow use as an identity array/map
116 template<class T>
117 constexpr T&& operator[](T&& val) const noexcept
118 {
119 return std::forward<T>(val);
120 }
121};
122
123
124// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
125
126//- Swap non-array types as per std::swap, but in Foam namespace.
127// \sa http://www.cplusplus.com/reference/utility/swap/
128//
129// \note For complex structures, it is usual to provide a swap() member
130// function and specialize Swap()
131template<class T>
132void Swap(T& a, T& b)
133{
134 std::swap(a, b);
135}
136
138//- Swap array types as per std::swap example, but in Foam namespace.
139// \sa http://www.cplusplus.com/reference/utility/swap/
140template<class T, size_t N>
141void Swap(T (&a)[N], T (&b)[N])
142{
143 for (size_t i = 0; i < N; ++i)
144 {
145 Foam::Swap(a[i], b[i]);
146 }
147}
148
149} // End namespace Foam
150
151
152// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
153
154//- Namespace for std templates that are are part of future C++ standards
155//- or that are in a state of change.
156//
157// SeeAlso
158// - https://en.cppreference.com/w/cpp/iterator/begin
159// - https://en.cppreference.com/w/cpp/iterator/end
160// - https://en.cppreference.com/w/cpp/iterator/rbegin
161// - https://en.cppreference.com/w/cpp/iterator/rend
162
163namespace stdFoam
164{
165
166//- Map any dependent type to false (workaround before CWG2518)
167template<typename...>
168inline constexpr bool dependent_false_v = false;
169
170//- Test for \c bool type (after removing cv)
171template<typename T>
172inline constexpr bool is_bool_v
173 = std::is_same<bool, std::remove_cv_t<T>>::value;
175//- Test for \c char type (after removing cv)
176template<typename T>
177inline constexpr bool is_char_v
178 = std::is_same<char, std::remove_cv_t<T>>::value;
179
180} // End namespace stdFoam
182
183// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
184
185//- Iterate across all elements in the \a container object.
186// \par Usage
187// \code
188// forAllIters(container, iter)
189// {
190// statements;
191// }
192// \endcode
193// \sa forAllConstIters, forAllIter, forAllConstIters
194#define forAllIters(container,iter) \
195 for \
196 ( \
197 auto iter = std::begin(container); \
198 iter != std::end(container); \
199 ++iter \
200 )
201
202
203//- Iterate across all elements of the \a container object with const access.
204// \par Usage
205// \code
206// forAllConstIters(container, iter)
207// {
208// statements;
209// }
210// \endcode
211// \sa forAllIters, forAllIter, forAllConstIter
212#define forAllConstIters(container,iter) \
213 for \
214 ( \
215 auto iter = std::cbegin(container); \
216 iter != std::cend(container); \
217 ++iter \
218 )
219
220
221//- Reverse iterate across elements in the \a container object of type
222// \a Container.
223// \par Usage
224// \code
225// forAllReverseIters(container, iter)
226// {
227// statements;
228// }
229// \endcode
230// \sa forAllConstReverseIters
231#define forAllReverseIters(container,iter) \
232 for \
233 ( \
234 auto iter = std::rbegin(container); \
235 iter != std::rend(container); \
236 ++iter \
237 )
238
239
240//- Reverse iterate across elements of \a container object with const access.
241// \par Usage
242// \code
243// forAllReverseConstIters(container, iter)
244// {
245// statements;
246// }
247// \endcode
248// \sa forAllReverseIters
249#define forAllConstReverseIters(container,iter) \
250 for \
251 ( \
252 auto iter = std::crbegin(container); \
253 iter != std::crend(container); \
254 ++iter \
255 )
256
258//- Loop across all elements in \a list
259// \par Usage
260// \code
261// forAll(anyList, i)
262// {
263// statements;
264// }
265// \endcode
266// \sa forAllReverse
267#define forAll(list, i) \
268 for (Foam::label i=0; i<(list).size(); ++i)
269
270
271//- Reverse loop across all elements in \a list
272// \par Usage
273// \code
274// forAllReverse(anyList, i)
275// {
276// statements;
277// }
278// \endcode
279// \sa forAll
280#define forAllReverse(list, i) \
281 for (Foam::label i=(list).size()-1; i>=0; --i)
282
283
284// Compatibility macros for pre C++11
285
286//- Iterate across all elements in the \a container object
287// of type \a Container.
288// \par Usage
289// \code
290// forAllIter(ContainerType, container, iter)
291// {
292// statements;
293// }
294// \endcode
295// \sa forAllConstIter
296#define forAllIter(Container,container,iter) \
297 for \
298 ( \
299 Container::iterator iter = (container).begin(); \
300 iter != (container).end(); \
301 ++iter \
302 )
303
304
305//- Iterate across all elements in the \a container object
306// of type \a Container with const access.
307// \par Usage
308// \code
309// forAllConstIter(ContainerType, container, iter)
310// {
311// statements;
312// }
313// \endcode
314// \sa forAllIter
315#define forAllConstIter(Container,container,iter) \
316 for \
317 ( \
318 Container::const_iterator iter = (container).cbegin(); \
319 iter != (container).cend(); \
320 ++iter \
321 )
322
323
324// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
325
326namespace stdFoam
327{
328
329/*---------------------------------------------------------------------------*\
330 Class stdFoam::span Declaration
331\*---------------------------------------------------------------------------*/
332
333//- Rudimentary functionality similar to std::span for holding memory view
334template<class Type>
335class span
336{
337 // Private Data
338
339 //- The data pointer
340 Type* data_;
341
342 //- The size of the span
343 std::size_t size_;
344
345
346public:
347
348 // STL type definitions
349
350 using element_type = Type;
351 using value_type = std::remove_cv_t<Type>;
352 using size_type = std::size_t;
353 using difference_type = std::ptrdiff_t;
354 using pointer = Type*;
355 using const_pointer = const Type*;
357 using const_reference = const element_type&;
358 using iterator = Type*;
359
360
361 // Generated Methods
362
363 //- Copy construct
364 span(const span& other) noexcept = default;
365
366 //- Copy assignment
367 span& operator=(const span& other) noexcept = default;
368
369
370 // Constructors
371
372 //- Default construct
373 constexpr span() noexcept
374 :
375 data_(nullptr),
376 size_(0)
377 {}
379 //- Construct from pointer and size
380 constexpr span(pointer ptr, size_type count) noexcept
381 :
382 data_(ptr),
383 size_(count)
384 {}
385
386 //- Construct from begin/end pointers
387 constexpr span(pointer first, pointer last) noexcept
388 :
389 data_(first),
390 size_(last - first)
391 {}
392
393
394 //- Destructor
395 ~span() noexcept = default;
396
398 // Member Functions
400 // Iterators
402 //- Iterator to begin of span
403 constexpr iterator begin() const noexcept { return data_; }
405 //- Iterator to one-past end of span
406 constexpr iterator end() const noexcept { return (data_ + size_); }
407
408
409 // Element access
410
411 //- Access the first element. Undefined if span is empty
412 constexpr reference front() const { return *(data_); }
414 //- Access the last element. Undefined if span is empty
415 constexpr reference back() const { return *(data_ + size_ - 1); }
416
417 //- Access an element of the sequence
418 constexpr reference operator[](size_type idx) const
419 {
420 return *(data_ + idx);
421 }
422
423 //- Return a pointer to the beginning of the sequence
424 constexpr pointer data() const noexcept { return data_; }
425
427 // Observers
428
429 //- Number of elements in the sequence
430 constexpr size_type size() const noexcept { return size_; }
431
432 //- The size of the sequence in bytes
433 constexpr size_type size_bytes() const noexcept
434 {
435 return (size_*sizeof(Type));
436 }
437
438 //- True if the sequence is empty
439 constexpr bool empty() const noexcept { return !size_; }
440
441
442 // Subview
443
444 //- Obtains a span of the first count elements
445 span<Type> first(size_type count) const noexcept
446 {
447 return span<Type>(data_, count);
448 }
449
450 //- Obtains a span of the last count elements
451 span<Type> last(size_type count) const noexcept
452 {
453 return span<Type>(data_ + (size_ - count), count);
455
456 //- Obtains a sub-span starting at pos until end of the sequence
457 span<Type> subspan(size_type pos) const noexcept
458 {
459 return span<Type>(data_ + pos, size_ - pos);
460 }
461
462 //- Obtains a sub-span of length len starting at pos.
463 // Graciously handles excess lengths.
464 span<Type> subspan(size_type pos, size_type len) const noexcept
465 {
466 return span<Type>(data_ + pos, std::min(size_ - pos, len));
467 }
468
470 // Additional members, similar to UList etc.
471 // The std::span has as_bytes() and as_writeable_bytes() as free functions
472
473 //- A readonly view as byte content
474 constexpr const char* cdata_bytes() const noexcept
475 {
476 return reinterpret_cast<const char*>(data_);
478
479 //- A writable view as byte content (if the pointer type is non-const).
480 //- Like data(), the access itself is const.
481 template
483 class TypeT = Type,
484 class = std::enable_if_t<!std::is_const_v<TypeT>>
485 >
486 char* data_bytes() const noexcept
488 return reinterpret_cast<char*>(data_);
489 }
490};
491
492
493// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
494
495} // End namespace stdFoam
496
497// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
498
499#endif
500
501// ************************************************************************* //
graph_traits< Graph >::vertices_size_type size_type
Rudimentary functionality similar to std::span for holding memory view.
Definition stdFoam.H:379
constexpr reference back() const
Access the last element. Undefined if span is empty.
Definition stdFoam.H:482
constexpr span() noexcept
Default construct.
Definition stdFoam.H:426
constexpr const char * cdata_bytes() const noexcept
A readonly view as byte content.
Definition stdFoam.H:562
element_type & reference
Definition stdFoam.H:403
constexpr size_type size_bytes() const noexcept
The size of the sequence in bytes.
Definition stdFoam.H:508
constexpr reference operator[](size_type idx) const
Access an element of the sequence.
Definition stdFoam.H:487
Type * pointer
Definition stdFoam.H:401
constexpr size_type size() const noexcept
Number of elements in the sequence.
Definition stdFoam.H:503
constexpr iterator end() const noexcept
Iterator to one-past end of span.
Definition stdFoam.H:469
constexpr span(pointer ptr, size_type count) noexcept
Construct from pointer and size.
Definition stdFoam.H:435
constexpr span(pointer first, pointer last) noexcept
Construct from begin/end pointers.
Definition stdFoam.H:444
Type * iterator
Definition stdFoam.H:405
span< Type > last(size_type count) const noexcept
Obtains a span of the last count elements.
Definition stdFoam.H:532
Type element_type
Definition stdFoam.H:397
~span() noexcept=default
Destructor.
constexpr pointer data() const noexcept
Return a pointer to the beginning of the sequence.
Definition stdFoam.H:495
span & operator=(const span &other) noexcept=default
Copy assignment.
std::remove_cv_t< Type > value_type
Definition stdFoam.H:398
std::ptrdiff_t difference_type
Definition stdFoam.H:400
span(const span &other) noexcept=default
Copy construct.
char * data_bytes() const noexcept
A writable view as byte content (if the pointer type is non-const). Like data(), the access itself is...
Definition stdFoam.H:576
constexpr reference front() const
Access the first element. Undefined if span is empty.
Definition stdFoam.H:477
span< Type > first(size_type count) const noexcept
Obtains a span of the first count elements.
Definition stdFoam.H:524
span< Type > subspan(size_type pos, size_type len) const noexcept
Obtains a sub-span of length len starting at pos.
Definition stdFoam.H:550
span< Type > subspan(size_type pos) const noexcept
Obtains a sub-span starting at pos until end of the sequence.
Definition stdFoam.H:540
constexpr iterator begin() const noexcept
Iterator to begin of span.
Definition stdFoam.H:464
const element_type & const_reference
Definition stdFoam.H:404
constexpr bool empty() const noexcept
True if the sequence is empty.
Definition stdFoam.H:516
std::size_t size_type
Definition stdFoam.H:399
const Type * const_pointer
Definition stdFoam.H:402
const volScalarField & T
Additional OpenFOAM modules.
Definition stdFoam.H:100
Namespace for OpenFOAM.
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Namespace for std templates that are are part of future C++ standards or that are in a state of chang...
Definition stdFoam.H:175
constexpr bool is_bool_v
Test for bool type (after removing cv).
Definition stdFoam.H:188
constexpr bool dependent_false_v
Map any dependent type to false (workaround before CWG2518).
Definition stdFoam.H:181
constexpr bool is_char_v
Test for char type (after removing cv).
Definition stdFoam.H:195
volScalarField & b
A functor that returns its argument unchanged (cf. C++20 std::identity) Should never be specialized.
Definition stdFoam.H:108
void is_transparent
Definition stdFoam.H:109
constexpr T && operator[](T &&val) const noexcept
Definition stdFoam.H:119
constexpr T && operator()(T &&val) const noexcept
Definition stdFoam.H:112
const Vector< label > N(dict.get< Vector< label > >("N"))