Circular Dependency with Pimpls

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

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.

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.

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?

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.

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.

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.

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.

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. :)

Ok, go for it :)

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

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.

This topic is closed to new replies.

Advertisement