ResInsight/ApplicationCode/CommandFileInterface/Core/RicfFieldCapability.h
2019-09-06 10:57:22 +02:00

260 lines
8.9 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// 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 <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "RicfFieldHandle.h"
#include "RicfMessages.h"
#include "cafAppEnum.h"
#include "cvfColor3.h"
#include <QString>
#include <QTextStream>
template <typename DataType>
struct RicfFieldReader
{
static void readFieldData( DataType& fieldValue,
QTextStream& inputStream,
RicfMessages* errorMessageContainer,
bool stringsAreQuoted = true )
{
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 <typename DataType>
struct RicfFieldWriter
{
static void writeFieldData( const DataType& fieldValue, QTextStream& outputStream, bool quoteStrings = true )
{
outputStream << fieldValue;
}
};
template <>
struct RicfFieldReader<QString>
{
static void readFieldData( QString& fieldValue,
QTextStream& inputStream,
RicfMessages* errorMessageContainer,
bool stringsAreQuoted = true );
};
template <>
struct RicfFieldWriter<QString>
{
static void writeFieldData( const QString& fieldValue, QTextStream& outputStream, bool quoteStrings = true );
};
template <>
struct RicfFieldReader<bool>
{
static void readFieldData( bool& fieldValue,
QTextStream& inputStream,
RicfMessages* errorMessageContainer,
bool stringsAreQuoted = true );
};
template <>
struct RicfFieldWriter<bool>
{
static void writeFieldData( const bool& fieldValue, QTextStream& outputStream, bool quoteStrings = true );
};
template <>
struct RicfFieldReader<cvf::Color3f>
{
static void readFieldData( cvf::Color3f& fieldValue,
QTextStream& inputStream,
RicfMessages* errorMessageContainer,
bool stringsAreQuoted = true );
};
template <>
struct RicfFieldWriter<cvf::Color3f>
{
static void writeFieldData( const cvf::Color3f& fieldValue, QTextStream& outputStream, bool quoteStrings = true );
};
template <typename T>
struct RicfFieldReader<caf::AppEnum<T>>
{
static void readFieldData( caf::AppEnum<T>& fieldValue,
QTextStream& inputStream,
RicfMessages* errorMessageContainer,
bool stringsAreQuoted = true )
{
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 <typename T>
struct RicfFieldWriter<caf::AppEnum<T>>
{
static void writeFieldData( const caf::AppEnum<T>& fieldValue, QTextStream& outputStream, bool quoteStrings = true )
{
outputStream << fieldValue;
}
};
template <typename T>
struct RicfFieldReader<std::vector<T>>
{
static void readFieldData( std::vector<T>& fieldValue,
QTextStream& inputStream,
RicfMessages* errorMessageContainer,
bool stringsAreQuoted = true )
{
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<T>::readFieldData( value, inputStream, errorMessageContainer, true );
fieldValue.push_back( value );
}
}
else
{
errorMessageContainer->addError( "Array argument is missing start '['. " +
errorMessageContainer->currentArgument + "\" argument of the command: \"" +
errorMessageContainer->currentCommand + "\"" );
}
}
};
template <typename T>
struct RicfFieldWriter<std::vector<T>>
{
static void writeFieldData( const std::vector<T>& fieldValue, QTextStream& outputStream, bool quoteStrings = true )
{
outputStream << "[";
for ( size_t i = 0; i < fieldValue.size(); ++i )
{
RicfFieldWriter<T>::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,
bool stringsAreQuoted = true ) override
{
typename FieldType::FieldDataType value;
RicfFieldReader<typename FieldType::FieldDataType>::readFieldData( value,
inputStream,
errorMessageContainer,
stringsAreQuoted );
if ( this->isIOWriteable() )
{
m_field->setValue( value );
}
}
void writeFieldData( QTextStream& outputStream, bool quoteStrings = true ) const override
{
RicfFieldWriter<typename FieldType::FieldDataType>::writeFieldData( m_field->value(), outputStream, quoteStrings );
}
private:
FieldType* m_field;
};
template <typename FieldType>
void AddRicfCapabilityToField( FieldType* field )
{
if ( field->template capability<RicfFieldCapability<FieldType>>() == nullptr )
{
new RicfFieldCapability<FieldType>( field, true );
}
}