Skip to content
Snippets Groups Projects
Commit d94b4dec authored by Jan de Cuveland's avatar Jan de Cuveland Committed by Florian Uhlig
Browse files

Add convenience functions to convert between ECbmModuleId and std::string

parent 8fcb9dfc
Branches
Tags
1 merge request!774Add convenience functions to convert between ECbmModuleId and std::string
Pipeline #16730 failed
......@@ -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)
......
......
......@@ -15,6 +15,7 @@
#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
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment