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