mirror of
https://github.com/boostorg/iterator.git
synced 2025-07-29 20:37:17 +02:00
updated
[SVN r10295]
This commit is contained in:
160
development/libs/iterator/iterator_categories.htm
Normal file
160
development/libs/iterator/iterator_categories.htm
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
<html>
|
||||||
|
<!--
|
||||||
|
-- Copyright (c) Jeremy Siek 2000,2001
|
||||||
|
--
|
||||||
|
-- Permission to use, copy, modify, distribute and sell this software
|
||||||
|
-- and its documentation for any purpose is hereby granted without fee,
|
||||||
|
-- provided that the above copyright notice appears in all copies and
|
||||||
|
-- that both that copyright notice and this permission notice appear
|
||||||
|
-- in supporting documentation. I make no representations about the
|
||||||
|
-- suitability of this software for any purpose. It is provided "as is"
|
||||||
|
-- without express or implied warranty.
|
||||||
|
-->
|
||||||
|
<head>
|
||||||
|
<title>Boost Iterator Traits</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
|
||||||
|
ALINK="#ff0000">
|
||||||
|
<IMG SRC="../../../../c++boost.gif"
|
||||||
|
ALT="C++ Boost" width="277" height="86">
|
||||||
|
<BR Clear>
|
||||||
|
|
||||||
|
<h1>Boost Iterator Category Traits</h1>
|
||||||
|
Header <tt><a href="../../boost/iterator_categories.hpp">boost/iterator_categories.hpp</a></tt>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
The <tt>boost::traversal_category</tt> and
|
||||||
|
<tt>boost::return_category</tt> traits classes provides access to the
|
||||||
|
category tags for iterators that model the Boost <a
|
||||||
|
href="./iterator_concepts.htm">Iterator Concepts</a>, which are a
|
||||||
|
replacement for the iterator requirements in the C++ standard. The
|
||||||
|
other associated types of the Boost iterator concepts are accessed
|
||||||
|
through the <tt>std::iterator_traits</tt> class.
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><tt>traversal_category<Iter>::type</tt> Can the iterator go forward, backward, etc.?
|
||||||
|
<li><tt>return_category<Iter>::type</tt> Is the iterator read or write only?
|
||||||
|
Is the dereferenced type an lvalue?
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
An important feature of the <tt>boost::traversal_category</tt> and
|
||||||
|
<tt>boost::return_category</tt> classes is that they are <b>backwards
|
||||||
|
compatible</b>, i.e., they automatically work for iterators for which
|
||||||
|
there are valid definitions of <tt>std::iterator_traits</tt>. The old
|
||||||
|
<tt>iterator_category</tt> is mapped to the appropriate traversal and
|
||||||
|
return categories.
|
||||||
|
|
||||||
|
<p>
|
||||||
|
When creating a new iterator type that is meant to work with
|
||||||
|
<tt>boost::traversal_category</tt> and
|
||||||
|
<tt>boost::return_category</tt>, you can either create a
|
||||||
|
specialization of these classes for your iterator type, or you can
|
||||||
|
provide all the necessary associated types as nested typedefs. In
|
||||||
|
this case, your iterator class will need to inherit from
|
||||||
|
<tt>new_iterator_base</tt> to let the category traits know
|
||||||
|
that it will be able to find typedefs for <tt>traversal_category</tt>
|
||||||
|
and <tt>return_category</tt> in you iterator class.
|
||||||
|
|
||||||
|
|
||||||
|
Each of the new iterator requirements will need a category tag.
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
namespace boost {
|
||||||
|
|
||||||
|
// Return Type Categories
|
||||||
|
struct readable_iterator_tag { };
|
||||||
|
struct writable_iterator_tag { };
|
||||||
|
struct swappable_iterator_tag { };
|
||||||
|
struct mutable_lvalue_iterator_tag : virtual public writable_iterator_tag,
|
||||||
|
virtual public readable_iterator_tag { };
|
||||||
|
struct constant_lvalue_iterator_tag : public readable_iterator_tag { };
|
||||||
|
|
||||||
|
// Traversal Categories
|
||||||
|
struct forward_traversal_tag { };
|
||||||
|
struct bidirectional_traversal_tag : public forward_traversal_tag { };
|
||||||
|
struct random_access_traversal_tag : public bidirectional_traversal_tag { };
|
||||||
|
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
The following is pseudo-code for the iterator category traits classes.
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
namespace boost {
|
||||||
|
|
||||||
|
<i>// Inherit from iterator_base if your iterator defines its own
|
||||||
|
// return_category and traversal_category. Otherwise, the "old style"
|
||||||
|
// iterator category will be mapped to the return_category and
|
||||||
|
// traversal_category.</i>
|
||||||
|
struct new_iterator_base { };
|
||||||
|
|
||||||
|
template <typename Iterator>
|
||||||
|
struct return_category
|
||||||
|
{
|
||||||
|
<b><i>// Pseudo-code</i></b>
|
||||||
|
if (Iterator inherits from new_iterator_base) {
|
||||||
|
typedef typename Iterator::return_category type;
|
||||||
|
} else {
|
||||||
|
typedef std::iterator_traits<Iterator> OldTraits;
|
||||||
|
typedef typename OldTraits::iterator_category Cat;
|
||||||
|
if (Cat inherits from std::forward_iterator_tag)
|
||||||
|
if (is-const(T))
|
||||||
|
typedef boost::constant_lvalue_iterator_tag type;
|
||||||
|
else
|
||||||
|
typedef boost::mutable_lvalue_iterator_tag type;
|
||||||
|
else if (Cat inherits from std::input_iterator_tag)
|
||||||
|
typedef boost::readable_iterator_tag type;
|
||||||
|
else if (Cat inherits from std::output_iterator_tag)
|
||||||
|
typedef boost::writable_iterator_tag type;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct return_category<T*>
|
||||||
|
{
|
||||||
|
<b><i>// Pseudo-code</i></b>
|
||||||
|
if (is-const(T))
|
||||||
|
typedef boost::constant_lvalue_iterator_tag type;
|
||||||
|
else
|
||||||
|
typedef boost::mutable_lvalue_iterator_tag type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Iterator>
|
||||||
|
struct traversal_category
|
||||||
|
{
|
||||||
|
<b><i>// Pseudo-code</i></b>
|
||||||
|
if (Iterator inherits from new_iterator_base) {
|
||||||
|
typedef typename Iterator::traversal_category type;
|
||||||
|
} else {
|
||||||
|
typedef std::iterator_traits<Iterator> OldTraits;
|
||||||
|
typedef typename OldTraits::iterator_category Cat;
|
||||||
|
|
||||||
|
if (Cat inherits from std::random_access_iterator_tag)
|
||||||
|
typedef boost::random_access_traversal_tag type;
|
||||||
|
else if (Cat inherits from std::bidirectional_iterator_tag)
|
||||||
|
typedef boost::bidirectional_traversal_tag type;
|
||||||
|
else if (Cat inherits from std::forward_iterator_tag)
|
||||||
|
typedef boost::forward_traversal_tag type;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct traversal_category<T*>
|
||||||
|
{
|
||||||
|
typedef boost::random_access_traversal_tag type;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
<address><a href="mailto:jsiek@lsc.nd.edu">jeremy siek</a></address>
|
||||||
|
<!-- Created: Sun Mar 18 14:06:57 EST 2001 -->
|
||||||
|
<!-- hhmts start -->
|
||||||
|
Last modified: Mon Mar 19 12:59:30 EST 2001
|
||||||
|
<!-- hhmts end -->
|
||||||
|
</body>
|
||||||
|
</html>
|
Reference in New Issue
Block a user