///////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2017 Statoil ASA // // ResInsight 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. // // ResInsight 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 at // for more details. // ///////////////////////////////////////////////////////////////////////////////// #pragma once #include "RicfFieldHandle.h" #include "RicfMessages.h" #include "cafAppEnum.h" #include #include template struct RicfFieldReader { static void readFieldData(DataType & fieldValue, QTextStream& inputStream, RicfMessages* errorMessageContainer) { inputStream >> fieldValue; if (inputStream.status() == QTextStream::ReadCorruptData) { errorMessageContainer->addError("Argument value is unreadable in the argument: \"" + errorMessageContainer->currentArgument + "\" in the command: \"" + errorMessageContainer->currentCommand + "\"" ); inputStream.setStatus( QTextStream::Ok); } } }; template struct RicfFieldWriter { static void writeFieldData(const DataType & fieldValue, QTextStream& outputStream) { outputStream << fieldValue; } }; template <> struct RicfFieldReader { static void readFieldData(QString & fieldValue, QTextStream& inputStream, RicfMessages* errorMessageContainer); }; template <> struct RicfFieldWriter { static void writeFieldData(const QString & fieldValue, QTextStream& outputStream); }; template <> struct RicfFieldReader { static void readFieldData(bool& fieldValue, QTextStream& inputStream, RicfMessages* errorMessageContainer); }; template <> struct RicfFieldWriter { static void writeFieldData(const bool& fieldValue, QTextStream& outputStream); }; template struct RicfFieldReader< caf::AppEnum > { static void readFieldData(caf::AppEnum& fieldValue, QTextStream& inputStream, RicfMessages* errorMessageContainer) { errorMessageContainer->skipWhiteSpaceWithLineNumberCount(inputStream); QString accumulatedFieldValue; QChar nextChar; QChar currentChar; while (!inputStream.atEnd()) { nextChar = errorMessageContainer->peekNextChar(inputStream); if (nextChar.isLetterOrNumber() || nextChar == QChar('_')) { currentChar = errorMessageContainer->readCharWithLineNumberCount(inputStream); accumulatedFieldValue += currentChar; } else { break; } } if (!fieldValue.setFromText(accumulatedFieldValue)) { // Unexpected enum value // Error message errorMessageContainer->addError("Argument must be valid enum value. " + errorMessageContainer->currentArgument + "\" argument of the command: \"" + errorMessageContainer->currentCommand + "\""); } } }; template struct RicfFieldWriter< caf::AppEnum > { static void writeFieldData(const caf::AppEnum& fieldValue, QTextStream& outputStream) { outputStream << fieldValue; } }; template struct RicfFieldReader< std::vector > { static void readFieldData(std::vector& fieldValue, QTextStream& inputStream, RicfMessages* errorMessageContainer) { errorMessageContainer->skipWhiteSpaceWithLineNumberCount(inputStream); QChar chr = errorMessageContainer->readCharWithLineNumberCount(inputStream); if (chr == QChar('[')) { while (!inputStream.atEnd()) { errorMessageContainer->skipWhiteSpaceWithLineNumberCount(inputStream); QChar nextChar = errorMessageContainer->peekNextChar(inputStream); if (nextChar == QChar(']')) { nextChar = errorMessageContainer->readCharWithLineNumberCount(inputStream); break; } else if (nextChar == QChar(',')) { nextChar = errorMessageContainer->readCharWithLineNumberCount(inputStream); errorMessageContainer->skipWhiteSpaceWithLineNumberCount(inputStream); } T value; RicfFieldReader::readFieldData(value, inputStream, errorMessageContainer); fieldValue.push_back(value); } } else { errorMessageContainer->addError("Array argument is missing start '['. " + errorMessageContainer->currentArgument + "\" argument of the command: \"" + errorMessageContainer->currentCommand + "\"" ); } } }; template struct RicfFieldWriter< std::vector > { static void writeFieldData(const std::vector& fieldValue, QTextStream& outputStream) { outputStream << "["; for (size_t i = 0; i < fieldValue.size(); ++i) { RicfFieldWriter::writeFieldData(fieldValue[i], outputStream); if (i < fieldValue.size() - 1) { outputStream << ", "; } } outputStream << "]"; } }; //================================================================================================== // // // //================================================================================================== template < typename FieldType> class RicfFieldCapability : public RicfFieldHandle { public: RicfFieldCapability(FieldType* field, bool giveOwnership) : RicfFieldHandle(field, giveOwnership) { m_field = field; } // Xml Serializing public: void readFieldData (QTextStream& inputStream, caf::PdmObjectFactory* objectFactory, RicfMessages* errorMessageContainer) override { //m_field->xmlCapability()->assertValid(); typename FieldType::FieldDataType value; RicfFieldReader::readFieldData(value, inputStream, errorMessageContainer); m_field->setValue(value); } void writeFieldData(QTextStream& outputStream) const override { //m_field->xmlCapability()->assertValid(); RicfFieldWriter::writeFieldData(m_field->value(), outputStream); } private: FieldType* m_field; }; template void AddRicfCapabilityToField(FieldType* field) { if(field->template capability< RicfFieldCapability >() == nullptr) { new RicfFieldCapability(field, true); } }