wrapping C++ functions that return objects with no default constructor

65 views
Skip to first unread message

buc...@gmail.com

unread,
Nov 25, 2016, 10:55:27 PM11/25/16
to cython-users
Hello,

I'm looking for a sanity check. I am trying to wrap the folly::Future class (https://github.com/facebook/folly/blob/master/folly/futures/Future.h), which doesn't have a default constructor. I've spent a couple hours searching google and this list and understand that Cython can't easily handle such cpp classes because of scoping issues. Most of the topics I've seen are attempting to stack allocate the object on a wrapper class, and this is easily solved by heap allocating the result instead.

However, I'm trying to assign the return value of a method call. I don't think I have the option to heap allocate it.  The method signature looks like this:

virtual folly::Future<int32_t> my_func()

Can anyone confirm that this really is impossible before I go about trying to write some sort of C++ shim to take care of it? Is there a recommended pattern for this kind of shim?



For reference, here's the pxd I'm using to wrap the class:

cdef extern from "folly/futures/Future.h" namespace "folly":
    cdef cppclass cFollyFuture "folly::Future"[T]:
        T get()
        cFollyFuture[T]& wait()



and I'm calling with:

cdef Future[int32] future_result = client.my_func()



Thanks for any hints,

Dusty

Kevin Thornton

unread,
Dec 9, 2016, 12:21:40 PM12/9/16
to cython-users
This may be a case for heap-allocating it into a shared_ptr/unique_ptr:

#pseudocode alert
from libcpp.memory cimport unique_ptr
cdef unique_ptr[Future] upFut

#Assume that object is copy constructible
upFut.reset(new Future(function_call_returning_type()))
Reply all
Reply to author
Forward
0 new messages