How to implement reflection in C++

Discussion in 'C++' started by Siegfried, Nov 27, 2005.

  1. Siegfried

    Siegfried Guest

    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
    Siegfried, Nov 27, 2005
    #1
    1. Advertising

  2. Siegfried

    Guest

    Siegfried wrote:
    > 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.
    , Nov 27, 2005
    #2
    1. Advertising

  3. Siegfried wrote:
    > 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
    peter steiner, Nov 27, 2005
    #3
  4. On 27 Nov 2005 01:51:05 -0800, "peter steiner" <>
    wrote:
    >Siegfried wrote:
    >> 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.


    or nm
    (http://www.gnu.org/software/binutils/manual/html_chapter/binutils_2.html)
    on Unix or dumpbin for VC++.

    Best wishes,
    Roland Pibinger
    Roland Pibinger, Nov 27, 2005
    #4
  5. Siegfried wrote:

    > 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.

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Nov 27, 2005
    #5
  6. Siegfried wrote:
    > 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.
    Gianni Mariani, Nov 27, 2005
    #6
  7. Siegfried

    WaRCHieFX Guest

    Take a look at the RTTI chapter in Thinking in C++
    (www.bruceeckel.com), i don't remember which volume.
    WaRCHieFX, Nov 28, 2005
    #7
  8. Siegfried

    Ira Baxter Guest

    "peter steiner" <> wrote in message
    news:...
    > Siegfried wrote:
    > > Java, C# and other languages allow one to enumerate all the data
    > > members of a class or struct thru their respective reflection APIs.
    > >
    > > 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?

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


    A front end C++ parser that can be straightforwardly configure to do this
    can be seen at
    www.semanticdesigns.com/Products/FrontEnds/CppFrontEnd
    It handles full GCC.


    --
    Ira D. Baxter, Ph.D., CTO 512-250-1018
    Semantic Designs, Inc. www.semdesigns.com
    Ira Baxter, Dec 7, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. [Gauthier]
    Replies:
    2
    Views:
    14,172
    [Gauthier]
    Dec 3, 2003
  2. Alan Seunarayan

    Reflection and COM

    Alan Seunarayan, Jan 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    596
    Mattias Sjögren
    Jan 14, 2004
  3. Replies:
    0
    Views:
    528
  4. jerry051
    Replies:
    2
    Views:
    745
    Oliver Sturm
    Aug 2, 2005
  5. WebDevHokie

    Re: Reflection and SqlConnection

    WebDevHokie, Jul 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    436
    WebDevHokie
    Jul 10, 2003
Loading...

Share This Page