base: Use a lambda to simplify the stl_helpers template.

Use a lambda to power the std::for_each instead of a functor class. This
takes *much* less code to implement.

Change-Id: Icc5d5a943630e7e19cd2e0fa6f2cef2c9b766e24
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/45900
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabe Black
2021-05-23 00:43:13 -07:00
parent f511aa977e
commit 85d1e1f803

View File

@@ -35,37 +35,6 @@
namespace m5 {
namespace stl_helpers {
template <class T>
class ContainerPrint
{
private:
std::ostream &out;
bool first;
public:
/**
* @ingroup api_base_utils
*/
ContainerPrint(std::ostream &out)
: out(out), first(true)
{}
/**
* @ingroup api_base_utils
*/
void
operator()(const T &elem)
{
// First one doesn't get a space before it. The rest do.
if (first)
first = false;
else
out << " ";
out << elem;
}
};
/**
* Write out all elements in an stl container as a space separated
* list enclosed in square brackets
@@ -77,7 +46,14 @@ std::ostream &
operator<<(std::ostream& out, const C<T,A> &vec)
{
out << "[ ";
std::for_each(vec.begin(), vec.end(), ContainerPrint<T>(out));
bool first = true;
auto printer = [&first, &out](const T &elem) {
if (first)
out << elem;
else
out << " " << elem;
};
std::for_each(vec.begin(), vec.end(), printer);
out << " ]";
out << std::flush;
return out;