TheNitesWhoSay / RareCpp

Creating a simpler, more intuitive means of C++ reflection

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

REFLECT Macro Simplification & typeStr updates

TheNitesWhoSay opened this issue · comments

If typeStr can be refactored to not depend on the user providing an explicit type in the REFLECT macro then the REFLECT macro could be greatly simplified.

There's also a big problem with passing types in the REFLECT macro: no commas are allowed in macros, therefore any type that involves a comma (e.g. std::map<int, int>) cannot be passed, at least not without aliasing it first (e.g. using IntMap = std::map<int, int>;) which is quite a burden and junks up the class you're trying to reflect.

What we want back from typeStr is either...

A.) The exact text the user declared the field with (which could indeed be std::map<int, int>)
B.) Text that could be used to declare a perfectly equivalent field

Using something like this allows for auto-determination of the type, rather than printing out the type aliases the user would be forced to provide (like "IntMap").

template <typename T>
struct TypeToStr {
    static constexpr auto Get() {
        return ConstexprStr::substr<
            ConstexprStr::length_between(__FUNCTION__, '<', '>')>
            (__FUNCTION__ + ConstexprStr::find(__FUNCTION__, '<') + 1);
    }
};

Now that typeStr is out of the way, there's no reason the user would have to pass in the type explicitly, everything in the REFLECT macro is a sub-class of the parent class, ergo we can get the type using decltype(fieldName), this greatly improves the ease of writing REFLECT macros, reduces maintenance (no need to change the type in the REFLECT macro if the type changes in the class) and the potential for user error.

Car Reflect Macro Before...

REFLECT(Car, (R<Wheel[4]>) wheels, (B<std::vector<std::string>>) occupants, (B<OccupantIdType>) occupantId,
    (R<OccupantCupHolderUsageType>) occupantCupHolderUsage,
    (R<std::vector<CupHolderPtr>>) cupHolders,
    (R<FuelTank>) fuelTank, (B<float>) milesPerGallon)

Car Reflect Macro After...

REFLECT(Car, (R) wheels, (B) occupants, (B) occupantId, (R) occupantCupHolderUsage,
    (R) cupHolders, (R) fuelTank, (B) milesPerGallon)