-
Notifications
You must be signed in to change notification settings - Fork 587
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
Comments
I see, there is already some facilities in We could thus declare 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.) |
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. |
C++ doesn't support reflection, so we need to create a bunch of if
statements that checks with dynamic_cast<>, but if we were to use it from
C++, we would usually have a good idea of the type, and would not need to
do that, right?
That said, the best way to map that might be to NOT make abstract
FloatLayer, or any other peer class for that matter... What do you think?
|
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. |
Right, there's typeid, thanks for reminding me. But then, we are limiting
the supported types to the ones we know at compile time, unless we provide
some way to register C++ types at runtime, but there's no way to automate
that, AFAIK?
This is getting complicated. I think in the end we should allow users to
create instances of FloatLayer, for example, and let them cast that to
something else, if required.
|
…factory methods and such from working properly (issue #25)
…Net` or `DoubleNet` in `caffe` (issue bytedeco/javacpp#25)
Ok, modifications done! Let me know if that is working satisfactorily. Thanks for reporting! |
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. |
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... |
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?
The text was updated successfully, but these errors were encountered: