#include <typeinfo>

namespace X3DTK {

template<class T, class V>
MFNodeFunctor<T, V>::MFNodeFunctor(MFNode T:: *nodes)
: _nodes(nodes)
{
}

template<class T, class V>
const MFNode &MFNodeFunctor<T, V>::getNodes(X3DAbstractNode const *N)
{
  return static_cast<T const *>(N)->*_nodes;
}

template<class T, class V>
bool MFNodeFunctor<T, V>::addNode(X3DAbstractNode *N, X3DAbstractNode *C)
{
  if (dynamic_cast<V *>(C) != 0)
  {
    (static_cast<T *>(N)->*_nodes).push_back(C);
    X3DAbstractNode_addParentToChild(N, C);
    return true;
  }
  
  return false;  
}

template<class T, class V>
bool MFNodeFunctor<T, V>::removeNode(X3DAbstractNode *N, X3DAbstractNode *C)
{
  MFNode &nodes = (static_cast<T *>(N)->*_nodes);
  MFNode::iterator res = find(nodes.begin(), nodes.end(), C);
  
  if (res != nodes.end())
  {
    X3DAbstractNode_removeParentFromChild(N, C);
    nodes.erase(res);
    return true;
  }
  
  return false;  
}

template<class T, class V>
void MFNodeFunctor<T, V>::removeTheseNodes(X3DAbstractNode *N)
{
  if (dynamic_cast<T *>(N) != 0)
  {
    MFNode &nodes = (static_cast<T *>(N)->*_nodes);

    for (MFNode::const_iterator it = nodes.begin(); it != nodes.end(); ++it)
      X3DAbstractNode_removeParentFromChild(N, *it);
   
    nodes.clear();  
  }  
}

}
