Click here to Skip to main content
15,891,689 members
Articles / Desktop Programming / MFC

An update to AGM::LibReflection: A reflection library for C++

Rate me:
Please Sign up or sign in to vote.
4.32/5 (8 votes)
11 May 20054 min read 111.8K   830   31  
This article is an update to the original AGM::LibReflection library.
#include "refcls.hpp"
#include <iostream>
using namespace agm::reflection;
using namespace std;


class Widget {
public:
  virtual ~Widget(){};
  
public:
  static const int version;

  bool add (Widget *child);

  static const char * get_version_string ();
private:
int m_x;
int m_y;
int m_width;
int m_height;
bool m_visible;
bool m_enabled;

    int get_x() const {
        return m_x;
    }

    void set_x(int v) {
        m_x = v;
    }

    int get_y() const {
        return m_y;
    }

    void set_y(int v) {
        m_y = v;
    }

    int get_width() const {
        return m_width;
    }

    void set_width(int v) {
        m_width = v;
    }

    int get_height() const {
        return m_height;
    }

    void set_height(int v) {
        m_height = v;
    }

    bool get_visible() const {
        return m_visible;
    }

    void set_visible(bool v) {
        m_visible = v;
    }

    bool get_enabled() const {
        return m_enabled;
    }

    void set_enabled(bool v) {
        m_enabled = v;
    }

public:
  static const ReflectiveClass<Widget> clsdef;
};

const ReflectiveClass<Widget> Widget::clsdef = ReflectiveClass<Widget>()
  .property(&Widget::get_x, &Widget::set_x, "x")
  .property(&Widget::get_y, &Widget::set_y, "y")
  .property(&Widget::get_width, &Widget::set_width, "width")
  .property(&Widget::get_height, &Widget::set_height, "height")
  .property(&Widget::get_visible, &Widget::set_visible, "visible")
  .property(&Widget::get_enabled, &Widget::set_enabled, "enabled")
  .static_field(&version, "version")
  .method(&Widget::add, "add")
  .static_method(&get_version_string, "get_version_string")
  .field(&Widget::m_x, "m_x", ACCESS_PRIVATE)
  .field(&Widget::m_y, "m_y", ACCESS_PRIVATE)
  .field(&Widget::m_width, "m_width", ACCESS_PRIVATE)
  .field(&Widget::m_height, "m_height", ACCESS_PRIVATE)
  .field(&Widget::m_visible, "m_visible", ACCESS_PRIVATE)
  .field(&Widget::m_enabled, "m_enabled", ACCESS_PRIVATE)
  .constructor();

const int Widget::version = 1;


const char *Widget::get_version_string()
{
    return "1.0";
}


bool Widget::add(Widget *child)
{
    cout << "Added widget of class " << Class::forType(typeid(*child))->getName() << endl;
    if (child == 0) return false;
    return true;
}


class Label : public Widget {
public:
protected:
private:
};

//declare outside is fine as well
static const ReflectiveClass<Label> label_clsdef =  ReflectiveClass<Label>( (Widget *)0).constructor();





int main3()
{
    Widget wgt;

    const Class *wgt_cls =  Class::forType(typeid(Widget));

    for(Class::PropertyList::const_iterator it = wgt_cls->getProperties().begin();
        it != wgt_cls->getProperties().end();
        ++it)
    {
        const Property &prop = **it;
        cout << prop.getType() << " " << prop.getName() << endl;
    }

    getchar();
    return 0;
}


int main4()
{
    Widget wgt;
    
    const Class *wgt_cls =  Class::forType(typeid(Widget));

    const Property &prop = wgt_cls->getProperty("width");

    cout << prop.getType() << " " << prop.getName() << endl;

    prop.set(&wgt, 5);
    int w;
    prop.get(w, &wgt);

    cout << prop.getName() << " = " << w << endl;

    getchar();
    return 0;
}


int main()
{
    Widget wgt;
    Label lbl;

    bool ok;
    const Class *wgt_cls =  Class::forType(typeid(Widget));
    wgt_cls->getMethod("add").invoke(ok, &wgt, (Widget *)&lbl);
    
    getchar();
    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions