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