Circular Dependency with Pimpls

Started by
19 comments, last by kunos 9 years, 11 months ago

Oh, I didn't think about it like that. Sure, that actually seems like a pretty good solution.

well its pretty much a standard when it comes to abstract different API in C++.. I didnt come up with it, it's used pretty much everywhere. DirectX/COM is doing exactly that abstracting the driver part (well sort of anyway).. OpenGL is using the other "C" based approach that I also use in my project, and I tend to prefer because it seems to map to less boilerplate.. it looks something like this.. very OpenGL-like (ODE also use this approach to expose it's C++ to the rest of the world through C):

You have a .h with your functions:

int CreatePhysicsEngine();

unsigned int CreateRigidBody();

void RigidBodyAddForce(unsigned int rigidBody,blablalblal);

The hacky part here is to translate that unsigned int to an instance of a class.. it could be a brutal static cast.. or a even more brutal C style cast.. so the implementation code.. in something like BulletPhysicsEngine.cpp would look like:

BulletPhysicsEngine* theEngine=nullptr;

int CreatePhysicsEngine()

{

theEngine=new BulletPhysicsEngine(); // This will only allow 1 physics engine per application.. YMMV

}

unsigned int CreateRigidBody()

{

// Same stuff

return (unsigned int) new BulletRigidBody(theEngine);

}

// Now the hacky part

void AddForce(unsigned int rigidBody,blalblblla)

{

auto bullet_rigid_body=(BulletRigidBody*)rigidBody;

bullet_rigid_body->addForce();

}

This is as hacky as a void* I have to admit.. but it does come with the added bonus the the code requires much less boilerplate and it is easily consumable from other languages because of the C interface... and perhaps because of its C nature it doesnt feel that bad to use C brutal casts tongue.png.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

This topic is closed to new replies.

Advertisement