-
Notifications
You must be signed in to change notification settings - Fork 586
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
Add explicitUpcast #671
Add explicitUpcast #671
Conversation
This looks like something reasonable, although instead of trying to work on unrelated changes all at once, let's at least separate all that into a couple of independent commits.
There must be a cleaner way of doing this...
I'm not sure what you're asking here. Functions like static_pointer_cast return a new shared_ptr, but we don't want to do that here anyway. Simply casting the pointer is sufficient. If you want to create a new shared_ptr though, we can still use SharedPtrAdapter for that, just like you did in pull #668.
Please do! |
Any idea ? Unless we accept of not making the thing transparent and ask the user to use the wrapped name of the method (
I can move this part to another PR if you wish, but then the explicitUpcast feature would be incompatible with the use of shared pointers. |
When not creating a new shared_ptr, we don't set the ownerAddress, so it won't get deallocated or anything, that's fine, it works. |
Yes, but then when you pass that object to the function taking a shared_ptr as argument, a new shared_ptr will be created. |
Right, anyway please use SharedPtrAdapter to create one, not static_pointer_cast. |
We need both, don't we ? That's how it works currently in the PR. |
I don't see why we need static_pointer_cast. We can do exactly the same thing with SharedPtrAdapter. |
Or actually we'd need to extend SharedPtrAdapter and add a constructor with 2 types like the "possible implementation" here I guess: https://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast#Version_1 |
Anyway, like I keep telling you, stop trying to do everything at the same time. You don't need to have all of it working exactly like it needs to be before we can merge. Let's get one thing at a time working. |
What bothers you with static_pointer_cast ? You're looking for something that would work for all adapters ? |
The implementation of asX and the explicitCast feature seems related to me, but no problem for splitting that in two PR. Anything else you want me to separate ? |
We should be able to use the adapter annotation from the constructor as is, that's what it's for. If you want to go into the details of all that and do a full POC before finalizing anything, that's fine, go ahead. I don't see where the problem is though. If you need to discuss this further, you'll need to provide more details about where you feel there is a problem. |
This PR currently generates either:
or
But, as you said, the second version could be generalized to something like:
With the addition of a Currently, the PR, to determine if the second version must be generated or the first one, looks for an annotation Info on the constructor of B or D. It looks clunky for me since:
But if we cannot find something better and not too complicated, we can live with that I guess. |
Sort of, like I said, we should be able to add a constructor instead of a method.
We shouldn't need to introspect anything at all. Look, just try to make it work, if you're not able to make it work, we'll look at more concrete problems that you encounter when you encounter them. And like I keep telling you, why do you want to merge everything all at the same time? It makes it very hard to get anything done! |
I asked you above if you want me to split off something else of this PR than the asX implementation. |
It's probably better that way yes, but if you want to have a complete POC that does absolutely everything you want, first, then by all means start working on that! As a first step, since I'm pretty sure we can do what you want to do with a new constructor to SharedPtrAdapter, please try to do that and see how that goes. Talking about every possible and impossible case isn't going to help. |
Sorry, I think I'm starting to understand what you're saying. Basically, we need something like a function call for asXXX()... unless we start doing something special in Generator. But let's say we don't do that, how should the user be able to pass the name of that function call... Let me think about that |
For me this PR is the POC. It works with Pytorch and I'm currently using it. It's the last one of the series of PR (but probably not split enough) for consolidating Pytorch presets. I realize I'm not very clear in my explanations. That could be interesting to generalize and pass an arbitrary function call for the cast. But:
Concerning the split of this PR, I can cut it into
Is that ok ? |
I like to consider one thing at a time, but if you're comfortable rewriting things, we can keep going in this thread, fixing one thing at a time, that's fine. Let's see, about the static_pointer_cast function template, we can think of it as something that isn't specific to shared_ptr, it just currently doesn't apply to anything else than shared_ptr. So, what if we just use it whenever there is a user specified annotation on the constructor and that upcast mode is enabled? It wouldn't break anything, and we don't need to add anything about "shared_ptr" in the Parser and/or new knobs that are most likely never going to get used, for now anyway. |
Ok, then I'll just split off the detection of virtual inheritance for now. That's something optional that you can decide to merge or not, and that concerns separate lines of codes. The other features are more related to each others.
I agree that we can postpone generalization until the need arises, if ever. Well, Let's sum up the implications of this
Can we assume that testing situation 1 is enough to exclude situation 3 ? I think so (although I'd prefer something to test on the class itself rather than on a constructor: an info or a We can check for
That could happen. If we limit to |
I checked all presets. No change in parsing (besides |
Many libraries use their own version of std::shared_ptr, such as OpenCV:
No, it's only needed when we want to cast explicitly some shared_ptr. That only happens in Parser for multiple inheritance or that upcast mode, and yes we only want to do that when
How is that not covered by your upcast mode?
Right, that's going to give a compiler error, so that's not a big issue.
Ideally, sure, but whatever we come up with is unlikely to be used by anyone at all, so let's not fix things that aren't broken.
Sure, but that's alright for backward compatibility.
static_pointer_cast doesn't get used unless there is multiple inheritance, so like I keep saying, you'll need to explain a bit more what you're worried about here.
Why? You mean we won't be changing the default behavior so that's a bad thing? There has been exactly zero issue reported about that, so there's no reason to fix what isn't broken. |
Argh. I didn't realize that. I see So calling a constructor or static factory function on the adapter seems the best. We don't need to implement it on all adapters and let the compiler throws an error when it's needed and doesn't exist.
You need it in
if we generate the adapter-version of
Won't it give a segmentation fault instead ?
It's not because there was no report that there is nothing to improve. You well see that if someone calls a The current behavior will be preserved in all cases since currently no constructor has annotations. |
This looks pretty much good to merge. I've adjusted a few nits, so could you confirm that it still does what you need it to do, without changing the results for any of the presets? |
With last commit, still working and no changes in presets parsing compared to master. |
This PR addresses issue #651.
A first attempt to solve it was PR #655.
But I find the solution brought by this PR better because:
explicitUpcast
is activated, so there is little chance of breaking anythingHow it works:
explicitUpcast
is used to mark those classes that need astatic_cast
for converting a pointer of a subclass to a pointer of this class.asB()
methods are added for all marked classesB
and all their direct subclasses._f
instead off
), in order to still support virtualization, we add an optional parameterjavaName
to annotation@Virtual
that tells the generator the name of the java callback method (f
in this example). This optional parameter is added for all virtualized wrapped method.explicitUpcast
. This feature could be removed if you prefer to minimize the code changes.asX
explicit casts were already used to map multiple inheritance. This PR proposes a version ofasX
that can takes care of smart pointers (could be in a separate PR), by using adapters and replacing thestatic_cast
by astatic_pointer_cast
. However this still need improvements because:shared_ptr
Finally, to improve readability and avoid code repetitions I introduced method
removeAnnotations
. This yields many line changes but no functional changes.I'll compare parsing result of existing presets to detect breakages if this PR is to be merged.