pythonc++cythoncythonize

How can I pass optional type from C++ to Cython?


I'm trying to pass an optional argument from C++ to Cython files. I don't know how to write it in .pxd and .pyx file:

[C++]

std::optional<std::shared_ptr<CPP_MyObject>> cpp_find_value();

[Work.pxd]

shared_ptr[CPP_MyObject] cpp_find_value()   # fix me

[Work.pyx]

def python_find_value(self):    # fix me
    cdef shared_ptr[CPP_MyObject] cpp_object = self.thisptr.get().cpp_find_value()
    cdef Python_MyObject python_object = Python_MyObject("0")
    python_object.thisptr = cpp_object
    return python_object

Solution

  • So recently, I found the answer for passing optional value from C++ to Cython Interface. And this is a more elaborated code.

    In Work.pxd file

    In the Work.pxd file, there's a feature in libcpp.optional that allows you to wrap your C++ data types with optional.

    from libcpp.optional cimport optional
    from libcpp.memory cimport shared_ptr
    
    cdef extern from "your_cpp_header.h":
        optional[shared_ptr[CPP_MyObject]] cpp_find_value()
    

    In Work.pyx file

    In the Work.pyx file, you can receive the result of find_value() as an optional C++ data type. With this, you can check if the value exists using .has_value(). If a value is present, you can insert it into a Python object pointer using .value() and return the Python object.

    If no value is present, simply return None.

    def python_find_value(self):
        cdef Python_MyObject python_object = Python_MyObject("0")
        cdef optional[shared_ptr[CPP_MyObject]] cpp_object = self.thisptr.get().cpp_find_value()
        
        if cpp_object.has_value():
           python_object.thisptr = cpp_object.value()
           return python_object
        return None