Skip to content

Introduce Enum Reflection

Simon Spannagel requested to merge magic_enum into master

This MR adds the Magic Enum class to allow for enum reflection. An overload of allpix::to_string_impl() and allpix::from_string_impl() for enum types enable parsing configuration keys directly into enums:

enum class FileType {
    ROOT,
    TXT,
    CSV,
};

auto model = config.get<FileType>("file_type");
if(model == FileType::ROOT) {
    // do sth
}

without odd string comparisons. If the requested key does not exist in the enum, an error message with all available options is printed, emulating the behavior of the manual else printouts used in string comparisons so far:

|23:08:27.240|   (FATAL) [C:DepositionPointCharge:dut] Error in the configuration:
                                                       Could not convert value '"spock"' from key 'model' in section 'DepositionPointCharge' to type DepositionPointChargeModule::DepositionModel: invalid value, possible values are: fixed, scan, spot
                                                       The configuration needs to be updated. Cannot continue.

Many modules have already been ported to this, the last missing part is Mobility where I'm not sure whether I want to pass an enum value to the constructor or rather the full Configuration object such that further keys could be queried (such as model parameters).

The "default" handling of non-implemented types had to go, I couldn't figure out how to re-implement it. But I'm not entirely sure it ever worked properly, at least it didn't for is_enum-like objects. 🙂

This MR is based on top of !482 (merged) which should be reviewed and merged first.

(cc) @kwolters

Merge request reports