Source

mana-core-navigation / Navigation / NavigationToken.icc

Full commit
////////////////////////////////////
// NavigationToken Implementation //
////////////////////////////////////

// query depth control
template<typename CHILD, typename CHILDPAR>
bool NavigationToken<CHILD,CHILDPAR>::pushQuery(const INavigable& parent, 
						const INavigable& /* child */ )
{
  return ( m_navCondition != 0 )
    ? ( m_navCondition->accept(&parent) )
    : true;
}

// relational parameter access
template<typename CHILD, typename CHILDPAR>
const CHILDPAR& 
NavigationToken<CHILD,CHILDPAR>::getParameter(const CHILD* data )
{
  NavigationToken<CHILD,CHILDPAR>::tokenIterator found = m_data.find(data);
  return ( found != m_data.end() 
	   ? (*found).second 
	   : CHILDPAR() ); 
}

  
// clear/reset token
template <typename CHILD, typename CHILDPAR>
void NavigationToken<CHILD,CHILDPAR>::clear()
{
  m_data.clear();
  if ( m_navSelector  != 0 ) m_navSelector->reset();
  if ( m_navCondition != 0 ) m_navCondition->reset();
}

// set object in token
template <typename CHILD, typename CHILDPAR>
void NavigationToken<CHILD,CHILDPAR>::setObject(const CHILD* data, 
						CHILDPAR weight )
{
  // check pointer validity
  if ( data != 0 )
    {
      if ( m_navSelector == 0 ||
	   ( m_navSelector !=0  && 
	     m_navSelector->accept(data, weight) ) )
	{
	  // important: add weights!
	  if ( m_data.find(data) != m_data.end() )
	    {
	      m_data[data] = m_data[data] + weight;
	    }
	  else
	    {
	      m_data[data] = weight;
	    }
	}
    }
}

// check if queried object is of requested type itself
template <typename CHILD, typename CHILDPAR>
bool NavigationToken<CHILD,CHILDPAR>::trySetObject(const INavigable* theObject)
{
  const CHILD* thePtr = dynamic_cast<const CHILD*>(theObject);
  if ( thePtr != 0 ) 
    {
      this->setObject(thePtr);
      return true;
    }
  
  return false;
}

// check if queried object is of requested type itself, including parameters
template <typename CHILD, typename CHILDPAR> 
bool NavigationToken<CHILD,CHILDPAR>::trySetObject(const INavigable* theObject,
						   const boost::any& theWeight)
{
  const CHILD* thePtr = dynamic_cast<const CHILD*>(theObject);
  try {
    CHILDPAR myWeight(boost::any_cast<CHILDPAR>(theWeight));
    if ( thePtr != 0 ) 
      {
	setObject(thePtr,myWeight);
	return true;
      }
  } catch(...) { }
  return false;
}