Sign in to follow this  
Alundra

Safe problem in scene Manager about root array + linear array

Recommended Posts

Alundra    2316

Hi,

I have a root actor array which is used to update recursively and load/save, the linear array is used to find actor and for the rendering.

To manage these two arrays the scene manager has two functions :

void AddActorInRoot( IActor* Actor );
void RemoveActorFromRoot( IActor* Actor );

These two functions only add in the root array or remove from the root array.

It's called by SetParent of the actor, if nullptr it's added in root array otherwise removed from root array.

The problem is since these two function are public, it's not safe, user of the API can call these functions everywhere.

Any idea to avoid this safe problem ?

Thanks

Edited by Alundra

Share this post


Link to post
Share on other sites
Wyrframe    2426

You could solve it using a change-if-needed paradigm.

 

SceneManager::AddActorInRoot(IActor &actor) checks if the actor is already a member of the root list. If it is not, then it adds it to the list and calls IActor::SetParent(this).

 

SceneManager::RemoveActorFromRoot(IActor &actor) checks if the actor is a member of the root list. If it is not, then we do nothing. Otherwise, we remove it from the list and call IActor::SetParent(null).

 

Actor::SetParent(SceneManager* new_parent) first checks if the new parent is the same as its current parent. If it is, we return without doing anything. If its current_parent is not null, we call current_parent->RemoveActorFromRoot(*this), then sets current_parent to the new_parent. Then if the new parent is not null, call new_parent->AddActorToRoot(*this).

 

 

The loop is not infinite because we recurse only if the requested final state is not the current state. We can call from either end (SetParent or AddActorInRoot/RemoveActorFromRoot) first because either path ends up calling the other if necessary. And as a Java programmer looking back on my C++ days, I strongly suggest using a reference parameter instead of a pointer wherever taking a null pointer as an argument would be 100% unacceptable.

Edited by Wyrframe

Share this post


Link to post
Share on other sites
Alundra    2316

I posted there because scene manager is part of the rendering code, the category is called : "Graphics Programming and Theory".

About the problem, other solution is to have one array and always have it sorted parent -> child but I don't know performance of that.

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