I'm using a physics library called Bullet and I'm having trouble preventing including the Bullet headers into my Physics header.
//Physics.h
#include <BulletPhysics/btBulletDynamicsCommon.h> // Contains lots of other headers
struct Physics
{
static btDiscreteDynamicsWorld* pDynamicsWorld;
static btAlignedObjectArray<btCollisionShape*> collisionShapes;
static vec3 findSomePoint();
};
Now in various other parts of my code I may want to access the Physics struct, and to do this I need to include the Physics header, but that will also include all the library headers in any other CPP file.
I'm trying to figure a way to have the library headers only included in Physics.cpp, but I'm having trouble getting the compiler to recognise the library types in the struct definition if I remove the header from Physics.h.
With some of the members I can just forward declare and that works fine, but it's not going to work for non pointer or reference types as the full definition is needed.
I noticed that if I use a namespace I can get away with declaring a member as extern, and the full type definition isn't needed. But it results in weird things with the forward declarations:
struct btDiscreteDynamicsWorld; // This seems fine
template <typename T>
struct btAlignedObjectArray; // This works but seems really hacky and I'm not sure if it's even valid
namespace Physics
{
extern btDiscreteDynamicsWorld* pDynamicsWorld;
extern btAlignedObjectArray<btCollisionShape*> collisionShapes; // Complete type not needed
vec3 findSomePoint();
}
Also using a namespace I lose the ability to use access specifiers.
I also thought instead of having the members themselves in the struct I could use getter functions and return references, but the return types also need at least a forward declaration (not complete type I think), and I would still have to forward declare that template type the way I did earlier.
Which is the best way to do this? Or am I placing too much emphasis on preventing the copying of extra headers. That one header contains many many other headers, so it is quite a lot, so I'm not sure if I should care or not.