Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function pointer returning shared_ptr #720

Closed
HGuillemet opened this issue Oct 16, 2023 · 10 comments
Closed

Function pointer returning shared_ptr #720

HGuillemet opened this issue Oct 16, 2023 · 10 comments
Labels

Comments

@HGuillemet
Copy link
Contributor

HGuillemet commented Oct 16, 2023

#include <memory>

struct S {
  int x;
};

std::shared_ptr<S> (*f)();

The JNI cannot compile with:

error: invalid conversion from 'std::shared_ptr<S> (*)()' to 'S* (*)()' [-fpermissive]
  628 |     if (rptr != NULL) rptr->ptr = f;

Because the generated class:

struct JavaCPP_hidden JavaCPP_j_S_1F {
    JavaCPP_j_S_1F() : ptr(NULL), obj(NULL) { }
    S* operator()();
    S* (*ptr)();
    jobject obj; static jmethodID mid;
};

has a function returning S* instead of std::shared_ptr<S>.

@saudet
Copy link
Member

saudet commented Oct 16, 2023

JavaCPP needs a FunctionPointer peer class

@HGuillemet
Copy link
Contributor Author

Sure. The problem, if I understood well, is the @SharedPtr annotation not taken into account by the Generator when generating the peer class.

@saudet
Copy link
Member

saudet commented Oct 17, 2023

Right, so where is that FunctionPointer class?

@HGuillemet
Copy link
Contributor Author

@Properties(inherit = presets.P.class)
public class S_F extends FunctionPointer {
    static { Loader.load(); }
    /** Pointer cast constructor. Invokes {@link Pointer#Pointer(Pointer)}. */
    public    S_F(Pointer p) { super(p); }
    protected S_F() { allocate(); }
    private native void allocate();
    public native @SharedPtr S call();
}

That's the class automatically generated by the parser from the C++ code above.

@saudet
Copy link
Member

saudet commented Oct 18, 2023

You'll need to add at least a @Cast to that...

@HGuillemet
Copy link
Contributor Author

HGuillemet commented Oct 18, 2023

public native @Cast({"", "std::shared_ptr<S>"}) @SharedPtr S call();

seems to work. Thanks.

Do we close this issue or keep it open in case there is something to fix to avoid this @Cast ?

@saudet
Copy link
Member

saudet commented Oct 19, 2023

We need to specify the type somehow. As I've mentioned somewhere before, think of @Cast as if it were named @As instead.

@HGuillemet
Copy link
Contributor Author

@SharedPtr already means that the function returns a shared_ptr of S. For a normal function, that calls a native function of the object pointed by the Pointer instead of the native function directly pointed by the Pointer, we don't need the @Cast.

@saudet
Copy link
Member

saudet commented Oct 19, 2023

No, it doesn't, for example, see issue #717

@HGuillemet
Copy link
Contributor Author

In #717 the @Cast is to disambiguate between 2 overloads of unique_ptr.operator=. The @Cast doesn't change the type of the field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants