diff --git a/core/data/CbmDefs.cxx b/core/data/CbmDefs.cxx index 86d4aacabf6a26f1f816c2a2e9c7491879220d41..1ace5dcc1591c2e463046288938bf5d4c9f0a957 100644 --- a/core/data/CbmDefs.cxx +++ b/core/data/CbmDefs.cxx @@ -4,8 +4,10 @@ #include "CbmDefs.h" +#include <algorithm> +#include <array> +#include <cctype> #include <stdexcept> // for out_of_range -#include <string> // for to_string // operator ++ for ECbmModuleId for convenient usage in loops // This operator is tuned for ECbmModuleID. It takes into account non @@ -38,6 +40,59 @@ std::ostream& operator<<(std::ostream& strm, const ECbmModuleId& modId) return strm; } +// conversion functions between ECbmModuleId and std::string +static const std::array<std::pair<ECbmModuleId, std::string>, 22> ModIdStrMap = { + {{ECbmModuleId::kRef, "Ref"}, + {ECbmModuleId::kMvd, "Mvd"}, + {ECbmModuleId::kSts, "Sts"}, + {ECbmModuleId::kRich, "Rich"}, + {ECbmModuleId::kMuch, "Much"}, + {ECbmModuleId::kTrd, "Trd"}, + {ECbmModuleId::kTof, "Tof"}, + {ECbmModuleId::kEcal, "Ecal"}, + {ECbmModuleId::kPsd, "Psd"}, + {ECbmModuleId::kHodo, "Hodo"}, + {ECbmModuleId::kDummyDet, "DummyDet"}, + {ECbmModuleId::kT0, "T0"}, + {ECbmModuleId::kTrd2d, "Trd2d"}, + {ECbmModuleId::kNofSystems, "NofSystems"}, + {ECbmModuleId::kMagnet, "Magnet"}, + {ECbmModuleId::kTarget, "Target"}, + {ECbmModuleId::kPipe, "Pipe"}, + {ECbmModuleId::kShield, "Shield"}, + {ECbmModuleId::kPlatform, "Platform"}, + {ECbmModuleId::kCave, "Cave"}, + {ECbmModuleId::kLastModule, "LastModule"}, + {ECbmModuleId::kNotExist, "NotExist"}}}; + +std::string ToString(ECbmModuleId modId) +{ + auto result = std::find_if(ModIdStrMap.begin(), ModIdStrMap.end(), + [modId](std::pair<ECbmModuleId, std::string> p) { return p.first == modId; }); + if (result == std::end(ModIdStrMap)) { return "NotExist"; } + return result->second; +}; + +ECbmModuleId ToCbmModuleId(std::string modIdStr) +{ + auto result = std::find_if(ModIdStrMap.begin(), ModIdStrMap.end(), + [modIdStr](std::pair<ECbmModuleId, std::string> p) { return p.second == modIdStr; }); + if (result == std::end(ModIdStrMap)) { return ECbmModuleId::kNotExist; } + return result->first; +}; + +ECbmModuleId ToCbmModuleIdCaseInsensitive(std::string modIdStr) +{ + auto result = + std::find_if(ModIdStrMap.begin(), ModIdStrMap.end(), [modIdStr](std::pair<ECbmModuleId, std::string> p) { + return p.second.size() == modIdStr.size() + && std::equal(p.second.begin(), p.second.end(), modIdStr.begin(), + [](unsigned char a, unsigned char b) { return std::tolower(a) == std::tolower(b); }); + }); + if (result == std::end(ModIdStrMap)) { return ECbmModuleId::kNotExist; } + return result->first; +}; + // operator << for convenient output to std::ostream. // Converts the enum value to a string which is put in the stream std::ostream& operator<<(std::ostream& strm, const ECbmDataType& dataType) diff --git a/core/data/CbmDefs.h b/core/data/CbmDefs.h index 6b55572c6f0ea6f5f3cafea16a14bd5fa4e4f036..b2a5c9405b767415b8efac08aa49cfdb23484fa4 100644 --- a/core/data/CbmDefs.h +++ b/core/data/CbmDefs.h @@ -12,9 +12,10 @@ #ifndef CBMDEFS_H #define CBMDEFS_H 1 -#include <type_traits> // for underlying_type +#include <type_traits> // for underlying_type #include <iostream> // for ostream +#include <string> // Convert an element of enum class to its underlying intergral type // since with C++11 the return type can't be deduced automatically it has @@ -76,6 +77,10 @@ ECbmModuleId& operator++(ECbmModuleId&); // Converts the enum value to a string which is put in the stream std::ostream& operator<<(std::ostream&, const ECbmModuleId&); +// conversion functions between ECbmModuleId and std::string +std::string ToString(ECbmModuleId modId); +ECbmModuleId ToCbmModuleId(std::string modIdStr); +ECbmModuleId ToCbmModuleIdCaseInsensitive(std::string modIdStr); /** Enumerator for CBM data types **/ enum class ECbmDataType