|
Boost : |
From: Pavel Vozenilek (pavel_vozenilek_at_[hidden])
Date: 2005-01-08 20:21:08
"Jason Hise" wrote:
>>Allocator just allocates N bytes, object creates itself via
>>placement new.
>>
> This will not work unless every class that derives from singleton defines
> its own create method. The singleton base itself cannot call placement
> new, because it does not have access to the protected parts of a derived
> class.
>
I think there are posibilities:
1. constructor/destructor of class T is public (the class may
be instantiated for other purposes as well)
2. the class provides static member function for construct/destroy
or there's object factory for it.
Singleton may provide specializations:
- default one using placement new/delete (+ allocator)
template < typename T, typename A = AllocateUsingNew >
/*
template < typename T, AllocateUsingMalloc>
template < typename T, CreateStatic>
*/
- one where you provide class member functions as
template parameters
template < typename T, CreateViaFunctors<T*(* Creator)(params), void(*
Deleter)(const T*)> >
- something like above for object factories
>>Instead of try/catch shared_array should be used to avoid
>>destroying stack frame when exception throws.
>>
> I'm tempted, except for the fact that it would only be needed in this one
> place and adds a dependency on the shared_array library, which is only
> needed for an implementation detail. Right now, there are no external
> dependencies and I don't think it would be worth it to save my
> implementation three lines of code.
>
It is possible to create tiny RAII class(es) just for this purpose.
/Pavel
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk