Boost logo

Boost :

From: Johan Torp (johan.torp_at_[hidden])
Date: 2007-09-11 16:20:48


I'd love to get some feedback on a tiny utility I've been thinking about.

I oftenly create small objects which I just want to keep alive as long
as some other object exists. It is often RAII objects, but many other
types of small classes fall into this category.

A simple "user heap" could take ownership of such instances and delay
deletion until itself is deleted. The basic interface of - let's call
it heap - would look like this:

  template<class T> T& heap::put(T*);

Example usage:

     boost::shared_ptr<SomeView> view;
     // Keep controller alive while view exits
     view->get_heap().put(new SomeController(*view));

For classes with loads of RAII object members, usage could look like this:

  class Foo {
    Foo() {
      keep_alive_.put(new RAIIBar(...));
      keep_alive_.put(new RAIIWhatever(...));
    }
  private:
    heap keep_alive_;
  };

Support should be added for putting shared_ptrs and auto_ptrs. I have
a wide variety of naming suggestions and an extended interface for
premature deletion but I'll save those for a later post.

What do you think? Please give me your thoughts.

Johan


Boost list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk