Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TPERSIST_INCLUDED
Toshihiro Shimizu 890ddd
#define TPERSIST_INCLUDED
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#include "tsmartpointer.h"
Toshihiro Shimizu 890ddd
//#include "tpixel.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TSTREAM_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TPersistDeclaration;
Toshihiro Shimizu 890ddd
class TIStream;
Toshihiro Shimizu 890ddd
class TOStream;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//! This is an abstract class for load and save data from and to the file system (i.e files on the disk)
Toshihiro Shimizu 890ddd
class DVAPI TPersist
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	virtual ~TPersist(){};
Toshihiro Shimizu 890ddd
	/*!
Toshihiro Shimizu 890ddd
		This is a pure virtual function and must be overridden with a method that implements 
Toshihiro Shimizu 890ddd
		the object loading from a stream (i.e take the object's features from a formatted data stream). 
Toshihiro Shimizu 890ddd
		For example the TStageObject class use his implementation to take the values of the parameters 
Toshihiro Shimizu 890ddd
		that characterizes a stage object as name, coordinates, etc... from a file.
Toshihiro Shimizu 890ddd
		\a is is an input file stream
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
	virtual void loadData(TIStream &is) = 0;
Toshihiro Shimizu 890ddd
	/*!
Toshihiro Shimizu 890ddd
		This is a pure virtual function and must be overridden with a method that implements 
Toshihiro Shimizu 890ddd
		the object saving to a data stream (i.e put the object's features on a formatted data stream). 
Toshihiro Shimizu 890ddd
		For example the TStageObject class use his implementation to save the values of the parameters 
Toshihiro Shimizu 890ddd
		that characterizes a stage object as name, coordinates, etc... on a file
Toshihiro Shimizu 890ddd
		\a os is an output file stream.
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
	virtual void saveData(TOStream &os) = 0;
Toshihiro Shimizu 890ddd
	/*!
Toshihiro Shimizu 890ddd
		Returns the string identifier of the object. For example a TXsheet object is identified by "xsheet", 
Toshihiro Shimizu 890ddd
		a TStageObjectTree is identified by PegbarTree.
Toshihiro Shimizu 890ddd
	*/
Shinya Kitaoka 3bfa54
	inline std::string getStreamTag() const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*!
Toshihiro Shimizu 890ddd
		This pure virtual method is used to define a global pointer to the object.
Toshihiro Shimizu 890ddd
		This method is overridden with the macro PERSIST_DECLARATION(T).
Toshihiro Shimizu 890ddd
	\n	For example:
Toshihiro Shimizu 890ddd
		\code
Toshihiro Shimizu 890ddd
class DVAPI TStageObjectTree : public TPersist {
Toshihiro Shimizu 890ddd
PERSIST_DECLARATION(TStageObjectTree)
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
...
Toshihiro Shimizu 890ddd
		
Toshihiro Shimizu 890ddd
		\endcode 
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
	virtual const TPersistDeclaration *getDeclaration() const = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	typedef TPersist *CreateProc();
Toshihiro Shimizu 890ddd
	static void declare(CreateProc *);
Toshihiro Shimizu 890ddd
	/*!
Toshihiro Shimizu 890ddd
		If the object identified by \a name doesn't exist,
Toshihiro Shimizu 890ddd
		this method creates it through TPersistDeclarationT class template.
Toshihiro Shimizu 890ddd
		\sa getDeclaration()
Toshihiro Shimizu 890ddd
	*/
Shinya Kitaoka 3bfa54
	static TPersist *create(const std::string &name);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
		This class is used to store and retrieve the id associated to an object.
Toshihiro Shimizu 890ddd
		\sa TPersist::getStreamTag().
Toshihiro Shimizu 890ddd
		The class is istantiated by the macro  PERSIST_DECLARATION(T).
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
class DVAPI TPersistDeclaration
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::string m_id;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 3bfa54
	TPersistDeclaration(const std::string &id);
Toshihiro Shimizu 890ddd
	virtual ~TPersistDeclaration() {}
Shinya Kitaoka 3bfa54
	std::string getId() const { return m_id; };
Toshihiro Shimizu 890ddd
	virtual TPersist *create() const = 0;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
inline std::string TPersist::getStreamTag() const { return getDeclaration()->getId(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
		This template class is used to create an istance of the class \a T.
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
class TPersistDeclarationT : public TPersistDeclaration
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	/*!
Toshihiro Shimizu 890ddd
		This is the constructor. Its argument is the id of the object.
Toshihiro Shimizu 890ddd
		\sa TPersist::getStreamTag()
Toshihiro Shimizu 890ddd
	*/
Shinya Kitaoka 3bfa54
	TPersistDeclarationT(const std::string &id) : TPersistDeclaration(id) {}
Toshihiro Shimizu 890ddd
	/*!
Toshihiro Shimizu 890ddd
		Returns a pointer to a newly created object of type TPersist.
Toshihiro Shimizu 890ddd
		This template class is called by the macro PERSIST_DECLARATION(T).
Toshihiro Shimizu 890ddd
		A class that calls PERSIST_DECLARATION(T) must inherits TPersist.
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
	TPersist *create() const { return new T; };
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*!	\file tpersist.h  
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
		This macro must be included at the beginning of a class declaration,
Toshihiro Shimizu 890ddd
		and permits to create a new object of type T with the use of template 
Toshihiro Shimizu 890ddd
		class TPersistDeclarationT
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
#define PERSIST_DECLARATION(T)                        \
Toshihiro Shimizu 890ddd
private:                                              \
Toshihiro Shimizu 890ddd
	static TPersistDeclarationT<t> m_declaration;     \</t>
Toshihiro Shimizu 890ddd
                                                      \
Toshihiro Shimizu 890ddd
public:                                               \
Toshihiro Shimizu 890ddd
	const TPersistDeclaration *getDeclaration() const \
Toshihiro Shimizu 890ddd
	{                                                 \
Toshihiro Shimizu 890ddd
		return &m_declaration;                        \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define PERSIST_IDENTIFIER(T, I) \
Toshihiro Shimizu 890ddd
	TPersistDeclarationT<t> T::m_declaration(I);</t>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif