Finished iterator_adaptor tutorial

Added example test code
Rolled forward old counting_iterator abstract for boost docs.


[SVN r21649]
This commit is contained in:
Dave Abrahams
2004-01-12 20:58:22 +00:00
parent 19dbb5304c
commit 3bf52ec2f2
13 changed files with 488 additions and 138 deletions

View File

@ -9,4 +9,5 @@ test-suite iterator_examples
: [ run reverse_iterator.cpp ]
[ run node_iterator1.cpp ]
[ run node_iterator2.cpp ]
[ run node_iterator3.cpp ]
;

View File

@ -5,7 +5,7 @@
# define NODE_ITERATOR2_DWA2004110_HPP
# include "node.hpp"
# include <boost/iterator/iterator_facade.hpp>
# include <boost/iterator/iterator_adaptor.hpp>
# ifndef BOOST_NO_SFINAE
# include <boost/type_traits/is_convertible.hpp>
@ -14,21 +14,26 @@
template <class Value>
class node_iter
: public boost::iterator_facade<
node_iter<Value>
, Value
, boost::forward_traversal_tag
: public boost::iterator_adaptor<
node_iter<Value> // Derived
, Value* // Base
, boost::use_default // Value
, boost::forward_traversal_tag // CategoryOrTraversal
>
{
private:
struct enabler {}; // a private type avoids misuse
typedef boost::iterator_adaptor<
node_iter<Value>, Value*, boost::use_default, boost::forward_traversal_tag
> super_t;
public:
node_iter()
: m_node(0) {}
: super_t(0) {}
explicit node_iter(Value* p)
: m_node(p) {}
: super_t(p) {}
template <class OtherValue>
node_iter(
@ -42,29 +47,9 @@ class node_iter
)
: m_node(other.m_node) {}
# if !BOOST_WORKAROUND(__GNUC__, == 2)
private: // GCC2 can't even grant that friendship to template member functions
# endif
friend class boost::iterator_core_access;
template <class OtherValue>
bool equal(node_iter<OtherValue> const& other) const
{
return this->m_node == other.m_node;
}
private:
friend class boost::iterator_core_access;
void increment() { m_node = m_node->next(); }
Value& dereference() const { return *m_node; }
# ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
public:
# else
template <class> friend class node_iter;
# endif
Value* m_node;
};
typedef node_iter<node_base> node_iterator;

43
example/node_iterator3.cpp Executable file
View File

@ -0,0 +1,43 @@
// Copyright David Abrahams 2004. Use, modification and distribution is
// subject to the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "node_iterator3.hpp"
#include <string>
#include <memory>
#include <iostream>
#include <algorithm>
#include <boost/mem_fn.hpp>
#include <cassert>
int main()
{
std::auto_ptr<node<int> > nodes(new node<int>(42));
nodes->append(new node<std::string>(" is greater than "));
nodes->append(new node<int>(13));
// Check interoperability
assert(node_iterator(nodes.get()) == node_const_iterator(nodes.get()));
assert(node_const_iterator(nodes.get()) == node_iterator(nodes.get()));
assert(node_iterator(nodes.get()) != node_const_iterator());
assert(node_const_iterator(nodes.get()) != node_iterator());
std::copy(
node_iterator(nodes.get()), node_iterator()
, std::ostream_iterator<node_base>(std::cout, " ")
);
std::cout << std::endl;
std::for_each(
node_iterator(nodes.get()), node_iterator()
, boost::mem_fn(&node_base::double_me)
);
std::copy(
node_const_iterator(nodes.get()), node_const_iterator()
, std::ostream_iterator<node_base>(std::cout, "/")
);
std::cout << std::endl;
return 0;
}

58
example/node_iterator3.hpp Executable file
View File

@ -0,0 +1,58 @@
// Copyright David Abrahams 2004. Use, modification and distribution is
// subject to the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef NODE_ITERATOR3_DWA2004110_HPP
# define NODE_ITERATOR3_DWA2004110_HPP
# include "node.hpp"
# include <boost/iterator/iterator_adaptor.hpp>
# ifndef BOOST_NO_SFINAE
# include <boost/type_traits/is_convertible.hpp>
# include <boost/utility/enable_if.hpp>
# endif
template <class Value>
class node_iter
: public boost::iterator_adaptor<
node_iter<Value> // Derived
, Value* // Base
, boost::use_default // Value
, boost::forward_traversal_tag // CategoryOrTraversal
>
{
private:
struct enabler {}; // a private type avoids misuse
typedef boost::iterator_adaptor<
node_iter<Value>, Value*, boost::use_default, boost::forward_traversal_tag
> super_t;
public:
node_iter()
: super_t(0) {}
explicit node_iter(Value* p)
: super_t(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 = enabler()
# endif
)
: m_node(other.m_node) {}
private:
friend class boost::iterator_core_access;
void increment() { this->base_reference() = this->base()->next(); }
};
typedef node_iter<node_base> node_iterator;
typedef node_iter<node_base const> node_const_iterator;
#endif // NODE_ITERATOR3_DWA2004110_HPP