Boost logo

Boost :

From: Petr Kocmid (pkocmid_at_[hidden])
Date: 2000-06-01 16:21:20


> From: Branko Èibej [mailto:branko.cibej_at_[hidden]]
> First, I'm not convinced that it's a good idea to be able to instantiate
> a class both as a singleton and an "ordinary" object. If interface
> compatifility is what you're after, there are other (better) ways to
> do that.

Well, I expected just that comment! Glad someone read it at all ;-).
The basic idea on behind of allowing any class to my singleton_ptr
mechanism is the non-invasiveness. Take it positively: I have no
special requirements on classs which instance is to be taken as a
singleton.
There are many conditions when such non-invasiveness comes handy:
legacy code, third party dll classes or so.
What better way you have on mind exactly?

> Petr Kocmid wrote:
> > - idea to have a singleton object of any arbitrary type which will come
> > on my mind later, declared by simple template expansion on that type
> > and intermix singleton functionality with class hiearchy architecture.

> If I understand your source correctly, you can't define a hierarchy of
> singletons, something like this:
> class A { virutal void foo() {/*...*/} };
> class B : public A { virtual void foo {/*...*/ };
> // We want to create singleton B ...
> singleton_ptr<B>();
> // ... and access it as an A
> singleton_ptr<A>.foo();

singleton_ptr<A>->foo() exactly. Yes, it calls A::foo(), not B::foo().
In what you write, singleton_ptr<A> and singleton_ptr<B> will made
even two different instances!
In the example I presented a way, how to create a singleton-like hierarchy:
by having singleton_ptr embedded in base class, you can derive and
virtualize from the base class at will. Singleton part of object and
hierarchy part of object are quite separate things.

> This will always call A::foo. It'd be nice to have a way to "register"
> a derived class singleton and use the base class interface to access it.

Actually I am using a different mechanism for that sort of things.
I call it DynamicClass. That's the generic object, which is allowed
to add members and methods dynamically with it's own virtualisation
scheme. But that's nothing to do with singletons! ;-)
But yes, it is possible to extend singleton_factory mechanism to get
some sort of virtual method registration, but I see it as overkill.

> What system are you using g++ on, and which version? As far as I know,
> recent versions shouldn't have that problem.

2.95.2 on debian and cygwin. I am sure I had some bloat somewhere,
but it's long time. Now you made me unsure if it was not some 2.8x...

Petr Kocmid (Koèmíd, if your mailer shows that damned diacritics properly)
pkocmid_at_[hidden]


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