stlab.adobe.com Adobe Systems Incorporated

any_iterator Class Reference
[Iterator Utilities]

An iterator wrapping another iterator of the same element type; a "polymorphic" iterator. More...

#include <adobe/iterator.hpp>


Detailed Description

Model Of:
  • Regular
  • Iterator concept specified by C
Types:
  • T: Value type of the sequence's elements; models Regular
  • C: Iterator category
  • R: Return type of operator*(); models Reference Type of T
  • D: Difference type between iterators; models Signed Integer

The adobe::any_iterator template is an experiment in concept based polymorphism. We would expect that in most cases it would be more efficient to copy a sequence into a connonical form, perform the operation, and then copy it back. However, there may be uses cases where the copy is not desired or would be less efficient (such as lower bounds on a random access sequence). Constructing an any_iterator does require a free store allocation and could throw an exception.

Todo:
(sparent) A future extension would be to include a tuple of "known types" into the interface which would be stored in a variant and eliminate the need for the heap allocation except for unknown types.

Example

This example illustrates how an any_iterator can be used to deal with runtime polymorphic sequences. We have a single concrete function, reverse_and_print(), which can be used with a list, vector, deque, or any other bidirectional sequence.

// start_of_example

#include <iostream>
#include <list>
#include <vector>
#include <deque>

#include <boost/range/functions.hpp>

#include <adobe/iterator.hpp>

typedef adobe::any_iterator<int, std::bidirectional_iterator_tag> any_iterator_to_int;

// Concrete (non-template) function
void reverse_and_print(any_iterator_to_int first, any_iterator_to_int last)
{
    std::reverse(first, last);
    while (first != last)
    {
        std::cout << *first << " ";
        ++first;
    }
    std::cout << std::endl;
}

int main()
{
    const int a[] = { 0, 1, 2, 3, 4, 5 };
    
    std::list<int> l(boost::begin(a), boost::end(a));
    reverse_and_print(any_iterator_to_int(boost::begin(l)), any_iterator_to_int(boost::end(l)));
    
    std::vector<int> v(boost::begin(a), boost::end(a));
    reverse_and_print(any_iterator_to_int(boost::begin(v)), any_iterator_to_int(boost::end(v)));
    
    std::deque<int> d(boost::begin(a), boost::end(a));
    reverse_and_print(any_iterator_to_int(boost::begin(d)), any_iterator_to_int(boost::end(d)));
    
    return 0;
}

// end_of_example

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google