Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members

s11n::pointer_list< ChildType > Class Template Reference

pointer_list is a simple template class for a container of pointers to T, plus some memory management features. More...

#include <pointer_list.h>

List of all members.

Public Types

typedef pointer_list< ChildType > ThisType
 A typedef to make the rest of the implementation easier to write.

typedef ChildType * value_type
 value_type defines the child type of this list.

typedef std::list< value_typelist_type
 list_type is the type of underlying pointer container.

typedef list_type::iterator iterator
 iterator which can be dereferenced to a (value_type *).

typedef list_type::const_iterator const_iterator
 const iterator which can be dereferenced to a (value_type *).

enum  DeletionPolicy { CheckAutoDelete = -1, DoNotDelete = 0, Delete = 1 }
 Defines the deletion policies recognized by remove(). More...


Public Member Functions

 pointer_list (bool autodel=false)
 Creates a pointer_list with the given auto_delete policy.

virtual ~pointer_list ()
 Deletes all child pointers if this->auto_delete().

bool auto_delete () const
 If auto_delete() is true then all objects in this list will be deleted when this object dies or when erase() is called.

void auto_delete (bool autodel)
 Sets the autodelete policy.

ThisType::const_iterator begin () const
 Return a const iterator for the first element in this container.

ThisType::iterator begin ()
 Return an iterator pointing to the first element in this container.

ThisType::const_iterator end () const
 Return the after-the-end iterator.

ThisType::iterator end ()
 Return the after-the-end iterator.

ThisType::iterator find (typename ThisType::value_type a)
 Returns an iterator pointing to the given pointer, or end() if this object does not contain that pointer.

unsigned long count () const
 Returns the number of child pointers in this list.

unsigned long size () const
 Same as count().

ThisType::value_type add (typename ThisType::value_type a, bool front=false)
 Adds the given pointer to the list.

ThisType::value_type push_back (typename ThisType::value_type a)
 For STL compatibility.

ThisType::value_type push_front (typename ThisType::value_type a)
 For STL compatibility.

ThisType::value_type remove (typename ThisType::value_type a, DeletionPolicy deletionPolicy=CheckAutoDelete)
 Removes the given pointer from this list, assuming it contains the pointer.

void erase (ThisType::iterator it)
 For STL compatibility.

void erase (ThisType::iterator begin, ThisType::iterator end)
 For STL compatibility.

void clear ()
 For STL compatibility.

void delete_all ()
 Deletes all children, irrespective of auto_delete().

virtual bool contains (typename ThisType::value_type a)
 Returns true if this list contains a.


Detailed Description

template<class ChildType>
class s11n::pointer_list< ChildType >

pointer_list is a simple template class for a container of pointers to T, plus some memory management features.

It's usage is STL-like.

Parameterized on:

pointer_lists with auto_delete(true) often make useful ad-hoc poor-man's garbage collectors.

Known caveats:

Inserting the same pointer into the same list multiple times is fatal if the list's auto_delete() is enabled. It will crash in the list's dtor when it deletes the pointer twice. Multiple pointers to the same object are allowed because the STL containers allow it, and i want to conform with that basic behaviour.

Definition at line 41 of file pointer_list.h.


Member Typedef Documentation

template<class ChildType>
typedef ChildType* s11n::pointer_list< ChildType >::value_type
 

value_type defines the child type of this list.

Note that it is a pointer type (because this is a /pointer list/ ;), so don't be confused by constructs like:

value_type objptr = 0;

(note the distinct lack of a *).

This odd definition is required for compatibility with std::vector and std::list. i would prefer to define it without the *. :/

Definition at line 62 of file pointer_list.h.


Member Enumeration Documentation

template<class ChildType>
enum s11n::pointer_list::DeletionPolicy
 

Defines the deletion policies recognized by remove().

Enumeration values:
CheckAutoDelete  Use the policy set in auto_delete().

DoNotDelete  Do not delete object.
Delete  Delete object.

Definition at line 239 of file pointer_list.h.


Member Function Documentation

template<class ChildType>
ThisType::value_type s11n::pointer_list< ChildType >::add typename ThisType::value_type  a,
bool  front = false
[inline]
 

Adds the given pointer to the list.

If front == true then the pointer is added to the front of the list, else the end of the list.

If the pointer is NULL then this function does nothing.

Returns the pointer passed to it.

Definition at line 198 of file pointer_list.h.

References s11n::pointer_list< ChildType >::add().

Referenced by s11n::pointer_list< ChildType >::add().

template<class ChildType>
void s11n::pointer_list< ChildType >::auto_delete bool  autodel  )  [inline]
 

Sets the autodelete policy.

See auto_delete().

Definition at line 113 of file pointer_list.h.

template<class ChildType>
bool s11n::pointer_list< ChildType >::auto_delete  )  const [inline]
 

If auto_delete() is true then all objects in this list will be deleted when this object dies or when erase() is called.

Autodelete is false by default.

Definition at line 105 of file pointer_list.h.

Referenced by s11n::pointer_list< ChildType >::remove(), and s11n::pointer_list< ChildType >::~pointer_list().

template<class ChildType>
void s11n::pointer_list< ChildType >::clear  )  [inline]
 

For STL compatibility.

Calls erase( begin(), end() ).

Definition at line 331 of file pointer_list.h.

References s11n::pointer_list< ChildType >::end().

template<class ChildType>
void s11n::pointer_list< ChildType >::erase ThisType::iterator  begin,
ThisType::iterator  end
[inline]
 

For STL compatibility.

if ( this->auto_delete() ) then the pointers in (*it) are deleted.

TODO: validate that the iterators are guaranteed to be valid after list_type::erase() is called!

Definition at line 319 of file pointer_list.h.

template<class ChildType>
void s11n::pointer_list< ChildType >::erase ThisType::iterator  it  )  [inline]
 

For STL compatibility.

if ( this->auto_delete() ) then (*it) is deleted.

Definition at line 305 of file pointer_list.h.

References s11n::pointer_list< ChildType >::erase().

Referenced by s11n::pointer_list< ChildType >::erase().

template<class ChildType>
ThisType::value_type s11n::pointer_list< ChildType >::push_back typename ThisType::value_type  a  )  [inline]
 

For STL compatibility.

Adds an entry to the back of this list.

Definition at line 220 of file pointer_list.h.

References s11n::pointer_list< ChildType >::push_back().

Referenced by s11n::pointer_list< ChildType >::push_back().

template<class ChildType>
ThisType::value_type s11n::pointer_list< ChildType >::push_front typename ThisType::value_type  a  )  [inline]
 

For STL compatibility.

Adds an entry to the front of this list.

Definition at line 230 of file pointer_list.h.

References s11n::pointer_list< ChildType >::push_front().

Referenced by s11n::pointer_list< ChildType >::push_front().

template<class ChildType>
ThisType::value_type s11n::pointer_list< ChildType >::remove typename ThisType::value_type  a,
DeletionPolicy  deletionPolicy = CheckAutoDelete
[inline]
 

Removes the given pointer from this list, assuming it contains the pointer.

If the pointer is NULL or the list does not contain the entry, it returns NULL, otherwise it returns the pointer passed to it.

The passed-in pointer may or may not be deleted, depending on the value of deletionPolicy:

  • CheckAutoDelete (-1, default) == check auto_delete() and do whatever that says.

  • DoNotDelete (0) == do not delete

  • Delete (1) == delete, regardless of auto_delete()

Obviously, if this function deletes the pointer then it will return NULL.

Definition at line 275 of file pointer_list.h.

References s11n::pointer_list< ChildType >::auto_delete(), and s11n::pointer_list< ChildType >::remove().

Referenced by s11n::pointer_list< ChildType >::remove().


The documentation for this class was generated from the following file:
Generated on Wed Jul 28 16:04:15 2004 for s11n by doxygen 1.3.7