/* * Copyright 2007-2009 Ben Boeckel * * 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 3 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, see . */ /** * \file sigmod/Object.h */ #ifndef SIGMOD_OBJECT #define SIGMOD_OBJECT // Sigcore includes #include #include #include // Sigmod includes #include "Global.h" // Qt includes #include #include #include #include #include #include #include #include // C includes #include namespace Sigmod { // Forward decarations class Game; /** * \class Sigmod::Object Object.h sigmod/Object.h * \brief Base class for all data structures in a game. * * The Object class sets up a parenting structure for all Sigmod classes. * It also provides an interface for validation, saving to XML format, * as well as loading from XML. */ class SIGMOD_EXPORT Object : public QObject { Q_OBJECT public: /** * Constructor. * * \param parent The parent of the object. * \param id The id of the object. */ Object(const Object* parent, const int id); /** * Destructor. */ virtual ~Object(); /** * \return The parent of the object. */ const Object* parent() const; /** * \return The game that the object belongs to. */ const Game* game() const; /** * \return The id of the object. */ int id() const; /** * \return The name of the class of the object without the namespace. */ QString className() const; /** * Convenience function to create a QDomDocument from the QDomElement that is returned from \link Object::save save \endlink. * * \param object The object to create an XML structure from. * \return The XML representation of \p object. */ static QDomDocument xml(const Object* object); /** * Method for validating the object. */ virtual void validate() = 0; /** * Loads data from an XML structure. * * \param xml The XML data structure to load data from. */ virtual void load(const QDomElement& xml) = 0; /** * Saves the data of the object to XML. * * \return An XML representation of the class. */ virtual QDomElement save() const = 0; signals: /** * Signal for when the object encounters something unexpected, but it does not invalid when validating. * * \param message The message. */ void valMessage(const QString& message) const; /** * Signal for when the object encounters a value that is either confusing or not advised, but does not * render the game as inconsistent. * * \param message The warning. */ void valWarning(const QString& message) const; /** * Signal for when the object encounters an unexpected value that introduces an inconsistency in the game. * * \param message The error. */ void valError(const QString& message) const; /** * Signal for when a value was set, but it may not be optimal. * * \param message The warning. */ void warning(const QString& message) const; /** * Signal for when a value assignment was attempted, but the value was invalid. * * \param message The error. */ void error(const QString& message) const; /** * Signal for when the object has changed. */ void changed() const; protected: void setId(const int id); bool idCheck(const int id) const; static QString unused(const QString& variable); static QString bounds(const QString& variable, const int min, const int max, const int value); static QString bounds(const QString& variable, const int min, const int max, const Sigcore::Fraction& value); static QString bounds(const QString& variable, const QString& min, const QString& max, const int value); static QString bounds(const QString& variable, const int value); static QString bounds(const QString& variable, const QString& value); static QString subclass(const QString& subclass, const int id); static QString subclass(const QString& subclass, const QString& name); virtual void clear(); private: int m_id; const Object* const m_parent; }; template inline void loadValue(const QDomElement& xml, T* value) { *value = QVariant(xml.firstChild().toText().data()).value(); } template<> inline void loadValue(const QDomElement& xml, bool* value) { *value = (xml.firstChild().toText().data() == "true"); } template<> inline void loadValue(const QDomElement& xml, int* value) { *value = xml.firstChild().toText().data().toInt(); } template<> inline void loadValue(const QDomElement& xml, Sigcore::Fraction* value) { value->set(xml.attribute("numerator", "1").toInt(), xml.attribute("denominator", "1").toInt()); value->reduce(); } template<> inline void loadValue(const QDomElement& xml, QPoint* value) { value->setX(xml.attribute("x", "0").toInt()); value->setY(xml.attribute("y", "0").toInt()); } template<> inline void loadValue(const QDomElement& xml, QPainterPath* value) { QDataStream stream(QByteArray::fromBase64(xml.firstChild().toText().data().toUtf8())); stream >> *value; } template<> inline void loadValue(const QDomElement& xml, QByteArray* value) { *value = QByteArray::fromBase64(xml.firstChild().toText().data().toUtf8()); } template<> inline void loadValue(const QDomElement& xml, Sigcore::Script* value) { value->setInterpreter(xml.attribute("interpreter", "")); value->setScript(xml.firstChild().toText().data()); } template inline void loadEnum(const QDomElement& xml, T* value, const QStringList& array) { *value = static_cast(array.indexOf(xml.firstChild().toText().data())); } template inline void loadArray(const QDomElement& xml, QVarLengthArray* value) { QDomElement node = xml.firstChildElement("element"); while (!node.isNull()) { int index = node.attribute("index", "-1").toInt(); if ((0 <= index) && (index < value->size())) loadValue(node, value->data() + index); node = node.nextSiblingElement("element"); } } inline void loadList(const QDomElement& xml, QList* value) { QDomElement node = xml.firstChildElement("element"); int element; while (!node.isNull()) { loadValue(node, &element); if (!value->contains(element)) value->append(element); node = node.nextSiblingElement("element"); } } inline void loadMap(const QDomElement& xml, QMap* value, const QString& valueName) { QDomElement node = xml.firstChildElement("element"); int key; int keyValue; while (!node.isNull()) { loadValue(node.firstChildElement("key"), &key); loadValue(node.firstChildElement(valueName), &keyValue); (*value)[key] = keyValue; node = node.nextSiblingElement("element"); } } template inline void loadMatrix(const QDomElement& xml, Sigcore::Matrix* value) { int height = xml.attribute("height", "1").toInt(); int width = xml.attribute("width", "1").toInt(); value->resize(width, height); QDomElement node = xml.firstChildElement("element"); while (!node.isNull()) { int row = node.attribute("row", "-1").toInt(); int column = node.attribute("column", "-1").toInt(); if ((0 <= row) && (row < height) && (0 <= column) && (column < width)) loadValue(node, &value->operator()(row, column)); node = node.nextSiblingElement("element"); } } template inline QDomElement saveValue(const QString& name, const T& value) { QDomElement element = QDomDocument().createElement(name); element.appendChild(QDomDocument().createTextNode(QVariant::fromValue(value).toString())); return element; } template<> inline QDomElement saveValue(const QString& name, const bool& value) { QDomElement element = QDomDocument().createElement(name); element.appendChild(QDomDocument().createTextNode(value ? "true" : "false")); return element; } template<> inline QDomElement saveValue(const QString& name, const int& value) { QDomElement element = QDomDocument().createElement(name); element.appendChild(QDomDocument().createTextNode(QString::number(value))); return element; } template<> inline QDomElement saveValue(const QString& name, const QString& value) { QDomElement element = QDomDocument().createElement(name); element.appendChild(QDomDocument().createTextNode(value)); return element; } template<> inline QDomElement saveValue(const QString& name, const Sigcore::Fraction& value) { QDomElement element = QDomDocument().createElement(name); element.setAttribute("numerator", value.numerator()); element.setAttribute("denominator", value.denominator()); return element; } template<> inline QDomElement saveValue(const QString& name, const QPoint& value) { QDomElement element = QDomDocument().createElement(name); element.setAttribute("x", value.x()); element.setAttribute("y", value.y()); return element; } template<> inline QDomElement saveValue(const QString& name, const QPainterPath& value) { QByteArray array; QDataStream stream(&array, QIODevice::WriteOnly); stream << value; QDomElement element = QDomDocument().createElement(name); element.appendChild(QDomDocument().createTextNode(array.toBase64())); return element; } template<> inline QDomElement saveValue(const QString& name, const QByteArray& value) { QDomElement element = QDomDocument().createElement(name); element.appendChild(QDomDocument().createTextNode(value.toBase64())); return element; } template<> inline QDomElement saveValue(const QString& name, const Sigcore::Script& value) { QDomElement element = QDomDocument().createElement(name); element.setAttribute("interpreter", value.interpreter()); element.appendChild(QDomDocument().createCDATASection(value.script())); return element; } template inline QDomElement saveEnum(const QString& name, const T& value, const QStringList& array) { return saveValue(name, array[value]); } template inline QDomElement saveArray(const QString& name, const QVarLengthArray& value) { QDomElement element = QDomDocument().createElement(name); for (int i = 0; i < value.size(); ++i) { QDomElement valueElement = saveValue("element", value[i]); valueElement.setAttribute("index", QString::number(i)); element.appendChild(valueElement); } return element; } inline QDomElement saveList(const QString& name, const QList& value) { QDomElement element = QDomDocument().createElement(name); for (int i = 0; i < value.size(); ++i) element.appendChild(saveValue("element", value.at(i))); return element; } inline QDomElement saveMap(const QString& name, const QMap& value, const QString& valueName) { QDomElement element = QDomDocument().createElement(name); const QList keys = value.keys(); foreach (int key, keys) { QDomElement data = QDomDocument().createElement("element"); data.appendChild(saveValue("key", key)); data.appendChild(saveValue(valueName, value[key])); element.appendChild(data); } return element; } template inline QDomElement saveMatrix(const QString& name, const Sigcore::Matrix& value) { QDomElement element = QDomDocument().createElement(name); element.setAttribute("height", value.height()); element.setAttribute("width", value.width()); for (int i = 0; i < value.height(); ++i) { for (int j = 0; j < value.width(); ++j) { QDomElement valueElement = saveValue("element", value(i, j)); valueElement.setAttribute("row", QString::number(i)); valueElement.setAttribute("column", QString::number(j)); element.appendChild(valueElement); } } return element; } } #endif