How to implement reflection in C++

S

Siegfried

Java, C# and other languages allow one to enumerate all the data
members of a class or struct thru their respective reflection APIs.

Varios Microsoft specific extensions to their C++ compiler allows one
to do the same (I'm thinking of both the COM extentions as wells as the
managed extensions).

I don't believe g++ supports either of these. Does anyone have some
sample code that will read a g++ .o file (that was compiled with the
debug option turned on), or a g++ executable image and extract the meta
data about a type so I can enumerate the datamembers?

Thanks,
Siegfried
 
R

roberts.noah

Siegfried said:
Java, C# and other languages allow one to enumerate all the data
members of a class or struct thru their respective reflection APIs.

Varios Microsoft specific extensions to their C++ compiler allows one
to do the same (I'm thinking of both the COM extentions as wells as the
managed extensions).

I don't believe g++ supports either of these. Does anyone have some
sample code that will read a g++ .o file (that was compiled with the
debug option turned on), or a g++ executable image and extract the meta
data about a type so I can enumerate the datamembers?

I'm gonna bet that such a thing isn't available. Maybe if you explain
why you need it, or think you need it, someone could help you find a
more available solution.
 
P

peter steiner

Siegfried said:
Java, C# and other languages allow one to enumerate all the data
members of a class or struct thru their respective reflection APIs.

Varios Microsoft specific extensions to their C++ compiler allows one
to do the same (I'm thinking of both the COM extentions as wells as the
managed extensions).

I don't believe g++ supports either of these. Does anyone have some
sample code that will read a g++ .o file (that was compiled with the
debug option turned on), or a g++ executable image and extract the meta
data about a type so I can enumerate the datamembers?

have a look at the objdump(1) sources which reads symbols from gcc
generated object files.

but i don't think you will be able to get the information that you need
from the object files, as these linker input files do not necessarily
contain the original C++ object layout in processable form.

you will need C++ parser output to get full identifier information for
C++ source files.

if you don't want to use a full-blown C++ parser implementation, for
example the wrapper/interface generator tool swig has a mode to output
a XML or lisp s-expression formatted abstract syntax tree for given
source files. this gives you parseable data to further work with.

see http://www.swig.org

-- peter
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Siegfried said:
I don't believe g++ supports either of these. Does anyone have some
sample code that will read a g++ .o file (that was compiled with the
debug option turned on), or a g++ executable image and extract the meta
data about a type so I can enumerate the datamembers?

Supposing that you find or write such a tool, you must consider that the
member functions of a class can be spawned over several .o files.
 
G

Gianni Mariani

Siegfried said:
Java, C# and other languages allow one to enumerate all the data
members of a class or struct thru their respective reflection APIs.

Varios Microsoft specific extensions to their C++ compiler allows one
to do the same (I'm thinking of both the COM extentions as wells as the
managed extensions).

I don't believe g++ supports either of these. Does anyone have some
sample code that will read a g++ .o file (that was compiled with the
debug option turned on), or a g++ executable image and extract the meta
data about a type so I can enumerate the datamembers?


Go ask the gcc people. It's not standard C++ (I wish it was too) so
it's off topic here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top