Mantid
Loading...
Searching...
No Matches
DeltaEMode.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <boost/algorithm/string/predicate.hpp>
10#include <map>
11#include <sstream>
12#include <stdexcept>
13#include <utility>
14
15namespace Mantid::Kernel {
16namespace // unnamed
17{
18struct ModeIndex {
19 std::map<DeltaEMode::Type, std::string> index{{DeltaEMode::Elastic, "Elastic"},
20 {DeltaEMode::Direct, "Direct"},
21 {DeltaEMode::Indirect, "Indirect"},
22 {DeltaEMode::Undefined, "Undefined"}};
23};
25ModeIndex &typeStringLookup() {
26 static ModeIndex typeLookup;
27 return typeLookup;
28}
29} // namespace
30
35const std::vector<std::string> DeltaEMode::availableTypes() {
36 const ModeIndex &lookup = typeStringLookup();
37 std::vector<std::string> modes;
38 modes.reserve(lookup.index.size());
39 for (const auto &iter : lookup.index) {
40 if (iter.first == DeltaEMode::Undefined)
41 continue;
42 modes.emplace_back(iter.second);
43 }
44 return modes;
45}
46
52std::string DeltaEMode::asString(const Type mode) {
53 const ModeIndex &lookup = typeStringLookup();
54 auto iter = lookup.index.find(mode);
55 if (iter != lookup.index.end()) {
56 return iter->second;
57 } else {
58 std::ostringstream os;
59 os << "DeltaEMode::asString - Unknown energy transfer mode: " << mode;
60 throw std::invalid_argument(os.str());
61 }
62}
63
69DeltaEMode::Type DeltaEMode::fromString(const std::string &modeStr) {
70 const ModeIndex &lookup = typeStringLookup();
71
72 auto emode = std::find_if(lookup.index.cbegin(), lookup.index.cend(),
73 [&modeStr](auto it) { return boost::iequals(it.second, modeStr); });
74
75 if (emode != lookup.index.cend()) {
76 return emode->first;
77 }
78
79 // Unknown mode
80 throw std::invalid_argument("DeltaEMode::fromString - Unknown energy transfer mode: " + modeStr);
81}
82} // namespace Mantid::Kernel
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
static std::string asString(const Type mode)
Return a string representation of the given mode.
Definition: DeltaEMode.cpp:52
static const std::vector< std::string > availableTypes()
Returns the string list of available modes.
Definition: DeltaEMode.cpp:35
static Type fromString(const std::string &modeStr)
Returns the emode from the given string.
Definition: DeltaEMode.cpp:69
Type
Define the available energy transfer modes It is important to assign enums proper numbers,...
Definition: DeltaEMode.h:29
@ Undefined
this mode should not be displayed among modes availible to select but may have string representation
Definition: DeltaEMode.h:33