Skip to content

Commit

Permalink
Use string view in new mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFeldmann committed Jul 21, 2024
1 parent 42ea197 commit 4052cec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
14 changes: 8 additions & 6 deletions YUViewLib/src/common/NewEnumMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,21 @@
#include <algorithm>
#include <array>
#include <cuchar>
#include <string_view>

using namespace std::string_view_literals;

template <class T, size_t N> struct NewEnumMapper
{
using VALUE_AND_NAME = std::pair<T, const char *>;
using VALUE_AND_NAME = std::pair<T, std::string_view>;

template <typename... Args> constexpr NewEnumMapper(Args... args)
{
constexpr auto nrArguments = sizeof...(Args);
static_assert(nrArguments == N);
static_assert(sizeof...(Args) == N);
this->addElementsRecursively(0, args...);
}

constexpr const char *getName(const T value) const
constexpr std::string_view getName(const T value) const
{
const auto it = std::find(this->items.begin(), this->items.end(), value);
if (it == this->items.end())
Expand All @@ -74,6 +76,6 @@ template <class T, size_t N> struct NewEnumMapper
addElementsRecursively(index + 1, args...);
}

std::array<T, N> items{};
std::array<const char *, N> names{};
std::array<T, N> items{};
std::array<std::string_view, N> names{};
};
8 changes: 4 additions & 4 deletions YUViewUnitTest/video/rgb/ConversionRGBTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ namespace
{

constexpr auto ALL_MODES_MAPPER =
NewEnumMapper<AlphaMode, 3>(std::make_pair(AlphaMode::First, "First"),
std::make_pair(AlphaMode::Last, "Last"),
std::make_pair(AlphaMode::None, "None"));
NewEnumMapper<AlphaMode, 3>(std::make_pair(AlphaMode::First, "First"sv),
std::make_pair(AlphaMode::Last, "Last"sv),
std::make_pair(AlphaMode::None, "None"sv));

constexpr auto EndianessMapper = NewEnumMapper<Endianness, 2>(
std::make_pair(Endianness::Little, "Little"), std::make_pair(Endianness::Big, "Big"));
std::make_pair(Endianness::Little, "Little"sv), std::make_pair(Endianness::Big, "Big"sv));

using TestParameters = std::tuple<Endianness, BitDepth, AlphaMode>;

Expand Down

0 comments on commit 4052cec

Please sign in to comment.