Skip to main content
edited tags; edited title
Link
user1430
user1430

Is there a robust way to How can I get a pointer to a resource when I havevia a handle to its base type that doesn't rely onhandle without dynamic_cast?

replaced http://gamedev.stackexchange.com/ with https://gamedev.stackexchange.com/
Source Link

I'm building my own pet engine and after struggling with how to handle resource ownership for a while, I came across this questionthis question and specifically Josh and Sean's responses (as well as Sean's blog on shared_ptr), which really made a whole lot of sense to me. So I went ahead and implemented my own similar system, and so far it's working great except for one minor snag - what do I do when I need to access a resource of a derived type when the handle only refers to the base type?

I'm building my own pet engine and after struggling with how to handle resource ownership for a while, I came across this question and specifically Josh and Sean's responses (as well as Sean's blog on shared_ptr), which really made a whole lot of sense to me. So I went ahead and implemented my own similar system, and so far it's working great except for one minor snag - what do I do when I need to access a resource of a derived type when the handle only refers to the base type?

I'm building my own pet engine and after struggling with how to handle resource ownership for a while, I came across this question and specifically Josh and Sean's responses (as well as Sean's blog on shared_ptr), which really made a whole lot of sense to me. So I went ahead and implemented my own similar system, and so far it's working great except for one minor snag - what do I do when I need to access a resource of a derived type when the handle only refers to the base type?

Tweeted twitter.com/#!/StackGameDev/status/436262381665406976
deleted 3 characters in body
Source Link
PJdev
  • 13
  • 3
  • When I dereference the handle, use dynamic_cast to cast the pointer to a RenderTarget* as necessary.

    • RTTI is "Bad" TM, which is to say that it very often gets disabled (along with C++ exceptions, etc.) in game applications due to unnecessary overhead, particularly on consoles. Plus resorting to dynamic_cast feels like I'm "doing it wrong," architecturally speaking.
  • I know it's a RenderTarget; it was when I created it, and if it weren't, I wouldn't be passing it to SetRenderTarget in the first place! reinterpret_cast!

    • Do I REALLY know? If I'm wrong, then I'm casting something else to something it's not, and I can't even check if it's nullptr if I'm wrong. Also, again, I'd like to avoid casts if possible. But I don't entirely disagree with the logic here...
  • Implement a lightweight homegrown alternative to RTTI, and use that instead.

    • I've seen this done in one or two engines, but it sounds like some fairly heavy-duty wheel-reinventing...
  • Don't derive RenderTarget from Texture; instead, add a flag that says whether or a Texture is a RenderTarget or not. If it's not, then SetRenderTarget will simply fail.

    • Potentially lots of excess baggage added to the Texture class. (What's another pointer to an ID3D11RenderTargetView among friends?) Doesn't sound like it scales very well.
  • Derive RenderTarget from Texture, but add a virtual method to Texture called GetRenderTarget() that normally returns nullptr but is overridden in RenderTarget to return this. Similar to the flag method above, but without the excess baggage.

    • Feels like a poor man's dynamic_cast. Again, doesn't scale well (add a new method for each derived class that needs it?). But if my RenderTarget scenario is maybe the only case I actually need something like this, I'm beginning to feel like maybe this solution isn't so bad...
  • When I dereference the handle, use dynamic_cast to cast the pointer to a RenderTarget* as necessary.

    • RTTI is "Bad" TM, which is to say that it very often gets disabled (along with C++ exceptions, etc.) in game applications due to unnecessary overhead, particularly on consoles. Plus resorting to dynamic_cast feels like I'm "doing it wrong," architecturally speaking.
  • I know it's a RenderTarget; it was when I created it, and if it weren't, I wouldn't be passing it to SetRenderTarget in the first place! reinterpret_cast!

    • Do I REALLY know? If I'm wrong, then I'm casting something else to something it's not, and I can't even check if it's nullptr if I'm wrong. Also, again, I'd like to avoid casts if possible. But I don't entirely disagree with the logic here...
  • Implement a lightweight homegrown alternative to RTTI, and use that instead.

    • I've seen this done in one or two engines, but it sounds like some fairly heavy-duty wheel-reinventing...
  • Don't derive RenderTarget from Texture; instead, add a flag that says whether or a Texture is a RenderTarget or not. If it's not, then SetRenderTarget will simply fail.

    • Potentially lots of excess baggage added to the Texture class. (What's another pointer to an ID3D11RenderTargetView among friends?) Doesn't sound like it scales very well.
  • Derive RenderTarget from Texture, but add a virtual method to Texture called GetRenderTarget() that normally returns nullptr but is overridden in RenderTarget to return this. Similar to the flag method above, but without the excess baggage.

    • Feels like a poor man's dynamic_cast. Again, doesn't scale well (add a new method for each derived class that needs it?). But if my RenderTarget scenario is maybe the only case I actually need something like this, I'm beginning to feel like maybe this solution isn't so bad...
  • When I dereference the handle, use dynamic_cast to cast the pointer to a RenderTarget* as necessary.

    • RTTI is "Bad" TM, which is to say that it very often gets disabled (along with C++ exceptions, etc.) in game applications due to unnecessary overhead, particularly on consoles. Plus resorting to dynamic_cast feels like I'm "doing it wrong," architecturally speaking.
  • I know it's a RenderTarget; it was when I created it, and if it weren't, I wouldn't be passing it to SetRenderTarget in the first place! reinterpret_cast!

    • Do I REALLY know? If I'm wrong, then I'm casting something else to something it's not, and I can't even check if it's nullptr if I'm wrong. Also, again, I'd like to avoid casts if possible. But I don't entirely disagree with the logic here...
  • Implement a lightweight homegrown alternative to RTTI, and use that instead.

    • I've seen this done in one or two engines, but it sounds like some fairly heavy-duty wheel-reinventing...
  • Don't derive RenderTarget from Texture; instead, add a flag that says whether a Texture is a RenderTarget or not. If it's not, then SetRenderTarget will simply fail.

    • Potentially lots of excess baggage added to the Texture class. (What's another pointer to an ID3D11RenderTargetView among friends?) Doesn't sound like it scales very well.
  • Derive RenderTarget from Texture, but add a virtual method to Texture called GetRenderTarget() that normally returns nullptr but is overridden in RenderTarget to return this. Similar to the flag method above, but without the excess baggage.

    • Feels like a poor man's dynamic_cast. Again, doesn't scale well (add a new method for each derived class that needs it?). But if my RenderTarget scenario is maybe the only case I actually need something like this, I'm beginning to feel like maybe this solution isn't so bad...
Source Link
PJdev
  • 13
  • 3
Loading