cgma
ManagedPtrVector< X > Class Template Reference

A vector of pointers to objects that are automatically deleted when the container itself is deleted. More...

#include <ManagedPtrVector.hpp>

List of all members.

Classes

class  iterator

Public Types

typedef X value_type
typedef Xreference_type
typedef size_t size_type
typedef std::vector< X * > container_type

Public Member Functions

 ManagedPtrVector ()
 ~ManagedPtrVector ()
void push_back (X *obj)
iterator begin ()
iterator end ()
reference_type operator[] (size_type i)
 Refer by index.
std::auto_ptr< Xrelease (iterator to_release)
 Remove an item from the list without deleting it.
std::auto_ptr< Xreplace (iterator to_replace, std::auto_ptr< X > new_val)
 Replace one object with another.
void clear ()
size_type size () const

Private Member Functions

 ManagedPtrVector (const ManagedPtrVector< X > &)
ManagedPtrVectoroperator= (const ManagedPtrVector< X > &)

Private Attributes

container_type mContainer

Detailed Description

template<typename X>
class ManagedPtrVector< X >

A vector of pointers to objects that are automatically deleted when the container itself is deleted.

Loosely modeled after the boost ptr containers.

Definition at line 13 of file ManagedPtrVector.hpp.


Member Typedef Documentation

template<typename X>
typedef std::vector<X*> ManagedPtrVector< X >::container_type

Definition at line 19 of file ManagedPtrVector.hpp.

template<typename X>
typedef X& ManagedPtrVector< X >::reference_type

Definition at line 17 of file ManagedPtrVector.hpp.

template<typename X>
typedef size_t ManagedPtrVector< X >::size_type

Definition at line 18 of file ManagedPtrVector.hpp.

template<typename X>
typedef X ManagedPtrVector< X >::value_type

Definition at line 16 of file ManagedPtrVector.hpp.


Constructor & Destructor Documentation

template<typename X>
ManagedPtrVector< X >::ManagedPtrVector ( ) [inline]

Definition at line 105 of file ManagedPtrVector.hpp.

  {}
template<typename X>
ManagedPtrVector< X >::~ManagedPtrVector ( ) [inline]

Definition at line 107 of file ManagedPtrVector.hpp.

  { clear(); }
template<typename X>
ManagedPtrVector< X >::ManagedPtrVector ( const ManagedPtrVector< X > &  ) [private]

Member Function Documentation

template<typename X>
iterator ManagedPtrVector< X >::begin ( ) [inline]

Definition at line 115 of file ManagedPtrVector.hpp.

  { return iterator(mContainer.begin()); }
template<typename X>
void ManagedPtrVector< X >::clear ( ) [inline]

Definition at line 169 of file ManagedPtrVector.hpp.

  {
    // delete all the pointers
    for (typename container_type::iterator i = mContainer.begin();
      i != mContainer.end();
      i++)
    {
      delete *i;
    }
    mContainer.clear();
  }
template<typename X>
iterator ManagedPtrVector< X >::end ( ) [inline]

Definition at line 117 of file ManagedPtrVector.hpp.

  { return iterator(mContainer.end()); }
template<typename X>
ManagedPtrVector& ManagedPtrVector< X >::operator= ( const ManagedPtrVector< X > &  ) [private]
template<typename X>
reference_type ManagedPtrVector< X >::operator[] ( size_type  i) [inline]

Refer by index.

Definition at line 121 of file ManagedPtrVector.hpp.

  { return *(mContainer[i]); }
template<typename X>
void ManagedPtrVector< X >::push_back ( X obj) [inline]

Definition at line 112 of file ManagedPtrVector.hpp.

  { mContainer.push_back(obj); }
template<typename X>
std::auto_ptr<X> ManagedPtrVector< X >::release ( iterator  to_release) [inline]

Remove an item from the list without deleting it.

The returned auto_ptr now owns the pointer.

Definition at line 126 of file ManagedPtrVector.hpp.

  {
    // save a raw pointer.
    X* rv = to_release.operator->();
    // find the iterator in the container.  We don't have access
    // to the internal iterator, so we have to loop through to find it.
    // This could probably be optimized.
    typename container_type::iterator i = mContainer.begin();
    for (;
      i != mContainer.end() && iterator(i) != to_release;
      ++i)
    {}
    // we either found the iterator, or the end.  erase it.
    mContainer.erase(i);
    return std::auto_ptr<X>(rv);
  }
template<typename X>
std::auto_ptr<X> ManagedPtrVector< X >::replace ( iterator  to_replace,
std::auto_ptr< X new_val 
) [inline]

Replace one object with another.

The returned auto_ptr now owns the pointer removed from the container, and the container owns the object pointed to by new_val.

Definition at line 148 of file ManagedPtrVector.hpp.

  {
    // save a raw pointer.
    X* rv = to_replace.operator->();
    // find the iterator in the container.  We don't have access
    // to the internal iterator, so we have to loop through to find it.
    // This could probably be optimized.
    typename container_type::iterator i = mContainer.begin();
    for (;
      i != mContainer.end();
      ++i)
    {
      if (iterator(i) == to_replace)
      {
        *i = new_val;
      }
    }
    return std::auto_ptr<X>(NULL);
  }
template<typename X>
size_type ManagedPtrVector< X >::size ( ) const [inline]

Definition at line 181 of file ManagedPtrVector.hpp.

  { return mContainer.size(); }

Member Data Documentation

template<typename X>
container_type ManagedPtrVector< X >::mContainer [private]

Definition at line 189 of file ManagedPtrVector.hpp.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines