Use StringRefs through the enum registration

This commit is contained in:
Martin Hořeňovský
2019-09-08 14:07:18 +02:00
parent fbbaadb704
commit 923db16322
7 changed files with 60 additions and 50 deletions

View File

@@ -18,13 +18,25 @@ namespace Catch {
namespace Detail {
std::vector<std::string> parseEnums( StringRef enums ) {
namespace {
// Extracts the actual name part of an enum instance
// In other words, it returns the Blue part of Bikeshed::Colour::Blue
StringRef extractInstanceName(StringRef enumInstance) {
// Find last occurence of ":"
size_t name_start = enumInstance.size();
while (name_start > 0 && enumInstance[name_start - 1] != ':') {
--name_start;
}
return enumInstance.substr(name_start, enumInstance.size() - name_start);
}
}
std::vector<StringRef> parseEnums( StringRef enums ) {
auto enumValues = splitStringRef( enums, ',' );
std::vector<std::string> parsed;
std::vector<StringRef> parsed;
parsed.reserve( enumValues.size() );
for( auto const& enumValue : enumValues ) {
auto identifiers = splitStringRef( enumValue, ':' );
parsed.push_back( Catch::trim( identifiers.back() ) );
parsed.push_back(trim(extractInstanceName(enumValue)));
}
return parsed;
}
@@ -54,10 +66,8 @@ namespace Catch {
}
EnumInfo const& EnumValuesRegistry::registerEnum( StringRef enumName, StringRef allValueNames, std::vector<int> const& values ) {
auto enumInfo = makeEnumInfo( enumName, allValueNames, values );
EnumInfo* raw = enumInfo.get();
m_enumInfos.push_back( std::move( enumInfo ) );
return *raw;
m_enumInfos.push_back(makeEnumInfo(enumName, allValueNames, values));
return *m_enumInfos.back();
}
} // Detail

View File

@@ -26,7 +26,7 @@ namespace Catch {
EnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::vector<int> const& values) override;
};
std::vector<std::string> parseEnums( StringRef enums );
std::vector<StringRef> parseEnums( StringRef enums );
} // Detail

View File

@@ -17,7 +17,7 @@ namespace Catch {
namespace Detail {
struct EnumInfo {
StringRef m_name;
std::vector<std::pair<int, std::string>> m_values;
std::vector<std::pair<int, StringRef>> m_values;
~EnumInfo();