bytedeco / javacpp

The missing bridge between Java and native C++
Other
4.46k stars 581 forks source link

Add support for `@Name` on allocators #706

Closed HGuillemet closed 11 months ago

HGuillemet commented 1 year ago

Add support for @Name on allocate() and allocateArray(). The functions passed as parameter to @Name will replace the call to new.

This will be used for instance in (see PR #668):

@SharedPtr @Name("std::make_shared<xxx>") private native void allocate();

or (see PR #700):

@Name("std::dynamic_cast<DerivedClass*>") private native void allocate(BaseClass pointer);

Note 1: if placed on a array allocator, the function is supposed to return a pointer to an array. For instance resulting from std::make_shared<xxx>(n) (unfortunately supported since C++20 only).

Note 2: in the special case of classes with @Virtual methods, the class to instantiate is the proxy class, with its mangled name, so we need to write things like:

@SharedPtr @Name("std::make_shared<JavaCPP_torch_0003a_0003ann_0003a_0003aModule>") private native void allocate();

which is a bit hacky, but works.

Also included in the PR is an updated and more detailed javadoc comment for @Adapter.

HGuillemet commented 11 months ago

This PR was merged into #700.