Boost logo

Boost :

From: Remy Blank (remy.blank_REMOVE_THIS_at_[hidden])
Date: 2002-11-22 08:39:13


On Fri, 22 Nov 2002 07:17:24 -0500, David Abrahams <dave_at_[hidden]> wrote:
> Remy Blank <remy.blank_REMOVE_THIS_at_[hidden]> writes:
>
> > Hello Boosters,
> >
> > I am trying to use dynamic_any to store either objects or pointers to
> > (polymorphic) objects.
> >
> > I am able to extract a pointer to the base class of a contained object:
> >
> > class B { };
> >
> > class D: public B { };
> >
> > void Test()
> > {
> > any d(D());
> >
> > B* pb = extract<B>(&d);
> > }
> >
> > This is alread quite useful. However the following code does not work:
> >
> > void Test2()
> > {
> > D d;
> > any pd(&d);
> >
> > B* pb = extract<B*>(pd); // pb = 0
> >
> > char c;
> > any AnyChar(c);
> >
> > int i = extract<int>(AnyChar); // bad_extract
> > }
> >
> > So I have to know the exact type of the contained object and cannot
> > rely on implicit conversions (D* -> B* or char -> int) to work.
> > This is quite understandable from the fact that any wraps non-class
> > types into unrelated classes.
> >
> > Now my question: is there a way to make these implicit conversions work?
>
> Heh. Boost.Python does it with a complicated class registry and
> upcast/downcast system. I don't know if you want to pay for that.

Yes, if there's no better way.

> > My best answer at the moment: explicitly register conversion functions
> > from one type to the other in a map<pair<type_info const&, type_info const&>, void* (*)(void*)>
> > and look up the right function based on the type contained and the
> > type requested.
>
> Oh, well, if you're willing to do that...

I didn't say that. I said that that was my best current answer. I'm not
satisfied with it, though.

> > But there has to be a better way, hasn't it?
>
> Yes**. The mechanism in Boost.Python allows you to register just the
> relationships between adjacent base and derived classes, and it fills
> in the rest of the graph. Maybe it's time to refactor this code for
> general use in Boost...

This sounds good. I'm trying to develop an introspection framework for
C++ classes (and later dynamically created classes), so I will already
have the inheritance information. I expect your implementation makes
heavy use of typeid() and dynamic_cast<>?

Other conversions would have to be registered explicitly (char -> int,
char const* -> std::string, ...), possibly half-automated by using
typelists and mpl algorithms.

It's too bad that we have to replicate at runtime what the compiler
already knows how to do at compile time (namely navigating inheritance
hierarchies)...

I have looked at Boost.Python, and it is very similar to what I had in
mind. Would it be possible to make Boost.Python more general to describe
C++ class information for runtime use, and have Boost.Python be a subset?
I don't have a lot of time on my hands, but if you think this would be a
good idea, I would love to give it a try (except that I'm a little scared
by Boost.Python's complexity, and I don't know Python (yet)).

BTW, how does Boost.Python compare to SWIG (http://www.swig.org/) ?
It seems to supports Python, amongst others.

> The code is in libs/python/src/object/inheritance.cpp.

I'll look into that. Thanks for the pointer.

Best regards.
--Remy

Remove anti-spam sequence in reply address for a timely response.


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