bytedeco/javacpp

How to deal with user-define-type array

Open

#493 opened on Jun 3, 2021

View on GitHub
 (10 comments) (0 reactions) (0 assignees)Java (620 forks)batch import
enhancementhelp wantedquestion

Repository metrics

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

Description

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

Contributor guide