diff --git a/src/main/java/org/bytedeco/javacpp/tools/Generator.java b/src/main/java/org/bytedeco/javacpp/tools/Generator.java index 903dbf151..a20c471f0 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/Generator.java +++ b/src/main/java/org/bytedeco/javacpp/tools/Generator.java @@ -1388,6 +1388,7 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver out.println(" UniquePtrAdapter( U& uniquePtr) : ptr(0), size(0), owner(0), uniquePtr2(U(NULL, D())), uniquePtr(uniquePtr) { }"); out.println(" UniquePtrAdapter(const U* uniquePtr) : ptr(0), size(0), owner(0), uniquePtr2(U(NULL, D())), uniquePtr(*(U*)uniquePtr) { }"); out.println(" void assign(T* ptr, size_t size, void* owner) {"); + out.println(" if (this->ptr == ptr && this->size == size && this->owner == owner) return;"); out.println(" this->ptr = ptr;"); out.println(" this->size = size;"); out.println(" this->owner = owner;"); @@ -3245,7 +3246,11 @@ void callback(Class cls, Method callbackMethod, String callbackName, int allo if (adapterInfo != null) { usesAdapters = true; - out.println(" " + adapterInfo.name + " adapter" + j + "(arg" + j + ");"); + String cast2 = adapterInfo.cast2.trim(); + if (cast2.length() > 0 && !cast2.startsWith("(") && !cast2.endsWith(")")) { + cast2 = "(" + cast2 + ")"; + } + out.println(" " + adapterInfo.name + " adapter" + j + "(" + cast2 + "arg" + j + ");"); } if (Pointer.class.isAssignableFrom(callbackParameterTypes[j]) || diff --git a/src/test/java/org/bytedeco/javacpp/AdapterTest.java b/src/test/java/org/bytedeco/javacpp/AdapterTest.java index 51807fa98..31f82e8f4 100644 --- a/src/test/java/org/bytedeco/javacpp/AdapterTest.java +++ b/src/test/java/org/bytedeco/javacpp/AdapterTest.java @@ -271,16 +271,19 @@ static class UniqueFunction extends FunctionPointer { sharedData = fetchSharedData(); assertEquals(13, sharedData.data()); + final SharedData[] sharedData2 = new SharedData[1]; int data = testCallback(new SharedFunction() { @Override public int call(SharedData d) { - int data = d.data(); - d.deallocate(); - return 2 * data; + d.data(2 * d.data()); + sharedData2[0] = d; + return 3 * d.data(); } }, sharedData); - assertEquals(2 * 13, data); + assertEquals(2 * 13, sharedData2[0].data()); + assertEquals(2 * 3 * 13, data); sharedData.deallocate(); + sharedData2[0].deallocate(); assertEquals(1, constructorCount()); assertEquals(1, destructorCount()); @@ -310,14 +313,17 @@ static class UniqueFunction extends FunctionPointer { uniqueData = fetchUniqueData(); assertEquals(42, uniqueData.data()); + final UniqueData[] uniqueData2 = new UniqueData[1]; int data = testCallback(new UniqueFunction() { @Override public int call(UniqueData d) { - int data = d.data(); - d.deallocate(); - return 2 * data; + d.data(2 * d.data()); + uniqueData2[0] = d; + return 3 * d.data(); } }, uniqueData); - assertEquals(2 * 42, data); + assertEquals(2 * 42, uniqueData2[0].data()); + assertEquals(2 * 3 * 42, data); + uniqueData2[0].deallocate(); System.gc(); }