tutorial updates

[SVN r21615]
This commit is contained in:
Dave Abrahams
2004-01-12 02:49:55 +00:00
parent a6b2a098c1
commit 06e1fa88b9
20 changed files with 3805 additions and 325 deletions

View File

@ -2,6 +2,9 @@ subproject libs/iterator/example ;
import testing ;
# Make tests run by default.
DEPENDS all : test ;
test-suite iterator_examples
: [ run reverse_iterator.cpp ]
[ run node_iterator1.cpp ]

View File

@ -23,7 +23,7 @@ struct node_base
}
virtual void print(std::ostream& s) const = 0;
virtual void twice() = 0;
virtual void double_me() = 0;
void append(node_base* p)
{
@ -51,7 +51,7 @@ struct node : node_base
{}
void print(std::ostream& s) const { s << this->m_value; }
void twice() { m_value += m_value; }
void double_me() { m_value += m_value; }
private:
T m_value;

View File

@ -23,7 +23,7 @@ int main()
std::for_each(
node_iterator(nodes.get()), node_iterator()
, std::mem_fun_ref(&node_base::twice)
, std::mem_fun_ref(&node_base::double_me)
);
std::copy(

View File

@ -23,9 +23,9 @@ class node_iterator
: m_node(p)
{}
private:
friend class boost::iterator_core_access;
private:
void increment()
{ m_node = m_node->next(); }

View File

@ -31,7 +31,7 @@ int main()
std::for_each(
node_iterator(nodes.get()), node_iterator()
, boost::mem_fn(&node_base::twice)
, boost::mem_fn(&node_base::double_me)
);
std::copy(

View File

@ -12,65 +12,62 @@
# include <boost/utility/enable_if.hpp>
# endif
namespace impl
template <class Value>
class node_iter
: public boost::iterator_facade<
node_iter<Value>
, Value
, boost::forward_traversal_tag
>
{
template <class Value>
class node_iterator
: public boost::iterator_facade<
node_iterator<Value>
, Value
, boost::forward_traversal_tag
>
{
private:
enum enabler {};
public:
node_iterator()
: m_node(0)
{}
private:
struct enabler {}; // a private type avoids misuse
explicit node_iterator(Value* p)
: m_node(p)
{}
public:
node_iter()
: m_node(0) {}
template <class OtherValue>
node_iterator(
node_iterator<OtherValue> const& other
explicit node_iter(Value* p)
: m_node(p) {}
template <class OtherValue>
node_iter(
node_iter<OtherValue> const& other
# ifndef BOOST_NO_SFINAE
, typename boost::enable_if<boost::is_convertible<OtherValue*,Value*>,enabler*>::type = 0
, typename boost::enable_if<
boost::is_convertible<OtherValue*,Value*>
, enabler
>::type = enabler()
# endif
)
: m_node(other.m_node)
{
}
)
: m_node(other.m_node) {}
friend class boost::iterator_core_access;
# if !BOOST_WORKAROUND(__GNUC__, == 2)
private: // GCC2 can't even grant that friendship to template member functions
private: // GCC2 can't even grant that friendship to template member functions
# endif
template <class OtherValue>
bool equal(node_iterator<OtherValue> const& other) const
{ return this->m_node == other.m_node; }
public:
void increment()
{ m_node = m_node->next(); }
friend class boost::iterator_core_access;
Value& dereference() const
{ return *m_node; }
template <class OtherValue>
bool equal(node_iter<OtherValue> const& other) const
{
return this->m_node == other.m_node;
}
private:
void increment() { m_node = m_node->next(); }
Value& dereference() const { return *m_node; }
# ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
public:
public:
# else
template <class> friend class node_iterator;
template <class> friend class node_iter;
# endif
Value* m_node;
};
}
Value* m_node;
};
typedef impl::node_iterator<node_base> node_iterator;
typedef impl::node_iterator<node_base const> node_const_iterator;
typedef node_iter<node_base> node_iterator;
typedef node_iter<node_base const> node_const_iterator;
#endif // NODE_ITERATOR2_DWA2004110_HPP