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 support for describe getters && setters #18

Open
denzor200 opened this issue Sep 11, 2021 · 5 comments
Open

Add support for describe getters && setters #18

denzor200 opened this issue Sep 11, 2021 · 5 comments

Comments

@denzor200
Copy link
Contributor

I suggest adding support for getters and setters to the library. I see this new functionality like this:

#include <boost/describe.hpp>
#include <iostream>

struct employee
{
    void                  set_name    (std::string name)         ;
    void                  set_age     (int age)                  ;
    const std::string&    get_name    ()                   const ;
    int                   get_age     ()                   const ;
};

BOOST_DESCRIBE_STRUCT(
    employee, (), (
        ("name", get_name, set_name),
        ("age", get_age,  set_age)))


int main()
{
    using boost::describe::describe_members;
    using boost::describe::mod_any_access;
	
    employee emp;
    emp.set_name("John");
    emp.set_age(20);
    
    boost::mp11::mp_for_each<describe_members<decltype(emp), mod_any_access>>([&](auto D){
        auto getter = D.getter; // D also have "setter" field, but a "pointer" field is unavailable
        std::cout << D.name << " = " << (emp.*getter)() << std::endl;
    }); 
	
    // Will print:
    //    name = John
    //    age = 20
    
    return 0;
}

@denzor200
Copy link
Contributor Author

I found a better way to do this:

struct employee
{
    void                  set_name    (std::string name)         ;
    void                  set_age     (int age)                  ;
    const std::string&    get_name    ()                   const ;
    int                   get_age     ()                   const ;
    struct employee_data& data        ()                         ;
};

BOOST_DESCRIBE_STRUCT(employee, (), (
    (set_name, (setter, "name")),
    (set_age,  (setter, "age")),
    (get_name, (getter, "name")),
    (get_age,  (getter, "age")),
    (data),    (accessor, "data")
))

int main()
{
    using boost::describe::describe_members;
    using boost::describe::mod_getters;
    using boost::describe::mod_accessors;
    using boost::describe::is_setter;
    using boost::describe::get_encapsulated_field_name;

    employee emp;
    emp.set_name("John");
    emp.set_age(20);
    emp.data() = employee_data();
   
    boost::mp11::mp_for_each<describe_members<decltype(emp), mod_getters | mod_accessors>>([&](auto D){
        const auto pointer = D.pointer;
        std::cout << get_encapsulated_field_name(D) << " = " << (emp.*pointer)() << std::endl;
    }); 

    // Will print:
    //    name = John
    //    age = 20
    //    data = employee_data

    return 0;
}

Also, its compatible with #13. We can start by making #13 and then separately declare the setter, getter and accessor as service attributes.

@klemens-morgenstern
Copy link

How's that different from a function on the language level?

@denzor200
Copy link
Contributor Author

How's that different from a function on the language level?

I'm not getting your question. Please reword or submit a little better example.

@klemens-morgenstern
Copy link

Getters and setter don't exist in C++, this isn't C# or python. You're trying to describe functions, which you already can do with boost.describe.

@denzor200
Copy link
Contributor Author

denzor200 commented Oct 11, 2022

Getters and setter don't exist in C++, this isn't C# or python. You're trying to describe functions, which you already can do with boost.describe.

I'm trying to suggest alternative for BOOST_HANA_ADAPT_ADT. I haven't thought about C# or Python getters and setters.

It's useful for #32

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