bytedeco/javacpp

How to deal with user-define-type array

Open

#493 aperta il 3 giu 2021

Vedi su GitHub
 (10 commenti) (0 reazioni) (0 assegnatari)Java (620 fork)batch import
enhancementhelp wantedquestion

Metriche repository

Star
 (4279 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

Here is a demo

#include <iostream>
class MyType {
    public:
        MyType() : a(0) {}
        MyType(int i) : a(i) {}
        int a;
};

class MyTypeUser {
    public:
        void use(MyType* a, int count) {
            for (int i = 0; i < count; i++) {
                std::cout<<a[i].a<<std::endl;
            }
        }
};

MyTypeUser::use expects an array of MyType. The generated code is only able to hold one MyType actually.

generated MyTypeUser.use definition

public native void use(MyType a, int count);

generated MyType

public class MyType extends Pointer {
    static { Loader.load(); }
    /** Pointer cast constructor. Invokes {@link Pointer#Pointer(Pointer)}. */
    public MyType(Pointer p) { super(p); }

public MyType() { super((Pointer)null); allocate(); }
private native void allocate();
public MyType(int i) { super((Pointer)null); allocate(i); }
private native void allocate(int i);
public native int a(); public native MyType a(int setter);
}

I tried to add the following mapping info following guide here(https://github.com/bytedeco/javacpp/wiki/Mapping-Recipes#specifying-names-to-use-in-java)

        infoMap.put(new Info("MyType").pointerTypes("MyTypePointer"));
        infoMap.put(new Info("MyTypePointer").valueTypes("MyTypePointer").pointerTypes("@Cast(\"MyType*\") PointerPointer", "@ByPtrPtr MyTypePointer").base("IntPointer"));

The generated code is almost the same.

        public native void use(MyTypePointer a, int count);
public class MyTypePointer extends Pointer {
    static { Loader.load(); }
    /** Pointer cast constructor. Invokes {@link Pointer#Pointer(Pointer)}. */
    public MyTypePointer(Pointer p) { super(p); }

        public MyTypePointer() { super((Pointer)null); allocate(); }
        private native void allocate();
        public MyTypePointer(int i) { super((Pointer)null); allocate(i); }
        private native void allocate(int i);
        public native int a(); public native MyTypePointer a(int setter);
}

Is it possible to define UDT pointer similarly to IntPointer, capable of holding one or more elements? @saudet

Guida contributor