aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-01-20 01:21:17 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-01-20 01:21:17 +0000
commit9ded15bf7720d70e0a0f3cb9d00fe244c7e2caad (patch)
tree1bd7f4a8f394191cc4aef031af4af7ea5f4369b4
parentf606692f3995b461df5dab6fa40ea86c91813769 (diff)
downloadpaludis-9ded15bf7720d70e0a0f3cb9d00fe244c7e2caad.tar.gz
paludis-9ded15bf7720d70e0a0f3cb9d00fe244c7e2caad.tar.xz
Create an AcceptVisitor class and accept_visitor convenience function to simplify calling accept on each item in a container of pointers to nodes. Use them.
-rw-r--r--paludis/args/args_handler.cc9
-rw-r--r--paludis/dep_atom_dumper.cc13
-rw-r--r--paludis/dep_list.cc5
-rw-r--r--paludis/visitor.hh40
-rw-r--r--paludis/visitor_TEST.cc8
5 files changed, 51 insertions, 24 deletions
diff --git a/paludis/args/args_handler.cc b/paludis/args/args_handler.cc
index 5d0be78..16428b6 100644
--- a/paludis/args/args_handler.cc
+++ b/paludis/args/args_handler.cc
@@ -19,6 +19,7 @@
#include "args.hh"
#include "args_dumper.hh"
+#include <algorithm>
/** \file
* Implementation for ArgsHandler.
@@ -103,12 +104,8 @@ ArgsHandler::dump_to_stream(std::ostream & s) const
{
s << (*g)->name() << ":" << std::endl;
- std::list<ArgsOption *>::const_iterator a((*g)->_args_options.begin()),
- a_end((*g)->_args_options.end());
- for ( ; a != a_end ; ++a)
- {
- (*a)->accept(&dump);
- }
+ std::for_each((*g)->_args_options.begin(), (*g)->_args_options.end(),
+ accept_visitor(&dump));
s << std::endl;
}
diff --git a/paludis/dep_atom_dumper.cc b/paludis/dep_atom_dumper.cc
index 5eaa96c..7c05032 100644
--- a/paludis/dep_atom_dumper.cc
+++ b/paludis/dep_atom_dumper.cc
@@ -24,6 +24,7 @@
#include "package_dep_atom.hh"
#include "dep_atom_visitor.hh"
#include "dep_atom_dumper.hh"
+#include <algorithm>
using namespace paludis;
@@ -36,9 +37,7 @@ void
DepAtomDumper::visit(const AllDepAtom * const a)
{
*_o << "<all>";
- for (CompositeDepAtom::Iterator i(a->begin()), i_end(a->end()) ;
- i != i_end ; ++i)
- (*i)->accept(this);
+ std::for_each(a->begin(), a->end(), accept_visitor(this));
*_o << "</all>";
}
@@ -46,9 +45,7 @@ void
DepAtomDumper::visit(const AnyDepAtom * const a)
{
*_o << "<any>";
- for (CompositeDepAtom::Iterator i(a->begin()), i_end(a->end()) ;
- i != i_end ; ++i)
- (*i)->accept(this);
+ std::for_each(a->begin(), a->end(), accept_visitor(this));
*_o << "</any>";
}
@@ -57,9 +54,7 @@ DepAtomDumper::visit(const UseDepAtom * const a)
{
*_o << "<use flag=\"" << a->flag() << "\" inverse=\""
<< (a->inverse() ? "true" : "false") << "\">";
- for (CompositeDepAtom::Iterator i(a->begin()), i_end(a->end()) ;
- i != i_end ; ++i)
- (*i)->accept(this);
+ std::for_each(a->begin(), a->end(), accept_visitor(this));
*_o << "</use>";
}
diff --git a/paludis/dep_list.cc b/paludis/dep_list.cc
index 31b4be6..f9aca2c 100644
--- a/paludis/dep_list.cc
+++ b/paludis/dep_list.cc
@@ -145,9 +145,8 @@ DepList::end() const
void
DepList::visit(const AllDepAtom * const v)
{
- for (CompositeDepAtom::Iterator i(v->begin()), i_end(v->end()) ;
- i != i_end ; ++i)
- (*i)->accept(this);
+ std::for_each(v->begin(), v->end(), accept_visitor(
+ static_cast<DepAtomVisitorTypes::ConstVisitor *>(this)));
}
#ifndef DOXYGEN
diff --git a/paludis/visitor.hh b/paludis/visitor.hh
index c2ea35b..5cd646a 100644
--- a/paludis/visitor.hh
+++ b/paludis/visitor.hh
@@ -234,6 +234,46 @@ namespace paludis
{
};
};
+
+ /**
+ * Functor: simplify calling accept on a visitor when we have a container of
+ * pointers to nodes.
+ *
+ * \ingroup Visitor
+ */
+ template <typename VisitorPointer_>
+ class AcceptVisitor
+ {
+ private:
+ VisitorPointer_ * const _p;
+
+ public:
+ /**
+ * Constructor.
+ */
+ AcceptVisitor(VisitorPointer_ * const p) :
+ _p(p)
+ {
+ }
+
+ /**
+ * Operator.
+ */
+ template <typename T_>
+ void operator() (T_ t) const
+ {
+ t->accept(_p);
+ }
+ };
+
+ /**
+ * Convenience function: create an AcceptVisitor.
+ */
+ template <typename VisitorPointer_>
+ AcceptVisitor<VisitorPointer_> accept_visitor(VisitorPointer_ * const p)
+ {
+ return AcceptVisitor<VisitorPointer_>(p);
+ }
}
#endif
diff --git a/paludis/visitor_TEST.cc b/paludis/visitor_TEST.cc
index 1bd2c57..d0c65ab 100644
--- a/paludis/visitor_TEST.cc
+++ b/paludis/visitor_TEST.cc
@@ -134,9 +134,7 @@ namespace test_cases
NodeCVisitor c;
TEST_CHECK_EQUAL(c.r, "");
- for (IndirectIterator<std::vector<Node *>::const_iterator, Node>
- i(v.begin()), i_end(v.end()) ; i != i_end ; ++i)
- i->accept(&c);
+ std::for_each(v.begin(), v.end(), accept_visitor(&c));
TEST_CHECK_EQUAL(c.r, "c_fooc_barc_foo");
std::for_each(v.begin(), v.end(), Deleter());
@@ -162,9 +160,7 @@ namespace test_cases
NodeVisitor c;
TEST_CHECK_EQUAL(c.r, "");
- for (IndirectIterator<std::vector<Node *>::iterator, Node>
- i(v.begin()), i_end(v.end()) ; i != i_end ; ++i)
- i->accept(&c);
+ std::for_each(v.begin(), v.end(), accept_visitor(&c));
TEST_CHECK_EQUAL(c.r, "foobarfoo");
std::for_each(v.begin(), v.end(), Deleter());