Reflection in RegEngine - Part 2

Warning! Some information on this page is older than 5 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

09:46
Sun
25
Jul 2010

Reflection in RegEngine - Part 2

In the Part 1 I've shown the basics of my reflection system - classes that describe a class, property and enum. Now the question arises about how do I actually create these objects? Where do I get the information about the classes and properties in my code from? There are many possibilities which I considered:

[*] The simplest one would be to explicitly build these data structures by hand - white some initialization code that would do new Class(...);, new FloatProperty(...) and so on. Oviously this is also the most inconvenient way.

[*] Objects describing classes and properties could be automatically generated by code created by some smart usage of C++ macros and templates. I mean declaring classes and their field using something like DECLARE_CLASS(MyClass, ParentClass) and DECLARE_PROPERTY(Prop1, float), either instead or next to the real class and member declarations. This might be a good idea.

[*] Finally, a separate file with either description of classes and properties in some format or a direct C++ code for building it could be generated from some input data. These input data could be extracted from:

I've decided to create class- and property-describing objects by hand in C++ code but on top of that I've coded an optional tool that takes description file in a special language and automtically generates such code. The code written by hand looks like this:

// Header file

class ComplexItemObject : public ItemObject
{
public:
    static void RegisterClass_();
    static Class * GetClass() { return m_Class; }
    virtual Class * Object_GetClass() { return m_Class; }
private:
    static Class *m_Class;
    wstring m_Name;
    bool m_B1;
    common::COLORF m_Colorf;
    ...
};

// CPP file

Class * ComplexItemObject::m_Class;

void ComplexItemObject::RegisterClass_()
{
    m_Class = new Class(L"ComplexItemObject", ItemObject::GetClass(), &CreateFunc, &CloneFunc);
    m_Class->SetLabel(L"Complex Item Object");

    StlStringProperty *prop1 = new StlStringProperty();
    prop1->Init(L"Name", 0);
    m_Class->AddProperty(prop1);

    BoolProperty *prop2 = new BoolProperty();
    prop2->Init(L"B1", 1);
    m_Class->AddProperty(prop2);

    FloatVectorProperty *prop3 = new FloatVectorProperty(3, VectorProperty::SEMANTICS_COLOR);
    prop3->Init(L"Colorf", 2);
    m_Class->AddProperty(prop3);

    g_Reflection->AddClass(m_Class);
}

Similar code can be automatically generated by my console tool called ReflectionAuto from a .reflect file. The file format is a description language based on my Tokenizer. ReflectionAuto takes in as input and outputs a separate C++ file that adds this boring reflection initialization code to the described classes. The description of the class shown above looks like:

class ComplexItemObject {
    extends ItemObject;
    label "Complex Item Object";
    property string Name { access m_Name; };
    property bool B1 { access m_B1; };
    property int4(color) Colori { access m_Colori; };
};

That covers the basics (not everything I actually have) when it comes to building description of the classes and properties from my code to make the reflection system aware of them. In the next part I'll show how do I access real objects from my reflection system, including reading and writing properties, as well as creating and manipulation whole objects.

Comments (0) | Tags: c++ RegEngine engine | Author: Adam Sawicki | Share

Comments

(No comments)

Post comment

Nick *
Your name or nickname
E-mail
Your contact information (optional, will not be shown)
Text *
Content of your comment
Calculate *
(* - required field)
STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2017 Adam Sawicki
Copyright © 2004-2017 Adam Sawicki