/// This file is part of the X3DToolKit library
/// Copyright (C) 2002-2004  Yannick Le Goc (legoc@imag.fr)

/// http://artis.imag.fr/Members/Yannick.Legoc/X3D/

/// This library is free software; you can redistribute it and/or
/// modify it under the terms of the GNU Lesser General Public
/// License as published by the Free Software Foundation; either
/// version 2.1 of the License, or (at your option) any later version.

/// This library is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
/// Lesser General Public License for more details.

/// You should have received a copy of the GNU Lesser General Public
/// License along with this library; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#ifndef X3DTK_X3DABSTRACTNODE_H
#define X3DTK_X3DABSTRACTNODE_H

#include "X3DTypes.h"
#include "X3DFileElement.h"
#include "SFType.h"
#include "Recorder.h"

namespace X3DTK {

class FileElement;

/*! \brief Class declared in <b><X3DTK/kernel.h></b> defining the root of the inheritance tree
 *  for the nodes of the different scene graphs.
 *
 * For more details, see the <a href="SceneGraphAPI.html">abstract</a> scene graph API page. 
 *
 *  \ingroup abstractNode
 */

class X3DAbstractNode
{
public:
  /// Constructor.
  X3DAbstractNode();
  /// Clones the node. This method has to be redefined in the children classes.
  SFNode clone() const;
  /// Virtual destructor.
  virtual ~X3DAbstractNode();
  
  /// Sets the name of the node. The name of the node is the DEF attribute in the X3D file.
  void setName(const SFString &name);

  /// Gets the name of the node.
  inline const SFString &getName() const {return _name;};
  /// Gets the SFType name of the node. It is a shortcut to getType()->getName().
  inline const SFString &getTypeName() const {return _type->getName();};
  /// Gets the scene graph name of the node.
  inline const SFString &getSceneGraphName() const {return _type->getSceneGraphName();};
  /// Gets the SFType of the node.
  inline SFType *getType() const {return _type;};
  /// Gets the list of children in the scene graph.
  MFNode getChildList();
  /// Gets the list of parents in the scene graph.
  inline MFNode getParentList() const {return _parentList;};
    
  /// Sets the child N. Returns FALSE if N is not a 
  /// valid child. If the child already exists then N becomes the new child.
  bool setChild(SFNode N);
  /// Removes the child if exists. Returns TRUE if succeeds.
  bool removeChild(SFNode N);

  /// Gets the name attribute.
  X3DField get(const SFString &name) const; 
  /// Sets the name attribute.
  void set(const SFString &name, const X3DField &field); 
  /// Loads the attribute from its name and its SFString value.
  void loadAttribute(const SFString &name, const SFString &value);
  /// Writes the attribute from its name.
  SFString writeAttribute(const SFString &name);
  /// Loads the attributes from an X3DFileElement.
  void loadAttributes(const X3DFileElement *element);
  /// Writes the attribute from its name.
  void writeAttributes(SFString &output);
  /// Adds the parent to child. 
  static void addParentToChild(SFNode parent, SFNode child);
  /// Removes the parent from child.
  static void removeParentFromChild(SFNode parent, SFNode child);
  
protected:
  /// Defines a new type name.
  inline void define(const TypeName &typeName) {SFType::defineTypeName(&_type, typeName.name, typeName.component, typeName.sceneGraph, typeName.cloner);};  
  /// Defines an attribute.
  inline void define(const X3DAttributeRecorder &recorder) {recorder.record(this);};
  /// Defines a node.
  inline void define(const X3DSFNodeRecorder &recorder) {recorder.record(this);};
  /// Defines a node.
  inline void define(const X3DMFNodeRecorder &recorder) {recorder.record(this);};

#ifdef TEMPLATE_SPECIALIZATION_SUPPORTED
  /// Method to call at the first line of the body of the constructor, and passing 
  /// in argument the name of the tag of the X3D file, and optionnally the name of the
  /// component and of the scene graph.
  inline void defineTypeName(const SFString &name, const SFString &component = SFString(""), const SFString &sceneGraph = SFString("")) {defineTempTypeName(this, name, component, sceneGraph);};
  /// Method to avoid defineTypeName having a template parameter.
  template<class T>
  inline void defineTempTypeName(T * /* t */, const SFString &name, const SFString &component, const SFString &sceneGraph) {define(Recorder<T>::getTypeName(name, component, sceneGraph));};
  /// Defines an attribute.
  template<class T, class V>
  inline void defineAttribute(const SFString &name, V T:: *member, const V &init);
  /// Defines a node.
  template<class T, class V>
  inline void defineNode(const SFString &name, V T:: *node);
  /// Defines a node.
  template<class T, class V>
  inline void defineNodes(const SFString &name, MFNode T:: *nodes);
#endif
  
  /// Method which must be called in the destructor. Removes the links with child nodes.
  void removeChildren();
  
private:   
  /// Copy constructor.
  X3DAbstractNode(const X3DAbstractNode &N);
  /// Adds one reference.
  inline void addOneReference() const {_type->addOneReferenceRecursive();};

  SFString _name;
  SFType *_type;
  MFNode _parentList;
  
  /// Adds a parent.
  void addParent(const SFNode &N);
  /// Removes a parent.
  bool removeParent(const SFNode &N);
};

inline void X3DAbstractNode_addParentToChild(SFNode parent, SFNode child) {
  X3DAbstractNode::addParentToChild(parent,child);
}
inline void X3DAbstractNode_removeParentFromChild(SFNode parent, SFNode child) {
  X3DAbstractNode::removeParentFromChild(parent,child);
}
inline SFType * X3DAbstractNode_getType(X3DAbstractNode * node) {
  return node->getType();
}

}

#include "X3DAbstractNode.inl"

#endif
