• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Diventurer

Circular Dependency with Pimpls

20 posts in this topic

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.

0

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.

2

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
0

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

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.

0

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.

0

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.

0

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

0

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

0

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. 

0

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

1

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.

0

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.

0

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.

0

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

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
-1

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
0

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.

0

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
1

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.

0

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
-1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0