Move stream operator from AppEnum header

This PR will reduce the compile time of code using AppEnum. 

* AppEnum: Move QTextStream operator to avoid include of QTextStream
* Avoid use of iostream in cafAssert
rator 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.
This commit is contained in:
Magne Sjaastad
2024-03-25 15:14:04 +01:00
committed by GitHub
parent d72a45d3fe
commit ef637e3053
18 changed files with 73 additions and 92 deletions

View File

@@ -25,6 +25,7 @@
#include <QAction>
#include <QDir>
#include <QFileInfo>
#include <QTextStream>
CAF_CMD_SOURCE_INIT( RicRunCommandFileFeature, "RicRunCommandFileFeature" );

View File

@@ -35,6 +35,7 @@
class RigEclipseCaseData;
class QFile;
class QTextStream;
//--------------------------------------------------------------------------------------------------
/// Structure used to cache file position of keywords

View File

@@ -30,6 +30,8 @@
#include <QDateTime>
#include <QString>
#include <chrono>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@@ -27,11 +27,12 @@
#include "RigEclipseCaseData.h"
#include "RigMainGrid.h"
#include "cvfGeometryTools.h"
#include "cafAssert.h"
#include "opm/io/eclipse/EGrid.hpp"
#include <algorithm>
#include <cmath>
//--------------------------------------------------------------------------------------------------
///

View File

@@ -46,6 +46,8 @@
#include "opm/output/eclipse/VectorItems/intehead.hpp"
#include "opm/output/eclipse/VectorItems/well.hpp"
#include <iostream>
using namespace Opm;
//--------------------------------------------------------------------------------------------------

View File

@@ -25,7 +25,7 @@
#include <QString>
#include <list>
#include <set>
#include <map>
class RigCell;
class RimStreamline;

View File

@@ -23,12 +23,13 @@
#include "cvfStructGrid.h"
#include "cvfVector3.h"
#include <QString>
#include <array>
#include <map>
#include <utility>
#include <vector>
#include <QString>
class RigFault;
class RigMainGrid;
class RigGriddedPart3d;

View File

@@ -21,6 +21,8 @@
#include "RiaEclipseUnitTools.h"
#include "RiaLogging.h"
#include "cafAssert.h"
#include <cmath>
//--------------------------------------------------------------------------------------------------

View File

@@ -43,6 +43,8 @@
#include "cafPdmXmlMat4d.h"
#include <QTextStream>
namespace caf
{
template <>

View File

@@ -43,6 +43,8 @@
#include "cafPdmXmlVec3d.h"
#include <QTextStream>
Q_DECLARE_METATYPE( cvf::Vec3d );
namespace caf

View File

@@ -45,6 +45,7 @@
#include "cafPdmScriptIOMessages.h"
#include <QIODevice>
#include <QTextStream>
#define CAF_PDM_InitScriptableField( field, keyword, default, uiName, ... ) \
{ \

View File

@@ -40,7 +40,6 @@
#include <QString>
#include <QStringList>
#include <QTextStream>
#include <vector>
@@ -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(); }
@@ -123,6 +132,9 @@ public:
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 @@ private:
};
} // 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;
}

View File

@@ -1,62 +1,15 @@
#pragma once
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define CAF_ASSERT( expr ) \
do \
{ \
if ( !( expr ) ) /* NOLINT */ \
{ \
std::cout << __FILE__ << ":" << __LINE__ << ": CAF_ASSERT(" << #expr << ") failed" << std::endl; \
std::printf( "%s : %i : CAF_ASSERT( %s ) failed\n", __FILE__, __LINE__, #expr ); \
std::abort(); \
} \
} while ( false )
#if 0 // Bits and pieces for reference to improve the assert
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( disable : 4668 )
#include <windows.h>
#pragma warning( pop )
#endif
void openDebugWindow()
{
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( disable : 4996 )
AllocConsole();
freopen("conin$", "r", stdin);
freopen("conout$", "w", stdout);
freopen("conout$", "w", stderr);
#pragma warning( pop )
#endif
}
void assertAbort()
{
#ifdef _MSC_VER
#if ( _MSC_VER >= 1600 )
//if (::IsDebuggerPresent())
#endif
{
__debugbreak();
}
#endif
std::terminate();
}
#define ASSERT_TEST( expr ) \
do \
{ \
if ( !( expr ) ) \
{ \
std::cout << __FILE__ << ":" << __LINE__ << ": CAF_ASSERT(" << #expr << ") failed" << std::endl; \
assertAbort(); \
} \
} while ( false )
#endif

View File

@@ -1,6 +1,7 @@
#include "cafInternalPdmFilePathStreamOperators.h"
#include <QStringList>
#include <QTextStream>
//--------------------------------------------------------------------------------------------------
///

View File

@@ -2,8 +2,6 @@
#include "cafFilePath.h"
#include <QTextStream>
#include <vector>
QTextStream& operator<<( QTextStream& str, const std::vector<caf::FilePath>& sobj );

View File

@@ -1,6 +1,8 @@
#include <QString>
#include <QTextStream>
#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;
}

View File

@@ -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
@@ -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

View File

@@ -46,6 +46,7 @@
#include <QThread>
#include <algorithm>
#include <iostream>
namespace caf
{