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

Use the runtime C++ type when instantiating a pointer #25

Closed
cypof opened this issue Jul 28, 2015 · 8 comments
Closed

Use the runtime C++ type when instantiating a pointer #25

cypof opened this issue Jul 28, 2015 · 8 comments

Comments

@cypof
Copy link
Contributor

cypof commented Jul 28, 2015

For example in Caffe, Net.layer_by_name tries to create an instance of the root layer class, a java FloatLayer. This class is abstract, so the call fails. How could javacpp find the actual C++ type, and map it to the right java subclass?

@saudet
Copy link
Member

saudet commented Jul 30, 2015

I see, there is already some facilities in Generator for this. If the type of the first argument of a native method is Class, it gets used to create the returned object, such as with PointerPointer.get() for example:
https://github.com/bytedeco/javacpp/blob/master/src/main/java/org/bytedeco/javacpp/PointerPointer.java#L308

We could thus declare FloatLayer.layer_by_name() this way and it should work:

public native @Const @Cast({"", "boost::shared_ptr<caffe::Layer<float> >"}) @SharedPtr @ByVal L layer_by_name(Class<L extends FloatLayer> cls, @StdString String layer_name);

We could simply put that in the presets, if it works. How does that sound? BTW, I don't feel it would be a good idea to hack something together to try to detect automatically the type, since in C++ we have to cast explicitly anyway. It seems hard to justify how we can safely do this without any explicitly cast. (Not that converting the pointer to long as is is guaranteed to be any safer, but at least we are not influencing the final user API in Java.)

@cypof
Copy link
Contributor Author

cypof commented Jul 30, 2015

Thanks, that should fix the issue for now, by passing e.g. FloatMemoryDataLayer.class as argument. For the general case though, I am not sure I understand what you mean with the explicit cast.

The example I'm thinking of is let's say a C++ method with a Layer* return type. At runtime, for a particular execution, it returns a pointer to a MemoryDataLayer object. It seems to me that the right thing to do would be to get the typeid, and have javacpp map to the the right java class at runtime. So that is can instantiate a FloatMemoryDataLayer object, even if the java method has a return type of FloatLayer.

@saudet
Copy link
Member

saudet commented Jul 31, 2015 via email

@cypof
Copy link
Contributor Author

cypof commented Jul 31, 2015

I believe there is this "typeid" feature that might help, that might be enough as you know the mapping in advance. But I haven't played with it at all.

@saudet
Copy link
Member

saudet commented Jul 31, 2015 via email

@saudet saudet added the bug label Jul 31, 2015
saudet added a commit that referenced this issue Jul 31, 2015
…factory methods and such from working properly (issue #25)
saudet added a commit to bytedeco/javacpp-presets that referenced this issue Aug 1, 2015
@saudet
Copy link
Member

saudet commented Aug 1, 2015

Ok, modifications done! Let me know if that is working satisfactorily. Thanks for reporting!

@cypof
Copy link
Contributor Author

cypof commented Aug 1, 2015

Yes that's simpler and should work for us, thanks. I believe that in the long run it might interesting to support type mapping, e.g. if you call net.layers() to get the full network for manipulation or visualization, if the layers have the right type already it would be simpler. Otherwise the java side needs to parse the proto file too, to know which layer is of what type etc. But there is no need right now, just to keep in mind.

@cypof cypof closed this as completed Aug 1, 2015
@saudet
Copy link
Member

saudet commented Aug 2, 2015

I'm sure it would make things easier down the road if we can figure out the type, but C++ doesn't offer much in that way. BTW, in C++, when calling net.layers(), how do people go about it? Maybe it would make most sense to figure out a way to wrap that particular idiom...

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

No branches or pull requests

2 participants