View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Circular Dependency with Pimpls

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

20 replies to this topic

#1irbaboon  Members

Posted 06 May 2014 - 10:43 AM

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*.

Does anyone have any suggestions?

#2Servant of the Lord  Members

Posted 06 May 2014 - 10:52 AM

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.

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

#3irbaboon  Members

Posted 06 May 2014 - 10:57 AM

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?

Edited by diventurer, 06 May 2014 - 11:00 AM.

#4Servant of the Lord  Members

Posted 06 May 2014 - 11:35 AM

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.

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.
It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

#5Olof Hedman  Members

Posted 06 May 2014 - 12:08 PM

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.

#6irbaboon  Members

Posted 06 May 2014 - 12:08 PM

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.

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.

#7Olof Hedman  Members

Posted 06 May 2014 - 12:13 PM

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.

#8irbaboon  Members

Posted 06 May 2014 - 12:15 PM

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.

#9Olof Hedman  Members

Posted 06 May 2014 - 12:20 PM

Ok, go for it

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

#10irbaboon  Members

Posted 06 May 2014 - 12:24 PM

Ok, go for it

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

Yeah, it really is.

#11Aardvajk  Members

Posted 07 May 2014 - 01:01 AM

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

#12irbaboon  Members

Posted 07 May 2014 - 01:11 AM

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

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.

#13Aardvajk  Members

Posted 07 May 2014 - 03:27 AM

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.

#14irbaboon  Members

Posted 07 May 2014 - 03:52 AM

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.

#15Aardvajk  Members

Posted 07 May 2014 - 05:23 AM

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.

#16irbaboon  Members

Posted 07 May 2014 - 05:32 AM

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

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

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, 07 May 2014 - 10:57 AM.

#17kunos  Members

Posted 07 May 2014 - 10:47 AM

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:

// 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:

{

// 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, 07 May 2014 - 10:49 AM.

#18irbaboon  Members

Posted 07 May 2014 - 11:03 AM

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:

// 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:

{

// 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.

#19kunos  Members

Posted 07 May 2014 - 11:10 AM

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);

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, 07 May 2014 - 11:11 AM.

#20irbaboon  Members

Posted 07 May 2014 - 11:14 AM

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);

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.