Skip to content

Commit

Permalink
AppEnum: Move QTextStream operator to avoid include of QTextStream
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
magnesj committed Mar 10, 2024
1 parent 1fe9a33 commit 6059759
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
48 changes: 18 additions & 30 deletions Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@

#pragma once

#include "cafAssert.h"

#include <QString>
#include <QStringList>
#include <QTextStream>

#include <vector>

Expand Down Expand Up @@ -98,8 +95,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 T>
class AppEnum
class AppEnum : public AppEnumInterface
{
public:
AppEnum() { m_value = EnumMapper::instance()->defaultValue(); }
Expand All @@ -123,6 +130,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 ); }
Expand Down Expand Up @@ -196,6 +206,9 @@ class AppEnum
public:
void addItem( T enumVal, const QString& text, QString uiText, const QStringList& aliases )
{
// This code is commented out due to cafAssert.h shows up when profiling include files. Leave the code for
// debugging.
/*
// Make sure the alias text is unique for enum
for ( const auto& alias : aliases )
{
Expand All @@ -204,6 +217,7 @@ class AppEnum
CAF_ASSERT( !enumData.isMatching( alias ) );
}
}
*/

// Make sure the text is trimmed, as this text is streamed to XML and will be trimmed when read back
// from XML text https://github.com/OPM/ResInsight/issues/7829
Expand Down Expand Up @@ -343,29 +357,3 @@ class AppEnum
};

} // namespace caf

//==================================================================================================
/// Implementation of stream operators to make PdmField<AppEnum<> > work smoothly
/// Assumes that the stream ends at the end of the enum text
//==================================================================================================

template <typename T>
QTextStream& operator>>( QTextStream& str, caf::AppEnum<T>& 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 <typename T>
QTextStream& operator<<( QTextStream& str, const caf::AppEnum<T>& appEnum )
{
str << appEnum.text();

return str;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <QString>
#include <QTextStream>

#include "cafAppEnum.h"

//--------------------------------------------------------------------------------------------------
/// Specialized read operation for Bool`s
//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <QTextStream>

//==================================================================================================
/// QTextStream Stream operator overloading for bool`s
/// Prints bool`s as "True"/"False", and reads them too
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6059759

Please sign in to comment.