You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.3 KiB
C++

#ifndef NBTMODIFIER_ABSTRACTTAG_H
#define NBTMODIFIER_ABSTRACTTAG_H
#include <QtGlobal>
class QDataStream;
/**
* Interface for all NBT tags
*/
class AbstractTag {
public:
/**
* When tag is destruct
*/
virtual inline ~AbstractTag () {};
/**
* The tag's id
*
* @return The tag's id
*/
const quint8 id () const;
/**
* Extract next tag's id from data (not consuming)
*
* @param data The data to read
*
* @return The next tag's id
*/
static const quint8 extractId (const QByteArray &data);
/**
* Create an empty tag of a giving id
*
* @param id The new tag's id
*
* @return The new tag, or nullptr if no valid id provided
*/
static AbstractTag *createEmptyFromId (const quint8 id);
/**
* Create an empty tag of the next tag's id from data (not consuming)
*
* @param data The data to read
*
* @return The new tag, or nullptr if invalid data provided
*/
static AbstractTag *createEmptyFromData (const QByteArray &data);
/**
* Read a tag from a data stream
*
* @param data The data stream
*
* @return True if read is success, otherwise false
*/
virtual void readFromData (QDataStream &data) = 0;
/**
* Write a tag to a data stream
*
* @param data The data stream
*
* @return True if write is success, otherwise false
*/
virtual void writeToData (QDataStream &data) const = 0;
/**
* Convenient operator to read a tag from a data stream
*
* @param tag The tag read
* @param data The data stream
*
* @return The data stream (allow call chaining)
*
* @see readFromData
*/
friend QDataStream &operator >> (QDataStream &data, AbstractTag &tag);
/**
* Convenient operator to write a tag to a data stream
*
* @param data The data stream
* @param tag The tag to write
*
* @return The data stream (allow call chaining)
*
* @see writeToData
*/
friend QDataStream &operator << (QDataStream &data, const AbstractTag &tag);
protected:
/**
* Create a new tag
*
* @param id
*/
explicit AbstractTag (const quint8 id);
private:
/**
* The tag's ID
*/
const quint8 m_id;
};
#endif //NBTMODIFIER_ABSTRACTTAG_H