1
0
forked from boostorg/bind

visit_each support added.

[SVN r11784]
This commit is contained in:
Peter Dimov
2001-11-26 13:28:16 +00:00
parent f17fa79f2b
commit 46a32f7547
3 changed files with 127 additions and 102 deletions

View File

@@ -8,8 +8,6 @@
// //
// bind_visitor.cpp - tests bind.hpp with a visitor // bind_visitor.cpp - tests bind.hpp with a visitor
// //
// Version 1.00.0003 (2001-10-18)
//
// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. // Copyright (c) 2001 Peter Dimov and Multi Media Ltd.
// //
// Permission to copy, use, modify, sell and distribute this software // Permission to copy, use, modify, sell and distribute this software
@@ -20,42 +18,48 @@
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/ref.hpp> #include <boost/ref.hpp>
#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
#pragma warning(push, 3)
#endif
#include <iostream> #include <iostream>
#include <typeinfo> #include <typeinfo>
#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
#pragma warning(pop)
#endif
// default implementation of visit_each
namespace boost
{
template<class V, class T> void visit_each(V & v, T const & t, long)
{
v(t, 0);
}
}
// visitor
int hash = 0; int hash = 0;
struct ref_visitor struct visitor
{ {
template<class R, class F, class L> void operator()(boost::_bi::bind_t<R, F, L> const & b) const template<class T> void operator()(boost::reference_wrapper<T> const & r, int) const
{
b.accept(*this);
}
template<class T> void operator()(boost::reference_wrapper<T> const & r) const
{ {
std::cout << "Reference to " << typeid(T).name() << " @ " << &r.get() << " (with value " << r.get() << ")\n"; std::cout << "Reference to " << typeid(T).name() << " @ " << &r.get() << " (with value " << r.get() << ")\n";
hash += r.get(); hash += r.get();
} }
#ifndef BOOST_MSVC template<class T> void operator()(T const &, long) const
template<class T> void operator()(T const &) const
{ {
std::cout << "Catch-all: " << typeid(T).name() << '\n'; std::cout << "Value of type " << typeid(T).name() << '\n';
++hash;
} }
#else
void operator()(...) const
{
}
#endif
}; };
int f(int & i, int & j) int f(int & i, int & j, int)
{ {
++i; ++i;
--j; --j;
@@ -83,7 +87,8 @@ int main()
{ {
using namespace boost; using namespace boost;
ref_visitor()(bind<int>(bind(f, ref(x), _1), ref(y))); visitor v;
visit_each(v, bind<int>(bind(f, ref(x), _1, 42), ref(y)), 0);
return detect_errors(hash == 9); return detect_errors(hash == 12);
} }

View File

@@ -23,6 +23,14 @@
#include <boost/ref.hpp> #include <boost/ref.hpp>
#include <boost/mem_fn.hpp> #include <boost/mem_fn.hpp>
// Borland-specific bug, visit_each() silently fails to produce code
#if defined(__BORLANDC__)
# define BOOST_BIND_VISIT_EACH boost::visit_each
#else
# define BOOST_BIND_VISIT_EACH visit_each
#endif
namespace boost namespace boost
{ {
@@ -120,10 +128,10 @@ public:
#ifdef BOOST_NO_VOID_RETURNS #ifdef BOOST_NO_VOID_RETURNS
template<class R> struct evaluator template<class R> struct evaluator
{ {
typedef evaluator0<R> type; typedef evaluator0<R> type;
}; };
#endif #endif
@@ -157,15 +165,15 @@ public:
template<class V> void accept(V & v) const template<class V> void accept(V & v) const
{ {
v(a1_); BOOST_BIND_VISIT_EACH(v, a1_, 0);
} }
#ifdef BOOST_NO_VOID_RETURNS #ifdef BOOST_NO_VOID_RETURNS
template<class R> struct evaluator template<class R> struct evaluator
{ {
typedef evaluator1<R> type; typedef evaluator1<R> type;
}; };
#else #else
@@ -206,16 +214,16 @@ public:
template<class V> void accept(V & v) const template<class V> void accept(V & v) const
{ {
v(a1_); BOOST_BIND_VISIT_EACH(v, a1_, 0);
v(a2_); BOOST_BIND_VISIT_EACH(v, a2_, 0);
} }
#ifdef BOOST_NO_VOID_RETURNS #ifdef BOOST_NO_VOID_RETURNS
template<class R> struct evaluator template<class R> struct evaluator
{ {
typedef evaluator2<R> type; typedef evaluator2<R> type;
}; };
#else #else
@@ -258,17 +266,17 @@ public:
template<class V> void accept(V & v) const template<class V> void accept(V & v) const
{ {
v(a1_); BOOST_BIND_VISIT_EACH(v, a1_, 0);
v(a2_); BOOST_BIND_VISIT_EACH(v, a2_, 0);
v(a3_); BOOST_BIND_VISIT_EACH(v, a3_, 0);
} }
#ifdef BOOST_NO_VOID_RETURNS #ifdef BOOST_NO_VOID_RETURNS
template<class R> struct evaluator template<class R> struct evaluator
{ {
typedef evaluator3<R> type; typedef evaluator3<R> type;
}; };
#else #else
@@ -313,18 +321,18 @@ public:
template<class V> void accept(V & v) const template<class V> void accept(V & v) const
{ {
v(a1_); BOOST_BIND_VISIT_EACH(v, a1_, 0);
v(a2_); BOOST_BIND_VISIT_EACH(v, a2_, 0);
v(a3_); BOOST_BIND_VISIT_EACH(v, a3_, 0);
v(a4_); BOOST_BIND_VISIT_EACH(v, a4_, 0);
} }
#ifdef BOOST_NO_VOID_RETURNS #ifdef BOOST_NO_VOID_RETURNS
template<class R> struct evaluator template<class R> struct evaluator
{ {
typedef evaluator4<R> type; typedef evaluator4<R> type;
}; };
#else #else
@@ -371,19 +379,19 @@ public:
template<class V> void accept(V & v) const template<class V> void accept(V & v) const
{ {
v(a1_); BOOST_BIND_VISIT_EACH(v, a1_, 0);
v(a2_); BOOST_BIND_VISIT_EACH(v, a2_, 0);
v(a3_); BOOST_BIND_VISIT_EACH(v, a3_, 0);
v(a4_); BOOST_BIND_VISIT_EACH(v, a4_, 0);
v(a5_); BOOST_BIND_VISIT_EACH(v, a5_, 0);
} }
#ifdef BOOST_NO_VOID_RETURNS #ifdef BOOST_NO_VOID_RETURNS
template<class R> struct evaluator template<class R> struct evaluator
{ {
typedef evaluator5<R> type; typedef evaluator5<R> type;
}; };
#else #else
@@ -432,20 +440,20 @@ public:
template<class V> void accept(V & v) const template<class V> void accept(V & v) const
{ {
v(a1_); BOOST_BIND_VISIT_EACH(v, a1_, 0);
v(a2_); BOOST_BIND_VISIT_EACH(v, a2_, 0);
v(a3_); BOOST_BIND_VISIT_EACH(v, a3_, 0);
v(a4_); BOOST_BIND_VISIT_EACH(v, a4_, 0);
v(a5_); BOOST_BIND_VISIT_EACH(v, a5_, 0);
v(a6_); BOOST_BIND_VISIT_EACH(v, a6_, 0);
} }
#ifdef BOOST_NO_VOID_RETURNS #ifdef BOOST_NO_VOID_RETURNS
template<class R> struct evaluator template<class R> struct evaluator
{ {
typedef evaluator6<R> type; typedef evaluator6<R> type;
}; };
#else #else
@@ -496,21 +504,21 @@ public:
template<class V> void accept(V & v) const template<class V> void accept(V & v) const
{ {
v(a1_); BOOST_BIND_VISIT_EACH(v, a1_, 0);
v(a2_); BOOST_BIND_VISIT_EACH(v, a2_, 0);
v(a3_); BOOST_BIND_VISIT_EACH(v, a3_, 0);
v(a4_); BOOST_BIND_VISIT_EACH(v, a4_, 0);
v(a5_); BOOST_BIND_VISIT_EACH(v, a5_, 0);
v(a6_); BOOST_BIND_VISIT_EACH(v, a6_, 0);
v(a7_); BOOST_BIND_VISIT_EACH(v, a7_, 0);
} }
#ifdef BOOST_NO_VOID_RETURNS #ifdef BOOST_NO_VOID_RETURNS
template<class R> struct evaluator template<class R> struct evaluator
{ {
typedef evaluator7<R> type; typedef evaluator7<R> type;
}; };
#else #else
@@ -563,22 +571,22 @@ public:
template<class V> void accept(V & v) const template<class V> void accept(V & v) const
{ {
v(a1_); BOOST_BIND_VISIT_EACH(v, a1_, 0);
v(a2_); BOOST_BIND_VISIT_EACH(v, a2_, 0);
v(a3_); BOOST_BIND_VISIT_EACH(v, a3_, 0);
v(a4_); BOOST_BIND_VISIT_EACH(v, a4_, 0);
v(a5_); BOOST_BIND_VISIT_EACH(v, a5_, 0);
v(a6_); BOOST_BIND_VISIT_EACH(v, a6_, 0);
v(a7_); BOOST_BIND_VISIT_EACH(v, a7_, 0);
v(a8_); BOOST_BIND_VISIT_EACH(v, a8_, 0);
} }
#ifdef BOOST_NO_VOID_RETURNS #ifdef BOOST_NO_VOID_RETURNS
template<class R> struct evaluator template<class R> struct evaluator
{ {
typedef evaluator8<R> type; typedef evaluator8<R> type;
}; };
#else #else
@@ -633,23 +641,23 @@ public:
template<class V> void accept(V & v) const template<class V> void accept(V & v) const
{ {
v(a1_); BOOST_BIND_VISIT_EACH(v, a1_, 0);
v(a2_); BOOST_BIND_VISIT_EACH(v, a2_, 0);
v(a3_); BOOST_BIND_VISIT_EACH(v, a3_, 0);
v(a4_); BOOST_BIND_VISIT_EACH(v, a4_, 0);
v(a5_); BOOST_BIND_VISIT_EACH(v, a5_, 0);
v(a6_); BOOST_BIND_VISIT_EACH(v, a6_, 0);
v(a7_); BOOST_BIND_VISIT_EACH(v, a7_, 0);
v(a8_); BOOST_BIND_VISIT_EACH(v, a8_, 0);
v(a9_); BOOST_BIND_VISIT_EACH(v, a9_, 0);
} }
#ifdef BOOST_NO_VOID_RETURNS #ifdef BOOST_NO_VOID_RETURNS
template<class R> struct evaluator template<class R> struct evaluator
{ {
typedef evaluator9<R> type; typedef evaluator9<R> type;
}; };
#else #else
@@ -1104,6 +1112,18 @@ template<class A1, class A2, class A3, class A4, class A5, class A6, class A7, c
} // namespace _bi } // namespace _bi
// visit_each
template<class V, class T> void visit_each(V & v, _bi::value<T> const & t, int)
{
BOOST_BIND_VISIT_EACH(v, t.get(), 0);
}
template<class V, class R, class F, class L> void visit_each(V & v, _bi::bind_t<R, F, L> const & t, int)
{
t.accept(v);
}
// bind // bind
#ifndef BOOST_BIND #ifndef BOOST_BIND

View File

@@ -1,7 +1,7 @@
// //
// bind/bind_template.hpp // bind/bind_template.hpp
// //
// Do not include this header directly. // Do not include this header directly.
// //
// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. // Copyright (c) 2001 Peter Dimov and Multi Media Ltd.
// //
@@ -147,7 +147,7 @@
template<class V> void accept(V & v) const template<class V> void accept(V & v) const
{ {
v(f_); BOOST_BIND_VISIT_EACH(v, f_, 0);
l_.accept(v); l_.accept(v);
} }