Jump to content

  • Log In with Google      Sign In   
  • Create Account

Getting to the data of the derrived class


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.

  • You cannot reply to this topic
10 replies to this topic

#1 Nanook   Members   -  Reputation: 505

Like
0Likes
Like

Posted 30 December 2011 - 07:25 AM

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?

Sponsor:

#2 e‍dd   Members   -  Reputation: 2105

Like
0Likes
Like

Posted 30 December 2011 - 08:25 AM

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.

#3 Nanook   Members   -  Reputation: 505

Like
0Likes
Like

Posted 30 December 2011 - 09:12 AM

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[i];
    }
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.
}


#4 SiCrane   Moderators   -  Reputation: 9597

Like
0Likes
Like

Posted 30 December 2011 - 09:22 AM

Why doesn't SceneGraphNode just have a MaterialState member variable instead of the weird state array?

#5 Nanook   Members   -  Reputation: 505

Like
0Likes
Like

Posted 30 December 2011 - 09:52 AM

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

#6 SiCrane   Moderators   -  Reputation: 9597

Like
0Likes
Like

Posted 30 December 2011 - 10:03 AM

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.

#7 Nanook   Members   -  Reputation: 505

Like
0Likes
Like

Posted 30 December 2011 - 11:57 AM

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[i].push(GlobalState::s_default[i]);
        }
        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[i] = stateStack[i].top();
            assert(m_localStates[i]);
     }
     m_lights.clear();
     m_lights.insert(m_lights.begin(),lightStack->begin(), lightStack->end());
}


#8 SiCrane   Moderators   -  Reputation: 9597

Like
0Likes
Like

Posted 30 December 2011 - 01:30 PM

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.

#9 Nanook   Members   -  Reputation: 505

Like
0Likes
Like

Posted 30 December 2011 - 03:50 PM

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

#10 SiCrane   Moderators   -  Reputation: 9597

Like
0Likes
Like

Posted 30 December 2011 - 04:42 PM

Ok, but one comment on it anyways: looking at it some more I think you could simplify it a lot by instead of having an array of all your states, have a single struct that holds all of them. Then you could have a stack of that struct instead an array of stacks, you could just use struct assignment rather than looping, and you could probably keep the type information you're losing by putting them all in an array.

#11 Nanook   Members   -  Reputation: 505

Like
0Likes
Like

Posted 30 December 2011 - 05:56 PM

Ah yes.. good idea.. I've looked at eberly's wildmagic 5 (the book is based on version 3) and he's gone away from this design.. he stores it in a VisualEffect class togheter with the shaders instead now..




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.



PARTNERS