From 26227dbcfcd3d350cdfb74d896c35f3a2a72399d Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Fri, 8 Mar 2024 16:43:09 +0100 Subject: [PATCH] Compiling and working test application --- .../cafPdmCore/cafAppEnum.h | 371 ++++++++++-------- .../cafPdmCore/cafAppEnumMapper.cpp | 42 +- .../cafPdmCore/cafAppEnumMapper.h | 10 +- .../cafPdmEnumMapperTest.cpp | 28 ++ 4 files changed, 273 insertions(+), 178 deletions(-) diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h index 7a09b0f028..9b9361c0fe 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h @@ -36,8 +36,10 @@ #pragma once +#include "cafAppEnumMapper.h" #include "cafAssert.h" +#include "cafTypeNameHelper.h" #include #include #include @@ -102,7 +104,11 @@ template class AppEnum { public: - AppEnum() { m_value = EnumMapper::instance()->defaultValue(); } + AppEnum() + { + auto enumInteger = EnumMapper::instance()->defaultEnumValue( caf::cafTypeName() ); + m_value = caf::convertToEnum( enumInteger ); + } AppEnum( T value ) : m_value( value ) { @@ -110,42 +116,66 @@ class AppEnum operator T() const { return m_value; } - T value() const { return m_value; } - size_t index() const { return EnumMapper::instance()->index( m_value ); } - QString text() const { return EnumMapper::instance()->text( m_value ); } - QString uiText() const { return EnumMapper::instance()->uiText( m_value ); } + T value() const { return m_value; } + size_t index() const + { + return EnumMapper::instance()->index( caf::cafTypeName(), caf::convertToInteger( m_value ) ); + } + QString text() const + { + return EnumMapper::instance()->text( caf::cafTypeName(), caf::convertToInteger( m_value ) ); + } + QString uiText() const + { + return EnumMapper::instance()->uiText( caf::cafTypeName(), caf::convertToInteger( m_value ) ); + } AppEnum& operator=( T value ) { m_value = value; return *this; } - bool setFromText( const QString& text ) { return EnumMapper::instance()->enumVal( m_value, text ); } - bool setFromIndex( size_t index ) { return EnumMapper::instance()->enumVal( m_value, index ); } + bool setFromText( const QString& text ) + { + 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 bool isValid( const QString& text ) { return EnumMapper::instance()->isValid( text ); } 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() ); } static QStringList uiTexts() { return EnumMapper::instance()->uiTexts(); } static T fromIndex( size_t idx ) { - T val; - EnumMapper::instance()->enumVal( val, idx ); - return val; + auto enumInteger = EnumMapper::instance()->fromIndex( caf::cafTypeName(), idx ); + return caf::convertToEnum( enumInteger ); } static T fromText( const QString& text ) { - T val; - EnumMapper::instance()->enumVal( val, text ); - return val; + auto enumInteger = EnumMapper::instance()->fromText( caf::cafTypeName(), text ); + return caf::convertToEnum( enumInteger ); + } + static size_t index( T enumValue ) + { + return EnumMapper::instance()->index( caf::cafTypeName(), caf::convertToInteger( enumValue ) ); + } + static QString text( T enumValue ) + { + return EnumMapper::instance()->text( caf::cafTypeName(), 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 uiText( T enumValue ) { return EnumMapper::instance()->uiText( enumValue ); } + static QString uiText( T enumValue ) + { + return EnumMapper::instance()->uiText( caf::cafTypeName(), caf::convertToInteger( enumValue ) ); + } static QString uiTextFromIndex( size_t idx ) { return uiText( fromIndex( idx ) ); } private: @@ -157,10 +187,15 @@ class AppEnum static void setUp(); 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(); + auto enumInteger = caf::convertToInteger( 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(), caf::convertToInteger( defaultEnumValue ) ); + } T m_value; @@ -173,172 +208,198 @@ class AppEnum class EnumMapper { - private: - class EnumData - { - public: - EnumData( T enumVal, const QString& text, const QString& uiText, const QStringList& aliases ) - : m_enumVal( enumVal ) - , m_text( text ) - , m_uiText( uiText ) - , m_aliases( aliases ) - { - } - - bool isMatching( const QString& text ) const { return ( text == m_text || m_aliases.contains( text ) ); } - - T m_enumVal; - QString m_text; - QString m_uiText; - QStringList m_aliases; - }; + /* + private: + class EnumData + { + public: + EnumData( T enumVal, const QString& text, const QString& uiText, const QStringList& aliases ) + : m_enumVal( enumVal ) + , m_text( text ) + , m_uiText( uiText ) + , m_aliases( aliases ) + { + } + + bool isMatching( const QString& text ) const { return ( text == m_text || m_aliases.contains( text ) + ); } + + T m_enumVal; + QString m_text; + QString m_uiText; + QStringList m_aliases; + }; + */ public: void addItem( T enumVal, const QString& text, QString uiText, const QStringList& aliases ) { - // Make sure the alias text is unique for enum - for ( const auto& alias : aliases ) - { - for ( const auto& enumData : instance()->m_mapping ) - { - 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 - instance()->m_mapping.push_back( EnumData( enumVal, text.trimmed(), uiText, aliases ) ); + caf::AppEnumMapper::instance()->addItem( caf::cafTypeName(), + caf::convertToInteger( enumVal ), + text, + uiText, + aliases ); + + /* + // Make sure the alias text is unique for enum + for ( const auto& alias : aliases ) + { + for ( const auto& enumData : instance()->m_mapping ) + { + 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 + instance()->m_mapping.push_back( EnumData( enumVal, text.trimmed(), uiText, aliases ) ); + */ } - static EnumMapper* instance() + /* + static EnumMapper* instance() + { + static EnumMapper storedInstance; + static bool isInitialized = false; + if ( !isInitialized ) + { + isInitialized = true; + AppEnum::setUp(); + } + return &storedInstance; + } + */ + static caf::AppEnumMapper* instance() { - static EnumMapper storedInstance; - static bool isInitialized = false; + static bool isInitialized = false; if ( !isInitialized ) { isInitialized = true; AppEnum::setUp(); } - return &storedInstance; + return caf::AppEnumMapper::instance(); } + /* - void setDefault( T defaultEnumValue ) - { - m_defaultValue = defaultEnumValue; - m_defaultValueIsSet = true; - } - - T defaultValue() const - { - if ( m_defaultValueIsSet ) - { - return m_defaultValue; - } - else - { - // CAF_ASSERT(m_mapping.size()); - return m_mapping[0].m_enumVal; - } - } - - bool isValid( const QString& text ) const - { - size_t idx; - for ( idx = 0; idx < m_mapping.size(); ++idx ) - { - if ( text == m_mapping[idx].m_text ) return true; - } + void setDefault( T defaultEnumValue ) + { + m_defaultValue = defaultEnumValue; + m_defaultValueIsSet = true; + } - return false; - } + T defaultValue() const + { + if ( m_defaultValueIsSet ) + { + return m_defaultValue; + } + else + { + // CAF_ASSERT(m_mapping.size()); + return m_mapping[0].m_enumVal; + } + } - size_t size() const { return m_mapping.size(); } + bool isValid( const QString& text ) const + { + size_t idx; + for ( idx = 0; idx < m_mapping.size(); ++idx ) + { + if ( text == m_mapping[idx].m_text ) return true; + } - bool enumVal( T& value, const QString& text ) const - { - value = defaultValue(); + return false; + } - QString trimmedText = text.trimmed(); + size_t size() const { return m_mapping.size(); } - for ( size_t idx = 0; idx < m_mapping.size(); ++idx ) - { - // Make sure the text parsed from a text stream is trimmed - // https://github.com/OPM/ResInsight/issues/7829 - if ( m_mapping[idx].isMatching( trimmedText ) ) + bool enumVal( T& value, const QString& text ) const { - value = m_mapping[idx].m_enumVal; - return true; + value = defaultValue(); + + QString trimmedText = text.trimmed(); + + for ( size_t idx = 0; idx < m_mapping.size(); ++idx ) + { + // Make sure the text parsed from a text stream is trimmed + // https://github.com/OPM/ResInsight/issues/7829 + if ( m_mapping[idx].isMatching( trimmedText ) ) + { + value = m_mapping[idx].m_enumVal; + return true; + } + } + return false; } - } - return false; - } - bool enumVal( T& value, size_t index ) const - { - value = defaultValue(); - if ( index < m_mapping.size() ) - { - value = m_mapping[index].m_enumVal; - return true; - } - else - return false; - } + bool enumVal( T& value, size_t index ) const + { + value = defaultValue(); + if ( index < m_mapping.size() ) + { + value = m_mapping[index].m_enumVal; + return true; + } + else + return false; + } - size_t index( T enumValue ) const - { - size_t idx; - for ( idx = 0; idx < m_mapping.size(); ++idx ) - { - if ( enumValue == m_mapping[idx].m_enumVal ) return idx; - } + size_t index( T enumValue ) const + { + size_t idx; + for ( idx = 0; idx < m_mapping.size(); ++idx ) + { + if ( enumValue == m_mapping[idx].m_enumVal ) return idx; + } - return idx; - } + return idx; + } - QString uiText( T value ) const - { - size_t idx; - for ( idx = 0; idx < m_mapping.size(); ++idx ) - { - if ( value == m_mapping[idx].m_enumVal ) return m_mapping[idx].m_uiText; - } - return ""; - } + QString uiText( T value ) const + { + size_t idx; + for ( idx = 0; idx < m_mapping.size(); ++idx ) + { + if ( value == m_mapping[idx].m_enumVal ) return m_mapping[idx].m_uiText; + } + return ""; + } - QStringList uiTexts() const - { - QStringList uiTextList; - size_t idx; - for ( idx = 0; idx < m_mapping.size(); ++idx ) - { - uiTextList.append( m_mapping[idx].m_uiText ); - } - return uiTextList; - } + QStringList uiTexts() const + { + QStringList uiTextList; + size_t idx; + for ( idx = 0; idx < m_mapping.size(); ++idx ) + { + uiTextList.append( m_mapping[idx].m_uiText ); + } + return uiTextList; + } - QString text( T value ) const - { - size_t idx; - for ( idx = 0; idx < m_mapping.size(); ++idx ) - { - if ( value == m_mapping[idx].m_enumVal ) return m_mapping[idx].m_text; - } - return ""; - } + QString text( T value ) const + { + size_t idx; + for ( idx = 0; idx < m_mapping.size(); ++idx ) + { + if ( value == m_mapping[idx].m_enumVal ) return m_mapping[idx].m_text; + } + return ""; + } - private: - EnumMapper() - : m_defaultValue( T() ) - , m_defaultValueIsSet( false ) - { - } + private: + EnumMapper() + : m_defaultValue( T() ) + , m_defaultValueIsSet( false ) + { + } - friend class AppEnum; + friend class AppEnum; - std::vector m_mapping; - T m_defaultValue; - bool m_defaultValueIsSet; + std::vector m_mapping; + T m_defaultValue; + bool m_defaultValueIsSet; + */ }; }; diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.cpp b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.cpp index 498ccd3d77..74427b7523 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.cpp +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.cpp @@ -28,18 +28,23 @@ void AppEnumMapper::addItem( const std::string& enumKey, //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void AppEnumMapper::addDefaultItem( const std::string& enumKey, - int enumValue, - const QString& text, - const QString& uiText, - const QStringList& aliases /*= {} */ ) +void AppEnumMapper::setDefault( const std::string& enumKey, int enumValue ) { - // 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 - auto enumData = AppEnumMapper::EnumData( enumValue, text.trimmed(), uiText, aliases ); - enumData.m_isDefault = true; - - m_enumMap[enumKey].emplace_back( enumData ); + auto it = m_enumMap.find( enumKey ); + if ( it != m_enumMap.end() ) + { + for ( auto& enumData : it->second ) + { + if ( enumData.m_enumVal == enumValue ) + { + enumData.m_isDefault = true; + } + else + { + enumData.m_isDefault = false; + } + } + } } //-------------------------------------------------------------------------------------------------- @@ -70,8 +75,13 @@ int AppEnumMapper::defaultEnumValue( const std::string& enumKey ) const 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 ); 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 ); 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 -1; + return defaultEnumValue( enumKey ); } //-------------------------------------------------------------------------------------------------- diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.h b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.h index cb5f8e6f4f..34259fd97b 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.h +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.h @@ -39,11 +39,7 @@ class AppEnumMapper const QString& uiText, const QStringList& aliases = {} ); - void addDefaultItem( const std::string& enumKey, - int enumValue, - const QString& text, - const QString& uiText, - const QStringList& aliases = {} ); + void setDefault( const std::string& enumKey, int enumValue ); size_t size( const std::string& enumKey ) const; size_t index( const std::string& enumKey, int enumValue ) const; @@ -51,8 +47,8 @@ class AppEnumMapper QString uiText( const std::string& enumKey, int enumValue ) const; int defaultEnumValue( const std::string& enumKey ) const; - int enumValue( const std::string& enumKey, const QString& text ) const; - int enumValue( const std::string& enumKey, int enumIndex ) const; + int fromText( const std::string& enumKey, const QString& text ) const; + int fromIndex( const std::string& enumKey, size_t enumIndex ) const; private: std::map> m_enumMap; diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmCore_UnitTests/cafPdmEnumMapperTest.cpp b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmCore_UnitTests/cafPdmEnumMapperTest.cpp index c25f9b879e..500b84ca86 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmCore_UnitTests/cafPdmEnumMapperTest.cpp +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmCore_UnitTests/cafPdmEnumMapperTest.cpp @@ -20,6 +20,8 @@ enum class TestEnum2 ValueC }; +//#define addItem( enumValue, text, uiText ) addItem( enumValue, #enumValue, text, uiText ) + namespace caf { template <> @@ -30,6 +32,17 @@ void caf::AppEnum::setUp() addItem( TestEnum::Value3, "VALUE_3", "Val 3" ); addItem( TestEnum::Value4, "VALUE_4", "Val 4" ); setDefault( TestEnum::Value2 ); + + auto instance = caf::AppEnumMapper::instance(); + instance->addItem( caf::cafTypeName(), caf::convertToInteger( TestEnum::Value1 ), "VALUE_1", "Val 1" ); + instance->addItem( caf::cafTypeName(), caf::convertToInteger( TestEnum::Value2 ), "VALUE_2", "Val 2" ); + instance->addItem( caf::cafTypeName(), caf::convertToInteger( TestEnum::Value3 ), "VALUE_3", "Val 3" ); + instance->addItem( caf::cafTypeName(), caf::convertToInteger( TestEnum::Value4 ), "VALUE_4", "Val 4" ); + instance->setDefault( caf::cafTypeName(), caf::convertToInteger( TestEnum::Value2 ) ); + + /* + * + */ } template <> @@ -92,3 +105,18 @@ TEST( PdmEnumMapperTest, ConverEnumToInteger ) EXPECT_EQ( enumValue, sourceEnumValue ); } } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +TEST( PdmEnumMapperTest, GetUiText ) +{ + caf::AppEnumMapper* instance = caf::AppEnumMapper::instance(); + + { + auto TestEnum_key = caf::cafTypeName(); + auto intValue = caf::convertToInteger( TestEnum::Value1 ); + + instance->uiText( TestEnum_key, intValue ); + } +}