Boost logo

Boost :

From: Johan Nilsson (johan.nilsson_at_[hidden])
Date: 2002-12-11 06:10:03


"Eric Woodruff" <Eric.Woodruff_at_[hidden]> wrote in message
news:at542o$2hq$1_at_main.gmane.org...
>
> "Johan Nilsson" <johan.nilsson_at_[hidden]> wrote in message
> news:at52vi$si6$1_at_main.gmane.org...
> [snip]
>
> > I'd like to be able to store references to objects of arbitrary types in
a
> > homogenous collection, not requiring them to be derived from a common
base
> > class. For COM users, that would be something like an "IUnknown in the
> > context of standard C++". I suspect that boost::any might be something
> > similar to what I need, but I just got that idea using void pointers.
> >
>
> What's the point of that? I mean, with no interface to the types, you
can't
> do anything with these objects you're storing, not even delete them.
>

If I can dynamically cast a void pointer to a polymorphic type, I can do
whatever I want to do.

> You could also do:
>
> struct IHolder {
> virtual ~IHolder () {}
> };
>
> template <typename Object>
> class holder : public IHolder{
> public:
> holder (Object* const object);
> ~holder ();
> };
>
> It's always better to try and look for a real (type safe) solution than
> playing around with type casting. It still depends on what you want to
_do_
> with theses objects behind this common, empty interface. I suspect
problems
> with your design if you have been led down this path.
>

I _am_ looking for a type safe solution, I was simply playing around trying
to find one that would fit my needs.

I'm actually experimenting with a kind of 'generic' object factory with the
following characteristics:

1) Objects should be dynamically creatable from a textual description using
named parameters, e.g.:
"InputDevice = Type:MulticastSocket,Interface:0.0.0.0,Port:12345,...", _or_
<Object><Name>InputDevice</Name><Type>MulticastSocket</Type><Interface>0.0.0
.0</Interface></Object>, _or_ whatever

2) Object creation if deferred until someone expliticly requests a reference
to them - 'lazy creation'

3) Later requests for the same object simply returns a reference to it -
_but_ the type need not be the same as the concrete type. Suppose the
MulticastSocket object implements the interfaces IStream, IAsyncStream,
INamedObject, etc. Clients should be able to do something like (fíctionary):

SharedPtr<IStream> pStream =
ObjectManager::instance().get<IStream>("InputDevice");

as well as

SharedPtr<INamedObject> pNamedObject =
ObjectManager::instance().get<INamedObject>("InputDevice");

(so who on earth would get a named object through an input device? Well,
forget it - the name of the object might be passed as a parameter itself -
it was just an example)

4) Named objects can be used as arguments to other objects.

-----------------

1) is easy enough to implement through some parser + 'extendable' object
factory.

2-4) are harder. The lookup/'reference by name' part + lazy creation are
rather easy stuff. The ability to cast to an arbitraty polymorphic type is
harder. Maybe I could use some kind of 'holder' or modify boost::any, making
it a mixin instead, but then comes the problem of constructor parameters. I
specifically don't want to use a common base, unless I can do it
non-intrusively (if that's an english word).

Note that I only use this stuff during object creation, initialization and
reference resolving phase, and not during the actual program execution. Once
all objects are created and initalized, no further dynamic casting is
required. I've done something similar using COM a while ago, but there I had
the possibility to use IUnknown and QueryInterface.

// Johan


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