Loading...
Searching...
No Matches
BALL::AutoDeletable Class Reference
Foundation Classes » Concepts » Miscellaneous

`#include <BALL/CONCEPT/autoDeletable.h>`

Inheritance diagram for BALL::AutoDeletable:

## Public Member Functions

Constructors and Destructors
The constructors of this class are protected.
virtual | ~AutoDeletable ()
void * | operator new (size_t size)
void | operator delete (void *ptr)
void * | operator new (size_t size, void *ptr)
void | operator delete (void *ptr, void *)

## Predicates

bool | isAutoDeletable () const
| AutoDeletable ()
| AutoDeletable (const AutoDeletable &auto_deletable, bool deep=false)

## Mutators

void | setAutoDeletable (bool enable)
static void | clearLastPtr ()

## Detailed Description

Auto-deletable concept. This class allows the distinction between objects that may be deleted automatically (because they are created dynamically on the heap) and instances that are static and should not be deleted automatically.

  Standard application is the class Composite . Composites may contain other composites (tree-like structure). If a composite is deleted, it has to decide whether all composites contained therein are to be deleted, too. If these composites are static objects, calling their destructor often is fatal! The same is true for objects contained in an array. Therefore, composite is derived from AutoDeletable .

  The AutoDeletable class determines whether it was instantiated via new and then sets an internal flag to false. The mechanism to determine this is as follows:

  AutoDeletable has on overloaded new operator. When invoked, this operator allocates storage for an instance of AutoDeletable using the global new operator and remembers the address of in a private variable last_ptr_. Each constructor of AutoDeletable checks whether its this pointer is equal to the address stored in last_ptr_. If both pointers are equal, the object has been created using the new operator and so it is safe to delete it automatically. If the adresses do not match, the object is either part of an array or static and should not be deleted automatically.

  The state of each object may be changed after it is constructed by a call to setAutoDeletable . This might be useful to protect certain instances of objects, however usually this should not be neccessary.

Definition at line 58 of file autoDeletable.h.

## Constructor & Destructor Documentation

## ~AutoDeletable()

virtual BALL::AutoDeletable::~AutoDeletable | ( |  | ) |

virtual

Destructor

## AutoDeletable() [1/2]

BALL::AutoDeletable::AutoDeletable | ( |  | ) |

protected

## AutoDeletable() [2/2]

BALL::AutoDeletable::AutoDeletable | ( | const AutoDeletable & | auto_deletable,
|  | bool | deep = false )

protected

## Member Function Documentation

## clearLastPtr()

void BALL::AutoDeletable::clearLastPtr | ( |  | ) |

inline static

Definition at line 129 of file autoDeletable.h.

## isAutoDeletable()

bool BALL::AutoDeletable::isAutoDeletable | ( |  | ) | const

Query the objects status. Returns true if the object should be automatically deleted if the objects it is contained in are deleted. Recursive destruction methods should honor this flag and should not call the destructor for objects that return true.

## operator delete() [1/2]

void BALL::AutoDeletable::operator delete | ( | void * | ptr | ) |

delete operator. This operator frees the space allocated for an Autodeletable object. It is implemented solely to achieve a consistent interface and to avoid warnings issued by some compilers if operator new/delete do not appear in pairs.

## operator delete() [2/2]

void BALL::AutoDeletable::operator delete | ( | void * | ptr,
|  | void * | )

Placement delete operator. This operator frees the space allocated for an Autodeletable object. It is implemented solely to achieve a consistent interface and to avoid warnings issued by some compilers if operator new/delete do not appear in pairs.

## operator new() [1/2]

void * BALL::AutoDeletable::operator new | ( | size_t | size | ) |

new operator. This operator allocates storage for the object and remembers its pointer. This pointer is static and is evaluated by the constructors. As this operator is only invoked for the creation of single dynamic objects, arrays and static objects can be identified.

## operator new() [2/2]

void * BALL::AutoDeletable::operator new | ( | size_t | size,
|  | void * | ptr )

Placement new operator. This operator allocates storage for the object and remembers its pointer. This pointer is static and is evaluated by the constructors. As this operator is only invoked for the creation of single dynamic objects, arrays and static objects can be identified.

## setAutoDeletable()

void BALL::AutoDeletable::setAutoDeletable | ( | bool | enable | ) |

Mark the object as auto-deletable. Objects can be marked as deletable or not deletable by this method. Use this method to protect objects from automatic deletion. You should never set static objects to autodeletable, as invoking delete on a static object may result in a crash.