Click here to Skip to main content
Click here to Skip to main content

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

, 2 Nov 2004
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Achilleas Margaritis
Software Developer (Senior)
Greece Greece
No Biography provided

Comments and Discussions

 
GeneralRe: Dude PinmemberDave Handley11-Jan-05 21:40 
GeneralRe: Dude PinsussAnonymous17-Oct-05 7:52 
AnswerRe: Dude PinmemberRetroBooster211-Dec-07 1:35 
GeneralMore documentation. Please! PinmemberJohn M. Drescher2-Nov-04 6:57 
GeneralRe: More documentation. Please! Pinmemberaxilmar2-Nov-04 7:44 
GeneralRe: More documentation. Please! PinmemberJohn M. Drescher2-Nov-04 8:39 
GeneralRe: More documentation. Here! PinsussArno Nym2-Nov-04 9:55 
GeneralRe: More documentation. Please! PinmemberJim Crafton2-Nov-04 15:14 
Reflection is generally the same as RTTI in the sense that it is information retrieved dynamically at runtime as opposed to compile time. Basically it allows the program to query a class instance dynamically at runtime for information about itself, such as it's class name (we already have this from typeid().name()) or it's super class, or even it's methods, fields and properties.
The most obvious usage is in development IDE's for UI form builders, as the form builder can be made quite generic, and you can manipulate the form and it's child controls all dynamically at runtime. RTTI/Reflection all help do this. Languages like Smalltalk, ObjectiveC, ObjectPascal (or at least Borland variant of it), and Java have all had this kind of support from the get go. C++ hasn't.
For toolkit's that want any sort of extensible, sophisticated UI, RTTI is a must. To my knowledge only the VCF[^] and Qt[^] really go the whole nine yards to provide support comparable to what you get in .Net, Java, or Delphi (ObjectPascal).

 
¡El diablo está en mis pantalones! ¡Mire, mire!
 
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
 
SELECT * FROM User WHERE Clue > 0
0 rows returned

GeneralRe: More documentation. Please! PinmemberJohn M. Drescher11-Nov-04 6:34 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 2 Nov 2004
Article Copyright 2004 by Achilleas Margaritis
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid