move the for_each helpers into a separate header

(because util.hpp is used pervasively, and I don't want
<tr1/functional> in such a widely used header...
This commit is contained in:
Fischlurch 2009-12-29 04:39:27 +01:00
parent 0dca7cbb4d
commit 2b46574da3
24 changed files with 233 additions and 163 deletions

View file

@ -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"

87
src/lib/util-foreach.hpp Normal file
View file

@ -0,0 +1,87 @@
/*
UTIL-FOREACH.hpp - helpers for doing something for each element
Copyright (C) Lumiera.org
2009, Hermann Vosseler <Ichthyostega@web.de>
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 <algorithm>
namespace util {
/** shortcut for operating on all elements of a container.
* Isn't this already defined somewhere? It's so obvious..
*/
template <typename Container, typename Oper>
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 <typename SEQ, typename Oper>
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 <typename SEQ, typename Oper>
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*/

View file

@ -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 <typename Container, typename Oper>
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 <typename SEQ, typename Oper>
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
*/

View file

@ -23,6 +23,7 @@
#include "proc/asset.hpp"
#include "proc/assetmanager.hpp"
#include "lib/util-foreach.hpp"
#include "lib/util.hpp"
#include <boost/format.hpp>

View file

@ -25,7 +25,7 @@
#include "proc/asset/db.hpp"
#include "lib/sync.hpp"
#include "lib/util.hpp"
#include "lib/util-foreach.hpp"
#include <tr1/functional>
#include <boost/format.hpp>

View file

@ -42,6 +42,7 @@
#include "proc/engine/procnode.hpp"
#include "lib/refarray.hpp"
#include "lib/util-foreach.hpp"
#include <boost/noncopyable.hpp>
//#include <boost/scoped_ptr.hpp>

View file

@ -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 <set>

View file

@ -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 <boost/format.hpp>

View file

@ -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 <tr1/functional>
#include <algorithm>

View file

@ -36,6 +36,7 @@
#include "backend/mediaaccessmock.hpp"
#include "lib/util.hpp"
#include "lib/util-foreach.hpp"
#include <iostream>
#include <vector>

View file

@ -34,6 +34,7 @@
#include "proc/assetmanager.hpp"
#include "lib/util-foreach.hpp"
#include "lib/util.hpp"
#include <boost/format.hpp>

View file

@ -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 <boost/format.hpp>

View file

@ -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 <boost/format.hpp>

View file

@ -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"

View file

@ -24,6 +24,7 @@
#include "lib/test/run.hpp"
#include "lib/util.hpp"
#include "lib/util-foreach.hpp"
#include "lib/iter-adapter.hpp"

View file

@ -24,6 +24,7 @@
#include "lib/test/run.hpp"
#include "lib/util.hpp"
#include "lib/util-foreach.hpp"
#include "lib/itertools.hpp"

View file

@ -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"

View file

@ -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"

View file

@ -22,7 +22,7 @@
#include "lib/test/run.hpp"
#include "lib/util.hpp"
#include "lib/util-foreach.hpp"
#include <boost/lexical_cast.hpp>
#include <boost/lambda/lambda.hpp>
@ -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<class COLL>
void
show (COLL const& coll)
using util::for_each;
using boost::lambda::_1;
using boost::lambda::bind;
using boost::lexical_cast;
template<class COLL>
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<string> (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<string> (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<uint> 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<uint> 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

View file

@ -23,6 +23,7 @@
#include "lib/test/run.hpp"
#include "lib/util.hpp"
#include "lib/util-foreach.hpp"
#include "lib/sub-id.hpp"

View file

@ -23,7 +23,7 @@
#include "lib/test/run.hpp"
#include "lib/cmdline.hpp"
#include "lib/util.hpp"
#include "lib/util-foreach.hpp"
#include <iostream>
#include <sstream>
@ -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

View file

@ -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 <boost/algorithm/string.hpp>
#include <tr1/functional>

View file

@ -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 <tr1/functional>
#include <vector>

View file

@ -22,7 +22,7 @@
#include "lib/test/run.hpp"
#include "lib/util.hpp"
#include "lib/util-foreach.hpp"
#include "lib/iter-adapter.hpp"