mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Compiling and working test application
This commit is contained in:
parent
2f86743b44
commit
26227dbcfc
@ -36,8 +36,10 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "cafAppEnumMapper.h"
|
||||||
#include "cafAssert.h"
|
#include "cafAssert.h"
|
||||||
|
|
||||||
|
#include "cafTypeNameHelper.h"
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
@ -102,7 +104,11 @@ template <class T>
|
|||||||
class AppEnum
|
class AppEnum
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AppEnum() { m_value = EnumMapper::instance()->defaultValue(); }
|
AppEnum()
|
||||||
|
{
|
||||||
|
auto enumInteger = EnumMapper::instance()->defaultEnumValue( caf::cafTypeName<T>() );
|
||||||
|
m_value = caf::convertToEnum<T>( enumInteger );
|
||||||
|
}
|
||||||
AppEnum( T value )
|
AppEnum( T value )
|
||||||
: m_value( value )
|
: m_value( value )
|
||||||
{
|
{
|
||||||
@ -111,41 +117,65 @@ public:
|
|||||||
operator T() const { return m_value; }
|
operator T() const { return m_value; }
|
||||||
|
|
||||||
T value() const { return m_value; }
|
T value() const { return m_value; }
|
||||||
size_t index() const { return EnumMapper::instance()->index( m_value ); }
|
size_t index() const
|
||||||
QString text() const { return EnumMapper::instance()->text( m_value ); }
|
{
|
||||||
QString uiText() const { return EnumMapper::instance()->uiText( m_value ); }
|
return EnumMapper::instance()->index( caf::cafTypeName<T>(), caf::convertToInteger( m_value ) );
|
||||||
|
}
|
||||||
|
QString text() const
|
||||||
|
{
|
||||||
|
return EnumMapper::instance()->text( caf::cafTypeName<T>(), caf::convertToInteger( m_value ) );
|
||||||
|
}
|
||||||
|
QString uiText() const
|
||||||
|
{
|
||||||
|
return EnumMapper::instance()->uiText( caf::cafTypeName<T>(), caf::convertToInteger( m_value ) );
|
||||||
|
}
|
||||||
|
|
||||||
AppEnum& operator=( T value )
|
AppEnum& operator=( T value )
|
||||||
{
|
{
|
||||||
m_value = value;
|
m_value = value;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
bool setFromText( const QString& text ) { return EnumMapper::instance()->enumVal( m_value, text ); }
|
bool setFromText( const QString& text )
|
||||||
bool setFromIndex( size_t index ) { return EnumMapper::instance()->enumVal( m_value, index ); }
|
{
|
||||||
|
m_value = fromText( text );
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool setFromIndex( size_t index )
|
||||||
|
{
|
||||||
|
m_value = fromIndex( index );
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// Static interface to access the properties of the enum definition
|
// Static interface to access the properties of the enum definition
|
||||||
|
|
||||||
static bool isValid( const QString& text ) { return EnumMapper::instance()->isValid( text ); }
|
static bool isValid( const QString& text ) { return EnumMapper::instance()->isValid( text ); }
|
||||||
static bool isValid( size_t index ) { return index < EnumMapper::instance()->size(); }
|
static bool isValid( size_t index ) { return index < EnumMapper::instance()->size(); }
|
||||||
static size_t size() { return EnumMapper::instance()->size(); }
|
static size_t size() { return EnumMapper::instance()->size( caf::cafTypeName<T>() ); }
|
||||||
|
|
||||||
static QStringList uiTexts() { return EnumMapper::instance()->uiTexts(); }
|
static QStringList uiTexts() { return EnumMapper::instance()->uiTexts(); }
|
||||||
static T fromIndex( size_t idx )
|
static T fromIndex( size_t idx )
|
||||||
{
|
{
|
||||||
T val;
|
auto enumInteger = EnumMapper::instance()->fromIndex( caf::cafTypeName<T>(), idx );
|
||||||
EnumMapper::instance()->enumVal( val, idx );
|
return caf::convertToEnum<T>( enumInteger );
|
||||||
return val;
|
|
||||||
}
|
}
|
||||||
static T fromText( const QString& text )
|
static T fromText( const QString& text )
|
||||||
{
|
{
|
||||||
T val;
|
auto enumInteger = EnumMapper::instance()->fromText( caf::cafTypeName<T>(), text );
|
||||||
EnumMapper::instance()->enumVal( val, text );
|
return caf::convertToEnum<T>( enumInteger );
|
||||||
return val;
|
}
|
||||||
|
static size_t index( T enumValue )
|
||||||
|
{
|
||||||
|
return EnumMapper::instance()->index( caf::cafTypeName<T>(), caf::convertToInteger( enumValue ) );
|
||||||
|
}
|
||||||
|
static QString text( T enumValue )
|
||||||
|
{
|
||||||
|
return EnumMapper::instance()->text( caf::cafTypeName<T>(), caf::convertToInteger( enumValue ) );
|
||||||
}
|
}
|
||||||
static size_t index( T enumValue ) { return EnumMapper::instance()->index( enumValue ); }
|
|
||||||
static QString text( T enumValue ) { return EnumMapper::instance()->text( enumValue ); }
|
|
||||||
static QString textFromIndex( size_t idx ) { return text( fromIndex( idx ) ); }
|
static QString textFromIndex( size_t idx ) { return text( fromIndex( idx ) ); }
|
||||||
static QString uiText( T enumValue ) { return EnumMapper::instance()->uiText( enumValue ); }
|
static QString uiText( T enumValue )
|
||||||
|
{
|
||||||
|
return EnumMapper::instance()->uiText( caf::cafTypeName<T>(), caf::convertToInteger( enumValue ) );
|
||||||
|
}
|
||||||
static QString uiTextFromIndex( size_t idx ) { return uiText( fromIndex( idx ) ); }
|
static QString uiTextFromIndex( size_t idx ) { return uiText( fromIndex( idx ) ); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -157,10 +187,15 @@ private:
|
|||||||
static void setUp();
|
static void setUp();
|
||||||
static void addItem( T enumVal, const QString& text, const QString& uiText, const QStringList& aliases = {} )
|
static void addItem( T enumVal, const QString& text, const QString& uiText, const QStringList& aliases = {} )
|
||||||
{
|
{
|
||||||
EnumMapper::instance()->addItem( enumVal, text, uiText, aliases );
|
auto key = caf::cafTypeName<T>();
|
||||||
|
auto enumInteger = caf::convertToInteger<T>( enumVal );
|
||||||
|
EnumMapper::instance()->addItem( key, enumInteger, text, uiText, aliases );
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setDefault( T defaultEnumValue ) { EnumMapper::instance()->setDefault( defaultEnumValue ); }
|
static void setDefault( T defaultEnumValue )
|
||||||
|
{
|
||||||
|
EnumMapper::instance()->setDefault( caf::cafTypeName<T>(), caf::convertToInteger<T>( defaultEnumValue ) );
|
||||||
|
}
|
||||||
|
|
||||||
T m_value;
|
T m_value;
|
||||||
|
|
||||||
@ -173,6 +208,7 @@ private:
|
|||||||
|
|
||||||
class EnumMapper
|
class EnumMapper
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
private:
|
private:
|
||||||
class EnumData
|
class EnumData
|
||||||
{
|
{
|
||||||
@ -185,17 +221,26 @@ private:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isMatching( const QString& text ) const { return ( text == m_text || m_aliases.contains( text ) ); }
|
bool isMatching( const QString& text ) const { return ( text == m_text || m_aliases.contains( text )
|
||||||
|
); }
|
||||||
|
|
||||||
T m_enumVal;
|
T m_enumVal;
|
||||||
QString m_text;
|
QString m_text;
|
||||||
QString m_uiText;
|
QString m_uiText;
|
||||||
QStringList m_aliases;
|
QStringList m_aliases;
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void addItem( T enumVal, const QString& text, QString uiText, const QStringList& aliases )
|
void addItem( T enumVal, const QString& text, QString uiText, const QStringList& aliases )
|
||||||
{
|
{
|
||||||
|
caf::AppEnumMapper::instance()->addItem( caf::cafTypeName<T>(),
|
||||||
|
caf::convertToInteger<T>( enumVal ),
|
||||||
|
text,
|
||||||
|
uiText,
|
||||||
|
aliases );
|
||||||
|
|
||||||
|
/*
|
||||||
// Make sure the alias text is unique for enum
|
// Make sure the alias text is unique for enum
|
||||||
for ( const auto& alias : aliases )
|
for ( const auto& alias : aliases )
|
||||||
{
|
{
|
||||||
@ -205,11 +250,14 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure the text is trimmed, as this text is streamed to XML and will be trimmed when read back
|
// 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
|
// from XML text https://github.com/OPM/ResInsight/issues/7829
|
||||||
instance()->m_mapping.push_back( EnumData( enumVal, text.trimmed(), uiText, aliases ) );
|
instance()->m_mapping.push_back( EnumData( enumVal, text.trimmed(), uiText, aliases ) );
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
static EnumMapper* instance()
|
static EnumMapper* instance()
|
||||||
{
|
{
|
||||||
static EnumMapper storedInstance;
|
static EnumMapper storedInstance;
|
||||||
@ -221,6 +269,18 @@ private:
|
|||||||
}
|
}
|
||||||
return &storedInstance;
|
return &storedInstance;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
static caf::AppEnumMapper* instance()
|
||||||
|
{
|
||||||
|
static bool isInitialized = false;
|
||||||
|
if ( !isInitialized )
|
||||||
|
{
|
||||||
|
isInitialized = true;
|
||||||
|
AppEnum<T>::setUp();
|
||||||
|
}
|
||||||
|
return caf::AppEnumMapper::instance();
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
|
||||||
void setDefault( T defaultEnumValue )
|
void setDefault( T defaultEnumValue )
|
||||||
{
|
{
|
||||||
@ -339,6 +399,7 @@ private:
|
|||||||
std::vector<EnumData> m_mapping;
|
std::vector<EnumData> m_mapping;
|
||||||
T m_defaultValue;
|
T m_defaultValue;
|
||||||
bool m_defaultValueIsSet;
|
bool m_defaultValueIsSet;
|
||||||
|
*/
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -28,18 +28,23 @@ void AppEnumMapper::addItem( const std::string& enumKey,
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void AppEnumMapper::addDefaultItem( const std::string& enumKey,
|
void AppEnumMapper::setDefault( const std::string& enumKey, int enumValue )
|
||||||
int enumValue,
|
|
||||||
const QString& text,
|
|
||||||
const QString& uiText,
|
|
||||||
const QStringList& aliases /*= {} */ )
|
|
||||||
{
|
{
|
||||||
// Make sure the text is trimmed, as this text is streamed to XML and will be trimmed when read back
|
auto it = m_enumMap.find( enumKey );
|
||||||
// from XML text https://github.com/OPM/ResInsight/issues/7829
|
if ( it != m_enumMap.end() )
|
||||||
auto enumData = AppEnumMapper::EnumData( enumValue, text.trimmed(), uiText, aliases );
|
{
|
||||||
|
for ( auto& enumData : it->second )
|
||||||
|
{
|
||||||
|
if ( enumData.m_enumVal == enumValue )
|
||||||
|
{
|
||||||
enumData.m_isDefault = true;
|
enumData.m_isDefault = true;
|
||||||
|
}
|
||||||
m_enumMap[enumKey].emplace_back( enumData );
|
else
|
||||||
|
{
|
||||||
|
enumData.m_isDefault = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -70,8 +75,13 @@ int AppEnumMapper::defaultEnumValue( const std::string& enumKey ) const
|
|||||||
return enumData.m_enumVal;
|
return enumData.m_enumVal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( !it->second.empty() )
|
||||||
|
{
|
||||||
|
return it->second.front().m_enumVal;
|
||||||
}
|
}
|
||||||
return -1;
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -134,7 +144,7 @@ QString AppEnumMapper::uiText( const std::string& enumKey, int enumValue ) const
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
int AppEnumMapper::enumValue( const std::string& enumKey, const QString& text ) const
|
int AppEnumMapper::fromText( const std::string& enumKey, const QString& text ) const
|
||||||
{
|
{
|
||||||
auto it = m_enumMap.find( enumKey );
|
auto it = m_enumMap.find( enumKey );
|
||||||
if ( it != m_enumMap.end() )
|
if ( it != m_enumMap.end() )
|
||||||
@ -147,13 +157,13 @@ int AppEnumMapper::enumValue( const std::string& enumKey, const QString& text )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return defaultEnumValue( enumKey );
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
int AppEnumMapper::enumValue( const std::string& enumKey, int enumIndex ) const
|
int AppEnumMapper::fromIndex( const std::string& enumKey, size_t enumIndex ) const
|
||||||
{
|
{
|
||||||
auto it = m_enumMap.find( enumKey );
|
auto it = m_enumMap.find( enumKey );
|
||||||
if ( it != m_enumMap.end() )
|
if ( it != m_enumMap.end() )
|
||||||
@ -163,7 +173,7 @@ int AppEnumMapper::enumValue( const std::string& enumKey, int enumIndex ) const
|
|||||||
return it->second[enumIndex].m_enumVal;
|
return it->second[enumIndex].m_enumVal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return defaultEnumValue( enumKey );
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
@ -39,11 +39,7 @@ public:
|
|||||||
const QString& uiText,
|
const QString& uiText,
|
||||||
const QStringList& aliases = {} );
|
const QStringList& aliases = {} );
|
||||||
|
|
||||||
void addDefaultItem( const std::string& enumKey,
|
void setDefault( const std::string& enumKey, int enumValue );
|
||||||
int enumValue,
|
|
||||||
const QString& text,
|
|
||||||
const QString& uiText,
|
|
||||||
const QStringList& aliases = {} );
|
|
||||||
|
|
||||||
size_t size( const std::string& enumKey ) const;
|
size_t size( const std::string& enumKey ) const;
|
||||||
size_t index( const std::string& enumKey, int enumValue ) const;
|
size_t index( const std::string& enumKey, int enumValue ) const;
|
||||||
@ -51,8 +47,8 @@ public:
|
|||||||
QString uiText( const std::string& enumKey, int enumValue ) const;
|
QString uiText( const std::string& enumKey, int enumValue ) const;
|
||||||
|
|
||||||
int defaultEnumValue( const std::string& enumKey ) const;
|
int defaultEnumValue( const std::string& enumKey ) const;
|
||||||
int enumValue( const std::string& enumKey, const QString& text ) const;
|
int fromText( const std::string& enumKey, const QString& text ) const;
|
||||||
int enumValue( const std::string& enumKey, int enumIndex ) const;
|
int fromIndex( const std::string& enumKey, size_t enumIndex ) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<std::string, std::vector<EnumData>> m_enumMap;
|
std::map<std::string, std::vector<EnumData>> m_enumMap;
|
||||||
|
@ -20,6 +20,8 @@ enum class TestEnum2
|
|||||||
ValueC
|
ValueC
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//#define addItem( enumValue, text, uiText ) addItem( enumValue, #enumValue, text, uiText )
|
||||||
|
|
||||||
namespace caf
|
namespace caf
|
||||||
{
|
{
|
||||||
template <>
|
template <>
|
||||||
@ -30,6 +32,17 @@ void caf::AppEnum<TestEnum>::setUp()
|
|||||||
addItem( TestEnum::Value3, "VALUE_3", "Val 3" );
|
addItem( TestEnum::Value3, "VALUE_3", "Val 3" );
|
||||||
addItem( TestEnum::Value4, "VALUE_4", "Val 4" );
|
addItem( TestEnum::Value4, "VALUE_4", "Val 4" );
|
||||||
setDefault( TestEnum::Value2 );
|
setDefault( TestEnum::Value2 );
|
||||||
|
|
||||||
|
auto instance = caf::AppEnumMapper::instance();
|
||||||
|
instance->addItem( caf::cafTypeName<TestEnum>(), caf::convertToInteger<TestEnum>( TestEnum::Value1 ), "VALUE_1", "Val 1" );
|
||||||
|
instance->addItem( caf::cafTypeName<TestEnum>(), caf::convertToInteger<TestEnum>( TestEnum::Value2 ), "VALUE_2", "Val 2" );
|
||||||
|
instance->addItem( caf::cafTypeName<TestEnum>(), caf::convertToInteger<TestEnum>( TestEnum::Value3 ), "VALUE_3", "Val 3" );
|
||||||
|
instance->addItem( caf::cafTypeName<TestEnum>(), caf::convertToInteger<TestEnum>( TestEnum::Value4 ), "VALUE_4", "Val 4" );
|
||||||
|
instance->setDefault( caf::cafTypeName<TestEnum>(), caf::convertToInteger<TestEnum>( TestEnum::Value2 ) );
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
@ -92,3 +105,18 @@ TEST( PdmEnumMapperTest, ConverEnumToInteger )
|
|||||||
EXPECT_EQ( enumValue, sourceEnumValue );
|
EXPECT_EQ( enumValue, sourceEnumValue );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
TEST( PdmEnumMapperTest, GetUiText )
|
||||||
|
{
|
||||||
|
caf::AppEnumMapper* instance = caf::AppEnumMapper::instance();
|
||||||
|
|
||||||
|
{
|
||||||
|
auto TestEnum_key = caf::cafTypeName<TestEnum>();
|
||||||
|
auto intValue = caf::convertToInteger( TestEnum::Value1 );
|
||||||
|
|
||||||
|
instance->uiText( TestEnum_key, intValue );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user