[request] Change user data to be any_ptr instead of void*

Hi,

I've made a small patch file (attached) which contains the modifications required for b2Joint, b2Body and b2Fixture to hold an any_ptr instead of void* to user data. The changes are really tiny (Just 18 lines of code modified, and any_ptr.h added to Box2D\Common). If you deem it worthy, I think it would be a nice addition to your already lovely library. It would save many a programmer from the headaches of debugging undefined behavior due to inadvertent incorrect casts.

Best Regards,
Francis Xavier

More info from the request posted at the box2d google code page:

Request
It would be nice to be able to set/retrieve user data in a type-safe manner. If user data were of type any_ptr instead of void*, a bad cast would yield a null pointer which the user can check for.

Article on any_ptr
http://www.gamedev.net/reference/programming/features/TypeSafeGenPtr/

Performance
Assuming that casting from any_ptr is not done in a tight loop, there should be no noticeable overhead in a C++ game which uses box2D. Being extremely lightweight, any_ptr does not require RTTI or exceptions to be enabled. However, the strong type-safety which any_ptr provides is sure to save some users the headache of debugging incorrectly cast void pointers.

The time/space overhead caused by the use of any_ptr is extremely tiny if not insignificant:
  • Size of any_ptr: 1 void pointer + 1 integer (~4 bytes bigger than a void*)
  • any_ptr casting overhead:
    • Cast to non-const object pointer: min/max: ~1 conditional check
    • Cast to const object pointer: min: ~1 conditional check, max: ~2 conditional checks

Edit: Please use the updated patch attached with my latest post in this thread.
This is a neat idea. The only problem I see with this is the static. This might cause problems for people who try to thread Box2D.
Hi,

I recently implemented a thread-safe version of any_ptr, keeping intact the features/requirements.

Please check this thread for more info:
http://www.gamedev.net/community/forums/topic.asp?topic_id=590033

I've also updated the patch file (attached) to use the thread-safe implementation of any_ptr.

Edit: Updated with portable implementation - corrected for C4311 (64-bit portability issue)
Edit: Bug fix - forgot to modify the SetUserData functions to take any_ptr instead of void*

Best Regards,
Francis Xavier
Question: I use the userData slot to store an integer in hex format in Box2CS (because pointers aren't very compatible between C# and C) which is used then in Box2CS to get the object associated with the number; will this patch break this behavior?

-P
Hi,

I'm not sure I understood you correctly; could you explain further, or post some code describing your usage scenario?
This patch is only applicable to the C++ version of Box2D. For the C# port, I assume userData would be implemented to be of type object which is already type-safe.

Edit: After seeing this post of yours, and having a look at Box2CS, I now understand! No, I don't think there would be any issues created by this patch for Box2CS; all the getuserdata and setuserdata functions in Box2C.cpp work with void* consistently, which any_ptr should correctly convert to/from.

Best Regards,
Francis Xavier
Alright, cool.

-P

Original thread: https://box2d.org/forum/viewtopic.php?t=5930