Boost logo

Boost :

From: Jason Hise (chaos_at_[hidden])
Date: 2005-02-13 16:41:53


Pavel Vozenilek wrote:

>"Dave Handley" wrote:
>
>[bald pointer out of singleton]
>
>
>>The only instance that I can think of where you may want to do this is in
>>a multi-threaded environment. And even then only in a very specific set
>>of circumstances. Specifically, you may want to do this when you don't
>>want the overhead of a lock, but, for some other reason, you can
>>absolutely guarantee that concurrent access to the object (or parts of the
>>object you are accessing) is valid. If I'm honest though, I'm having some
>>difficulty convincing myself that something like this would actually
>>genuinely be useful. Perhaps if you could get a "bald pointer" through a
>>protected member, that was therefore only accessible to derived classes
>>(hence the singletons themselves). This could be useful to build into a
>>static access function that provided a safe pointer, but perhaps without
>>the overhead of a lock?
>>
>>
>>
>The use case is a legacy code that accepts only bald pointer.
>
>A singleton may have function unsafe_get(). The object would be
>instantiated if not already.
>
>Code using its result will promise not to destroy given object.
>
>/Pavel
>
In such cases, I wonder if it might be best for the user's singleton to
provide the function:

class Example : public singleton < Example >
{
// protected ctor and dtor
public:
    Example * unsafe_get ( )
    {
        return this;
    }
};

That way not all singleons would need to suffer the 'security hole' just
because a few need it.

-Jason


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