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

Mapping data members of class with functions in it #418

Open
minecraft2048 opened this issue Mar 10, 2024 · 1 comment
Open

Mapping data members of class with functions in it #418

minecraft2048 opened this issue Mar 10, 2024 · 1 comment

Comments

@minecraft2048
Copy link

minecraft2048 commented Mar 10, 2024

Let say I have this simple C++ class:

class test{
    public:
        int my_data_member;
        int my_function(){
            return 1;
        }
};

I know how to map the my_function member function:

JLCXX_MODULE define_julia_module(jlcxx::Module& mod)
{
  mod.method("greet", &greet);

  mod.add_type<test>("test")
    .constructor()
    .method("my_function",&test::my_function);
}

How do I map the my_data_member data member so I can get and set the value from Julia?

tst = CppHello.test() 
tst.my_data_member = 1 # How do I do this?
x = tst.my_data_member #and this

In Boost.Python they have this: https://www.boost.org/doc/libs/1_84_0/libs/python/doc/html/tutorial/tutorial/exposing.html#tutorial.exposing.class_data_members

@barche
Copy link
Collaborator

barche commented Mar 21, 2024

Hi, currently, the only way is to add setters and getters explicitly, e.g:

mod.add_type<test>("test")
    .constructor()
    .method("get_my_data_member", [] (const test& t) { return t.my_data_member; })
    .method("set_my_data_member", [] (test& t, int d) { t.my_data_member = d; });

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