-
-
Save bkaestner/58f0bfdf7e66aaaa2a373bc662b1c08c to your computer and use it in GitHub Desktop.
breaking enum_name on magic_enum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.15) | |
project (mve LANGUAGES CXX) | |
set(CMAKE_CXX_STANDARD 17) | |
option(ENABLE_FIX "Enable MAGIC_ENUM_ENABLE_HASH" OFF) | |
if(ENABLE_FIX) | |
add_compile_definitions(MAGIC_ENUM_ENABLE_HASH) | |
endif() | |
add_library(enum STATIC enum.cc) | |
add_executable(mve main.cc) | |
target_link_libraries(mve enum) | |
add_executable(mve_bad main_with_range.cc) | |
target_link_libraries(mve_bad enum) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "enum.h" | |
#include "magic_enum.hpp" | |
template <> | |
struct magic_enum::customize::enum_range<flag> | |
{ | |
static constexpr int min = static_cast<int>(flag::low); | |
static constexpr int max = static_cast<int>(flag::high); | |
static_assert(static_cast<uint64_t>(max) - static_cast<uint64_t>(min) < std::numeric_limits<uint16_t>::max(), | |
"magic_enum only supports names for the uint16_t range."); | |
}; | |
std::string_view to_string(flag f){ | |
return magic_enum::enum_name(f); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef EXAMPLE_ENUM_H | |
#define EXAMPLE_ENUM_H | |
#include <string_view> | |
enum class flag | |
{ | |
low = 0, | |
high = 500, | |
}; | |
std::string_view to_string(flag); | |
#endif // EXAMPLE_ENUM_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "enum.h" | |
#include "magic_enum.hpp" | |
int main() { | |
std::cout << to_string(flag::low) << std::endl; | |
std::cout << to_string(flag::high) << std::endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "enum.h" | |
#include "magic_enum.hpp" | |
int main() { | |
for (auto f : magic_enum::enum_values<flag>()) { | |
std::cout << to_string(f) << std::endl; | |
} | |
std::cout << "---\n"; | |
std::cout << "low: " << to_string(flag::low) << std::endl; | |
std::cout << "high: " << to_string(flag::high) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment