Add enum_reflection.h & preproc.h#10665
Conversation
mrambacher
left a comment
There was a problem hiding this comment.
I fully support the need for this reflection. The OptionType has code which would use this for Parse/SerializeEnum already which relies on user-defined maps.
Having said that, I worry that this is a bit confusing and hard to push into the consumer-facing (include/rocksdb) API. Could we have some simple methods added to the public API (like the to/from string), perhaps through some macro and have the implementation of those methods be in some internal code path (util for example). That internal representation could be based on something like this package or some other that is available (like magic_enum.hpp).
My concern is not so much in the functionality being exposed -- I think these methods are highly valuable -- but more as to what ends up in the public API and how. Once enum_reflection etc is in include/rocksdb, it is much harder to change and folks could use it for purposes not intended.
Finally, I think it would be helpful to introduce and use this functionality in a single PR. Would it be possible to introduce the new methods and use them to replace the ParseEnum code (for example)? This would make it easier to validate and understand that the code is "doing the right thing" rather than code without any tests or usage.
| }; | ||
|
|
||
| template<class Enum> | ||
| Slice enum_name(Enum v, const char* unkown = "") { |
| if (v == values[i]) | ||
| return names.first[i].ToString(); | ||
| } | ||
| return "unkown:" + (sizeof(Enum) <= sizeof(int) |
| } | ||
|
|
||
| template<class Enum> | ||
| bool enum_value(const ROCKSDB_NAMESPACE::Slice& name, Enum* result) { |
There was a problem hiding this comment.
Why "Slice" and not std::string" ?
There was a problem hiding this comment.
std::string and const char* can be implicitly converted to Slice, and Slice is a fundamental type in rocksdb.
| } | ||
|
|
||
| template<class Enum> | ||
| const char* enum_cstr(Enum v, const char* unkown = "") { |
There was a problem hiding this comment.
Why not NULL as default?
2 years ago, I had created PR #7081 which failed on old MSVC.
Now rocksdb has upgraded to c++17, PR #7081 will not fail in CI, we can continuously migrating existing enum/string conversion code by using this enum refelection.
Below is the brief introduction about enum reflection(copied from PR #7081):
With enum reflection, we can convert enum to/from string
For example:
There are 4 macros to define a enum with reflection