Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
catch_enum_values_registry.cpp
Go to the documentation of this file.
1/*
2 * Created by Phil on 4/4/2019.
3 * Copyright 2019 Two Blue Cubes Ltd. All rights reserved.
4 *
5 * Distributed under the Boost Software License, Version 1.0. (See accompanying
6 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 */
10#include "catch_stream.h"
11
12#include <map>
13#include <cassert>
14
15namespace Catch {
16
18
19 namespace Detail {
20
21 std::vector<std::string> parseEnums( StringRef enums ) {
22 auto enumValues = splitStringRef( enums, ',' );
23 std::vector<std::string> parsed;
24 parsed.reserve( enumValues.size() );
25 for( auto const& enumValue : enumValues ) {
26 auto identifiers = splitStringRef( enumValue, ':' );
27 parsed.push_back( Catch::trim( identifiers.back() ) );
28 }
29 return parsed;
30 }
31
33
35 for( auto const& valueToName : m_values ) {
36 if( valueToName.first == value )
37 return valueToName.second;
38 }
39 return "{** unexpected enum value **}";
40 }
41
42 std::unique_ptr<EnumInfo> makeEnumInfo( StringRef enumName, StringRef allValueNames, std::vector<int> const& values ) {
43 std::unique_ptr<EnumInfo> enumInfo( new EnumInfo );
44 enumInfo->m_name = enumName;
45 enumInfo->m_values.reserve( values.size() );
46
47 const auto valueNames = Catch::Detail::parseEnums( allValueNames );
48 assert( valueNames.size() == values.size() );
49 std::size_t i = 0;
50 for( auto value : values )
51 enumInfo->m_values.push_back({ value, valueNames[i++] });
52
53 return enumInfo;
54 }
55
56 EnumInfo const& EnumValuesRegistry::registerEnum( StringRef enumName, StringRef allValueNames, std::vector<int> const& values ) {
57 auto enumInfo = makeEnumInfo( enumName, allValueNames, values );
58 EnumInfo* raw = enumInfo.get();
59 m_enumInfos.push_back( std::move( enumInfo ) );
60 return *raw;
61 }
62
63 } // Detail
64} // Catch
65
std::unique_ptr< EnumInfo > makeEnumInfo(StringRef enumName, StringRef allValueNames, std::vector< int > const &values)
std::vector< std::string > parseEnums(StringRef enums)
std::string trim(std::string const &str)
std::vector< StringRef > splitStringRef(StringRef str, char delimiter)
#define value
Definition pkcs11.h:157
std::vector< std::pair< int, std::string > > m_values
StringRef lookup(int value) const