/* ImLib3D
 * Copyright (c) 2001, ULP-IPB Strasbourg.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program 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
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/*! \file  ImLib3DFile.hpp  \brief base classes for streamable objects */

#ifndef _ImLib3DFile_hpp
#define _ImLib3DFile_hpp


#include<ImLib3D/XMLBinaryFile.hpp>

//! ImLib3DFile
class ImLib3DFile : public XMLBinaryFile
{
public:
	static bool IsImLib3DImage(const string &fname);
	~ImLib3DFile(){if(isOpen){Close();}}
	ImLib3DFile(const string &_fileName="",const string &mode="",bool _exceptionOnFail=false):
		XMLBinaryFile(_fileName,mode,_exceptionOnFail)
	{;}
};

//! Base class for all classes that write themselves to an ImLib3DFile
class Streamable
{
public:
	//! Convenience fct that optionally creates root, or current node.
	/*! Should always be called at the begining of each Write */
	XMLNode *CreateWriteNode(const string &topNodeName,ImLib3DFile *file,XMLNode *parentNode,XMLNode *node) const;
	//! Convenience fct that optionally creates root, or current node.
	/*! Should always be called at the begining of each Read */
	XMLNode *CreateReadNode(const string &topNodeName,ImLib3DFile *file,XMLNode *parentNode,XMLNode *node);
	//! Read this object, either from a current node, a parent node, or the root node of the file
	virtual void Read( ImLib3DFile *file,XMLNode *parentNode=NULL,XMLNode *node=NULL)=0;
	//! Write this object, either to a current node, a parent node, or the root node of the file
	virtual void Write(ImLib3DFile *file,XMLNode *parentNode=NULL,XMLNode *node=NULL) const=0;
	//! Read this object from a file
	virtual void ReadFromFile( const string &fname)       {ImLib3DFile file(fname,"r");Read (&file);}
	//! Write this object to a file
	virtual void WriteToFile(const string &fname) const {ImLib3DFile file(fname,"w");Write(&file);}
};

#endif //_ImLib3DFile_hpp
