#include "X3DAbstractNodeForward.h"

namespace X3DTK {

template<class T, class V>
SFNodeFunctor<T, V>::SFNodeFunctor(V *T:: *node)
: _node(node)
{
}

template<class T, class V>
X3DAbstractNode *SFNodeFunctor<T, V>::getNode(X3DAbstractNode const *N)
{
  return static_cast<T const *>(N)->*_node;
}

template<class T, class V>
bool SFNodeFunctor<T, V>::setNode(X3DAbstractNode *N, X3DAbstractNode *C)
{
  if (dynamic_cast<V *>(C) != 0)
  {
    X3DAbstractNode_removeParentFromChild(N, static_cast<T *>(N)->*_node);
    static_cast<T *>(N)->*_node = static_cast<V *>(C);
    X3DAbstractNode_addParentToChild(N, static_cast<T *>(N)->*_node);
    return true;
  }
  
  return false;  
}

template<class T, class V>
bool SFNodeFunctor<T, V>::removeNode(X3DAbstractNode *N, X3DAbstractNode *C)
{
  if (C == static_cast<T *>(N)->*_node)
  {
    X3DAbstractNode_removeParentFromChild(N, static_cast<T *>(N)->*_node);
    static_cast<T *>(N)->*_node = 0;
    return true;
  }
    
  return false;  
}

template<class T, class V>
void SFNodeFunctor<T, V>::removeThisNode(X3DAbstractNode *N)
{
  if (dynamic_cast<T *>(N) != 0)
  {
    X3DAbstractNode_removeParentFromChild(N, static_cast<T *>(N)->*_node);
    static_cast<T *>(N)->*_node = 0;
  }  
}

}
