Open
Description
Note: this issue is a reopening of #97.
I'm working with a lot of enums from a C library that look like this:
enum FOO_BAR {
FOO_BAR_BAZ,
FOO_BAR_QUUX,
// etc
};
So when I call enum_name()
I get "FOO_BAR_BAZ"
, or if it's a bitflag I might get "FOO_BAR_BAZ|FOO_BAR_QUUX"
etc. But in a lot of cases I already know I'm dealing with a FOO_BAR
and this is noise. I would like the strings to omit FOO_BAR_
.
Existing solutions to this problem:
- Define another enum with the same variants and cast to that one before calling
enum_name
/enum_flags_name
. The issue with this is that the enums are large and in third party generated code so the point of using magic enum is to reduce such chores (I'm replacing old code that is full ofconst map<FOO_BAR, string>
). - Remove the prefix at runtime using
enum_name(v).substr(8)
. This is what I'm doing now and it works but is extra computation. It's also more cumbersome for the flags case where I have to doreplace_all(enum_flags_name(f), "FOO_BAR_", "")
.
My desired solution:
A boolean or other setting in the customize
struct that makes magic_enum do this for me automagically.
template <>
struct magic_enum::customize::enum_range<FOO_BAR> {
// one possibility, assuming the prefix is always the name of the enum plus an underscore
constexpr bool remove_name_prefix = true;
// more flexible but maybe harder to implement?
constexpr char* prefix = "FOO_BAR_";
// another idea
constexpr int prefix_len = 8;
};
There could also be a flag to pass to enum_[flags_]name
to override the setting and keep the prefix.
Metadata
Metadata
Assignees
Labels
No labels