catch2/include/internal/catch_enum_values_registry.cpp

66 lines
2.2 KiB
C++
Raw Normal View History

2019-04-04 16:55:46 +02:00
/*
* Created by Phil on 4/4/2019.
* Copyright 2019 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include "catch_enum_values_registry.h"
#include "catch_string_manip.h"
#include "catch_stream.h"
#include <map>
2019-04-25 15:19:00 +02:00
#include <cassert>
2019-04-04 16:55:46 +02:00
namespace Catch {
IMutableEnumValuesRegistry::~IMutableEnumValuesRegistry() {}
namespace Detail {
std::vector<std::string> parseEnums( StringRef enums ) {
auto enumValues = splitStringRef( enums, ',' );
2019-04-04 16:55:46 +02:00
std::vector<std::string> parsed;
parsed.reserve( enumValues.size() );
for( auto const& enumValue : enumValues ) {
auto identifiers = splitStringRef( enumValue, ':' );
2019-04-04 16:55:46 +02:00
parsed.push_back( Catch::trim( identifiers.back() ) );
}
return parsed;
}
EnumInfo::~EnumInfo() {}
2019-04-04 16:55:46 +02:00
StringRef EnumInfo::lookup( int value ) const {
for( auto const& valueToName : m_values ) {
if( valueToName.first == value )
return valueToName.second;
2019-04-04 16:55:46 +02:00
}
return "{** unexpected enum value **}";
}
2019-04-04 16:55:46 +02:00
std::unique_ptr<EnumInfo> makeEnumInfo( StringRef enumName, StringRef allValueNames, std::vector<int> const& values ) {
2019-04-04 16:55:46 +02:00
std::unique_ptr<EnumInfo> enumInfo( new EnumInfo );
enumInfo->m_name = enumName;
enumInfo->m_values.reserve( values.size() );
2019-04-04 16:55:46 +02:00
const auto valueNames = Catch::Detail::parseEnums( allValueNames );
assert( valueNames.size() == values.size() );
std::size_t i = 0;
for( auto value : values )
enumInfo->m_values.push_back({ value, valueNames[i++] });
2019-04-04 16:55:46 +02:00
return enumInfo;
}
EnumInfo const& EnumValuesRegistry::registerEnum( StringRef enumName, StringRef allValueNames, std::vector<int> const& values ) {
auto enumInfo = makeEnumInfo( enumName, allValueNames, values );
2019-04-04 16:55:46 +02:00
EnumInfo* raw = enumInfo.get();
m_enumInfos.push_back( std::move( enumInfo ) );
return *raw;
}
} // Detail
} // Catch