Boost logo

Boost :

Subject: Re: [boost] Is there any interest in a library which solves C++ class reflection feature just like Java does?
From: Matus Chochlik (chochlik_at_[hidden])
Date: 2011-12-07 10:30:30


On Wed, Dec 7, 2011 at 4:03 PM, jinhua luo <ljh.home.king_at_[hidden]> wrote:
> Hi,
>
> Another quick question:
> Could you show how to invoke one specific method (can be any prototype) of
> the reflected class?

This is not yet implemented in the run-time layer but basically
it will follow the way constructors are called (since constructors are
functions too).

Generally when you want to construct an instance of a class which may
have several different constructors (there is a parallel with function
overloads),
some of which may require parameters,
you need to supply the data for the construction in some form.

Currently supported are XML, JSON, a C++-like script, relational database,
and a wxWidgets-based GUI but the basic mechanism is extensible
and is completely independent from the reflected classes.

See for example this:
http://kifri.fri.uniza.sk/~chochlik/mirror-lib/html/doxygen/lagoon/html/d3/d46/lagoon_2example_2factories_2person_mijson_8cpp-example.html

The important things you need to do are:

1) get the data from which you want to construct the
class. In that example above it is loaded from a JSON
string (hardcoded for simplicity).

2) get the meta_class for a particular class
auto meta_person = reflected_class<test::person>();

The above uses the real class but there is not reason why the
line above could not look something like this (it is just not implemented)

auto meta_person = reflected_class("test::person");

3) then you instantiate something called a factory-builder
and factory-input-data.

4) you build a factory for the person type using the builder
and the meta-class reflecting the person class.
The builder basically uses the meta-data provided by
the meta-class to construct a specialized JSON parser
and some code for calling the constructor (function)

5) you feed the input data (the JSON string) into the factory
The factory takes a look at the input data chooses which
constructor to use (by matching the data to the available
constructor signatures)

6) The factory calls the selected constructor and
constructs an instance of person (wrapped
in a untyped pointer of boost::any)

The invocation of (member) functions will work in a similar
fashion. You provide the input data in some form,
the Invoker will use factories like the described above
to construct the parameters and call the function you want.
You can specify which one or have the invoker select it
automatically.

> Do you need to cast it into that real class type before you can invoke it?

The example above does it but generally, No if you don't want to.

> If so, then I think it doesn't satisfies what I mention that "the main
> program does not need to include any compile-time information of the
> reflected class, e.g. class header file".
> I do not see some clear example about this on your website, so I am
> wondering that.

Yes, the docs are still messy.
>
> BR
> JinHua
>

Matus


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