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 11, 2024
1 parent 3f84249 commit e3f5efb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
42 changes: 14 additions & 28 deletions Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

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

#include <vector>

Expand Down Expand Up @@ -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 T>
class AppEnum
class AppEnum : public AppEnumInterface
{
public:
AppEnum() { m_value = EnumMapper::instance()->defaultValue(); }
Expand All @@ -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 ); }
Expand Down Expand Up @@ -343,29 +355,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 e3f5efb

Please sign in to comment.