Skip to content

Problem with std::unique_ptr as callback parameter #613

@ValentinaBaranova

Description

@ValentinaBaranova

I have problem with creating FunctionPointer for callback which receives std::unique_ptr as argument.
This is my test function:

class Stream {
public:
    using StreamId = std::int32_t;
public:
    virtual ~Stream() = default;
    [[nodiscard]] virtual StreamId getStreamId() const noexcept = 0;
};

void testOnStreamOpenCb(std::function<void(std::unique_ptr<Stream> stream)>) {
    std::cout << "testOnStreamOpenCb";
}

This is my FunctionPointer:

    public static class OnStreamOpenCbCallback extends FunctionPointer {
        // Loader.load() and allocate() are required only when explicitly creating an instance
        static {
            Loader.load();
        }

        protected OnStreamOpenCbCallback() {
            allocate();
        }

        private native void allocate();

//        public native void call(@ByVal @UniquePtr @Cast("Stream*") Pointer stream);
//        public native void call(@ByVal @Cast("std::unique_ptr<Stream>*") Pointer stream);
        public native void call(@MoveUniquePtr @Cast("Stream*") Pointer stream);
    }

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD, ElementType.PARAMETER})
    @Cast({"std::unique_ptr", "&&"}) @Adapter("UniquePtrAdapter")
    public @interface MoveUniquePtr {
        String value() default "";
    }

I saw uning MoveUniquePtr in tensorflow preset, but there callback returns unique_ptr, not use it as argument.
Here is my map:

infoMap.put(new Info("std::function<void(std::unique_ptr<Stream>stream)>").pointerTypes("OnStreamOpenCbCallback").define());

If I remove cpp function testOnStreamOpenCb, compilation fails because of testOnStreamOpenCb.
Could you please advice me something?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions