Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Shared Container Iterator

The Shared Container Iterator Type
The Shared Container Iterator Object Generator
The Shared Container Iterator Range Generator

Defined in header boost/shared_container_iterator.hpp.

The purpose of the shared container iterator is to attach the lifetime of a container to the lifetime of its iterators. In other words, the container will not be deleted until after all its iterators are destroyed. The shared container iterator is typically used to implement functions that return iterators over a range of objects that only need to exist for the lifetime of the iterators. By returning a pair of shared iterators from a function, the callee can return a heap-allocated range of objects whose lifetime is automatically managed.

The shared container iterator augments an iterator over a shared container. It maintains a reference count on the shared container. If only shared container iterators hold references to the container, the container's lifetime will end when the last shared container iterator over it is destroyed. In any case, the shared container is guaranteed to persist beyond the lifetime of all the iterators. In all other ways, the shared container iterator behaves the same as its base iterator.

Synopsis

namespace boost {
  template <typename Container>
  class shared_container_iterator;

  template <typename Container>
  shared_container_iterator<Container>
  make_shared_container_iterator(typename Container::iterator base,
    boost::shared_ptr<Container> const& container);

  std::pair<
    typename shared_container_iterator<Container>,
    typename shared_container_iterator<Container>
  >
  make_shared_container_range(boost::shared_ptr<Container> const& container);
}
template <typename Container> class shared_container_iterator;

The class template shared_container_iterator is the shared container iterator type. The Container template type argument must model the Container concept.

Example

The following example illustrates how to create an iterator that regulates the lifetime of a reference counted std::vector. Though the original shared pointer ints ceases to exist after set_range() returns, the shared_counter_iterator objects maintain references to the underlying vector and thereby extend the container's lifetime.

shared_iterator_example1.cpp:

#include "shared_container_iterator.hpp"
#include "boost/shared_ptr.hpp"
#include <algorithm>
#include <iostream>
#include <vector>

typedef boost::shared_container_iterator< std::vector<int> > iterator;


void set_range(iterator& i, iterator& end)  {

  boost::shared_ptr< std::vector<int> > ints(new std::vector<int>());

  ints->push_back(0);
  ints->push_back(1);
  ints->push_back(2);
  ints->push_back(3);
  ints->push_back(4);
  ints->push_back(5);

  i = iterator(ints->begin(),ints);
  end = iterator(ints->end(),ints);
}


int main() {

  iterator i,end;

  set_range(i,end);

  std::copy(i,end,std::ostream_iterator<int>(std::cout,","));
  std::cout.put('\n');

  return 0;
}

The output from this part is:

0,1,2,3,4,5,

Table 1.16. Template Parameters

Parameter

Description

Container

The type of the container that we wish to iterate over. It must be a model of the Container concept.


Concepts

The shared_container_iterator type models the same iterator concept as the base iterator (Container::iterator).

Operations

The shared_container_iterator type implements the member functions and operators required of the Random Access Iterator concept, though only operations defined for the base iterator will be valid. In addition it has the following constructor:

shared_container_iterator(Container::iterator const& it,
                          boost::shared_ptr<Container> const& container)

PrevUpHomeNext