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

Provide interface for constructing smart pointer instances from Julia #371

Open
omus opened this issue Aug 31, 2023 · 1 comment
Open

Provide interface for constructing smart pointer instances from Julia #371

omus opened this issue Aug 31, 2023 · 1 comment

Comments

@omus
Copy link
Contributor

omus commented Aug 31, 2023

It would be nice to be able to construct things like std::shared_ptrs (ala. make_shared<>()) from Julia. Possibly the Julia interface for this could be SharedPtr{World}(StdString(...)).

On the C++ side this could internally be implemented via make_shared and the wrapper code could look like:

mod.add_type<World>("World")
    .constructor<const std::string &>()
    .apply_shared<const std::string &>();
@AUK1939
Copy link

AUK1939 commented Nov 10, 2023

Is the use case for this to be able to call functions/constructors from julia which take shared_ptr's as arguments on the C++ side? If so then yes, would be very useful.

My current hack to wrap a constructor like

class Handle{
   Handle(const std::shared_ptr<Foo>& p);
   ...
}

is to create a factory method create_handle in the wrapper

mod.add_type<Handle>("Handle")
        .constructor<const std::shared_ptr<Foo>&>()  
        .method("create_handle", [](const Foo& foo) { return Handle(std::make_shared<Foo>(foo)); }  );

Now on the julia side if I try and call the Handle constructor directly then it will fail with a type error but the factory works and I can pass the handle to other functions.

foo = Foo()
handle1 = Handle(foo)  # fails with type error because Handle expects a shared_ptr
handle1 = Handle(SharedPtr{Foo}(foo))  # with the above feature I could write this, which is convenient 

handle2 = create_handle(foo) // works by calling the factory method

I wonder if currently there is a better way to do this, rather than having to create a custom wrapper function for every constructor/method that takes a smart pointer?? I have literally 100s of such functions to wrap.

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