Removed IEnumInfo (just use EnumInfo directly)

This commit is contained in:
Phil Nash 2019-04-21 19:56:19 +03:00
parent 9d5d719868
commit 5a74fcc9c9
4 changed files with 25 additions and 30 deletions

View File

@ -14,7 +14,6 @@
namespace Catch { namespace Catch {
IMutableEnumValuesRegistry::~IMutableEnumValuesRegistry() {} IMutableEnumValuesRegistry::~IMutableEnumValuesRegistry() {}
IEnumInfo::~IEnumInfo() {}
namespace Detail { namespace Detail {
@ -29,25 +28,17 @@ namespace Catch {
return parsed; return parsed;
} }
struct EnumInfo : IEnumInfo {
std::string m_name;
std::map<int, std::string> m_values;
~EnumInfo();
std::string lookup( int value ) const override {
auto it = m_values.find( value );
if( it == m_values.end() ) {
ReusableStringStream rss;
rss << "{** unexpected value for " << m_name << ": " << value << "**}";
return rss.str();
}
return it->second;
}
};
EnumInfo::~EnumInfo() {} EnumInfo::~EnumInfo() {}
IEnumInfo const& EnumValuesRegistry::registerEnum( StringRef enumName, StringRef allValueNames, std::vector<int> const& values ) { StringRef EnumInfo::lookup( int value ) const {
for( auto const& valueToName : m_values ) {
if( valueToName.first == value )
return valueToName.second;
}
return "{** unexpected enum value **}";
}
EnumInfo const& EnumValuesRegistry::registerEnum( StringRef enumName, StringRef allValueNames, std::vector<int> const& values ) {
std::unique_ptr<EnumInfo> enumInfo( new EnumInfo ); std::unique_ptr<EnumInfo> enumInfo( new EnumInfo );
enumInfo->m_name = enumName; enumInfo->m_name = enumName;
@ -55,7 +46,7 @@ namespace Catch {
assert( valueNames.size() == values.size() ); assert( valueNames.size() == values.size() );
std::size_t i = 0; std::size_t i = 0;
for( auto value : values ) for( auto value : values )
enumInfo->m_values.insert({ value, valueNames[i++] }); enumInfo->m_values.push_back({ value, valueNames[i++] });
EnumInfo* raw = enumInfo.get(); EnumInfo* raw = enumInfo.get();
m_enumInfos.push_back( std::move( enumInfo ) ); m_enumInfos.push_back( std::move( enumInfo ) );
@ -63,6 +54,5 @@ namespace Catch {
} }
} // Detail } // Detail
} // Catch } // Catch

View File

@ -18,9 +18,9 @@ namespace Catch {
class EnumValuesRegistry : public IMutableEnumValuesRegistry { class EnumValuesRegistry : public IMutableEnumValuesRegistry {
std::vector<std::unique_ptr<IEnumInfo>> m_enumInfos; std::vector<std::unique_ptr<EnumInfo>> m_enumInfos;
IEnumInfo const& registerEnum(StringRef enumName, StringRef allEnums, std::vector<int> const& values) override; EnumInfo const& registerEnum(StringRef enumName, StringRef allEnums, std::vector<int> const& values) override;
}; };
std::vector<std::string> parseEnums( StringRef enums ); std::vector<std::string> parseEnums( StringRef enums );

View File

@ -14,19 +14,24 @@
namespace Catch { namespace Catch {
struct IEnumInfo { namespace Detail {
virtual ~IEnumInfo(); struct EnumInfo {
StringRef m_name;
std::vector<std::pair<int, std::string>> m_values;
virtual std::string lookup( int value ) const = 0; ~EnumInfo();
};
StringRef lookup( int value ) const;
};
} // namespace Detail
struct IMutableEnumValuesRegistry { struct IMutableEnumValuesRegistry {
virtual ~IMutableEnumValuesRegistry(); virtual ~IMutableEnumValuesRegistry();
virtual IEnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::vector<int> const& values ) = 0; virtual Detail::EnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::vector<int> const& values ) = 0;
template<typename E> template<typename E>
IEnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::initializer_list<E> values ) { Detail::EnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::initializer_list<E> values ) {
std::vector<int> intValues; std::vector<int> intValues;
intValues.reserve( values.size() ); intValues.reserve( values.size() );
for( auto enumValue : values ) for( auto enumValue : values )

View File

@ -75,7 +75,7 @@ TEST_CASE( "STRINGIFY_ENUM" ) {
REQUIRE( stringify( EnumClass3::Value1 ) == "Value1" ); REQUIRE( stringify( EnumClass3::Value1 ) == "Value1" );
REQUIRE( stringify( EnumClass3::Value2 ) == "Value2" ); REQUIRE( stringify( EnumClass3::Value2 ) == "Value2" );
REQUIRE( stringify( EnumClass3::Value3 ) == "Value3" ); REQUIRE( stringify( EnumClass3::Value3 ) == "Value3" );
REQUIRE( stringify( EnumClass3::Value4 ) == "{** unexpected value for EnumClass3: 3**}" ); REQUIRE( stringify( EnumClass3::Value4 ) == "{** unexpected enum value **}" );
EnumClass3 ec3 = EnumClass3 ::Value2; EnumClass3 ec3 = EnumClass3 ::Value2;
REQUIRE( stringify( ec3 ) == "Value2" ); REQUIRE( stringify( ec3 ) == "Value2" );
@ -91,7 +91,7 @@ TEST_CASE( "EnumInfo" ) {
CHECK( enumInfo.lookup(0) == "Value1" ); CHECK( enumInfo.lookup(0) == "Value1" );
CHECK( enumInfo.lookup(1) == "Value2" ); CHECK( enumInfo.lookup(1) == "Value2" );
CHECK( enumInfo.lookup(3) == "{** unexpected value for EnumName: 3**}" ); CHECK( enumInfo.lookup(3) == "{** unexpected enum value **}" );
} }
#include "internal/catch_enum_values_registry.h" #include "internal/catch_enum_values_registry.h"