From e3f5efb5e549f3e4f203636d261ecc9b4b5c8e31 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Mon, 11 Mar 2024 12:24:10 +0100 Subject: [PATCH] AppEnum: Move QTextStream operator to avoid include of QTextStream Include file profiling shows that include of QTextStream is a performance issue. Create a non-templated base class for AppEnum. Implement the QTextStream operator for this interface. --- .../cafPdmCore/cafAppEnum.h | 42 +++++++------------ .../cafInternalPdmStreamOperators.cpp | 20 +++++++++ .../cafPdmXml/cafInternalPdmStreamOperators.h | 11 ++++- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h index 7a09b0f0286..02e4ab362ae 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h @@ -40,7 +40,6 @@ #include #include -#include #include @@ -98,8 +97,18 @@ namespace caf /// } //================================================================================================== +class AppEnumInterface +{ + // This non-templated base class is used in cafInternalPdmStreamOperators.h to create a QTextStream operator. Having + // the QTextStream operator working on each templated type was seen as a performance issue when profiling use of + // include files +public: + virtual QString textForSerialization() const = 0; + virtual void setTextForSerialization( const QString& text ) = 0; +}; + template -class AppEnum +class AppEnum : public AppEnumInterface { public: AppEnum() { m_value = EnumMapper::instance()->defaultValue(); } @@ -123,6 +132,9 @@ class AppEnum bool setFromText( const QString& text ) { return EnumMapper::instance()->enumVal( m_value, text ); } bool setFromIndex( size_t index ) { return EnumMapper::instance()->enumVal( m_value, index ); } + QString textForSerialization() const override { return text(); } + void setTextForSerialization( const QString& text ) override { setFromText( text ); } + // Static interface to access the properties of the enum definition static bool isValid( const QString& text ) { return EnumMapper::instance()->isValid( text ); } @@ -343,29 +355,3 @@ class AppEnum }; } // namespace caf - -//================================================================================================== -/// Implementation of stream operators to make PdmField > work smoothly -/// Assumes that the stream ends at the end of the enum text -//================================================================================================== - -template -QTextStream& operator>>( QTextStream& str, caf::AppEnum& appEnum ) -{ - QString text; - str >> text; - - // Make sure the text parsed from a text stream is trimmed - // https://github.com/OPM/ResInsight/issues/7829 - appEnum.setFromText( text.trimmed() ); - - return str; -} - -template -QTextStream& operator<<( QTextStream& str, const caf::AppEnum& appEnum ) -{ - str << appEnum.text(); - - return str; -} diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.cpp b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.cpp index d326cefcb8c..964bacbc26b 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.cpp +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.cpp @@ -1,6 +1,8 @@ #include #include +#include "cafAppEnum.h" + //-------------------------------------------------------------------------------------------------- /// Specialized read operation for Bool`s //-------------------------------------------------------------------------------------------------- @@ -83,3 +85,21 @@ QTextStream& operator<<( QTextStream& str, const QTime& value ) str << text; return str; } + +//-------------------------------------------------------------------------------------------------- +/// Specialized read operation for AppEnum +//-------------------------------------------------------------------------------------------------- +QTextStream& operator>>( QTextStream& str, caf::AppEnumInterface& value ) +{ + QString text; + str >> text; + value.setTextForSerialization( text ); + return str; +} + +QTextStream& operator<<( QTextStream& str, const caf::AppEnumInterface& value ) +{ + QString text = value.textForSerialization(); + str << text; + return str; +} diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.h b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.h index 8983658a4bd..cc7a04201ca 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.h +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.h @@ -1,7 +1,5 @@ #pragma once -#include - //================================================================================================== /// QTextStream Stream operator overloading for bool`s /// Prints bool`s as "True"/"False", and reads them too @@ -34,6 +32,15 @@ QTextStream& operator<<( QTextStream& str, const QDate& value ); QTextStream& operator>>( QTextStream& str, QTime& value ); QTextStream& operator<<( QTextStream& str, const QTime& value ); +// AppEnum +namespace caf +{ +class AppEnumInterface; +} + +QTextStream& operator>>( QTextStream& str, caf::AppEnumInterface& value ); +QTextStream& operator<<( QTextStream& str, const caf::AppEnumInterface& value ); + //================================================================================================== /// QTextStream Stream operator overloading for std::vector of things. /// Makes automated IO of PdmField< std::vector< Whatever > possible as long as