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 -------------------------------------------------------------------------------
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 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>
42 
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
64 
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
93 namespace Foam
94 {
95 
96 //- Implementation details for various OpenFOAM classes
97 namespace Detail {}
98 
99 //- Additional OpenFOAM modules
100 namespace Module {}
101 
102 
103 //- A functor that returns its argument unchanged (cf. C++20 std::identity)
104 //- Should \em never be specialized.
105 struct 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()
131 template<class T>
132 void 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/
140 template<class T, size_t N>
141 void 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 
163 
164 namespace stdFoam
165 {
166 
167 //- Map any dependent type to false (workaround before CWG2518)
168 template<typename...>
169 inline constexpr bool dependent_false_v = false;
170 
171 } // End namespace stdFoam
172 
173 
174 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
176 //- Iterate across all elements in the \a container object.
177 // \par Usage
178 // \code
179 // forAllIters(container, iter)
180 // {
181 // statements;
182 // }
183 // \endcode
184 // \sa forAllConstIters, forAllIter, forAllConstIters
185 #define forAllIters(container,iter) \
186  for \
187  ( \
188  auto iter = std::begin(container); \
189  iter != std::end(container); \
190  ++iter \
191  )
192 
193 
194 //- Iterate across all elements of the \a container object with const access.
195 // \par Usage
196 // \code
197 // forAllConstIters(container, iter)
198 // {
199 // statements;
200 // }
201 // \endcode
202 // \sa forAllIters, forAllIter, forAllConstIter
203 #define forAllConstIters(container,iter) \
204  for \
205  ( \
206  auto iter = std::cbegin(container); \
207  iter != std::cend(container); \
208  ++iter \
209  )
210 
211 
212 //- Reverse iterate across elements in the \a container object of type
213 // \a Container.
214 // \par Usage
215 // \code
216 // forAllReverseIters(container, iter)
217 // {
218 // statements;
219 // }
220 // \endcode
221 // \sa forAllConstReverseIters
222 #define forAllReverseIters(container,iter) \
223  for \
224  ( \
225  auto iter = std::rbegin(container); \
226  iter != std::rend(container); \
227  ++iter \
228  )
229 
230 
231 //- Reverse iterate across elements of \a container object with const access.
232 // \par Usage
233 // \code
234 // forAllReverseConstIters(container, iter)
235 // {
236 // statements;
237 // }
238 // \endcode
239 // \sa forAllReverseIters
240 #define forAllConstReverseIters(container,iter) \
241  for \
242  ( \
243  auto iter = std::crbegin(container); \
244  iter != std::crend(container); \
245  ++iter \
246  )
247 
248 
249 //- Loop across all elements in \a list
250 // \par Usage
251 // \code
252 // forAll(anyList, i)
253 // {
254 // statements;
255 // }
256 // \endcode
257 // \sa forAllReverse
258 #define forAll(list, i) \
259  for (Foam::label i=0; i<(list).size(); ++i)
260 
261 
262 //- Reverse loop across all elements in \a list
263 // \par Usage
264 // \code
265 // forAllReverse(anyList, i)
266 // {
267 // statements;
268 // }
269 // \endcode
270 // \sa forAll
271 #define forAllReverse(list, i) \
272  for (Foam::label i=(list).size()-1; i>=0; --i)
273 
274 
275 // Compatibility macros for pre C++11
276 
277 //- Iterate across all elements in the \a container object
278 // of type \a Container.
279 // \par Usage
280 // \code
281 // forAllIter(ContainerType, container, iter)
282 // {
283 // statements;
284 // }
285 // \endcode
286 // \sa forAllConstIter
287 #define forAllIter(Container,container,iter) \
288  for \
289  ( \
290  Container::iterator iter = (container).begin(); \
291  iter != (container).end(); \
292  ++iter \
293  )
294 
295 
296 //- Iterate across all elements in the \a container object
297 // of type \a Container with const access.
298 // \par Usage
299 // \code
300 // forAllConstIter(ContainerType, container, iter)
301 // {
302 // statements;
303 // }
304 // \endcode
305 // \sa forAllIter
306 #define forAllConstIter(Container,container,iter) \
307  for \
308  ( \
309  Container::const_iterator iter = (container).cbegin(); \
310  iter != (container).cend(); \
311  ++iter \
312  )
313 
314 
315 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
316 
317 namespace stdFoam
318 {
319 
320 /*---------------------------------------------------------------------------*\
321  Class stdFoam::span Declaration
322 \*---------------------------------------------------------------------------*/
323 
324 //- Rudimentary functionality similar to std::span for holding memory view
325 template<class Type>
326 class span
327 {
328  // Private Data
329 
330  //- The data pointer
331  Type* data_;
332 
333  //- The size of the span
334  std::size_t size_;
335 
336 
337 public:
338 
339  // STL type definitions
340 
341  using element_type = Type;
342  using value_type = std::remove_cv_t<Type>;
343  using size_type = std::size_t;
344  using difference_type = std::ptrdiff_t;
345  using pointer = Type*;
346  using const_pointer = const Type*;
347  using reference = element_type&;
348  using const_reference = const element_type&;
349  using iterator = Type*;
350 
351 
352  // Generated Methods
353 
354  //- Copy construct
355  span(const span& other) noexcept = default;
356 
357  //- Copy assignment
358  span& operator=(const span& other) noexcept = default;
359 
360 
361  // Constructors
362 
363  //- Default construct
364  constexpr span() noexcept
365  :
366  data_(nullptr),
367  size_(0)
368  {}
369 
370  //- Construct from pointer and size
371  constexpr span(pointer ptr, size_type count) noexcept
372  :
373  data_(ptr),
374  size_(count)
375  {}
376 
377  //- Construct from begin/end pointers
378  constexpr span(pointer first, pointer last) noexcept
379  :
380  data_(first),
381  size_(last - first)
382  {}
383 
385  //- Destructor
386  ~span() noexcept = default;
389  // Member Functions
391  // Iterators
393  //- Iterator to begin of span
394  constexpr iterator begin() const noexcept { return data_; }
395 
396  //- Iterator to one-past end of span
397  constexpr iterator end() const noexcept { return (data_ + size_); }
398 
399 
400  // Element access
401 
402  //- Access the first element. Undefined if span is empty
403  constexpr reference front() const { return *(data_); }
404 
405  //- Access the last element. Undefined if span is empty
406  constexpr reference back() const { return *(data_ + size_ - 1); }
407 
408  //- Access an element of the sequence
409  constexpr reference operator[](size_type idx) const
410  {
411  return *(data_ + idx);
412  }
414  //- Return a pointer to the beginning of the sequence
415  constexpr pointer data() const noexcept { return data_; }
416 
417 
418  // Observers
419 
420  //- Number of elements in the sequence
421  constexpr size_type size() const noexcept { return size_; }
423  //- The size of the sequence in bytes
424  constexpr size_type size_bytes() const noexcept
425  {
426  return (size_*sizeof(Type));
427  }
428 
429  //- True if the sequence is empty
430  constexpr bool empty() const noexcept { return !size_; }
432 
433  // Subview
434 
435  //- Obtains a span of the first count elements
436  span<Type> first(size_type count) const noexcept
437  {
438  return span<Type>(data_, count);
439  }
440 
441  //- Obtains a span of the last count elements
442  span<Type> last(size_type count) const noexcept
443  {
444  return span<Type>(data_ + (size_ - count), count);
445  }
446 
447  //- Obtains a sub-span starting at pos until end of the sequence
448  span<Type> subspan(size_type pos) const noexcept
449  {
450  return span<Type>(data_ + pos, size_ - pos);
451  }
452 
453  //- Obtains a sub-span of length len starting at pos.
454  // Graciously handles excess lengths.
455  span<Type> subspan(size_type pos, size_type len) const noexcept
456  {
457  return span<Type>(data_ + pos, std::min(size_ - pos, len));
458  }
459 
460 
461  // Additional members, similar to UList etc.
462  // The std::span has as_bytes() and as_writeable_bytes() as free functions
463 
464  //- A readonly view as byte content
465  constexpr const char* cdata_bytes() const noexcept
466  {
467  return reinterpret_cast<const char*>(data_);
468  }
470  //- A writable view as byte content (if the pointer type is non-const).
471  //- Like data(), the const access itself is const.
472  template<class TypeT = Type>
473  std::enable_if_t<!std::is_const_v<TypeT>, char*>
474  data_bytes() const noexcept
475  {
476  return reinterpret_cast<char*>(data_);
477  }
478 };
479 
480 
481 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
483 } // End namespace stdFoam
484 
485 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
486 
487 #endif
488 
489 // ************************************************************************* //
constexpr T && operator[](T &&val) const noexcept
Definition: stdFoam.H:119
dimensionedScalar pos(const dimensionedScalar &ds)
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:67
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
constexpr bool dependent_false_v
Map any dependent type to false (workaround before CWG2518)
Definition: stdFoam.H:182
void is_transparent
Definition: stdFoam.H:109
const direction noexcept
Definition: scalarImpl.H:255
A functor that returns its argument unchanged (cf. C++20 std::identity) Should never be specialized...
Definition: stdFoam.H:107
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const Vector< label > N(dict.get< Vector< label >>("N"))
const char * end
Definition: SVGTools.H:223
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
Definition: DynamicList.H:717
Rudimentary functionality similar to std::span for holding memory view.
Definition: stdFoam.H:365
Type * iterator
Definition: stdFoam.H:392
Type * pointer
Definition: stdFoam.H:388
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 T && operator()(T &&val) const noexcept
Definition: stdFoam.H:112
std::size_t size_type
Definition: stdFoam.H:386
Namespace for OpenFOAM.