weikipeng / javacpp

Automatically exported from code.google.com/p/javacpp
GNU General Public License v2.0
0 stars 0 forks source link

@StdString annotation is ignored for function pointers #34

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have a class derived from FunctionPointer like this:

public static abstract class Observer extends FunctionPointer {
        public Observer() {
            allocate();
        }

        private native void allocate();

        public abstract void valueChanged(String key, String value);

        public void call(@StdString String key, @StdString String value) {
            this.valueChanged(key, value);
        }
}

The C++ equivalent looks like this:

std::function<void (std::string, std::string)> observer

The generated code will use const char* instead of std::string, leading to 
compiler errors.

Note:

As a workaround, one can post-process the generated file like this:

sed -i 's/(const char\* arg0, const char\* arg1)/(std::string arg0, std::string 
arg1)/g' jniFile.cpp
sed -i 's/obj0 = arg0 == NULL/obj0 = arg0.length() == 0/g' jniFile.cpp
sed -i 's/obj1 = arg1 == NULL/obj1 = arg1.length() == 0/g' jniFile.cpp

Original issue reported on code.google.com by neoco...@googlemail.com on 5 Jul 2013 at 12:26

GoogleCodeExporter commented 9 years ago
Correct, it wasn't doing what one would expect from it. I think I've fixed that 
with this change:
http://code.google.com/p/javacpp/source/detail?r=2b3ce614120d0b813dd83901294e006
4b62f97cb
Let me know how that goes, and thanks for reporting!

Original comment by samuel.a...@gmail.com on 7 Jul 2013 at 1:15

GoogleCodeExporter commented 9 years ago
This almost works for me, but it now generates a "std::string&" even when 
explicitly putting a @ByVal in.

Original comment by neoco...@googlemail.com on 8 Jul 2013 at 7:13

GoogleCodeExporter commented 9 years ago
We can adjust the type to whatever we want with the second cast (the first cast 
gets applied to the `const char *` of the `String`, not the `StringAdapter`), 
i.e.:
public void call(@Cast({"", "std::string"}) @StdString String key, @Cast({"", 
"std::string"}) @StdString String value);

Original comment by samuel.a...@gmail.com on 8 Jul 2013 at 7:41

GoogleCodeExporter commented 9 years ago
Ah, thanks - that works great!

Original comment by neoco...@googlemail.com on 8 Jul 2013 at 1:29

GoogleCodeExporter commented 9 years ago
Good! BTW, it's also possible to create your own annotation and put the @Cast 
there, i.e.: Copy StdString.java to MyStdString.java or something, change the 
content of the @Cast annotation in the file, and use @MyStdString on parameters.

Original comment by samuel.a...@gmail.com on 8 Jul 2013 at 1:51

GoogleCodeExporter commented 9 years ago
Fix included in newly released version 0.6, thanks for reporting!

Original comment by samuel.a...@gmail.com on 15 Sep 2013 at 1:32