Getting to the data of the derrived class

Started by
9 comments, last by Nanook 12 years, 3 months ago
I have this issue that I never find a good solution for that I'm happy with.

Eg. for global states in my scenegraph I have a base GlobalState class that I derrive different states from. Then I store an array with GlobalState pointers in the nodes. Now since the derrived classes stores data I need a way to get to this data.

The different ways I know of is:
- Use cast. Either dynamic_cast (I never use this) or have a virtual function in base class that returns the type of the GlobalState and use static_cast based on this. I like working with this method as I get the concrete class, but since casts should be avoided I want to find another way of doing it.
- Have functions in the interface to get all the different data. This gets messy with lots of derrived classes and the base class needs to be altered for each added derrived
- Use the visitor pattern to get to the data. I like this as it is efficient and it uses clean features of c++, but I find it kind of messy too have lots of visitors.
- have some sort of structure with unions and a set of different types of data. This is ok, but If a derrived class wants to store a type that is not in the union this wont work.

Another case where I come into the same issue is with events. I have A base Event class and when I'm handeling the events I need to get to the data stored in the derrived event.. atm I'm solving this with the static cast method where I got some template magic and casts the event to the concrete type before I call the handle function.. void OnDerrivedEvent(DerrivedEvent& event);

So how do you solve these issues? Sugestions for other ways than the one I've listed? Should I design things differently? How?
Advertisement
A little vague on exactly what you mean, but could you add virtual functions that act on the internal data as necessary? Or you could make these 'data' types abstract and give them virtual functions.

Hard to give any more advice without seeing something that resembles your actual code.
Okay, here's a stripped down version of the GlobalState example.


class GlobalState
{
enum
{
STATE_MATERIAL,
STATE_COUNT
};
}
class MaterialState : public GlobalState
{
public:
ColorRGBA& GetSomeColor()
{
return m_someColor;
}
private:
ColorRGBA m_someColor;
};
class SceneGraphNode
{
GlobalState* GetGlobalState(int i)
{
return states;
}
private:
GlobalState* states[STATE_COUNT];
};
void Draw(SceneGraphNode& node)
{
GlobalState* materialStateRef = node.GetGlobalState(STATE_MATERIAL);
//Need to use one of the methods described to get to the color data of the material.
}
Okay, heres a stripped down version of the GlobalState Example..


class GlobalState
{
enum
{
STATE_MATERIAL,
STATE_COUNT
};
}
class MaterialState : public GlobalState
{
public:
ColorRGBA& GetSomeColor()
{
return m_someColor;
}
private:
ColorRGBA m_someColor;
};
class SceneGraphNode
{
GlobalState* GetGlobalState(int i)
{
return states;
}
private:
GlobalState* states[STATE_COUNT];
};
void Draw(SceneGraphNode& node)
{
GlobalState* materialStateRef = node.GetGlobalState(STATE_MATERIAL);
//Need to use one of the methods described to get to the color data of the material.
}
Why doesn't SceneGraphNode just have a MaterialState member variable instead of the weird state array?
Yeah I thought about that too.. but when adding new states it has to be added to the scenegraphnode.. also I have a function for propagating a state change down the scenegraph and with the array its easy to update all the child nodes.. with storing members for each of the states in the nodes I would have to add code to the propagation function for each new state I add to the system.. The idea is from David Eberlys 3d game engine architecture book..
What does your propagation function look like? It's entirely possible that either templating or use a pointer to member argument can allow you to not have to special case every member variable. Alternately, give your SceneGraphNode a get MaterialState member function rather than needing to use the index to access it. The the getter does the cast and you can pretend it is a member variable for everything but the propagation function.
Spatial is the base. Node and Renderable is derrived from Spatial.
UpdateRenderStateLocal is pure virtual.. UpdateRenderState is the entry point..
It starts by propagating the state from the root of the tree (if propagation was started from one of the child nodes), then using the passed stacks to push and pop states..


void TE::SceneGraph::Spatial::UpdateRenderState( std::stack< boost::shared_ptr< GlobalState > >* stateStack, std::vector< boost::shared_ptr< Light > >* lightStack )
{
bool initiator = stateStack == NULL;

if(initiator)
{
stateStack = new std::stack< boost::shared_ptr<GlobalState > >[GlobalState::STATE_COUNT];
for (unsigned i = 0; i < GlobalState::STATE_COUNT ; i++)
{
stateStack.push(GlobalState::s_default);
}
lightStack = new std::vector< boost::shared_ptr<Light > >;

PropagateStateFromRoot(stateStack, lightStack);
}
else
{
PushState(stateStack, lightStack);
}

UpdateRenderStateLocal(stateStack, lightStack);

if (initiator)
{
delete []stateStack;
delete lightStack;
}
else
{
PopState(stateStack, lightStack);
}
}


void TE::SceneGraph::Spatial::PropagateStateFromRoot( std::stack< boost::shared_ptr< GlobalState > >* stateStack, std::vector< boost::shared_ptr< Light > >* lightStack )
{
if (m_parent) PropagateStateFromRoot(stateStack, lightStack);

PushState(stateStack, lightStack);
}

void TE::SceneGraph::Spatial::PushState( std::stack< boost::shared_ptr< GlobalState > >* stateStack, std::vector< boost::shared_ptr< Light > >* lightStack )
{
std::list<boost::shared_ptr<GlobalState> >::iterator stateItr = m_globalStates.begin();
while (stateItr != m_globalStates.end())
{
int type = (*stateItr)->GetGlobalStateType();
stateStack[type].push(*stateItr);
}

std::list<boost::shared_ptr<Light> >::iterator lightItr = m_lights.begin();
while (lightItr != m_lights.end())
{
lightStack->push_back(*lightItr);
}
}

void TE::SceneGraph::Spatial::PopState( std::stack< boost::shared_ptr< GlobalState > >* stateStack, std::vector< boost::shared_ptr< Light > >* lightStack )
{
std::list<boost::shared_ptr<GlobalState> >::iterator stateItr = m_globalStates.begin();
while (stateItr != m_globalStates.end())
{
int type = (*stateItr)->GetGlobalStateType();
stateStack[type].pop();
}

std::list<boost::shared_ptr<Light> >::iterator lightItr = m_lights.begin();
while (lightItr != m_lights.end())
{
lightStack->pop_back();
}
}





void TE::SceneGraph::Node::UpdateRenderStateLocal( std::stack< boost::shared_ptr< GlobalState > >* stateStack, std::vector< boost::shared_ptr< Light > >* lightStack )
{
std::vector<Spatial*>::iterator current = m_children.begin();
std::vector<Spatial*>::const_iterator last = m_children.end();
while(current != last)
{
(*current)->UpdateRenderState(stateStack, lightStack);
++current;
}
}



void TE::SceneGraph::Renderable::UpdateRenderStateLocal( std::stack< boost::shared_ptr< GlobalState > >* stateStack, std::vector< boost::shared_ptr< Light > >* lightStack )
{
for (unsigned i = 0; i < GlobalState::STATE_COUNT ; i++)
{
m_localStates = stateStack.top();
assert(m_localStates);
}
m_lights.clear();
m_lights.insert(m_lights.begin(),lightStack->begin(), lightStack->end());
}
Is this your actual code? I don't understand why you don't get a stack overflow calling PropagateStateFromRoot() from a node with a parent. I also don't see how Spatial::PushState() ever terminates because none of the iterators are ever incremented. Same deal with Spatial::PopState(), it seems like it should crash because no pointer iteration is going on. In short, I just don't understand what your code is trying to do.
I'm still working on this so I haven't tested it yet.. Its based on eberly's game engine architecture design book..

I think I'm going to redesign this now anyways..

This topic is closed to new replies.

Advertisement