-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce bitmask support and std::format specialization
- Loading branch information
Showing
7 changed files
with
726 additions
and
569 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
{ | ||
"files.associations": { | ||
"cmath": "cpp", | ||
"string_view": "cpp" | ||
} | ||
} | ||
"files.associations": { | ||
"cmath": "cpp", | ||
"string_view": "cpp", | ||
"sstream": "cpp", | ||
"optional": "cpp", | ||
"memory": "cpp", | ||
"random": "cpp" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
cmake_minimum_required(VERSION 3.14) | ||
project(enum_name_example VERSION 0.1 LANGUAGES CXX) | ||
project( | ||
enum_name_example | ||
VERSION 0.1 | ||
LANGUAGES CXX) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD 20) | ||
|
||
include(FetchContent) | ||
|
||
FetchContent_Declare( | ||
DocTest | ||
GIT_REPOSITORY "https://github.com/onqtam/doctest" | ||
GIT_TAG "v2.4.11" | ||
) | ||
DocTest | ||
GIT_REPOSITORY "https://github.com/onqtam/doctest" | ||
GIT_TAG "v2.4.11") | ||
|
||
FetchContent_MakeAvailable(DocTest) | ||
|
||
add_executable(${PROJECT_NAME} example/main.cpp) | ||
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/include) | ||
|
||
enable_testing() | ||
add_subdirectory(test) | ||
add_subdirectory(test) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,31 @@ | ||
#include <iostream> | ||
#include "enum_name.hpp" | ||
#include <iostream> | ||
|
||
enum class rgb_color { red, green, blue, unknown = -1 }; | ||
|
||
enum class rgb_color { red, green, blue, unknown = -1}; | ||
|
||
// you can specialize enum ranges with specialize struct per enum types (option 1) | ||
namespace mgutility{ | ||
template<> | ||
struct enum_range<rgb_color> | ||
{ | ||
static constexpr auto min = -1; | ||
static constexpr auto max = 3; | ||
}; | ||
} | ||
// you can specialize enum ranges with specialize struct per enum types (option | ||
// 1) | ||
namespace mgutility { | ||
template <> struct enum_range<rgb_color> { | ||
static constexpr auto min = -1; | ||
static constexpr auto max = 3; | ||
}; | ||
} // namespace mgutility | ||
|
||
// you can specialize enum ranges with overload per enum types (option 2) | ||
auto enum_name = [](rgb_color c){ return mgutility::enum_name<-1, 3>(c); }; | ||
auto enum_name = [](rgb_color c) { return mgutility::enum_name<-1, 3>(c); }; | ||
|
||
int main() { | ||
auto x = rgb_color::blue; | ||
auto y = mgutility::to_enum<rgb_color>("green"); | ||
|
||
// default signature: enum_name<min_value = -128, max_value = 128, Enum | ||
// typename>(Enum&&) Changing max_value to not too much greater than enum's | ||
// max value, it will compiles faster | ||
std::cout << mgutility::enum_name(x) << '\n'; // will print "blue" to output | ||
|
||
int main() | ||
{ | ||
auto x = rgb_color::blue; | ||
auto y = mgutility::to_enum<rgb_color>("green"); | ||
|
||
// default signature: enum_name<min_value = -128, max_value = 128, Enum typename>(Enum&&) | ||
// Changing max_value to not too much greater than enum's max value, it will compiles faster | ||
std::cout << mgutility::enum_name(x) << '\n'; // will print "blue" to output | ||
|
||
// calling specialized enum ranges function for rgb_color type | ||
// will print "green" to output, if y can't convert to rgb_color prints "unknown" | ||
std::cout << enum_name(y.value_or(rgb_color::unknown)) << '\n'; | ||
// calling specialized enum ranges function for rgb_color type | ||
// will print "green" to output, if y can't convert to rgb_color prints | ||
// "unknown" | ||
std::cout << enum_name(y.value_or(rgb_color::unknown)) << '\n'; | ||
} |
Oops, something went wrong.