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

@@ -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::abort(); \
} \
#define CAF_ASSERT( expr ) \
do \
{ \
if ( !( expr ) ) /* NOLINT */ \
{ \
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