coolfluid/coolfluid3 — Issue #143: Shared pointers are evil
Author: barche
Created: 2011-10-26 09:30:00+00:00
State: open
Labels: API, IDEA

As demonstrated by issue #72, passing boost::shared_ptr everywhere can in all fairness be considered to be the root of all evil. The problem is that many functions return shared_ptr, encouraging its use as class members and in other containers.

The current work-around for this is to use weak_ptr, but this has two issues:
- Locking a weak_ptr is expensive, because it creates a new shared_ptr
- It can tell us nothing about who still uses what component where

To solve this, we could create our own version of a weak pointer, perhaps called common::Handle, providing the following functionality:
- Cache the raw pointer to the component, thus avoiding the expensive lock() whenever possible
- Allow (optional) registration of a "user", which is another component that uses the Handle
- Allow (optional) registration to an event, to check what links exist and who uses them

All functions that return a shared_ptr now would then return a Handle, with the exception of the build_component and allocate_component functions, which need to be used to create the initial shared_ptr as stored in the list of child components. The "shared_from_this" functionality would be removed.

Concrete advantages of this system:
- circular shared_ptr references as in issue #72 are next to impossible
- we can add fancy checks, such as see who uses what component when the Handles are registered explicitely. So if the user wants to remove a component, he could get a warning showing who still uses it.