behdad / box2d

Automatically exported from code.google.com/p/box2d
2 stars 12 forks source link

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

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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/

Best Regards,
Francis Xavier

Original issue reported on code.google.com by FrancisX...@gmail.com on 21 Oct 2010 at 12:33

GoogleCodeExporter commented 9 years ago
Seeing as any_ptr is both bigger and slower than a void*, I would perfer not to 
do that.

You could of course choose to always make your void* always point to an 
any_ptr, and get type-safety that way yourself.

Alternatively, there could be a typedef which allows you to change the type of 
the user data. I believe it is never used in the library.

Original comment by azupodca...@gmail.com on 25 Oct 2010 at 9:54

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
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

Best Regards,
Francis Xavier

Original comment by FrancisX...@gmail.com on 26 Oct 2010 at 3:37

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
In fact, being concerned over the performance of any_ptr compared to void* is 
akin to being concerned over the performance of std::vector compared to normal 
C++ arrays.

Best Regards,
Francis Xavier

Original comment by FrancisX...@gmail.com on 26 Oct 2010 at 3:54

GoogleCodeExporter commented 9 years ago
This is a neat idea.

Original comment by erinca...@gmail.com on 7 Nov 2010 at 12:30

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Just wanted to point people reading this thread to here:
http://box2d.org/forum/viewtopic.php?f=4&t=5930

I've made a small patch file which contains the modifications required for 
b2Joint, b2Body and b2Fixture to hold an any_ptr instead of void* to user data. 
It can be downloaded from the thread link mentioned above.

Original comment by FrancisX...@gmail.com on 3 Jan 2011 at 9:57

GoogleCodeExporter commented 9 years ago
I think a better solution would be to add a typedef to b2Config.h for the type 
of the user data. That way it can be kept as void* by default but can be easily 
changed by users if they want a safer option. I'll make a patch for this soon.

Original comment by zzymyn on 8 Mar 2011 at 5:20

GoogleCodeExporter commented 9 years ago
While this is an interesting idea, ultimately I think it unnecessary. You 
should store the same user data type in all bodies. You can use a different 
type for fixtures and joints. For example:
struct MyBodyData;
struct MyFixtureData;
struct MyJointData;

This will make working with Box2D easier and uncomplicated.

Original comment by erinca...@gmail.com on 25 Mar 2011 at 6:58

GoogleCodeExporter commented 9 years ago

Original comment by erinca...@gmail.com on 25 Mar 2011 at 6:59