#include "X3DAbstractNodeForward.h"

namespace X3DTK {

template<class T, class V>
AttributeRecorder<T, V>::AttributeRecorder(const SFString &name, V T:: *member, const V &init)
: X3DAttributeRecorder(), _name(name), _member(member), _init(init)
{
}

template<class T, class V>
void AttributeRecorder<T, V>::record(X3DAbstractNode *N) const
{
  SFType *type = X3DAbstractNode_getType(N);
  
  std::map<SFString, FieldManager>::iterator it = type->attributesMap().find(_name);
  if (it == type->attributesMap().end())
  {
    type->attributesMap()[_name].memberFunctor = new MemberFunctor<T, V>(_member);
    type->attributesMap()[_name].loader = new FieldLoader<V>();
    FieldWriter<V> *writer = new FieldWriter<V>();
    type->attributesMap()[_name].writer = writer;
    writer->write(type->attributesMap()[_name].init, _init);
  }
  
  static_cast<T *>(N)->*_member = _init;
}

}
