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-2023 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 #include <type_traits>
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 // Compile-time warning for use of deprecated methods (compiler-dependent).
44 // Use within the class declaration.
45 
46 #if (__cplusplus >= 201402L)
47 # define FOAM_DEPRECATED(since) [[deprecated("Since " #since)]]
48 # define FOAM_DEPRECATED_FOR(since, replacement) [[deprecated("Since " #since "; use " #replacement)]]
49 #elif defined(__GNUC__)
50 # define FOAM_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
51 # define FOAM_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
52 #else
53 # define FOAM_DEPRECATED(since)
54 # define FOAM_DEPRECATED_FOR(since, replacement)
55 #endif
56 
57 // Compile-time warning about unused result
58 // FUTURE: check __has_cpp_attribute(nodiscard) and define with [[nodiscard]]
59 #if defined(__GNUC__)
60 # define FOAM_NODISCARD __attribute__((warn_unused_result))
61 #else
62 # define FOAM_NODISCARD
63 #endif
64 
65 
66 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
67 
68 //- Namespace for OpenFOAM
69 namespace Foam
70 {
71 
72 //- Implementation details for various OpenFOAM classes
73 namespace Detail {}
74 
75 //- Additional OpenFOAM modules
76 namespace Module {}
77 
78 // Standard items
79 
80 //- Allow use of std::unique_ptr directly as Foam::unique_ptr
81 using std::unique_ptr;
82 
83 
84 //- A functor that returns its argument unchanged (cf. C++20 std::identity)
85 //- Should \em never be specialized.
86 struct identityOp
87 {
88  using is_transparent = void;
89 
90  template<class T>
91  constexpr T&& operator()(T&& val) const noexcept
92  {
93  return std::forward<T>(val);
94  }
95 };
96 
97 
98 //- Swap non-array types as per std::swap, but in Foam namespace.
99 // \sa http://www.cplusplus.com/reference/utility/swap/
100 //
101 // \note For complex structures, it is usual to provide a swap() member
102 // function and specialize Swap()
103 template<class T>
104 void Swap(T& a, T& b)
105 {
106  std::swap(a, b);
107 }
108 
109 
110 //- Swap array types as per std::swap example, but in Foam namespace.
111 // \sa http://www.cplusplus.com/reference/utility/swap/
112 template<class T, size_t N>
113 void Swap(T (&a)[N], T (&b)[N])
114 {
115  for (size_t i = 0; i < N; ++i)
116  {
117  Foam::Swap(a[i], b[i]);
118  }
119 }
120 
121 } // End namespace Foam
122 
124 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
125 
126 //- Namespace for std templates that are are part of future C++ standards
127 //- or that are in a state of change.
128 //
129 // SeeAlso
130 // - http://en.cppreference.com/w/cpp/iterator/begin
131 // - http://en.cppreference.com/w/cpp/iterator/end
132 // - http://en.cppreference.com/w/cpp/iterator/rbegin
133 // - http://en.cppreference.com/w/cpp/iterator/rend
134 
135 namespace stdFoam
136 {
137 
138 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
139 
140 // Forward iteration
141 
142 //- Return iterator to the beginning of the container \a c.
143 // Definition as per std::begin C++17
144 template<class C>
145 constexpr auto begin(C& c) -> decltype(c.begin())
146 {
147  return c.begin();
148 }
149 
150 //- Return const_iterator to the beginning of the container \a c.
151 // Definition as per std::begin C++17
152 template<class C>
153 constexpr auto begin(const C& c) -> decltype(c.begin())
154 {
155  return c.begin();
156 }
157 
158 //- Return const_iterator to the beginning of the container \a c.
159 // Definition as per std::cbegin C++17
160 template<class C>
161 constexpr auto cbegin(const C& c) -> decltype(c.begin())
162 {
163  return c.begin();
164 }
165 
166 //- Return iterator to the end of the container \a c.
167 // Definition as per std::end C++17
168 template<class C>
169 constexpr auto end(C& c) -> decltype(c.end())
170 {
171  return c.end();
172 }
173 
174 //- Return const_iterator to the end of the container \a c.
175 // Definition as per std::end C++17
176 template<class C>
177 constexpr auto end(const C& c) -> decltype(c.end())
178 {
179  return c.end();
180 }
181 
182 //- Return const_iterator to the end of the container \a c.
183 // Definition as per std::cend C++17
184 template<class C>
185 constexpr auto cend(const C& c) -> decltype(c.end())
186 {
187  return c.end();
188 }
189 
190 
191 // Reverse iteration
192 
193 //- Return reverse_iterator to the reverse-begin of container \a c.
194 // Definition as per std::rbegin C++17
195 template<class C>
196 constexpr auto rbegin(C& c) -> decltype(c.rbegin())
197 {
198  return c.rbegin();
199 }
200 
201 //- Return const_reverse_iterator to the reverse-begin of container \a c.
202 // Definition as per std::rbegin C++17
203 template<class C>
204 constexpr auto rbegin(const C& c) -> decltype(c.rbegin())
205 {
206  return c.rbegin();
207 }
208 
209 //- Return const_reverse_iterator to the reverse-begin of container \a c.
210 // Definition as per std::crbegin C++17
211 template<class C>
212 constexpr auto crbegin(const C& c) -> decltype(c.rbegin())
213 {
214  return c.rbegin();
215 }
217 //- Return reverse_iterator to reverse-end of container \a c.
218 // Definition as per std::rend C++17
219 template<class C>
220 constexpr auto rend(C& c) -> decltype(c.rend())
221 {
222  return c.rend();
223 }
224 
225 //- Return const_reverse_iterator to reverse-end of container \a c.
226 // Definition as per std::rend C++17
227 template<class C>
228 constexpr auto rend(const C& c) -> decltype(c.rend())
229 {
230  return c.rend();
231 }
232 
233 //- Return const_reverse_iterator to reverse-end of container \a c.
234 // Definition as per std::crend C++17
235 template<class C>
236 constexpr auto crend(const C& c) -> decltype(c.rend())
237 {
238  return c.rend();
239 }
240 
241 //- Return the lesser of the parameters.
242 // Definition as per std::min C++14
243 template<class T>
244 constexpr inline const T& min(const T& a, const T& b)
245 {
246  return (b < a) ? b : a;
247 }
248 
249 //- Return the greater of the parameters.
250 // Definition as per std::max C++14
251 template<class T>
252 constexpr inline const T& max(const T& a, const T& b)
253 {
254  return (a < b) ? b : a;
255 }
256 
257 } // End namespace stdFoam
258 
259 
260 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
261 
262 //- Iterate across all elements in the \a container object.
263 // \par Usage
264 // \code
265 // forAllIters(container, iter)
266 // {
267 // statements;
268 // }
269 // \endcode
270 // \sa forAllConstIters, forAllIter, forAllConstIters
271 #define forAllIters(container,iter) \
272  for \
273  ( \
274  auto iter = stdFoam::begin(container); \
275  iter != stdFoam::end(container); \
276  ++iter \
277  )
278 
279 
280 //- Iterate across all elements of the \a container object with const access.
281 // \par Usage
282 // \code
283 // forAllConstIters(container, iter)
284 // {
285 // statements;
286 // }
287 // \endcode
288 // \sa forAllIters, forAllIter, forAllConstIter
289 #define forAllConstIters(container,iter) \
290  for \
291  ( \
292  auto iter = stdFoam::cbegin(container); \
293  iter != stdFoam::cend(container); \
294  ++iter \
295  )
297 
298 //- Reverse iterate across elements in the \a container object of type
299 // \a Container.
300 // \par Usage
301 // \code
302 // forAllReverseIters(container, iter)
303 // {
304 // statements;
305 // }
306 // \endcode
307 // \sa forAllConstReverseIters
308 #define forAllReverseIters(container,iter) \
309  for \
310  ( \
311  auto iter = stdFoam::rbegin(container); \
312  iter != stdFoam::rend(container); \
313  ++iter \
314  )
315 
316 
317 //- Reverse iterate across elements of \a container object with const access.
318 // \par Usage
319 // \code
320 // forAllReverseConstIters(container, iter)
321 // {
322 // statements;
323 // }
324 // \endcode
325 // \sa forAllReverseIters
326 #define forAllConstReverseIters(container,iter) \
327  for \
328  ( \
329  auto iter = stdFoam::crbegin(container); \
330  iter != stdFoam::crend(container); \
331  ++iter \
332  )
333 
334 
335 //- Loop across all elements in \a list
336 // \par Usage
337 // \code
338 // forAll(anyList, i)
339 // {
340 // statements;
341 // }
342 // \endcode
343 // \sa forAllReverse
344 #define forAll(list, i) \
345  for (Foam::label i=0; i<(list).size(); ++i)
346 
347 
348 //- Reverse loop across all elements in \a list
349 // \par Usage
350 // \code
351 // forAllReverse(anyList, i)
352 // {
353 // statements;
354 // }
355 // \endcode
356 // \sa forAll
357 #define forAllReverse(list, i) \
358  for (Foam::label i=(list).size()-1; i>=0; --i)
359 
360 
361 // Compatibility macros for pre C++11
362 
363 //- Iterate across all elements in the \a container object
364 // of type \a Container.
365 // \par Usage
366 // \code
367 // forAllIter(ContainerType, container, iter)
368 // {
369 // statements;
370 // }
371 // \endcode
372 // \sa forAllConstIter
373 #define forAllIter(Container,container,iter) \
374  for \
375  ( \
376  Container::iterator iter = (container).begin(); \
377  iter != (container).end(); \
378  ++iter \
379  )
380 
381 
382 //- Iterate across all elements in the \a container object
383 // of type \a Container with const access.
384 // \par Usage
385 // \code
386 // forAllConstIter(ContainerType, container, iter)
387 // {
388 // statements;
389 // }
390 // \endcode
391 // \sa forAllIter
392 #define forAllConstIter(Container,container,iter) \
393  for \
394  ( \
395  Container::const_iterator iter = (container).cbegin(); \
396  iter != (container).cend(); \
397  ++iter \
398  )
399 
400 
401 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
402 
403 namespace stdFoam
404 {
405 
406 /*---------------------------------------------------------------------------*\
407  Class stdFoam::span Declaration
408 \*---------------------------------------------------------------------------*/
409 
410 //- Rudimentary functionality similar to std::span for holding memory view
411 template<class Type>
412 class span
413 {
414  // Private Data
415 
416  //- The data pointer
417  Type* data_;
418 
419  //- The size of the span
420  std::size_t size_;
421 
422 
423 public:
424 
425  // STL type definitions
426 
427  using element_type = Type;
428  using value_type = std::remove_cv<Type>;
429  using size_type = std::size_t;
430  using pointer = Type*;
431  using const_pointer = const Type*;
432  using reference = Type&;
433  using const_reference = const Type&;
434  using difference_type = std::ptrdiff_t;
435  using iterator = Type*;
436 
437 
438  // Generated Methods
439 
440  //- Copy construct
441  span(const span& other) noexcept = default;
442 
443  //- Copy assignment
444  span& operator=(const span& other) noexcept = default;
445 
446 
447  // Constructors
448 
449  //- Default construct
450  constexpr span() noexcept
451  :
452  data_(nullptr),
453  size_(0)
454  {}
455 
456  //- Construct from pointer and size
457  constexpr span(pointer ptr, size_type count) noexcept
458  :
459  data_(ptr),
460  size_(count)
461  {}
462 
463  //- Construct from begin/end pointers
464  constexpr span(pointer first, pointer last) noexcept
465  :
466  data_(first),
467  size_(last - first)
468  {}
469 
470 
471  //- Destructor
472  ~span() noexcept = default;
473 
474 
475  // Member Functions
476 
477  // Iterators
478 
479  //- Iterator to begin of span
480  constexpr iterator begin() const noexcept { return data_; }
481 
482  //- Iterator to one-past end of span
483  constexpr iterator end() const noexcept { return (data_ + size_); }
484 
485 
486  // Element access
487 
488  //- Access the first element. Undefined if span is empty
489  constexpr reference front() const { return *(data_); }
490 
491  //- Access the last element. Undefined if span is empty
492  constexpr reference back() const { return *(data_ + size_ - 1); }
494  //- Access an element of the sequence
495  constexpr reference operator[](size_type idx) const
496  {
497  return *(data_ + idx);
498  }
499 
500  //- Return a pointer to the beginning of the sequence
501  constexpr pointer data() const noexcept { return data_; }
502 
503 
504  // Observers
505 
506  //- Number of elements in the sequence
507  constexpr size_type size() const noexcept { return size_; }
508 
509  //- The size of the sequence in bytes
510  constexpr size_type size_bytes() const noexcept
511  {
512  return (size_*sizeof(Type));
513  }
515  //- True if the sequence is empty
516  constexpr bool empty() const noexcept { return !size_; }
519  // Subview
521  //- Obtains a span of the first count elements
523  {
524  return span<Type>(data_, count);
525  }
526 
527  //- Obtains a span of the last count elements
529  {
530  return span<Type>(data_ + (size_ - count), count);
531  }
532 
533  //- Obtains a sub-span starting at pos until end of the sequence
534  span<Type> subspan(size_type pos) const noexcept
535  {
536  return span<Type>(data_ + pos, size_ - pos);
537  }
538 
539  //- Obtains a sub-span of length len starting at pos.
540  // Graciously handles excess lengths.
542  {
543  return span<Type>(data_ + pos, std::min(size_ - pos, len));
544  }
545 
546 
547  // Additional members, similar to UList etc.
548  // The std::span has as_bytes() and as_writeable_bytes() as free functions
549 
550  //- A readonly view as byte content
551  constexpr const char* cdata_bytes() const noexcept
552  {
553  return reinterpret_cast<const char*>(data_);
554  }
555 
556  //- A writable view as byte content (if the pointer type is non-const).
557  //- Like data(), the const access itself is const.
558  template<class TypeT = Type>
559  typename std::enable_if<!std::is_const<TypeT>::value, char*>::type
560  data_bytes() const noexcept
561  {
562  return reinterpret_cast<char*>(data_);
563  }
564 };
565 
566 
567 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
568 
569 } // End namespace stdFoam
570 
571 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
572 
573 #endif
574 
575 // ************************************************************************* //
Graphite solid properties.
Definition: C.H:46
type
Types of root.
Definition: Roots.H:52
const Type & const_reference
Definition: stdFoam.H:518
constexpr size_type size() const noexcept
Number of elements in the sequence.
Definition: stdFoam.H:618
Type & reference
Definition: stdFoam.H:517
Type element_type
Definition: stdFoam.H:512
constexpr auto crend(const C &c) -> decltype(c.rend())
Return const_reverse_iterator to reverse-end of container c.
Definition: stdFoam.H:285
constexpr auto crbegin(const C &c) -> decltype(c.rbegin())
Return const_reverse_iterator to the reverse-begin of container c.
Definition: stdFoam.H:252
std::remove_cv< Type > value_type
Definition: stdFoam.H:513
constexpr reference back() const
Access the last element. Undefined if span is empty.
Definition: stdFoam.H:597
~span() noexcept=default
Destructor.
constexpr span() noexcept
Default construct.
Definition: stdFoam.H:541
span< Type > subspan(size_type pos) const noexcept
Obtains a sub-span starting at pos until end of the sequence.
Definition: stdFoam.H:655
constexpr reference operator[](size_type idx) const
Access an element of the sequence.
Definition: stdFoam.H:602
constexpr pointer data() const noexcept
Return a pointer to the beginning of the sequence.
Definition: stdFoam.H:610
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
constexpr const T & max(const T &a, const T &b)
Return the greater of the parameters.
Definition: stdFoam.H:307
span< Type > first(size_type count) const noexcept
Obtains a span of the first count elements.
Definition: stdFoam.H:639
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
const Type * const_pointer
Definition: stdFoam.H:516
std::ptrdiff_t difference_type
Definition: stdFoam.H:519
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:216
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:68
constexpr bool empty() const noexcept
True if the sequence is empty.
Definition: stdFoam.H:631
std::enable_if<!std::is_const< TypeT >::value, char * >::type data_bytes() const noexcept
A writable view as byte content (if the pointer type is non-const). Like data(), the const access its...
Definition: stdFoam.H:688
constexpr size_type size_bytes() const noexcept
The size of the sequence in bytes.
Definition: stdFoam.H:623
void is_transparent
Definition: stdFoam.H:92
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:90
span & operator=(const span &other) noexcept=default
Copy assignment.
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:194
volScalarField & C
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:183
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const volScalarField & T
constexpr reference front() const
Access the first element. Undefined if span is empty.
Definition: stdFoam.H:592
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:230
constexpr const T & min(const T &a, const T &b)
Return the lesser of the parameters.
Definition: stdFoam.H:296
const dimensionedScalar c
Speed of light in a vacuum.
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:659
Rudimentary functionality similar to std::span for holding memory view.
Definition: stdFoam.H:493
constexpr iterator end() const noexcept
Iterator to one-past end of span.
Definition: stdFoam.H:584
Type * iterator
Definition: stdFoam.H:520
constexpr auto rend(C &c) -> decltype(c.rend())
Return reverse_iterator to reverse-end of container c.
Definition: stdFoam.H:263
Type * pointer
Definition: stdFoam.H:515
constexpr const char * cdata_bytes() const noexcept
A readonly view as byte content.
Definition: stdFoam.H:677
span< Type > last(size_type count) const noexcept
Obtains a span of the last count elements.
Definition: stdFoam.H:647
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:161
constexpr iterator begin() const noexcept
Iterator to begin of span.
Definition: stdFoam.H:579
Namespace for std templates that are are part of future C++ standards or that are in a state of chang...
Definition: stdFoam.H:148
constexpr T && operator()(T &&val) const noexcept
Definition: stdFoam.H:95
std::size_t size_type
Definition: stdFoam.H:514
Namespace for OpenFOAM.