Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Set() setter property method chaining #13

Open
RobLoach opened this issue Mar 19, 2020 · 2 comments
Open

Add Set() setter property method chaining #13

RobLoach opened this issue Mar 19, 2020 · 2 comments

Comments

@RobLoach
Copy link
Owner

Could use auto, or hardcode the return *this in the property utility.

@GaniAliguzhinov
Copy link
Contributor

GaniAliguzhinov commented Dec 8, 2024

I tried it, here is what I found:

Attempt 1

#define GETTERSETTER(type, method, name)
    GETTER(type, method, name)
    auto Set##method(type value) {
        name = value;
        return *this;
    }
#endif

Works for simple classes, but not classes like ModelAnimation, where the copy constructor is explicitly deleted in favor of the overloaded move by rvalue reference, as the return *this line calls the deleted constructor.

Also, for classes where there is a user-provided Class& Class::operator=(const Class&) operator, we get warnings that the implicitly-declared Class(const Class&) is deprecated, for example in the Text class.

Another note, I get the warnings: deduced return type only available with ‘-std=c++14’ or ‘-std=gnu++14’ until I update the cpp standard.

Attempt 2

#define GETTERSETTER(base, type, method, name)
    GETTER(type, method, name)
    base&& Set##method(type value) && {
      name = value;
      return std::move(*this);
    }
#endif

Works well, but needs refactoring to pass the base type to the utility macro. Maybe we could define a special macro for classes with deleted copy constructor / user-defined = operator?

P.S. Referenced stackoverflow

Attempt 3

#define GETTERSETTER(type, method, name)                                                       \
    GETTER(type, method, name)                                                                 \
    /** Sets the name value for the object. @param value The value of which to set name to. */ \
    auto& Set##method(type value) & {                                                          \
        name = value;                                                                          \
        return *this;                                                                          \
    }                                                                                          \
    auto&& Set##method(type value) && {                                                        \
        name = value;                                                                          \
        return std::move(*this);                                                               \
    }
#endif

This works, but needs CXX_STANDARD to be updated to 14 from 11 (also true for other Attempt 1)

@GaniAliguzhinov
Copy link
Contributor

Here is example test with the Attempt 3:

raylib::Vector2 chained = raylib::Vector2(1, 2).SetX(2).SetY(4);
AssertEqual(chained.x, 2);
AssertEqual(chained.y, 4);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants