Controller Const correctness

The Controller system is missing some const declarations on its methods.

I added const to methods that seemed to need it.

In particular was b2ControllerDef::Create and parameter to b2World::CreateController so that constant b2ControllerDef's can be used to create Controllers.

Following is the diff with the relevant changes from box2d revision 214


Index: Source/Dynamics/b2World.cpp
===================================================================
--- Source/Dynamics/b2World.cpp (revision 214)
+++ Source/Dynamics/b2World.cpp (working copy)
@@ -325,7 +325,7 @@
}
}

-b2Controller* b2World::CreateController(b2ControllerDef* def)
+b2Controller* b2World::CreateController( const b2ControllerDef* def)
{
b2Controller* controller = def->Create(&m_blockAllocator);

@@ -1225,4 +1225,4 @@
}

return lambda;
-}
\ No newline at end of file
+}
Index: Source/Dynamics/b2World.h
===================================================================
--- Source/Dynamics/b2World.h (revision 214)
+++ Source/Dynamics/b2World.h (working copy)
@@ -100,7 +100,7 @@
void DestroyJoint(b2Joint* joint);

/// Add a controller to the world.
- b2Controller* CreateController(b2ControllerDef* def);
+ b2Controller* CreateController( const b2ControllerDef* def);

/// Removes a controller from the world.
void DestroyController(b2Controller* controller);
Index: Source/Dynamics/Controllers/b2ConstantAccelController.h
===================================================================
--- Source/Dynamics/Controllers/b2ConstantAccelController.h (revision 214)
+++ Source/Dynamics/Controllers/b2ConstantAccelController.h (working copy)
@@ -48,7 +48,7 @@
/// The force to apply
b2Vec2 A;
private:
- b2ConstantAccelController* Create(b2BlockAllocator* allocator);
+ b2Controller* Create(b2BlockAllocator* allocator) const;
};

#endif
Index: Source/Dynamics/Controllers/b2ConstantForceController.cpp
===================================================================
--- Source/Dynamics/Controllers/b2ConstantForceController.cpp (revision 214)
+++ Source/Dynamics/Controllers/b2ConstantForceController.cpp (working copy)
@@ -40,8 +40,8 @@
}


-b2ConstantForceController* b2ConstantForceControllerDef::Create(b2BlockAllocator* allocator)
+b2Controller* b2ConstantForceControllerDef::Create(b2BlockAllocator* allocator) const
{
void* mem = allocator->Allocate(sizeof(b2ConstantForceController));
return new (mem) b2ConstantForceController(this);
-}
\ No newline at end of file
+}
Index: Source/Dynamics/Controllers/b2BuoyancyController.cpp
===================================================================
--- Source/Dynamics/Controllers/b2BuoyancyController.cpp (revision 214)
+++ Source/Dynamics/Controllers/b2BuoyancyController.cpp (working copy)
@@ -112,8 +112,8 @@
allocator->Free(this, sizeof(b2BuoyancyController));
}

-b2BuoyancyController* b2BuoyancyControllerDef::Create(b2BlockAllocator* allocator)
+b2Controller* b2BuoyancyControllerDef::Create(b2BlockAllocator* allocator) const
{
void* mem = allocator->Allocate(sizeof(b2BuoyancyController));
return new (mem) b2BuoyancyController(this);
-}
\ No newline at end of file
+}
Index: Source/Dynamics/Controllers/b2GravityController.h
===================================================================
--- Source/Dynamics/Controllers/b2GravityController.h (revision 214)
+++ Source/Dynamics/Controllers/b2GravityController.h (working copy)
@@ -53,7 +53,7 @@
/// If true, gravity is proportional to r^-2, otherwise r^-1
bool invSqr;
private:
- b2GravityController* Create(b2BlockAllocator* allocator);
+ b2Controller* Create(b2BlockAllocator* allocator) const;
};

#endif
Index: Source/Dynamics/Controllers/b2TensorDampingController.cpp
===================================================================
--- Source/Dynamics/Controllers/b2TensorDampingController.cpp (revision 214)
+++ Source/Dynamics/Controllers/b2TensorDampingController.cpp (working copy)
@@ -65,8 +65,8 @@
}


-b2TensorDampingController* b2TensorDampingControllerDef::Create(b2BlockAllocator* allocator)
+b2Controller* b2TensorDampingControllerDef::Create(b2BlockAllocator* allocator) const
{
void* mem = allocator->Allocate(sizeof(b2TensorDampingController));
return new (mem) b2TensorDampingController(this);
-}
\ No newline at end of file
+}
Index: Source/Dynamics/Controllers/b2ConstantForceController.h
===================================================================
--- Source/Dynamics/Controllers/b2ConstantForceController.h (revision 214)
+++ Source/Dynamics/Controllers/b2ConstantForceController.h (working copy)
@@ -48,7 +48,7 @@
/// The force to apply
b2Vec2 F;
private:
- b2ConstantForceController* Create(b2BlockAllocator* allocator);
+ b2Controller* Create(b2BlockAllocator* allocator) const;
};

#endif
Index: Source/Dynamics/Controllers/b2BuoyancyController.h
===================================================================
--- Source/Dynamics/Controllers/b2BuoyancyController.h (revision 214)
+++ Source/Dynamics/Controllers/b2BuoyancyController.h (working copy)
@@ -96,7 +96,7 @@
}

private:
- b2BuoyancyController* Create(b2BlockAllocator* allocator);
+ b2Controller* Create(b2BlockAllocator* allocator) const;
};

#endif
Index: Source/Dynamics/Controllers/b2TensorDampingController.h
===================================================================
--- Source/Dynamics/Controllers/b2TensorDampingController.h (revision 214)
+++ Source/Dynamics/Controllers/b2TensorDampingController.h (working copy)
@@ -63,7 +63,7 @@
/// Sets damping independantly along the x and y axes
void SetAxisAligned(float32 xDamping,float32 yDamping);
private:
- b2TensorDampingController* Create(b2BlockAllocator* allocator);
+ b2Controller* Create(b2BlockAllocator* allocator) const;
};

#endif
Index: Source/Dynamics/Controllers/b2ConstantAccelController.cpp
===================================================================
--- Source/Dynamics/Controllers/b2ConstantAccelController.cpp (revision 214)
+++ Source/Dynamics/Controllers/b2ConstantAccelController.cpp (working copy)
@@ -39,8 +39,8 @@
}


-b2ConstantAccelController* b2ConstantAccelControllerDef::Create(b2BlockAllocator* allocator)
+b2Controller* b2ConstantAccelControllerDef::Create(b2BlockAllocator* allocator) const
{
void* mem = allocator->Allocate(sizeof(b2ConstantAccelController));
return new (mem) b2ConstantAccelController(this);
-}
\ No newline at end of file
+}
Index: Source/Dynamics/Controllers/b2Controller.h
===================================================================
--- Source/Dynamics/Controllers/b2Controller.h (revision 214)
+++ Source/Dynamics/Controllers/b2Controller.h (working copy)
@@ -64,13 +64,13 @@
void Clear();

/// Get the next controller in the world's body list.
- b2Controller* GetNext();
+ b2Controller* GetNext() const;

/// Get the parent world of this body.
- b2World* GetWorld();
+ b2World* GetWorld() const;

/// Get the attached body list
- b2ControllerEdge* GetBodyList();
+ b2ControllerEdge* GetBodyList() const;


protected:
@@ -107,20 +107,20 @@

private:
friend class b2World;
- virtual b2Controller* Create(b2BlockAllocator* allocator) = 0;
+ virtual b2Controller* Create(b2BlockAllocator* allocator) const = 0;
};

-inline b2Controller* b2Controller::GetNext()
+inline b2Controller* b2Controller::GetNext() const
{
return m_next;
}

-inline b2World* b2Controller::GetWorld()
+inline b2World* b2Controller::GetWorld() const
{
return m_world;
}

-inline b2ControllerEdge* b2Controller::GetBodyList()
+inline b2ControllerEdge* b2Controller::GetBodyList() const
{
return m_bodyList;
}
Index: Source/Dynamics/Controllers/b2GravityController.cpp
===================================================================
--- Source/Dynamics/Controllers/b2GravityController.cpp (revision 214)
+++ Source/Dynamics/Controllers/b2GravityController.cpp (working copy)
@@ -63,8 +63,8 @@
allocator->Free(this, sizeof(b2GravityController));
}

-b2GravityController* b2GravityControllerDef::Create(b2BlockAllocator* allocator)
+b2Controller* b2GravityControllerDef::Create(b2BlockAllocator* allocator) const
{
void* mem = allocator->Allocate(sizeof(b2GravityController));
return new (mem) b2GravityController(this);
-}
\ No newline at end of file
+}
In my mind a few of those const casts are dangerous. It shouldn't be possible to get from a const b2World* to a non-const reference to anything contained within. But you can eliminate constness using:
m_world->GetControllerList()->GetWorld();
Similarly GetNext etc.

Your patch as reminded me to commit a few similar const correctness issues. I suggest you double up some of the methods to const/non-const pairs, such as I've done for b2Body.

Also, minor complaint: Why did you remove the co-variant Create return types? Was your compiler complaining?
Sorry about goofing up the return types.

I see your point with the implicit const cast. I really should have doubled up the accessors.

My goal for the change was to allow me use use a const b2ControllerDef in b2World::CreateController. This would make controller system more cohesive with the joint and body systems, they already allow constant definitions.

following is a diff with your suggestions implemented.

-nick

Attachment:
File comment: svn diff of changes needed to allow constant definitions in controller creation
controller_def_const.diff.txt [8.21 KiB]
Downloaded 22 times
Ok, committed. And yeah, no question that we should take const definitions.

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