Click here to Skip to main content
6,596,602 members and growing! (22,290 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Libraries » General     Intermediate

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

By Achilleas Margaritis

Description of the library AGM::LibReflection.
VC6Win2K, MFC, Dev
Posted:2 Nov 2004
Views:62,398
Bookmarked:40 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 5.33 Rating: 4.78 out of 5

1

2
2 votes, 16.7%
3

4
10 votes, 83.3%
5

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

About the Author

Achilleas Margaritis


Member

Occupation: Software Developer (Senior)
Location: Greece Greece

Other popular Libraries articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
Generaltypeless parameters PinmemberMember 1811246:26 6 Oct '08  
GeneralWindows CE compilation PinmemberDuvidaCruel10:11 9 Feb '07  
GeneralLibReflection for gcc PinsussAnonymous5:51 3 May '05  
GeneralArrays? Pinmemberneo260077710:30 6 Apr '05  
GeneralAn Update Version Posted Pinmembermy20388:52 29 Mar '05  
GeneralNew Changes Made to LibReflection Pinmembermy203811:22 23 Mar '05  
GeneralRe: New Changes Made to LibReflection PinmemberWREY1:14 28 Mar '05  
GeneralRe: New Changes Made to LibReflection Pinmembermy20387:40 28 Mar '05  
GeneralNot totally true. PinmemberWREY9:23 28 Mar '05  
GeneralRe: Not totally true. Pinmembermy20388:53 29 Mar '05  
GeneralCompile Error in VS.NET 1.1 Pinmembercolormegjian4:26 22 Mar '05  
GeneralRe: Compile Error in VS.NET 1.1 Pinmembertomthorne28:23 2 Apr '05  
GeneralRe: Compile Error in VS.NET 1.1 Pinmemberderchoff1:15 6 Oct '05  
GeneralCan i use it in BCB? PinmemberFlameBlade3:41 17 Feb '05  
GeneralContact Pinsusspsyclonist (Stephan K.)2:41 22 Nov '04  
GeneralCan Instance be Created Dynamicly? PinmemberLaisser7:21 21 Nov '04  
GeneralVery Interesting! Pinmemberggerules6:19 10 Nov '04  
GeneralCool! PinmemberJim Crafton15:15 2 Nov '04  
GeneralRe: Cool! Pinmemberaxilmar3:37 3 Nov '04  
GeneralRe: Cool! Pinmembermy203811:27 15 Mar '05  
GeneralDude Pinmemberarmentage10:23 2 Nov '04  
GeneralRe: Dude Pinmemberaxilmar10:42 2 Nov '04  
GeneralRe: Dude PinmemberDave Handley7:10 3 Nov '04  
GeneralRe: Dude PinmemberAnders Dalvander3:03 13 Nov '04  
GeneralRe: Dude PinmemberDave Handley4:08 13 Nov '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Nov 2004
Editor: Smitha Vijayan
Copyright 2004 by Achilleas Margaritis
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project