-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Casting down in class hierarchy #105
Comments
When Pybind11 sees an object with a pointer representation that has been turned into a Python object before, it will return that Python object instead of doing another conversion. This is to avoid serious headaches with reference counting (imagine two different Python objects pointing to the same memory region). If this bothers you, you could create a copy or return the desired type in the first place that created the object. |
the problem it, the API of Box2D just returns the base type, I noticed that a copy does the right thing, but edit: I'll think about some hacks and will report back if you are interested |
This kind of thing is simply not intended by the current design, and changing that might complicate the library quite a bit (more involved reference counting & can have multiple different types associated with one pointer). |
Sure, I agree that this might complicate things. |
In this simple case, |
I guess one potential solution will be to extend the registered_instance hashtable to include the type in addition to the instance pointer as key. It will be a fair bit of work though and is not something that I need for my own projects, so I fear that you are on your own. |
I am willing to do the work. |
I found a solution which is good enough for my self. And I made a few hacks in cast.h in the cast function. In the The actual casting function now looks like:
|
As you mention this is a hack -- for a PR, it would need to be a more generic and clean solution. |
Would you consider to add the "isCasted" bool to I am thinking about the following: I guess one cannot use this wrong or mess up things? Implemented it: code looks now like
I would say from the users point of view that solution would be acceptable, under the hood I still do it as described, but it does not change much and can be replaced with a better solution without users ever knowing. |
I can set up an example and a PR, just in case you want to have a detailed look at It. |
To consider adding such a feature, I'd really be looking for a way to make it work in any setting, not just a cast. This means that the underlying hash data structure must be extended as I mentioned at the beginning of the ticket, I don't see another way. |
As a work-around for the interum, dynamic casts can be refactored away using a visitor pattern. |
could you give an example? @wjakob I sped a few hours trying to extend the underlying hash data structure. |
So it turns out that there is a very simple way to always return the best variant of a polymorphic type without the uglyness of a special casting function, which can be realized in just a few lines of code. This should hopefully resolve this issue. |
updates: - [github.com/pre-commit/pre-commit-hooks: v4.3.0 → v4.4.0](pre-commit/pre-commit-hooks@v4.3.0...v4.4.0) - [github.com/PyCQA/flake8: 5.0.4 → 6.0.0](PyCQA/flake8@5.0.4...6.0.0) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Hi, I have a base class with virtual functions "b2Shape" and a derived class "b2PolygonShape"
and i exported them as suggested (using the "extra" pyb2Shape class/trampoline) and everything works well.
But at some point i need to "cast" a b2Shape ptr to a b2PolygonShape.
I used the following code.
pybox2dModule.def("b2PolygonShapeCast",[](b2Shape * shape) -> b2PolygonShape * { b2PolygonShape * ps = dynamic_cast<b2PolygonShape*>(shape); return ps; });
But I always get back a b2Shape object on the python side.
So far i have no idea why this should not work.
Is this a bug or intended?
If intended, whats the best way of doing such a "downcast"
The text was updated successfully, but these errors were encountered: