All pastes #2069452 Raw Edit

Something

public text v1 · immutable
#2069452 ·published 2011-05-26 13:28 UTC
rendered paste body
template <class T>
struct life_time_controller
{
    typedef life_time_controller me;
    life_time_controller()          : id(new_id()) { collection().insert(this); }
    life_time_controller(const me&) : id(new_id()) { collection().insert(this); }
    ~life_time_controller()                        { collection().erase (this); }
    const int id;
private:
    static int new_id()
    {
        static int ret=0;
        ++ret;
        return ret; // set conditional break point there
    }

    struct collection_type
    {
        boost::unordered_set<void*> living_objects;
        ~collection_type()
        {
            if(!living_objects.empty())
            {
                std::cerr << "type: " << typeid(T).name()
                          << " living_objects: " << living_objects.size()
                          << " ids: ";
                BOOST_FOREACH(void*ptr, living_objects)
                        std::cerr <<  static_cast<life_time_controller<T>*>(ptr)->id << " ";
                std::cerr <<  std::endl;
            }
        }
    };

    static boost::unordered_set<void*>& collection()
    {
        static collection_type ret;
        return ret.living_objects;
    }
};
 

template <class T>
struct unit : life_time_controller<T> //, ...
{
     // ...
};
struct A : unit<A> {};
struct B : unit<B> {};
struct C : unit<C> {};

int main(int argc, char *argv[])
{
    new A();
    new B[3]();
    return 0;
}