"Eric Woodruff" <[EMAIL PROTECTED]> wrote in message at542o$2hq$[EMAIL PROTECTED]">news:at542o$2hq$[EMAIL PROTECTED]... > > "Johan Nilsson" <[EMAIL PROTECTED]> wrote in message > at52vi$si6$[EMAIL PROTECTED]">news:at52vi$si6$[EMAIL PROTECTED]... > [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 _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost