Files
openfoam/src/OpenFOAM/include/stdFoam.H
Mark Olesen c65e2e580d ENH: add some standard templates and macros into stdFoam.H
- some functionality similar to what the standary library <iterator>
  provides.

  * stdFoam::begin() and stdFoam::end() do type deduction,
    which means that many cases it is possible to manage these types
    of changes.

    For example, when managing a number of indices:
       Map<labelHashSet> lookup;

    1) Longhand:

        for
        (
            Map<labelHashSet>::const_iterator iter = lookup.begin();
            iter != lookup.end();
            ++iter
        )
        { .... }

    1b) The same, but wrapped via a macro:

        forAllConstIter(Map<labelHashSet>, lookup, iter)
        { .... }

    2) Using stdFoam begin/end templates directly

        for
        (
            auto iter = stdFoam::begin(lookup);
            iter != stdFoam::end(lookup);
            ++iter
        )
        { .... }

    2b) The same, but wrapped via a macro:

        forAllConstIters(lookup, iter)
        { .... }

Note that in many cases it is possible to simply use a range-based for.
Eg,
     labelList myList;

     for (auto val : myList)
     { ... }

     for (const auto& val : myList)
     { ... }

These however will not work with any of the OpenFOAM hash-tables,
since the standard C++ concept of an iterator would return a key,value
pair when deferencing the *iter.

The deduction methods also exhibits some slightly odd behaviour with
some PtrLists (needs some more investigation).
2017-04-29 22:28:16 +02:00

213 lines
6.6 KiB
C++

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Namespace
stdFoam
Description
Includes some global templates and macros used by OpenFOAM.
Some of the templates are defined here correspond to useful
std templates that are part of future C++ standards, or that
are in a state of change. Defining them here provides some additional
control over which definition are used within the OpenFOAM code-base.
SeeAlso
- http://en.cppreference.com/w/cpp/iterator/end
- http://en.cppreference.com/w/cpp/iterator/begin
\*---------------------------------------------------------------------------*/
#ifndef StdFoam_H
#define StdFoam_H
#include <initializer_list>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace stdFoam
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Return iterator to the beginning of the container \a c or array.
// Definition as per std::begin C++17
template<class C>
constexpr auto begin(C& c) -> decltype(c.begin())
{
return c.begin();
}
//- Return const_iterator to the beginning of the container \a c or array.
// Definition as per std::begin C++17
template<class C>
constexpr auto begin(const C& c) -> decltype(c.begin())
{
return c.begin();
}
//- Return const_iterator to the beginning of the container \a c or array.
// Definition as per std::cbegin C++17
template<class C>
constexpr auto cbegin(const C& c) -> decltype(c.begin())
{
return c.begin();
}
//- Return iterator to the end of the container \a c or array.
// Definition as per std::end C++17
template<class C>
constexpr auto end(C& c) -> decltype(c.end())
{
return c.end();
}
//- Return const_iterator to the end of the container \a c or array.
// Definition as per std::end C++17
template<class C>
constexpr auto end(const C& c) -> decltype(c.end())
{
return c.end();
}
//- Return const_iterator to the end of the container \a c or array.
// Definition as per std::cend C++17
template<class C>
constexpr auto cend(const C& c) -> decltype(c.end())
{
return c.end();
}
} // End namespace stdFoam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Iterate across all elements in the \a container object of type
// \a Container.
// \par Usage
// \code
// forAllIters(container, iter)
// {
// statements;
// }
// \endcode
// \sa forAllConstIters, forAllIter, forAllConstIters
#define forAllIters(container,it) \
for \
( \
auto it = stdFoam::begin(container); \
it != stdFoam::end(container); \
++it \
)
//- Iterate across all elements of the \a container object with const access.
// \par Usage
// \code
// forAllConstIters(container, iter)
// {
// statements;
// }
// \endcode
// \sa forAllIters, forAllIter, forAllConstIter
#define forAllConstIters(container,cit) \
for \
( \
auto cit = stdFoam::cbegin(container); \
cit != stdFoam::cend(container); \
++cit \
)
//- Loop across all elements in \a list
// \par Usage
// \code
// forAll(anyList, i)
// {
// statements;
// }
// \endcode
// \sa forAllReverse
#define forAll(list, i) \
for (Foam::label i=0; i<(list).size(); ++i)
//- Reverse loop across all elements in \a list
// \par Usage
// \code
// forAllReverse(anyList, i)
// {
// statements;
// }
// \endcode
// \sa forAll
#define forAllReverse(list, i) \
for (Foam::label i=(list).size()-1; i>=0; --i)
//- Iterate across all elements in the \a container object
// of type \a Container.
// \par Usage
// \code
// forAllIter(ContainerType, container, iter)
// {
// statements;
// }
// \endcode
// \sa forAllConstIter
#define forAllIter(Container,container,iter) \
for \
( \
Container::iterator iter = (container).begin(); \
iter != (container).end(); \
++iter \
)
//- Iterate across all elements in the \a container object
// of type \a Container with const access.
// \par Usage
// \code
// forAllConstIter(ContainerType, container, iter)
// {
// statements;
// }
// \endcode
// \sa forAllIter
#define forAllConstIter(Container,container,iter) \
for \
( \
Container::const_iterator iter = (container).cbegin(); \
iter != (container).cend(); \
++iter \
)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //