diff --git a/src/common/subsystem-runner.hpp b/src/common/subsystem-runner.hpp index d8fa5bb6d..8e7edcffc 100644 --- a/src/common/subsystem-runner.hpp +++ b/src/common/subsystem-runner.hpp @@ -26,6 +26,7 @@ #include "lib/error.hpp" #include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include "common/subsys.hpp" #include "lib/sync.hpp" diff --git a/src/lib/util-foreach.hpp b/src/lib/util-foreach.hpp new file mode 100644 index 000000000..2d11430d3 --- /dev/null +++ b/src/lib/util-foreach.hpp @@ -0,0 +1,87 @@ +/* + UTIL-FOREACH.hpp - helpers for doing something for each element + + Copyright (C) Lumiera.org + 2009, Hermann Vosseler + + This program 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 2 of the + License, or (at your option) any later version. + + This program 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 this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + + +#ifndef UTIL_FOREACH_H +#define UTIL_FOREACH_H + +#include "lib/util.hpp" + +#include + + + +namespace util { + + + + /** shortcut for operating on all elements of a container. + * Isn't this already defined somewhere? It's so obvious.. + */ + template + inline Oper + for_each (Container& c, Oper doIt) + { + return std::for_each (c.begin(),c.end(), doIt); + } + + + /** All quantification: check if all elements of a collection + * satisfy the given predicate. Actually a short-circuit + * evaluation is performed. + */ + template + inline bool + and_all (SEQ& coll, Oper predicate) + { + typename SEQ::const_iterator e = coll.end(); + typename SEQ::const_iterator i = coll.begin(); + + for ( ; i!=e; ++i ) + if (!predicate(*i)) + return false; + + return true; + } + + + /** Existential quantification: check if any element + * of a collection satisfies the given predicate. + * Actually, a short-circuit evaluation is performed. + */ + template + inline bool + has_any (SEQ& coll, Oper predicate) + { + typename SEQ::const_iterator e = coll.end(); + typename SEQ::const_iterator i = coll.begin(); + + for ( ; i!=e; ++i ) + if (predicate(*i)) + return true; + + return false; + } + + +} // namespace util +#endif /*UTIL_FOREACH_H*/ diff --git a/src/lib/util.hpp b/src/lib/util.hpp index 02a52941b..3d3abf038 100644 --- a/src/lib/util.hpp +++ b/src/lib/util.hpp @@ -191,32 +191,6 @@ namespace util { } - /** shortcut for operating on all elements of a container. - * Isn't this already defined somewhere? It's so obvious.. - */ - template - inline Oper - for_each (Container& c, Oper doIt) - { - return std::for_each (c.begin(),c.end(), doIt); - } - - - /** shortcut for testing all elements of a collection - * with the given predicate. - */ - template - inline bool - and_all (SEQ& coll, Oper predicate) - { - typename SEQ::const_iterator e = coll.end(); - typename SEQ::const_iterator i = coll.begin(); - - while (i!=e && predicate(*i)) ++i; - return i==e; - } - - /** shortcut to save some typing when having to define * const and non-const variants of member functions */ diff --git a/src/proc/asset.cpp b/src/proc/asset.cpp index 3d4b8fc79..0cdbcf8f1 100644 --- a/src/proc/asset.cpp +++ b/src/proc/asset.cpp @@ -23,6 +23,7 @@ #include "proc/asset.hpp" #include "proc/assetmanager.hpp" +#include "lib/util-foreach.hpp" #include "lib/util.hpp" #include diff --git a/src/proc/assetmanager.cpp b/src/proc/assetmanager.cpp index c26811bed..625c189c1 100644 --- a/src/proc/assetmanager.cpp +++ b/src/proc/assetmanager.cpp @@ -25,7 +25,7 @@ #include "proc/asset/db.hpp" #include "lib/sync.hpp" -#include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include #include diff --git a/src/proc/engine/nodewiring-def.hpp b/src/proc/engine/nodewiring-def.hpp index 4fc4f14c2..c659d2493 100644 --- a/src/proc/engine/nodewiring-def.hpp +++ b/src/proc/engine/nodewiring-def.hpp @@ -42,6 +42,7 @@ #include "proc/engine/procnode.hpp" #include "lib/refarray.hpp" +#include "lib/util-foreach.hpp" #include //#include diff --git a/src/proc/mobject/session/defsregistry.hpp b/src/proc/mobject/session/defsregistry.hpp index 3fbd4aa1f..f37f08109 100644 --- a/src/proc/mobject/session/defsregistry.hpp +++ b/src/proc/mobject/session/defsregistry.hpp @@ -43,6 +43,7 @@ #include "lib/sync-classlock.hpp" #include "lib/query.hpp" #include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include "lib/p.hpp" #include diff --git a/src/proc/mobject/session/placement-index.cpp b/src/proc/mobject/session/placement-index.cpp index 174350c32..df5a6d206 100644 --- a/src/proc/mobject/session/placement-index.cpp +++ b/src/proc/mobject/session/placement-index.cpp @@ -55,7 +55,7 @@ #include "proc/mobject/session/scope.hpp" #include "lib/typed-allocation-manager.hpp" //#include "proc/mobject/mobject.hpp" ///////////////////////TODO necessary? -#include "lib/util.hpp" +#include "lib/util-foreach.hpp" //#include diff --git a/src/proc/mobject/session/scope-path.cpp b/src/proc/mobject/session/scope-path.cpp index f464cc7d9..0179a1ed8 100644 --- a/src/proc/mobject/session/scope-path.cpp +++ b/src/proc/mobject/session/scope-path.cpp @@ -26,10 +26,10 @@ #include "proc/mobject/session/scope-locator.hpp" #include "proc/mobject/session/session-service-explore-scope.hpp" #include "proc/mobject/mobject.hpp" +#include "lib/util-foreach.hpp" #include "lib/itertools.hpp" #include "lib/symbol.hpp" #include "lib/error.hpp" -#include "lib/util.hpp" #include #include diff --git a/tests/components/backend/mediaaccessmock.cpp b/tests/components/backend/mediaaccessmock.cpp index 25b5c6a0d..718bc55ee 100644 --- a/tests/components/backend/mediaaccessmock.cpp +++ b/tests/components/backend/mediaaccessmock.cpp @@ -36,6 +36,7 @@ #include "backend/mediaaccessmock.hpp" #include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include #include diff --git a/tests/components/proc/asset/asset-diagnostics.hpp b/tests/components/proc/asset/asset-diagnostics.hpp index 2ae2a232f..856f131b3 100644 --- a/tests/components/proc/asset/asset-diagnostics.hpp +++ b/tests/components/proc/asset/asset-diagnostics.hpp @@ -34,6 +34,7 @@ #include "proc/assetmanager.hpp" +#include "lib/util-foreach.hpp" #include "lib/util.hpp" #include diff --git a/tests/components/proc/control/command-argument-test.cpp b/tests/components/proc/control/command-argument-test.cpp index 2b9368bd3..bdf7b2724 100644 --- a/tests/components/proc/control/command-argument-test.cpp +++ b/tests/components/proc/control/command-argument-test.cpp @@ -27,6 +27,7 @@ #include "lib/scoped-ptrvect.hpp" #include "lib/lumitime-fmt.hpp" #include "lib/meta/tuple.hpp" +#include "lib/util-foreach.hpp" #include "lib/util.hpp" #include diff --git a/tests/components/proc/mobject/session/rebuildfixturetest.cpp b/tests/components/proc/mobject/session/rebuildfixturetest.cpp index ae287786c..a0970ff8b 100644 --- a/tests/components/proc/mobject/session/rebuildfixturetest.cpp +++ b/tests/components/proc/mobject/session/rebuildfixturetest.cpp @@ -25,6 +25,7 @@ #include "proc/mobject/session.hpp" #include "proc/mobject/session/edl.hpp" #include "proc/mobject/session/testsession1.hpp" +#include "lib/util-foreach.hpp" #include "lib/util.hpp" //#include diff --git a/tests/lib/allocationclustertest.cpp b/tests/lib/allocationclustertest.cpp index 6b07775ad..ffbba08cd 100644 --- a/tests/lib/allocationclustertest.cpp +++ b/tests/lib/allocationclustertest.cpp @@ -24,6 +24,7 @@ #include "lib/test/run.hpp" #include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include "lib/allocationcluster.hpp" #include "lib/scoped-holder.hpp" diff --git a/tests/lib/iter-adapter-test.cpp b/tests/lib/iter-adapter-test.cpp index 79732aaa1..6acf63f35 100644 --- a/tests/lib/iter-adapter-test.cpp +++ b/tests/lib/iter-adapter-test.cpp @@ -24,6 +24,7 @@ #include "lib/test/run.hpp" #include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include "lib/iter-adapter.hpp" diff --git a/tests/lib/itertools-test.cpp b/tests/lib/itertools-test.cpp index 8599f080f..850c8a03e 100644 --- a/tests/lib/itertools-test.cpp +++ b/tests/lib/itertools-test.cpp @@ -24,6 +24,7 @@ #include "lib/test/run.hpp" #include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include "lib/itertools.hpp" diff --git a/tests/lib/opaque-holder-test.cpp b/tests/lib/opaque-holder-test.cpp index b7be45f8a..09c595d07 100644 --- a/tests/lib/opaque-holder-test.cpp +++ b/tests/lib/opaque-holder-test.cpp @@ -25,6 +25,7 @@ #include "lib/test/run.hpp" #include "lib/test/test-helper.hpp" #include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include "lib/opaque-holder.hpp" #include "lib/bool-checkable.hpp" diff --git a/tests/lib/query/queryutilstest.cpp b/tests/lib/query/queryutilstest.cpp index 61b464118..9fd5e6677 100644 --- a/tests/lib/query/queryutilstest.cpp +++ b/tests/lib/query/queryutilstest.cpp @@ -23,6 +23,7 @@ #include "lib/test/run.hpp" #include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include "lib/cmdline.hpp" #include "lib/query.hpp" diff --git a/tests/lib/removefromsettest.cpp b/tests/lib/removefromsettest.cpp index dd512782f..ce9f825ec 100644 --- a/tests/lib/removefromsettest.cpp +++ b/tests/lib/removefromsettest.cpp @@ -22,7 +22,7 @@ #include "lib/test/run.hpp" -#include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include #include @@ -36,75 +36,72 @@ using std::string; -namespace util - { - namespace test - { - using util::for_each; - using boost::lambda::_1; - using boost::lambda::bind; - using boost::lexical_cast; - - +namespace util { +namespace test { - template - void - show (COLL const& coll) + using util::for_each; + using boost::lambda::_1; + using boost::lambda::bind; + using boost::lexical_cast; + + + + template + void + show (COLL const& coll) + { + cout << "[ "; + for_each (coll, cout << _1 << ", "); + cout << "]\n"; + } + + bool + killerselector (string description, uint candidate) + { + return string::npos != description.find( lexical_cast (candidate)); + } + + + + class RemoveFromSet_test : public Test { - cout << "[ "; - for_each (coll, cout << _1 << ", "); - cout << "]\n"; - } - - bool - killerselector (string description, uint candidate) - { - return string::npos != description.find( lexical_cast (candidate)); - } - - - - class RemoveFromSet_test : public Test - { - virtual void - run (Arg) - { - test_remove (" nothing "); - test_remove ("0"); - test_remove ("9"); - test_remove ("5"); - test_remove ("0 2 4 6 8 "); - test_remove (" 1 3 5 7 9"); - test_remove ("0 1 2 3 4 5 6 7 8 9"); - test_remove ("0 1 2 3 4 5 6 7 8 "); - test_remove (" 1 2 3 4 5 6 7 8 9"); - test_remove ("0 1 2 3 4 6 7 8 9"); - } - - - /** @test populate a test set, - * remove the denoted elements - * and print the result... */ - void - test_remove (string elems_to_remove) - { - std::set theSet; - for (int i=0; i<10; ++i) - theSet.insert (i); - - util::remove_if (theSet, bind( killerselector, elems_to_remove, _1)); - - cout << "removed " << elems_to_remove << " ---> "; - show (theSet); - } - - }; - + virtual void + run (Arg) + { + test_remove (" nothing "); + test_remove ("0"); + test_remove ("9"); + test_remove ("5"); + test_remove ("0 2 4 6 8 "); + test_remove (" 1 3 5 7 9"); + test_remove ("0 1 2 3 4 5 6 7 8 9"); + test_remove ("0 1 2 3 4 5 6 7 8 "); + test_remove (" 1 2 3 4 5 6 7 8 9"); + test_remove ("0 1 2 3 4 6 7 8 9"); + } - LAUNCHER (RemoveFromSet_test, "unit common"); - - } // namespace test - -} // namespace util + /** @test populate a test set, + * remove the denoted elements + * and print the result... */ + void + test_remove (string elems_to_remove) + { + std::set theSet; + for (int i=0; i<10; ++i) + theSet.insert (i); + + util::remove_if (theSet, bind( killerselector, elems_to_remove, _1)); + + cout << "removed " << elems_to_remove << " ---> "; + show (theSet); + } + + }; + + + LAUNCHER (RemoveFromSet_test, "unit common"); + + +}} // namespace util::test diff --git a/tests/lib/sub-id-test.cpp b/tests/lib/sub-id-test.cpp index c61a6b8c4..b52778c21 100644 --- a/tests/lib/sub-id-test.cpp +++ b/tests/lib/sub-id-test.cpp @@ -23,6 +23,7 @@ #include "lib/test/run.hpp" #include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include "lib/sub-id.hpp" diff --git a/tests/lib/test/cmdlinewrappertest.cpp b/tests/lib/test/cmdlinewrappertest.cpp index 7ef194c1d..00a016d9f 100644 --- a/tests/lib/test/cmdlinewrappertest.cpp +++ b/tests/lib/test/cmdlinewrappertest.cpp @@ -23,7 +23,7 @@ #include "lib/test/run.hpp" #include "lib/cmdline.hpp" -#include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include #include @@ -34,68 +34,66 @@ using std::cout; -namespace util - { - namespace test - { - using boost::lambda::_1; - using boost::lambda::var; - - +namespace util { +namespace test { + using boost::lambda::_1; + using boost::lambda::var; - /** @test for util::Cmdline, wrapping various example cmdlines */ - class CmdlineWrapper_test : public Test - { - void - run (Arg) - { - testLine(""); - testLine("\n\t "); - testLine("spam"); - testLine("\nspam"); - testLine("eat more spam"); - testLine(" oo _O()O_ ä + €"); - testLine("Ω\tooΩ\toΩo\tΩoo"); - - testStandardCmdlineformat(); - } - - void testLine (const string cmdline) - { - cout << "wrapping cmdline:" << cmdline << "..." << "\n"; - - int i=0; - Cmdline theCmdline (cmdline); - for_each(theCmdline, (cout << var(i)++ << "|" << _1 << "|\n")); - cout << "-->" << theCmdline << "\n"; - - // consistency checks - std::ostringstream output; - output << theCmdline; - ENSURE (output.str() == string(theCmdline)); - - i=0; - string token; - std::istringstream input(theCmdline); - while (input >> token) - ENSURE (token == theCmdline[i++]); - } - - /** @test wrapping a (albeit faked) standard commandline - * given as (argc, argv[]) - */ - void testStandardCmdlineformat() - { - const char* fakeArg[3] = {"CMD", "one ", "two"}; - Cmdline theCmdline(3, fakeArg); - cout << "Standard Cmdlineformat:" << theCmdline << "\n"; - } - }; - - LAUNCHER (CmdlineWrapper_test, "unit common"); - + + + /** @test for util::Cmdline, wrapping various example cmdlines */ + class CmdlineWrapper_test : public Test + { + void + run (Arg) + { + testLine(""); + testLine("\n\t "); + testLine("spam"); + testLine("\nspam"); + testLine("eat more spam"); + testLine(" oo _O()O_ ä + €"); + testLine("Ω\tooΩ\toΩo\tΩoo"); + + testStandardCmdlineformat(); + } - } // namespace test - -} // namespace util + void + testLine (const string cmdline) + { + cout << "wrapping cmdline:" << cmdline << "..." << "\n"; + + int i=0; + Cmdline theCmdline (cmdline); + for_each(theCmdline, (cout << var(i)++ << "|" << _1 << "|\n")); + cout << "-->" << theCmdline << "\n"; + + // consistency checks + std::ostringstream output; + output << theCmdline; + ENSURE (output.str() == string(theCmdline)); + + i=0; + string token; + std::istringstream input(theCmdline); + while (input >> token) + ENSURE (token == theCmdline[i++]); + } + + /** @test wrapping a (albeit faked) standard commandline + * given as (argc, argv[]) + */ + void + testStandardCmdlineformat() + { + const char* fakeArg[3] = {"CMD", "one ", "two"}; + Cmdline theCmdline(3, fakeArg); + cout << "Standard Cmdlineformat:" << theCmdline << "\n"; + } + }; + + LAUNCHER (CmdlineWrapper_test, "unit common"); + + +}} // namespace util::test diff --git a/tests/lib/test/test-helper-test.cpp b/tests/lib/test/test-helper-test.cpp index f9c940741..84d3cfe92 100644 --- a/tests/lib/test/test-helper-test.cpp +++ b/tests/lib/test/test-helper-test.cpp @@ -24,7 +24,7 @@ #include "lib/test/run.hpp" #include "lib/test/test-helper.hpp" #include "lib/error.hpp" -#include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include #include diff --git a/tests/lib/typed-counter-test.cpp b/tests/lib/typed-counter-test.cpp index a118d8d26..02d9e26d4 100644 --- a/tests/lib/typed-counter-test.cpp +++ b/tests/lib/typed-counter-test.cpp @@ -46,8 +46,8 @@ #include "lib/typed-counter.hpp" #include "lib/scoped-ptrvect.hpp" #include "backend/thread-wrapper.hpp" +#include "lib/util-foreach.hpp" #include "lib/sync.hpp" -#include "lib/util.hpp" #include #include diff --git a/tests/lib/util-foreach-test.cpp b/tests/lib/util-foreach-test.cpp index e78f873b5..a1f5294e8 100644 --- a/tests/lib/util-foreach-test.cpp +++ b/tests/lib/util-foreach-test.cpp @@ -22,7 +22,7 @@ #include "lib/test/run.hpp" -#include "lib/util.hpp" +#include "lib/util-foreach.hpp" #include "lib/iter-adapter.hpp"