Click here to Skip to main content
15,888,060 members
Articles / Desktop Programming / MFC
Article

AGM::LibReflection: A reflection library for C++.

Rate me:
Please Sign up or sign in to vote.
4.97/5 (17 votes)
1 Nov 2004 139.1K   1.8K   55   35
Description of the library AGM::LibReflection.

Introduction

LibReflection is a little library (well, a header to be specific) that gives reflection capabilities to C++ classes. When we talk about reflection, we don't mean just RTTI, but a rich palette of capabilities useful in every day programming:

  • specify and examine class inheritance
  • declare and examine normal and static fields
  • declare and examine normal, virtual, and static methods
  • declare and use properties and events
  • set and get field values
  • call methods and get results
  • create instances without having the headers at hand, by using a class registry

And all the above almost happen automatically, with very few macros that the programmer has to put in the application's classes...and you also get the added benefit of class properties and events, something that C++ does not provide by default.

Demo

Using LibReflection is very easy. The following piece of code shows a class with fields, properties and methods, all reflected in the class' Class object:

//what you need to include
#include "reflection.hpp"


//namespace usage
using namespace agm::reflection;


//example base class
class Base {
public:
    //needed so as that the class gets reflection capabilities
    CLASS(Base, NullClass);

    //a reflected property
    PROPERTY(int, length);

    //a reflected method
    METHOD(public, bool, processLength, (int l));

private:
    //a reflected field
    FIELD(private, int, m_length);

    //property getter
    int get_length() const {
        return m_length;
    }

    //property setter
    void set_length(int l) {
        m_length = l;
    }
};


//a reflected method implementation
bool Base::processLength(int l)
{
    return l == m_length;
}


//a derived class
class Derived : public Base {
public:
    //derived reflected class
    CLASS(Derived, Base);
};


//for the demo
#include <iostream>
using namespace std;


int main()
{
    //a class instance
    Derived derived;

    //get the class of the Derived class
    const Class &derived_class = derived.getClass();

    //print the class name
    cout << derived_class.getName() << endl;

    //print the the m_length field
    const Field &length_field = derived_class.getField("m_length");
    cout << length_field.getType() << " " 
         << length_field.getName() << endl;

    //print the the length property
    const Property &length_prop = derived_class.getProperty("length");
    cout << length_prop.getType() << " " 
         << length_prop.getName() << endl;

    //print the 'processLength()' method
    const Method &process_length_method = 
                 derived_class.getMethod("processLength");
    cout << process_length_method.getType() << " "
         << process_length_method.getName()
         << process_length_method.getArgs()
         << endl;

    //set the length property
    cout << "using length property" << endl;
    length_prop.set(&derived, 10);
    int i;
    length_prop.get(i, &derived);
    cout << "length = " << i << endl;

    //calling the length method
    cout << "calling bool Base::processLength(int)" << endl;
    bool b;
    process_length_method.invoke(b, (Base *)&derived, 10);
    cout << "processLength=" << b << endl;

    getchar();
    return 0;
}

Documentation

For more information, you can check out my little site here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generaltypeless parameters Pin
Member 1811246-Oct-08 5:26
Member 1811246-Oct-08 5:26 
GeneralWindows CE compilation Pin
DuvidaCruel9-Feb-07 9:11
DuvidaCruel9-Feb-07 9:11 
GeneralLibReflection for gcc Pin
Anonymous3-May-05 4:51
Anonymous3-May-05 4:51 
QuestionArrays? Pin
neo26007776-Apr-05 9:30
neo26007776-Apr-05 9:30 
GeneralAn Update Version Posted Pin
my203829-Mar-05 7:52
my203829-Mar-05 7:52 
GeneralNew Changes Made to LibReflection Pin
my203823-Mar-05 10:22
my203823-Mar-05 10:22 
GeneralRe: New Changes Made to LibReflection Pin
WREY28-Mar-05 0:14
WREY28-Mar-05 0:14 
GeneralRe: New Changes Made to LibReflection Pin
my203828-Mar-05 6:40
my203828-Mar-05 6:40 
GeneralNot totally true. Pin
WREY28-Mar-05 8:23
WREY28-Mar-05 8:23 
GeneralRe: Not totally true. Pin
my203829-Mar-05 7:53
my203829-Mar-05 7:53 
GeneralCompile Error in VS.NET 1.1 Pin
colormegjian22-Mar-05 3:26
colormegjian22-Mar-05 3:26 
GeneralRe: Compile Error in VS.NET 1.1 Pin
tomthorne22-Apr-05 7:23
tomthorne22-Apr-05 7:23 
GeneralRe: Compile Error in VS.NET 1.1 Pin
derchoff6-Oct-05 0:15
derchoff6-Oct-05 0:15 
QuestionCan i use it in BCB? Pin
FlameBlade17-Feb-05 2:41
FlameBlade17-Feb-05 2:41 
GeneralContact Pin
psyclonist (Stephan K.)22-Nov-04 1:41
susspsyclonist (Stephan K.)22-Nov-04 1:41 
QuestionCan Instance be Created Dynamicly? Pin
Laisser21-Nov-04 6:21
Laisser21-Nov-04 6:21 
GeneralVery Interesting! Pin
ggerules10-Nov-04 5:19
ggerules10-Nov-04 5:19 
GeneralCool! Pin
Jim Crafton2-Nov-04 14:15
Jim Crafton2-Nov-04 14:15 
GeneralRe: Cool! Pin
Achilleas Margaritis3-Nov-04 2:37
Achilleas Margaritis3-Nov-04 2:37 
GeneralRe: Cool! Pin
my203815-Mar-05 10:27
my203815-Mar-05 10:27 
GeneralDude Pin
armentage2-Nov-04 9:23
armentage2-Nov-04 9:23 
GeneralRe: Dude Pin
Achilleas Margaritis2-Nov-04 9:42
Achilleas Margaritis2-Nov-04 9:42 
GeneralRe: Dude Pin
Dave Handley3-Nov-04 6:10
Dave Handley3-Nov-04 6:10 
GeneralRe: Dude Pin
Anders Dalvander13-Nov-04 2:03
Anders Dalvander13-Nov-04 2:03 
GeneralRe: Dude Pin
Dave Handley13-Nov-04 3:08
Dave Handley13-Nov-04 3:08 
"CFileDialog isn't really standard C++, it is a MFC class, and thus will only work in Microsoft Windows. But should't you use the Win32 function SHBrowseForFolder, and not derive from CFileDialog anyway?"

Unfortunately, even using SHBrowseForFolder isn't trivial in the .Net Framework - see http://www.netomatix.com/FolderBrowser.aspx

"I wouldn't state that standard C++ is clear, either."

ANSI Standard C++ is very clear in my opinion - much more so than many other languages. Especially given the amount of explicit control you have over data management.

"The CLR, CLI, C# and C++/CLI are standards, but you're stuck in .NET, that is true. But .NET is ported to other systems than Windows. See DotGnu and Mono."

C++/CLI is a standard but it still is NOT ANSI Standard C++ which makes it much less portable. .Net may be ported to a few other platforms, but it still isn't properly cross-platform.

"You are not working through an intermediate language, the JIT-compiler will create machine code. In C++/CLI you don't have to use garbage collection, you can use deterministic destruction instead."

As far as I know the only way to do deterministic destruction is with the IDisposable interface, and even then the Garbage Collector does some of the work so it isn't strictly fully deterministic. Also any form of Just in Time Compiler directly implies the existence of intermediate code - of course the IL is converted to machine code before it is used, but it is still converted and that takes time.

"Vertigo Software ported Quake 2 to .NET with a performance decrease of only 15%. And that was done in 5 days: 4 days for porting from C to C++ and one day to port to Managed C++."

Unfortunately, something like Quake 2 isn't really a good example of a performance intensive application. From what I remember Quake 2 was written to run on a Pentium 133, and as with many games most of the really performance intensive stuff is done inside the OpenGL library down on the Graphics Card. That will still be the same in a managed port. When someone gets a ray-tracer working in managed code with similar performance to native C++ I'll be impressed, but I can't see it happening. Given that most of the really intensive stuff in Quake 2 is happening down on the graphics card, a 15% performance drop is huge.

Dave

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.