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).
This commit is contained in:
Mark Olesen
2017-04-29 22:28:16 +02:00
parent 6a5ea9a2bf
commit c65e2e580d
9 changed files with 258 additions and 115 deletions

View File

@ -52,29 +52,29 @@ int main(int argc, char *argv[])
Info<< nl << "And again using STL iterator: " << nl << endl;
forAllIter(SLList<scalar>, myList, iter)
for (auto const& val : myList)
{
Info<< "element:" << *iter << endl;
Info<< "element:" << val << endl;
}
Info<< nl << "And again using STL const_iterator: " << nl << endl;
const SLList<scalar>& const_myList = myList;
forAllConstIter(SLList<scalar>, const_myList, iter)
forAllConstIters(const_myList, iter)
{
Info<< "element:" << *iter << endl;
}
forAllIter(SLList<scalar>, myList, iter)
forAllIters(myList, iter)
{
Info<< "Removing element:" << *iter << endl;
myList.remove(iter);
}
forAllConstIter(SLList<scalar>, const_myList, iter)
for (auto const& val : const_myList)
{
Info<< "element:" << *iter << endl;
Info<< "element:" << val << endl;
}