billyquith / ponder

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

Support for setters that return a reference to the object (catch phrase "named parameter idiom") #28

Closed Weeena closed 8 years ago

Weeena commented 8 years ago

If you have some class with some property where the setter returns a reference to the object (which is known as the "named parameter idiom", see e.g. Method chaining),

class MyClass
{
public:
    int Getter() { return i_; };
    MyClass& Setter(int i) { i_ = i; return *this; };
private:
    int i_;
};

and you like to declare this class to ponder with

PONDER_TYPE(MyClass)

void declare_MyClass()
{
    ponder::Class::declare<MyClass>("MyClass")
        .constructor()
        .property("Value", &MyClass::Getter, &MyClass::Setter);
}

you get several compile errors. These are caused by the fact that, in propertyfactory.hpp, Acessor3 is chosen instead of Acessor2.

I suggest to explicitly check for the "named parameter idiom" in the template specialication for Acessor3 (where now it is only checked whether the F2 return type is void).

billyquith commented 8 years ago

I think I have a fix for this. I've started the 1.2 branch as this may start changing behaviour. Please get that for now. Other changes will appear there.

Looks like the CI is broken though due to an old default clang version. Just sorting that.

Weeena commented 8 years ago

I just got your fix and made a short test. Thanks a lot, it works.