Boost logo

Boost :

From: Jason Hise (chaos_at_[hidden])
Date: 2005-09-29 16:29:44


Joel Eidsath wrote:

>My main purposes right now are familiarizing myself with Boost.
>
> From reading the header file for aligned_storage, I believe that I
>understand how to do what you're suggesting, although I admit that I am
>somewhat shaky -- I'd have to figure it out by implementing it, I think.
>
>But correct me if I'm wrong: I don't think that it would be possible to
>use purge_memory to destruct everything at once like that. You'd have
>to destruct everything individually.
>
I haven't personally worked with the pool allocators much, so perhaps my
suggestion was misplaced. I was thinking that you wanted a pool that
allocated only enough memory for a single instance, rather than a single
instance of a pool that could store many objects. Anyhow, to use
aligned_storage like I was thinking, you might do something like:

template < typename Type >
class SingletonPool
{
private:
    typename boost::aligned_storage < Type >::type mem;
    bool exists;
    Type * pmem;

public:
    SingletonPool ( )
        : exists ( false )
        , pmem ( reinterpret_cast<Type *>( &mem ) )
    {
    }

    Type * instance ( )
    {
        if ( !exists )
        {
            new ( pmem ) Type ( );
            exists = true;
        }
        return pmem;
    }

    void release ( )
    {
        if ( exists )
        {
            pmem->~Type ( );
            exists = false;
        }
    }

    ~ SingletonPool ( )
    {
        release ( );
    }
};

But this probably wasn't what you were after...

-Jason


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