bytedeco/javacpp

Problem with std::string_view as callback parameter

Open

#606 opened on Sep 19, 2022

View on GitHub
 (26 comments) (0 reactions) (0 assignees)Java (620 forks)batch import
bugenhancementhelp wanted

Repository metrics

Stars
 (4,279 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

I have an error during creation jni lib by javacpp in case I have function with callback with std::string_view parameter

greeter-jni.cpp:1858:9: error: no matching function for call to 'testStringView'
        testStringView(*ptr0);

In my case I created several c++ functions in my lib:

void testStringView(std::function<void(std::string_view message)> callback) {
    callback("hello");
}

void testBoolean(std::function<void(bool success)> callback) {
    callback(true);
}

void plainStringView(std::string_view message) {
std::cout << message;
}

Also I created java helper class for javacpp:

public class GreeterHelper extends GreeterConfig {

    public static class BooleanCallback extends FunctionPointer {
        static { Loader.load(); }
        protected BooleanCallback() { allocate(); }
        private native void allocate();

        public native void call(boolean success);
    }

    public static class StringCallback extends FunctionPointer {
        static { Loader.load(); }
        protected StringCallback() { allocate(); }
        private native void allocate();

        public native void call(@StdString String error_message);
    }
}

And config:

@Properties(
        value = @Platform(
                compiler = "cpp11",
                define = {"SHARED_PTR_NAMESPACE std", "UNIQUE_PTR_NAMESPACE std"},
                includepath = {"../greeter/include/acvp/"},
                include = {"make_cloud_agent.h"},
                library = "greeter-jni",
                link = {"acvp-cloud-agent-cpp", "acvp-core"},
                linkpath = {
                        "../greeter/build/",
                        "../greeter/build/module/acvp-core/"
                }
        ),
        target = "arrival.Greeter",
        helper = "arrival.GreeterHelper"
)
public class GreeterConfig implements InfoMapper {
    public void map(InfoMap infoMap) {
        infoMap.put(new Info("std::function<void(boolsuccess)>").pointerTypes("BooleanCallback").define());
        infoMap.put(new Info("std::function<void(std::string_viewmessage)>").pointerTypes("StringCallback").define());
        infoMap.put(new Info("std::string_view").pointerTypes("@StdString String").define());
    }
}

Here is generated file:

public static native void testStringView(@ByVal StringCallback callback);

public static native void testBoolean(@ByVal BooleanCallback callback);

public static native void plainStringView(@ByVal @StdString String message);

public static native void plainBoolean(@Cast("bool") boolean success);

With these settings it works ok for plainStringView function (plain function which receives string_view) and testBoolean function (function which receives callback with bool parameter), but I have an error for testStringView - function which receives callback with string_view parameter. How it can be fixed? Also this is strange but initial file which generates javacpp contains missed space symbol between callback parameter type and parameter name: std::function<void(boolsuccess)>.

Contributor guide