billyquith / ponder

C++ reflection library with Lua binding, and JSON and XML serialisation.
http://billyquith.github.io/ponder/
Other
640 stars 93 forks source link

Getting information about the type of a property #63

Closed MicBe closed 7 years ago

MicBe commented 7 years ago

First, congratulations, your library is great!

My goal is to use ponder to dynamically generate a user interface, based on the list of properties of a class. There is a part I seem to be missing though. How can I get precise information about the type of a property at runtime?

Here is what I want to do: I want to iterate over the properties of my class. For each one, depending on the type, I would render a specific widget in the UI.

Let's say I want to display the properties of this class:

class User
{
public:
    string Name;
    nullable<string> PhoneNumber;
    nullable<vector<string>> Projects;
    nullable<vector<Email>> UnreadEmails;
    nullable<vector<Email>> DeletedEmails;
}

The logic I would like to use would resemble be the following:

void RenderUser()
{
    for(const auto& property : metaClass.propertyIterator())
    {
        if(property.type == "nullableString") // How could something like this be done?
            RenderNullable(property);
        else if(property.type == "nullableStringVector")
            RenderNullableStringVector(property);
       else if(property.type == "userTypeVector")
           RenderUserTypeVector(property);
    }
}

Is there a built-in mechanism in Ponder to do something like this?

Would it also be possible to attach other misc attributes to each property (booleans)? The reason is that I already have a very basic property system in which I store those attributes. If possible, I would put them at the same place where I declare property names and member pointers.

Have a great day, Michael

billyquith commented 7 years ago

Thanks for the positive feedback! 👍

In answer to your question, you could use a ClassVisitor to get more detailed information about the properties. This may be too generic?

Ponder, was formerly, CAMP, which exposed a name/getter/setter API, which is a form of reflection. I appreciate that you may want to go beyond that functionality. Currently type access is somewhat generic at the moment. This might be extended.

billyquith commented 7 years ago

Is this question considered answered?

MicBe commented 7 years ago

Yes, thank you!