Toshihiro Shimizu 890ddd
#ifndef TSTREAM_H
Toshihiro Shimizu 890ddd
#define TSTREAM_H
Toshihiro Shimizu 890ddd
Shinya Kitaoka 262a92
#include <memory></memory>
Shinya Kitaoka 262a92
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tpixel.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Qt includes
Toshihiro Shimizu 890ddd
#ifndef TNZCORE_LIGHT
Toshihiro Shimizu 890ddd
#include <qstring></qstring>
Toshihiro Shimizu 890ddd
#endif
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
//    Forward declarations
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TPersist;
Toshihiro Shimizu 890ddd
class TFilePath;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef std::pair<int, int=""> VersionNumber; //!< Integer pair storing the major and minor</int,>
Toshihiro Shimizu 890ddd
										   //!  application version numbers.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  \brief Toonz's XML-like \a input file parser.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  This class is Toonz's standard \a input parser for simple XML files.
Toshihiro Shimizu 890ddd
  It is specifically designed to interact with object types derived
Toshihiro Shimizu 890ddd
  from the TPersist base class.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI TIStream
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	class Imp;
Shinya Kitaoka 262a92
	std::unique_ptr<imp> m_imp;</imp>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	/*!
Toshihiro Shimizu 890ddd
    \warning  Stream construction  may throw  on files \b compressed using
Toshihiro Shimizu 890ddd
              TOStream 's compression.
Toshihiro Shimizu 890ddd
              
Toshihiro Shimizu 890ddd
    \warning  Even if construction does not throw, the stream could still be
Toshihiro Shimizu 890ddd
              constructed with an invalid status. Remember to check the stream
Toshihiro Shimizu 890ddd
              status using operator bool().
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
	TIStream(const TFilePath &is); //!< Opens the document at the specified path
Toshihiro Shimizu 890ddd
	~TIStream();				   //!< Destroys the stream
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! \sa std::basic_istream::operator void*().
Toshihiro Shimizu 890ddd
	operator bool() const; //!< Returns whether the stream has a valid status.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIStream &operator>>(int &v);		//!< Reads an integer from the stream
Toshihiro Shimizu 890ddd
	TIStream &operator>>(double &v);	//!< Reads a double from the stream
Shinya Kitaoka 3bfa54
	TIStream &operator>>(std::string &v);	//!< Reads a string from the stream
Shinya Kitaoka 3bfa54
	TIStream &operator>>(std::wstring &v);   //!< Reads a wstring from the stream
Toshihiro Shimizu 890ddd
	TIStream &operator>>(TFilePath &v); //!< Reads a TFilePath from the stream
Toshihiro Shimizu 890ddd
	TIStream &operator>>(TPixel32 &v);  //!< Reads a TPixel32 from the stream
Toshihiro Shimizu 890ddd
	TIStream &operator>>(TPixel64 &v);  //!< Reads a TPixel64 from the stream
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TNZCORE_LIGHT
Toshihiro Shimizu 890ddd
	TIStream &operator>>(QString &v); //!< Reads an integer from the stream
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*! \detail
Toshihiro Shimizu 890ddd
    This function dispatches the loading process to the derived class'
Toshihiro Shimizu 890ddd
    reimplementation of the TPersist::loadData() function.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    \note Unlinke operator>>(TPersist*&), this function \a requires that
Toshihiro Shimizu 890ddd
          the passed object is of the \b correct type to read the current tag.
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
	TIStream &operator>>(TPersist &v);  //!< Reads a TPersist derived object data from the stream.
Toshihiro Shimizu 890ddd
	TIStream &operator>>(TPersist *&v); //!< \a Allocates and reads a TPersist derived object data from the stream.
Toshihiro Shimizu 890ddd
										//!  \sa operator>>(TPersist&)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//!  \deprecated
Shinya Kitaoka 3bfa54
	std::string getString(); //!< Returns the stream content as a string, up to the next tag.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//!  \deprecated
Toshihiro Shimizu 890ddd
	bool eos(); //!< \brief Returns \e true in case of end of string (a StreamTag::EndTag
Toshihiro Shimizu 890ddd
				//!  is encountered or the string is empty).
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*!
Toshihiro Shimizu 890ddd
    \param      tagName Output name of a matched tag.
Toshihiro Shimizu 890ddd
    \return     Whether a tag was found.
Toshihiro Shimizu 890ddd
  */
Shinya Kitaoka 3bfa54
	bool matchTag(std::string &tagName); //!< Attempts matching a tag and returns its name.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! \return   Whether an end tag was found.
Toshihiro Shimizu 890ddd
	bool matchEndTag(); //!< Attempts matching a StreamTag::EndTag.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*!
Toshihiro Shimizu 890ddd
    \brief Attempts retrieval of the value associated with the
Toshihiro Shimizu 890ddd
           specified tag attribute in current tag.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    \param paramName  Tag attribute name.
Toshihiro Shimizu 890ddd
    \param value      Output value of the attribute.
Toshihiro Shimizu 890ddd
    \return           Whether the tag attribute was found.
Toshihiro Shimizu 890ddd
  */
Shinya Kitaoka 3bfa54
	bool getTagParam(std::string paramName, std::string &value);
Shinya Kitaoka 3bfa54
	bool getTagParam(std::string paramName, int &value); //!< \sa getTagParam(string, string&)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool isBeginEndTag(); //!< Returns whether current tag is of type StreamTag::BeginEndTag.
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	bool openChild(std::string &tagName); //!< \deprecated Use matchTag(string&) instead.
Toshihiro Shimizu 890ddd
	void closeChild();				 //!< \deprecated Use matchEndTag() instead.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool match(char c) const; //! \deprecated
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::string getTagAttribute(std::string name) const; //!< \sa getTagParam(string, string&),
Shinya Kitaoka 3bfa54
											   //!  TOStream::openChild(string, const map<std::string, string="">&).</std::string,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath getFilePath();	   //!< Returns the stream's path (i.e. the opened filename associated to the input stream).
Toshihiro Shimizu 890ddd
	TFilePath getRepositoryPath(); //!< \deprecated
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int getLine() const; //!< Returns the line number of the stream <tt>+1</tt>.
Toshihiro Shimizu 890ddd
						 //!  \warning I've not idea why the +1, though.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	VersionNumber getVersion() const; //!< Returns the currently stored version of the opened document.
Toshihiro Shimizu 890ddd
									  //!  \sa setVersion()
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setVersion(const VersionNumber &version); //!< Returns the currently stored version of the opened document.
Toshihiro Shimizu 890ddd
												   //!  \sa setVersion()
Toshihiro Shimizu 890ddd
	/*!
Toshihiro Shimizu 890ddd
    \note After skipping the tag content, the stream is positioned immediately
Toshihiro Shimizu 890ddd
          after the end tag.
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
	void skipCurrentTag(); //!< Silently ignores the content of currently opened tag up to its end.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// Not copyable
Toshihiro Shimizu 890ddd
	TIStream(const TIStream &);			   //!< Not implemented
Toshihiro Shimizu 890ddd
	TIStream &operator=(const TIStream &); //!< Not implemented
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Toshihiro Shimizu 890ddd
TIStream &operator>>(TIStream &is, T *&v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPersist *persist = 0;
Toshihiro Shimizu 890ddd
	is >> persist;
Toshihiro Shimizu 890ddd
	v = persist ? dynamic_cast<t *="">(persist) : 0;</t>
Toshihiro Shimizu 890ddd
	return is;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  \brief Toonz's XML-like \a output file parser.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  This class is Toonz's standard \a output parser for simple XML files.
Toshihiro Shimizu 890ddd
  It is specifically designed to interact with object types derived
Toshihiro Shimizu 890ddd
  from the TPersist base class.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI TOStream
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	class Imp;
Shinya Kitaoka 262a92
	std::shared_ptr<imp> m_imp;</imp>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 5f2af0
	explicit TOStream(std::shared_ptr<imp> imp); //!< deprecated</imp>
Shinya Kitaoka 5f2af0
Shinya Kitaoka 5f2af0
	TOStream(TOStream &&);
Shinya Kitaoka 5f2af0
	TOStream& operator=(TOStream &&);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	/*!
Toshihiro Shimizu 890ddd
    \param fp           Output file path
Toshihiro Shimizu 890ddd
    \param compressed   Enables compression of the whole file
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    \note     Stream construction  does not throw . However, the stream
Toshihiro Shimizu 890ddd
              status could be invalid. Remeber to check the stream validity using
Toshihiro Shimizu 890ddd
              operator bool().
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    \warning  Stream compression has been verified to be unsafe.
Toshihiro Shimizu 890ddd
              Please consider it \a deprecated.
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
	TOStream(const TFilePath &fp, bool compressed = false); //!< Opens the specified file for write
Toshihiro Shimizu 890ddd
	~TOStream();											//!< Closes the file and destroys the stream
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! \sa std::basic_ostream::operator void*().
Toshihiro Shimizu 890ddd
	operator bool() const; //!< Returns whether the stream has a valid status.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TOStream &operator<<(int v);			  //!< Writes an int to the stream.
Toshihiro Shimizu 890ddd
	TOStream &operator<<(double v);			  //!< Writes a double to the stream.
Shinya Kitaoka 3bfa54
	TOStream &operator<<(std::string v);			  //!< Writes a string to the stream.
Shinya Kitaoka 3bfa54
	TOStream &operator<<(std::wstring v);		  //!< Writes a wstring to the stream.
Toshihiro Shimizu 890ddd
	TOStream &operator<<(const TFilePath &v); //!< Writes a TFilePath to the stream.
Toshihiro Shimizu 890ddd
	TOStream &operator<<(const TPixel32 &v);  //!< Writes a TPixel32 to the stream.
Toshihiro Shimizu 890ddd
	TOStream &operator<<(const TPixel64 &v);  //!< Writes a TPixel64 to the stream.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TNZCORE_LIGHT
Toshihiro Shimizu 890ddd
	TOStream &operator<<(QString v); //!< Writes a QString to the stream.
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TOStream &operator<<(TPersist *v); //!< deprecated Use operator<<(TPersist&) instead.
Toshihiro Shimizu 890ddd
	TOStream &operator<<(TPersist &v); //!< Saves data to the stream according
Toshihiro Shimizu 890ddd
									   //!  to the reimplemented TPersist::saveData.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! \deprecated Use openChild(string) instead
Shinya Kitaoka 3bfa54
	TOStream child(std::string tagName);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	void openChild(std::string tagName); //!< Writes a <tagname> to the stream, opening a tag.</tagname>
Shinya Kitaoka 3bfa54
	void openChild(std::string tagName,
Shinya Kitaoka 3bfa54
				   const std::map<std::string, std::string=""> &attributes); //!< \brief Writes a <tagname ..="" attribute1="value1"></tagname></std::string,>
Toshihiro Shimizu 890ddd
														   //!  to the stream, opening a tag with embedded attributes.
Shinya Kitaoka 3bfa54
	void openCloseChild(std::string tagName,
Shinya Kitaoka 3bfa54
						const std::map<std::string, std::string=""> &attributes); //!< \brief Writes a tag <tagname ..="" attribute1="value1"></tagname></std::string,>
Toshihiro Shimizu 890ddd
																//!  to the stream, opening a tag with embedded attributes
Toshihiro Shimizu 890ddd
																//!  which is immediately closed.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void closeChild(); //!< Closes current tag, writing  to the stream.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void cr(); //!< Writes carriage return to the stream. \deprecated
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void tab(int dt); //!< \deprecated
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath getFilePath();	   //!< Returns the file path of the file associated to this output stream.
Toshihiro Shimizu 890ddd
	TFilePath getRepositoryPath(); //!< \deprecated
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*! \detail
Toshihiro Shimizu 890ddd
    This function is similar to operator bool(), but \b flushes the stream before
Toshihiro Shimizu 890ddd
    checking the status.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    \return   Whether the stream is in a good state (no fails in writing to).
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
	bool checkStatus() const; //!< \b Flushes the stream and checks its validity.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// Not copyable
Shinya Kitaoka 5f2af0
	TOStream(const TOStream &) = delete;			      //!< Not implemented
Shinya Kitaoka 262a92
	TOStream &operator=(const TOStream &) = delete; //!< Not implemented
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif // TSTREAM_H