#include <pointer_list.h>
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_type > | list_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. |
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.
|
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. |
|
Defines the deletion policies recognized by remove().
Definition at line 239 of file pointer_list.h. |
|
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. |
|
Sets the autodelete policy. See auto_delete(). Definition at line 113 of file pointer_list.h. |
|
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. |
|
For STL compatibility. Calls erase( begin(), end() ). Definition at line 331 of file pointer_list.h. |
|
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. |
|
For STL compatibility. if ( this->auto_delete() ) then (*it) is deleted. Definition at line 305 of file pointer_list.h. |
|
For STL compatibility. Adds an entry to the back of this list. Definition at line 220 of file pointer_list.h. |
|
For STL compatibility. Adds an entry to the front of this list. Definition at line 230 of file pointer_list.h. |
|
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:
Obviously, if this function deletes the pointer then it will return NULL. Definition at line 275 of file pointer_list.h. |