1

Possible Duplicate:
How can I add reflection to a C++ application?

Like in python, we have dir() to list all the attributes/functions/members of an object, is there a way for C++ objects to get all the members, either in gdb or some other utility ?

Community
  • 1
  • 1
avd
  • 13,993
  • 32
  • 78
  • 99
  • Well, would you count doxygen? – jxh Jun 09 '12 at 04:48
  • I dont want any doxygen like tool to run on all my sources, since when I add any new attribute, I would have to rerun. I want something which can infer after reading from the library, like gdb, may be there is a way in gdb ? – avd Jun 09 '12 at 05:00
  • Since you seem comfortable with python, you could try to create your own tool with python. You define your C++ object in python, and you create a tool that generates the corresponding C++ header file at compile time, and the info you want available about the C++ object. – jxh Jun 09 '12 at 05:02
  • Could you explain in more detail, I did not understand "generate the corresponding C++ header file" ? I have used swig etc, but I dont want to create an object in python, some tool which is truly C++'ish and gets from the library. Why I am saying this is, when we instantiate in python, python adds its own attributes, I dont want that. – avd Jun 09 '12 at 05:06

1 Answers1

2

C++ does not provide a built-in mechanism for achieving this. This topic seems pretty well covered in the discussion Keith Randall pointed out.

gdb can provide to you the data members for your object, but you need to have an instance of the object to refer to, and do print a, if the name of the global was a. To find the methods, you would use info functions A::*, if the class name was A. This will not find inline functions (note baz_inline in the example).

class A {
public:
    void foo ();
    int bar (int);
    void baz_inline () {}
    double d;
    short p;
};
A a;
void A::foo () {}
int A::bar (int) { return 0; }
int main () {}

$ g++ -g c.cc
$ gdb a.out
(gdb) p a
$1 = {d = 0, p = 0}
(gdb) p &a.d
$2 = (double *) 0x600920
(gdb) p &a.p
$3 = (short *) 0x600928
(gdb) info functions A::*
All functions matching regular expression "A::*":

File c.cc:
int A::bar(int);
void A::foo();
(gdb) quit

Originally, I was thinking middleware, because there are middleware technologies that partially do what you want. There, the C++ class is defined in a meta-language (IDL - interface definition language), and the middleware tools generate C++ class definitions by parsing the IDL description of the class. One of the purposes for this was to allow code to be generated that understood how to package an object of that type, deliver it to another machine, unpack it at that machine, and let software on that machine use the object.

You have not described such a need, but you want similar features, in that you wanted to know about the C++ interface and its data members. Since you said python already had this facility, I was thinking it might be easy to write your own little tool that used python code as your IDL. That is, you create a python object that you actually want to be your C++ object, and your tool takes the result of dir() to generate the C++ header file to represent that object. You could query the python version of the object (with embedded python) for the information you wanted to know about the C++ object.

Community
  • 1
  • 1
jxh
  • 69,070
  • 8
  • 110
  • 193