• Advertisement
Sign in to follow this  

Circular Dependency with Pimpls

This topic is 1353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently writing a wrapper, and I am using pimpl to be able to use the same interface for multiple libraries. I'm not quite sure if I'm approaching this in a good way however. Is it worth using pimpl for things that aren't platform specific? What I'm writing now is a Bullet Physics library wrapper. I have made a RigidBody class and a World class. These classes then have their respective "btRigidBody" and "btDiscreteDynamicsWorld" pointers. My goal is to make my API consistent and not need to use any Bullet code in my client code.

 

Now, to my problem:

The World class needs to access RigidBody's btRigidBody pointer. And the RigidBody needs World's btDiscreteDynamicsWorld pointer.

How do I do this without having any Bullet-specific includes in the header files?

 

I have come up with an idea, but I feel that it is relatively ugly. My idea is that I have a "void* getRigidBody()" and "void* getWorld()", and then cast them to either btRigidBody* or btDiscreteDynamicsWorld*. 

 

I could just go ahead and implement that idea, but it feels like I'm thinking about this in a wrong way.

Does anyone have any suggestions?

 

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement

If you are returning a pointer or reference, you only need to pre-declare the classes, you don't need to #include their headers.

 

Example:

/* #include <some_bullet_library>     <--- Not needed for pre-declarations */
 
class btRigidBody; //Pre-declaration.
 
class RigidBody
{
    public:
    btRigidBody &GetRigidBody();
    
    private:
    struct Impl;
    Pimpl<Impl> pImpl;
};

 

Though, it sounds like btRigidBody and btDiscreteDynamicsWorld are mutually dependant on each other. One can know about the other, but it's usually a code-smell if they both know about each other. If those are Bullet's classes, then I guess that can't be helped on your side of it.

Share this post


Link to post
Share on other sites

If you are returning a pointer or reference, you only need to pre-declare the classes, you don't need to #include their headers.

 

Example:

/* #include <some_bullet_library>     <--- Not needed for pre-declarations */
 
class btRigidBody; //Pre-declaration.
 
class RigidBody
{
    public:
    btRigidBody &GetRigidBody();
    
    private:
    struct Impl;
    Pimpl<Impl> pImpl;
};

Though, it sounds like btRigidBody and btDiscreteDynamicsWorld are mutually dependant on each other. One can know about the other, but it's usually a code-smell if they both know about each other. If those are Bullet's classes, then I guess that can't be helped on your side of it.

 

Hmm, well. That might actually be a better solution. Thanks.

Regarding the circular dependency, I think there is a way around just that.

I'll probably end up forward-declaring the Bullet classes though. Even though it might get a bit messy.

 

Edit:

But then again, with a "void*", I'd only need to have one function, instead of "getBulletRigidBody".

The void pointer would only be checked in the implementation anyway, so it might not be that bad.

 

Does anyone think I should do a complete redesign though?

Am I thinking way too, err, narrow, about this?

Edited by diventurer

Share this post


Link to post
Share on other sites

But then again, with a "void*", I'd only need to have one function, instead of "getBulletRigidBody".

I'm not sure I understand. Are you saying that you want your World class and your RigidBody class to be identical?
 

The void pointer would only be checked in the implementation anyway, so it might not be that bad.

If it's entirely an implementation detail, you could have:
btRigidBody* getBulletRigidBody();
btDiscreteDynamicsWorld* getBulletWorld();
 

Does anyone think I should do a complete redesign though?

If 'btRigidBody' and 'btDiscreteDynamicsWorld' are your classes, you might consider and think about why they need to talk to each other, instead of just one talking to the other. If it's not too much hassle to change, and you come up with a better solution, then you might want to change it.
But if they aren't your classes, then there's nothing you can do about that, so your code is fine.

Am I thinking way too, err, narrow, about this?

I don't think so. void* is a code smell, and circular dependency that causes overly tight coupling is also considered a code smell. 'Code smell' doesn't mean it's wrong, it just means non-ideal and something to be alert about. Sometimes less-than-ideal solutions are preferred over investing too much time in coming up with the "perfect" solution. But they usually warrant a quick look around to see if a better solution is easily achievable.

To me, "better solution" in this case is just returning a reference to the actual class and not a void pointer.
And if the bt_____ classes are from a third-party library, then you have to work with/around their less-than-ideal interface - which is normal. So I wouldn't bother too much about the circular dependency issue, unless it starts affecting your higher-level interfaces in an undesirable way.

Share this post


Link to post
Share on other sites

I think it is a bit of a code smell...

 

To me, it seems a bit pointless to just wrap the API as is.

 

If you just copy the api straight off, you don't really gain anything, you are still just as dependent on bullet.

 

All you do is create more code to maintain and more sources for errors.

 

If you can find a good abstraction, that is more higher level and more suited to the rest of your design, you should implement it, if not, you should just use bullet as is I think...

 

Any dependencies in the API should only be on other classes in the api.

 

Any interdependencies in bullet should not "leak through", all those can be hidden within the implementation.

 

If it can't, then there is a problem with the api.

Share this post


Link to post
Share on other sites

 

But then again, with a "void*", I'd only need to have one function, instead of "getBulletRigidBody".

I'm not sure I understand. Are you saying that you want your World class and your RigidBody class to be identical?
 

The void pointer would only be checked in the implementation anyway, so it might not be that bad.

If it's entirely an implementation detail, you could have:
btRigidBody* getBulletRigidBody();
btDiscreteDynamicsWorld* getBulletWorld();
 

Does anyone think I should do a complete redesign though?

If 'btRigidBody' and 'btDiscreteDynamicsWorld' are your classes, you might consider and think about why they need to talk to each other, instead of just one talking to the other. If it's not too much hassle to change, and you come up with a better solution, then you might want to change it.
But if they aren't your classes, then there's nothing you can do about that, so your code is fine.

Am I thinking way too, err, narrow, about this?

I don't think so. void* is a code smell, and circular dependency that causes overly tight coupling is also considered a code smell. 'Code smell' doesn't mean it's wrong, it just means non-ideal and something to be alert about. Sometimes less-than-ideal solutions are preferred over investing too much time in coming up with the "perfect" solution. But they usually warrant a quick look around to see if a better solution is easily achievable.

To me, "better solution" in this case is just returning a reference to the actual class and not a void pointer.
And if the bt_____ classes are from a third-party library, then you have to work with/around their less-than-ideal interface - which is normal. So I wouldn't bother too much about the circular dependency issue, unless it starts affecting your higher-level interfaces in an undesirable way.

 

Alright, the bt classes are from Bullet. 

I think I'm ready to decide, and I'll go with a void pointer. 

 

The reason I do not want a "getBulletRigidBody" function for example,  is that it affects the API.

With a void pointer, I can easily cast to whichever class is needed. It is only in the implementation on the library side, so I think this won't be a big issue.

 

Although, I'm still open for new ideas. But I feel this is the way to go for now.

Share this post


Link to post
Share on other sites

Another reason for just using bullet as is without complicating things, is that bullet itself compiles on pretty much anything 32 bit with floating point, so its not really needed to abstract from it for platform reasons.

Share this post


Link to post
Share on other sites

I think it is a bit of a code smell...

 

To me, it seems a bit pointless to just wrap the API as is.

 

If you just copy the api straight off, you don't really gain anything, you are still just as dependent on bullet.

 

All you do is create more code to maintain and more sources for errors.

 

If you can find a good abstraction, that is more higher level and more suited to the rest of your design, you should implement it, if not, you should just use bullet as is I think...

 

Any dependencies in the API should only be on other classes in the api.

 

Any interdependencies in bullet should not "leak through", all those can be hidden within the implementation.

 

If it can't, then there is a problem with the api.

 

Yeah, I'm making Bullet more higher level. So that it is easy to use physics in a new project.

Another reason is that I want consistency in the library. For example, Bullet has its btVector3, and GLM has its glm::vec3, and then Assimp has its aiVector3D. It just gets messy having lots of different types of classes.

 

Also, regarding the fact there is more code to maintain; If I do it like this, I won't need to change any client-side code if I change a library. As my library can just wrap it in the implementation.

 

I am aware that I am most likely not creating reusable code, but it's worth trying. That's how you get better, I guess. :)

Share this post


Link to post
Share on other sites

Ok, go for it :)

 

Just wanted to warn a bit for overenginering it, its easy to get caught up in creating api:s :)

Share this post


Link to post
Share on other sites

Ok, go for it smile.png

 

Just wanted to warn a bit for overenginering it, its easy to get caught up in creating api:s smile.png

Yeah, it really is. 

Share this post


Link to post
Share on other sites

Rather than just crowbarring it with a void, something you can do is:
 

class PhysicsBody
{
public:
    ~virtual PhysicsBody(){ }
};

// inside the Bullet specific Body

class BulletPhysicsBody : public PhysicsBody
{
public:
    btRigidBody *getBulletBody() const;
};

// inside the Bullet specific World

void handleBody(PhysicsBody *body)
{
#ifdef DEBUG
    BulletPhysicsBody *b = dynamic_cast<BulletPhysicsBody*>(body);
    if(!b) throw SomeKindOfAssertException();
#else
    BulletPhysicsBody *b = static_cast<BulletPhysicsBody*>(body);
#endif
    b->getBulletBody();
}

Static cast is pretty much free, but this gives you the option to check the cast in a debug build for example.

 

Just a thought. Creates even more interfaces to maintain of course :)

Share this post


Link to post
Share on other sites

Rather than just crowbarring it with a void, something you can do is:
 

class PhysicsBody
{
public:
    ~virtual PhysicsBody(){ }
};

// inside the Bullet specific Body

class BulletPhysicsBody : public PhysicsBody
{
public:
    btRigidBody *getBulletBody() const;
};

// inside the Bullet specific World

void handleBody(PhysicsBody *body)
{
#ifdef DEBUG
    BulletPhysicsBody *b = dynamic_cast<BulletPhysicsBody*>(body);
    if(!b) throw SomeKindOfAssertException();
#else
    BulletPhysicsBody *b = static_cast<BulletPhysicsBody*>(body);
#endif
    b->getBulletBody();
}

Static cast is pretty much free, but this gives you the option to check the cast in a debug build for example.

 

Just a thought. Creates even more interfaces to maintain of course smile.png

Well, that's what I want to avoid. I don't want "Bullet" in the interface.

It could be a good solution, but personally I feel it is easier to change the implementation if the interface is consistent.

Share this post


Link to post
Share on other sites


Well, that's what I want to avoid. I don't want "Bullet" in the interface.
It could be a good solution, but personally I feel it is easier to change the implementation if the interface is consistent.

 

You're missing my point. The "Bullet" stuff would only be inside the Bullet implementation of your interface. PhysicsBody is the only class the API would expose to the rest of the program.

Share this post


Link to post
Share on other sites

 


Well, that's what I want to avoid. I don't want "Bullet" in the interface.
It could be a good solution, but personally I feel it is easier to change the implementation if the interface is consistent.

 

You're missing my point. The "Bullet" stuff would only be inside the Bullet implementation of your interface. PhysicsBody is the only class the API would expose to the rest of the program.

Wait, but I don't see how I can avoid calling "BulletRigidBody" constructor in the client code?

The BulletRigidBody would be inherited from RigidBody, so if I want to cast it to one, I'd need to create the derived class. 

Further explanation would be nice.

Share this post


Link to post
Share on other sites
Your PhysicsWorld interface should create the RigidBody instances via a virtual method. Then the Bullet implementation of PhysicsWorld privately creates BulletRigidBody instances and returns them as RigidBody pointers.

BulletPhysicsWorld can later privately retrieve BulletRigidBody from a RigidBody using a static or dynamic cast.

Share this post


Link to post
Share on other sites

Well, it seems like this would become a huge mess.

I'll stick with void pointers for now, and see what happens.

 

Thanks for your posts however.

 

Edit:

Lol, just because I said I will see how it goes with void pointers, I get downvoted?

I mean, I get that they're hated, but seriously?

Edited by diventurer

Share this post


Link to post
Share on other sites

The way I do it, and they way I think most people abstract API is through either a pure abstract base class.. ie:

 

class IPhysicsEngine

{

public:

virtual IRigidBody* createRigidBody()=0;

// and so on

};

 

class IRigidBody

{

public:

virtual void addForce(blabla)=0;

// And so on

};

 

This is all that is visible to the client code, the application never knows what's behind those pure interfaces.

While the concrete (either in a DLL or in a static lib, or directly include in the project) implementation that looks like:

 

class BulletPhysicsEngine : public IPhysicsEngine

{

public:

IRigidBody* createRigidBody()

{

return new BulletRigidBody(this);

}

};

 

class BulletRigidBody : public IRigidBody

{

public:

void addForce(bla bla)

{

// Call bullet and do your stuff

}

};

 

Or through a C interface, for maximum "projectability" to different languages if that is a requirement.

 

Honestly, in 2014, seeing somebody thinking he has got a "solution" and that "solution" involves the use of a void* makes me want to puke some green yellow nasty stuff.

Edited by kunos

Share this post


Link to post
Share on other sites

The way I do it, and they way I think most people abstract API is through either a pure abstract base class.. ie:

 

class IPhysicsEngine

{

public:

virtual IRigidBody* createRigidBody()=0;

// and so on

};

 

class IRigidBody

{

public:

virtual void addForce(blabla)=0;

// And so on

};

 

This is all that is visible to the client code, the application never knows what's behind those pure interfaces.

While the concrete (either in a DLL or in a static lib, or directly include in the project) implementation that looks like:

 

class BulletPhysicsEngine : public IPhysicsEngine

{

public:

IRigidBody* createRigidBody()

{

return new BulletRigidBody(this);

}

};

 

class BulletRigidBody : public IRigidBody

{

public:

void addForce(bla bla)

{

// Call bullet and do your stuff

}

};

 

Or through a C interface, for maximum "projectability" to different languages if that is a requirement.

 

Honestly, in 2014, seeing somebody thinking he has got a "solution" and that "solution" involves the use of a void* makes me want to puke some green yellow nasty stuff.

 

I do not see how I can avoid calling "BulletPhysicsEngine::createRigidBody()". 

Could you please give an example of client code as well? Thanks.

 

Also, I did not say that void pointers was a "solution". I said I'll see how it goes. I haven't even continued with it yet, because I'm still thinking about it.

Share this post


Link to post
Share on other sites

The client code will look something like this:

 

Somewhere you'll have to decide which concrete implementation to instantiate.. but that's going to be only one line in your code:

 

IPhysicsEngine* pengine=new BulletPhysicsEngine();

 

This is going to be the only API dependent call in the client code.. the rest is done through interface.. you have your IPhysicsEngine*, all your rigid bodies will be IRigidBody*:

 

IRigidBody* body=pengine->createRigidBody();

 

body->setMass(whateva);

body->addForce(whateva);

 

IRigidBody* body2=pengine->createRigidBody();

 

Similarly:

 

ISliderJoint* slider1=pengine->createSliderJoint();

 

slider1->attach(body,body2);

 

If one day you decide Bullet is not cutting it and you want to swap it over with something else.. you implement your concrete classes and change one line in the client code:

 

IPhysicsEngine* pengine=new HavokPhysicsEngine();

 

everything else stays the same.

Been there, done that.. it works, simple and clean.

Edited by kunos

Share this post


Link to post
Share on other sites

The client code will look something like this:

 

Somewhere you'll have to decide which concrete implementation to instantiate.. but that's going to be only one line in your code:

 

IPhysicsEngine* pengine=new BulletPhysicsEngine();

 

This is going to be the only API dependent call in the client code.. the rest is done through interface.. you have your IPhysicsEngine*, all your rigid bodies will be IRigidBody*:

 

IRigidBody* body=pengine->createRigidBody();

 

body->setMass(whateva);

body->addForce(whateva);

 

IRigidBody* body2=pengine->createRigidBody();

 

Similarly:

 

ISliderJoint* slider1=pengine->createSliderJoint();

 

slider1->attach(body,body2);

 

If one day you decide Bullet is not cutting it and you want to swap it over with something else.. you implement your concrete classes and change one line in the client code:

 

IPhysicsEngine* pengine=new HavokPhysicsEngine();

 

everything else stays the same.

Been there, done that.. it works, simple and clean.

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

+1'd you even though I'm a bit upset you -1'd me just because I didn't quite get the client side part, and that I mentioned I'd see how void pointers would go.

Either way, thanks again.

Share this post


Link to post
Share on other sites

 

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.

Edited by kunos

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement