Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2005-02-14 17:42:22


From: Jason Hise <chaos_at_[hidden]>
> Pavel Vozenilek wrote:
> >>
> >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.
> >
> 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.

That's definitely better. You don't want to make it pretty when
someone adds this feature. However, given that the need for this
behavior might be just common enough to make this addition to
every Singleton type too much pain, how about another class
template:

template <typename T>
class unsafe_singleton : public singleton<T>
{
public:
   //! WARNING: Do not delete the returned pointer!
   T * unsafe_get()
   {
      return this;
   }
};

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

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