b2Body::CreateShape should take a const b2ShapeDef*

When edge shapes where added to Box2D the signature of b2Body::CreateShape change. Its parameter used to a const b2ShapeDef*, now it has lost the const. I don't know why this was, and the change broke a lot of my code using Box2D, the const change was quite traumatic.

I think it should be reverted back. I've included a patch that shows my few changes (3 lines).

On a side note: When I first attempted the change, I just change the signature back to const and compile. The thought was that I'd then jump in and change whatever else was needed based on compiler warnings and errors. Surprisingly, box2d compiled and ran fine. The C-Cast used to convert the ShapeDef* to a b2EdgeChainDef* is a bit over zealous and casts away the const as well. Just one more reason to prefer C++-Casts over C-Casts.


The diff:
Code:
Index: Source/Dynamics/b2Body.cpp
===================================================================
--- Source/Dynamics/b2Body.cpp   (revision 190)
+++ Source/Dynamics/b2Body.cpp   (working copy)
@@ -128,7 +128,7 @@
   return angle2;
}

-b2Shape* b2Body::CreateShape(b2ShapeDef* def)
+b2Shape* b2Body::CreateShape( const b2ShapeDef* def)
{
   b2Assert(m_world->m_lock == false);
   if (m_world->m_lock == true)
@@ -141,7 +141,7 @@
   //       return more than one shape to add to parent body... maybe it should add
   //       shapes directly to the body instead of returning them?)
   if (def->type == e_edgeShape) {
-      b2EdgeChainDef* edgeDef = (b2EdgeChainDef*)def;
+      const b2EdgeChainDef* edgeDef = static_cast<const b2EdgeChainDef*>(def);
      b2Vec2 v1;
      b2Vec2 v2;
      int i;
Index: Source/Dynamics/b2Body.h
===================================================================
--- Source/Dynamics/b2Body.h   (revision 190)
+++ Source/Dynamics/b2Body.h   (working copy)
@@ -104,7 +104,7 @@
   /// Creates a shape and attach it to this body.
   /// @param shapeDef the shape definition.
   /// @warning This function is locked during callbacks.
-   b2Shape* CreateShape(b2ShapeDef* shapeDef);
+   b2Shape* CreateShape( const b2ShapeDef* shapeDef);

   /// Destroy a shape. This removes the shape from the broad-phase and
   /// therefore destroys any contacts associated with this shape. All shapes
Ok, cool. Though looking over the SVN, CreateShape never originally took a const. I can't see any problems with changing it though, clearly it should be const.

I'm less keen on committing a static_cast though. Again, I know they are good practise, but they don't appear in the codebase for whatever reason, so I'd like to get Erin's say so first. I suppose a lot of C++ features have been avoid for portability, either because other platforms may not support the feature, or (as in this case), it increases the level of C++ expertise required to port.
Having it const seems ok. Box2D uses C-style casts, mainly because dynamic_cast implies RTTI, which is generally avoided by game programmers.
While dynamic_cast does require RTTI, const_cast (add/remove const), static_cast (between types known to be related at compile time), and reinterpret_cast (closest to C-style) have no overhead whatsoever. Those are all very useful, if a bit verbose in syntax, and all have their uses. I have never used dynamic_cast since any case involving that can usually be solved with an enum and switch statement, a virtual function, a function pointer, or a delegate (my personal favorite 🙂).
I've commited the constness. Sorry for the wait.

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