2003-08-05 19:48:41 +00:00
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" lang = "en" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" />
2004-01-12 15:53:04 +00:00
< meta name = "generator" content = "Docutils 0.2.8: http://docutils.sourceforge.net/" />
2003-08-05 19:48:41 +00:00
< title > Indirect Iterator</ title >
< meta name = "author" content = "David Abrahams, Jeremy Siek, Thomas Witt" />
< meta name = "organization" content = "Boost Consulting, Indiana University Open Systems Lab, University of Hanover Institute for Transport Railway Operation and Construction" />
2004-01-12 04:36:23 +00:00
< meta name = "date" content = "2004-01-12" />
2003-09-14 02:17:41 +00:00
< meta name = "copyright" content = "Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. All rights reserved" />
2004-01-12 04:17:26 +00:00
< link rel = "stylesheet" href = "default.css" type = "text/css" />
2003-08-05 19:48:41 +00:00
</ head >
< body >
< div class = "document" id = "indirect-iterator" >
< h1 class = "title" > Indirect Iterator</ h1 >
< table class = "docinfo" frame = "void" rules = "none" >
< col class = "docinfo-name" />
< col class = "docinfo-content" />
< tbody valign = "top" >
< tr >< th class = "docinfo-name" > Author:</ th >
< td > David Abrahams, Jeremy Siek, Thomas Witt</ td ></ tr >
< tr >< th class = "docinfo-name" > Contact:</ th >
< td >< a class = "first reference" href = "mailto:dave@boost-consulting.com" > dave@ boost-consulting.com</ a > , < a class = "reference" href = "mailto:jsiek@osl.iu.edu" > jsiek@ osl.iu.edu</ a > , < a class = "last reference" href = "mailto:witt@ive.uni-hannover.de" > witt@ ive.uni-hannover.de</ a ></ td ></ tr >
< tr >< th class = "docinfo-name" > Organization:</ th >
< td >< a class = "first reference" href = "http://www.boost-consulting.com" > Boost Consulting</ a > , Indiana University < a class = "reference" href = "http://www.osl.iu.edu" > Open Systems
Lab</ a > , University of Hanover < a class = "last reference" href = "http://www.ive.uni-hannover.de" > Institute for Transport
Railway Operation and Construction</ a ></ td ></ tr >
< tr >< th class = "docinfo-name" > Date:</ th >
2004-01-12 04:36:23 +00:00
< td > 2004-01-12</ td ></ tr >
2003-08-05 19:48:41 +00:00
< tr >< th class = "docinfo-name" > Copyright:</ th >
2003-09-14 02:17:41 +00:00
< td > Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. All rights reserved</ td ></ tr >
2003-08-05 19:48:41 +00:00
</ tbody >
</ table >
< table class = "field-list" frame = "void" rules = "none" >
< col class = "field-name" />
< col class = "field-body" />
< tbody valign = "top" >
< tr class = "field" >< th class = "field-name" > abstract:</ th >< td class = "field-body" ></ td >
</ tr >
</ tbody >
</ table >
< p > The indirect iterator adapts an iterator by applying an < em > extra</ em >
dereference inside of < tt class = "literal" >< span class = "pre" > operator*()</ span ></ tt > . For example, this iterator
adaptor makes it possible to view a container of pointers
(e.g. < tt class = "literal" >< span class = "pre" > list< foo*> </ span ></ tt > ) as if it were a container of the pointed-to type
(e.g. < tt class = "literal" >< span class = "pre" > list< foo> </ span ></ tt > ) .</ p >
<!-- At some point we should add the capability to handle
iterators over smart pointers, which the impl handles. -JGS -->
< div class = "contents topic" id = "table-of-contents" >
< p class = "topic-title" >< a name = "table-of-contents" > Table of Contents</ a ></ p >
< ul class = "simple" >
2004-01-12 16:10:29 +00:00
< li >< a class = "reference" href = "#indirect-iterator-synopsis" id = "id2" name = "id2" >< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > synopsis</ a ></ li >
< li >< a class = "reference" href = "#indirect-iterator-requirements" id = "id3" name = "id3" >< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > requirements</ a ></ li >
< li >< a class = "reference" href = "#indirect-iterator-models" id = "id4" name = "id4" >< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > models</ a ></ li >
< li >< a class = "reference" href = "#indirect-iterator-operations" id = "id5" name = "id5" >< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > operations</ a ></ li >
< li >< a class = "reference" href = "#example" id = "id6" name = "id6" > Example</ a ></ li >
2003-08-05 19:48:41 +00:00
</ ul >
</ div >
2004-01-12 15:53:04 +00:00
< div class = "section" id = "indirect-iterator-synopsis" >
2004-01-12 16:10:29 +00:00
< h1 >< a class = "toc-backref" href = "#id2" name = "indirect-iterator-synopsis" >< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > synopsis</ a ></ h1 >
2003-08-05 19:48:41 +00:00
< pre class = "literal-block" >
template <
class Iterator
, class Value = use_default
2004-01-10 19:00:48 +00:00
, class CategoryOrTraversal = use_default
2003-08-05 19:48:41 +00:00
, class Reference = use_default
, class Difference = use_default
>
class indirect_iterator
{
public:
2004-01-10 19:00:48 +00:00
typedef /* see below */ value_type;
typedef /* see below */ reference;
typedef /* see below */ pointer;
typedef /* see below */ difference_type;
typedef /* see below */ iterator_category;
2003-08-05 19:48:41 +00:00
indirect_iterator();
indirect_iterator(Iterator x);
2004-01-10 19:00:48 +00:00
2003-08-05 19:48:41 +00:00
template <
2004-01-10 19:00:48 +00:00
class Iterator2, class Value2, class Category2
2003-08-05 19:48:41 +00:00
, class Reference2, class Difference2
>
indirect_iterator(
indirect_iterator<
2004-01-10 19:00:48 +00:00
Iterator2, Value2, Category2, Reference2, Difference2
2003-08-05 19:48:41 +00:00
> const& y
, typename enable_if_convertible< Iterator2, Iterator> ::type* = 0 // exposition
);
2004-01-12 04:17:26 +00:00
Iterator base() const;
2004-01-12 15:53:04 +00:00
reference operator*() const;
indirect_iterator& operator++();
indirect_iterator& operator--();
2004-01-12 04:17:26 +00:00
private:
Iterator m_iterator; // exposition
2003-08-05 19:48:41 +00:00
};
</ pre >
2004-01-10 19:00:48 +00:00
< p > The member types of < tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > are defined according to the
following pseudo-code. We use the abbreviation
< tt class = "literal" >< span class = "pre" > V=iterator_traits< Iterator> ::value_type</ span ></ tt > .:</ p >
< pre class = "literal-block" >
if (Value is use_default) then
typedef iterator_traits< V> ::value_type value_type;
else
typedef remove_const< Value> ::type value_type;
if (Reference is use_default) then
if (Value is use_default) then
typedef iterator_traits< V> ::reference reference;
else
typedef Value& reference;
else
typedef Reference reference;
if (Value is use_default) then
typedef ?? pointer;
else
typedef Value* pointer;
if (Difference is use_default)
typedef iterator_traits< Iterator> ::difference_type difference_type;
else
typedef Difference difference_type;
</ pre >
2004-01-12 19:01:57 +00:00
< p > The member < tt class = "literal" >< span class = "pre" > iterator_category</ span ></ tt > is a type that satisfies the
requirements of the concepts modeled by the < tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > as
specified in the models section.</ p >
2004-01-12 15:53:04 +00:00
</ div >
2003-08-05 19:48:41 +00:00
< div class = "section" id = "indirect-iterator-requirements" >
2004-01-12 16:10:29 +00:00
< h1 >< a class = "toc-backref" href = "#id3" name = "indirect-iterator-requirements" >< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > requirements</ a ></ h1 >
2004-01-10 19:00:48 +00:00
< p > The < tt class = "literal" >< span class = "pre" > Iterator</ span ></ tt > argument shall meet the requirements of Readable
Iterator. The < tt class = "literal" >< span class = "pre" > CategoryOrTraversal</ span ></ tt > argument shall be one of the
standard iterator tags or < tt class = "literal" >< span class = "pre" > use_default</ span ></ tt > . If < tt class = "literal" >< span class = "pre" > CategoryOrTraversal</ span ></ tt >
is an iterator tag, the template parameter < tt class = "literal" >< span class = "pre" > Iterator</ span ></ tt > argument shall
meet the traversal requirements corresponding to the iterator tag.</ p >
< p > The expression < tt class = "literal" >< span class = "pre" > *v</ span ></ tt > , where < tt class = "literal" >< span class = "pre" > v</ span ></ tt > is an object of type
< tt class = "literal" >< span class = "pre" > iterator_traits< Iterator> ::value_type</ span ></ tt > , must be a valid expression
and must be convertible to < tt class = "literal" >< span class = "pre" > indirect_iterator::reference</ span ></ tt > . Also
< tt class = "literal" >< span class = "pre" > indirect_iterator::reference</ span ></ tt > must be convertible to
< tt class = "literal" >< span class = "pre" > indirect_iterator::value</ span ></ tt > . There are further requirements on the
< tt class = "literal" >< span class = "pre" > iterator_traits< Iterator> ::value_type</ span ></ tt > if the < tt class = "literal" >< span class = "pre" > Value</ span ></ tt > parameter
is not < tt class = "literal" >< span class = "pre" > use_default</ span ></ tt > , as implied by the algorithm for deducing the
default for the < tt class = "literal" >< span class = "pre" > value_type</ span ></ tt > member.</ p >
</ div >
< div class = "section" id = "indirect-iterator-models" >
2004-01-12 16:10:29 +00:00
< h1 >< a class = "toc-backref" href = "#id4" name = "indirect-iterator-models" >< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > models</ a ></ h1 >
2004-01-10 19:00:48 +00:00
< p > If < tt class = "literal" >< span class = "pre" > CategoryOrTraversal</ span ></ tt > is a standard iterator tag,
< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > is a model of the iterator concept corresponding
to the tag, otherwise < tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > satisfies the requirements
of the most refined standard traversal concept that is satisfied by
the < tt class = "literal" >< span class = "pre" > Iterator</ span ></ tt > argument.</ p >
< p >< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > models Readable Iterator. If
< tt class = "literal" >< span class = "pre" > indirect_iterator::reference(*v)</ span > < span class = "pre" > =</ span > < span class = "pre" > t</ span ></ tt > is a valid expression (where
< tt class = "literal" >< span class = "pre" > t</ span ></ tt > is an object of type < tt class = "literal" >< span class = "pre" > indirect_iterator::value_type</ span ></ tt > ) then
< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > models Writable Iterator. If
< tt class = "literal" >< span class = "pre" > indirect_iterator::reference</ span ></ tt > is a reference then
< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > models Lvalue Iterator.</ p >
2003-08-05 19:48:41 +00:00
</ div >
< div class = "section" id = "indirect-iterator-operations" >
2004-01-12 16:10:29 +00:00
< h1 >< a class = "toc-backref" href = "#id5" name = "indirect-iterator-operations" >< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > operations</ a ></ h1 >
2004-01-12 15:53:04 +00:00
< p > In addition to the operations required by the concepts modeled by
< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > , < tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > provides the following
operations.</ p >
2003-08-05 19:48:41 +00:00
< p >< tt class = "literal" >< span class = "pre" > indirect_iterator();</ span ></ tt ></ p >
< table class = "field-list" frame = "void" rules = "none" >
< col class = "field-name" />
< col class = "field-body" />
< tbody valign = "top" >
< tr class = "field" >< th class = "field-name" > Requires:</ th >< td class = "field-body" >< tt class = "literal" >< span class = "pre" > Iterator</ span ></ tt > must be Default Constructible.</ td >
</ tr >
2004-01-10 19:00:48 +00:00
< tr class = "field" >< th class = "field-name" > Returns:</ th >< td class = "field-body" > An instance of < tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > with
2004-01-12 04:17:26 +00:00
a default-constructed < tt class = "literal" >< span class = "pre" > m_iterator</ span ></ tt > .</ td >
2003-08-05 19:48:41 +00:00
</ tr >
</ tbody >
</ table >
< p >< tt class = "literal" >< span class = "pre" > indirect_iterator(Iterator</ span > < span class = "pre" > x);</ span ></ tt ></ p >
< table class = "field-list" frame = "void" rules = "none" >
< col class = "field-name" />
< col class = "field-body" />
< tbody valign = "top" >
< tr class = "field" >< th class = "field-name" > Returns:</ th >< td class = "field-body" > An instance of < tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > with
2004-01-12 04:17:26 +00:00
< tt class = "literal" >< span class = "pre" > m_iterator</ span ></ tt > copy constructed from < tt class = "literal" >< span class = "pre" > x</ span ></ tt > .</ td >
2003-08-05 19:48:41 +00:00
</ tr >
</ tbody >
</ table >
< pre class = "literal-block" >
template <
2003-11-24 05:02:46 +00:00
class Iterator2, class Value2, unsigned Access, class Traversal
2003-08-05 19:48:41 +00:00
, class Reference2, class Difference2
>
indirect_iterator(
indirect_iterator<
2003-11-24 05:02:46 +00:00
Iterator2, Value2, Access, Traversal, Reference2, Difference2
2003-08-05 19:48:41 +00:00
> const& y
, typename enable_if_convertible< Iterator2, Iterator> ::type* = 0 // exposition
);
</ pre >
< table class = "field-list" frame = "void" rules = "none" >
< col class = "field-name" />
< col class = "field-body" />
< tbody valign = "top" >
< tr class = "field" >< th class = "field-name" > Requires:</ th >< td class = "field-body" >< tt class = "literal" >< span class = "pre" > Iterator2</ span ></ tt > is implicitly convertible to < tt class = "literal" >< span class = "pre" > Iterator</ span ></ tt > .</ td >
</ tr >
2004-01-10 19:00:48 +00:00
< tr class = "field" >< th class = "field-name" > Returns:</ th >< td class = "field-body" > An instance of < tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > whose
2004-01-12 04:17:26 +00:00
< tt class = "literal" >< span class = "pre" > m_iterator</ span ></ tt > subobject is constructed from < tt class = "literal" >< span class = "pre" > y.base()</ span ></ tt > .</ td >
</ tr >
</ tbody >
</ table >
< p >< tt class = "literal" >< span class = "pre" > Iterator</ span > < span class = "pre" > base()</ span > < span class = "pre" > const;</ span ></ tt ></ p >
< table class = "field-list" frame = "void" rules = "none" >
< col class = "field-name" />
< col class = "field-body" />
< tbody valign = "top" >
< tr class = "field" >< th class = "field-name" > Returns:</ th >< td class = "field-body" >< tt class = "literal" >< span class = "pre" > m_iterator</ span ></ tt ></ td >
2003-08-05 19:48:41 +00:00
</ tr >
</ tbody >
</ table >
2004-01-12 15:53:04 +00:00
< p >< tt class = "literal" >< span class = "pre" > reference</ span > < span class = "pre" > operator*()</ span > < span class = "pre" > const;</ span ></ tt ></ p >
< table class = "field-list" frame = "void" rules = "none" >
< col class = "field-name" />
< col class = "field-body" />
< tbody valign = "top" >
< tr class = "field" >< th class = "field-name" > Returns:</ th >< td class = "field-body" >< tt class = "literal" >< span class = "pre" > **m_iterator</ span ></ tt ></ td >
</ tr >
</ tbody >
</ table >
< p >< tt class = "literal" >< span class = "pre" > indirect_iterator& </ span > < span class = "pre" > operator++();</ span ></ tt ></ p >
< table class = "field-list" frame = "void" rules = "none" >
< col class = "field-name" />
< col class = "field-body" />
< tbody valign = "top" >
< tr class = "field" >< th class = "field-name" > Effects:</ th >< td class = "field-body" >< tt class = "literal" >< span class = "pre" > ++m_iterator</ span ></ tt ></ td >
</ tr >
< tr class = "field" >< th class = "field-name" > Returns:</ th >< td class = "field-body" >< tt class = "literal" >< span class = "pre" > *this</ span ></ tt ></ td >
</ tr >
</ tbody >
</ table >
< p >< tt class = "literal" >< span class = "pre" > indirect_iterator& </ span > < span class = "pre" > operator--();</ span ></ tt ></ p >
< table class = "field-list" frame = "void" rules = "none" >
< col class = "field-name" />
< col class = "field-body" />
< tbody valign = "top" >
< tr class = "field" >< th class = "field-name" > Effects:</ th >< td class = "field-body" >< tt class = "literal" >< span class = "pre" > --m_iterator</ span ></ tt ></ td >
</ tr >
< tr class = "field" >< th class = "field-name" > Returns:</ th >< td class = "field-body" >< tt class = "literal" >< span class = "pre" > *this</ span ></ tt ></ td >
</ tr >
</ tbody >
</ table >
2003-08-05 19:48:41 +00:00
</ div >
2004-01-12 04:17:26 +00:00
< div class = "section" id = "example" >
2004-01-12 16:10:29 +00:00
< h1 >< a class = "toc-backref" href = "#id6" name = "example" > Example</ a ></ h1 >
2004-01-12 15:53:04 +00:00
< p > This example prints an array of characters, using
< tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > to access the array of characters through an
array of pointers. Next < tt class = "literal" >< span class = "pre" > indirect_iterator</ span ></ tt > is used with the
< tt class = "literal" >< span class = "pre" > transform</ span ></ tt > algorithm to copy the characters (incremented by one) to
another array. A constant indirect iterator is used for the source and
a mutable indirect iterator is used for the destination. The last part
of the example prints the original array of characters, but this time
using the < tt class = "literal" >< span class = "pre" > make_indirect_iterator</ span ></ tt > helper function.</ p >
2004-01-12 04:17:26 +00:00
< pre class = "literal-block" >
char characters[] = " abcdefg" ;
const int N = sizeof(characters)/sizeof(char) - 1; // -1 since characters has a null char
char* pointers_to_chars[N]; // at the end.
for (int i = 0; i < N; ++i)
pointers_to_chars[i] = & characters[i];
// Example of using indirect_iterator
boost::indirect_iterator< char**, char>
indirect_first(pointers_to_chars), indirect_last(pointers_to_chars + N);
std::copy(indirect_first, indirect_last, std::ostream_iterator< char> (std::cout, " ," ));
std::cout << std::endl;
// Example of making mutable and constant indirect iterators
char mutable_characters[N];
char* pointers_to_mutable_chars[N];
for (int j = 0; j < N; ++j)
pointers_to_mutable_chars[j] = & mutable_characters[j];
boost::indirect_iterator< char* const*> mutable_indirect_first(pointers_to_mutable_chars),
mutable_indirect_last(pointers_to_mutable_chars + N);
boost::indirect_iterator< char* const*, char const> const_indirect_first(pointers_to_chars),
const_indirect_last(pointers_to_chars + N);
std::transform(const_indirect_first, const_indirect_last,
mutable_indirect_first, std::bind1st(std::plus< char> (), 1));
std::copy(mutable_indirect_first, mutable_indirect_last,
std::ostream_iterator< char> (std::cout, " ," ));
std::cout << std::endl;
// Example of using make_indirect_iterator()
std::copy(boost::make_indirect_iterator(pointers_to_chars),
boost::make_indirect_iterator(pointers_to_chars + N),
std::ostream_iterator< char> (std::cout, " ," ));
std::cout << std::endl;
</ pre >
< p > The output is:</ p >
< pre class = "literal-block" >
a,b,c,d,e,f,g,
b,c,d,e,f,g,h,
a,b,c,d,e,f,g,
</ pre >
2004-01-12 16:10:29 +00:00
< p > The source code for this example can be found < a class = "reference" href = "../example/indirect_iterator_example.cpp" > here</ a > .</ p >
2003-08-05 19:48:41 +00:00
</ div >
2004-01-12 02:49:55 +00:00
</ div >
2004-01-12 15:53:04 +00:00
< hr class = "footer" />
< div class = "footer" >
< a class = "reference" href = "indirect_iterator.rst" > View document source</ a > .
2004-01-12 19:01:57 +00:00
Generated on: 2004-01-12 19:01 UTC.
2004-01-12 15:53:04 +00:00
Generated by < a class = "reference" href = "http://docutils.sourceforge.net/" > Docutils</ a > from < a class = "reference" href = "http://docutils.sourceforge.net/rst.html" > reStructuredText</ a > source.
</ div >
2003-08-05 19:48:41 +00:00
</ body >
</ html >