forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MDUnitFactory.cpp
64 lines (51 loc) · 2.26 KB
/
MDUnitFactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidKernel/MDUnitFactory.h"
#include "MantidKernel/UnitLabelTypes.h"
#include <boost/regex.hpp>
namespace Mantid {
namespace Kernel {
LabelUnit *LabelUnitFactory::createRaw(const std::string &unitString) const {
return new LabelUnit(UnitLabel(unitString));
}
bool LabelUnitFactory::canInterpret(const std::string & /*unitString*/) const {
return true; // Can always treat a unit as a label unit.
}
InverseAngstromsUnit *InverseAngstromsUnitFactory::createRaw(const std::string & /*unitString*/) const {
return new InverseAngstromsUnit;
}
bool InverseAngstromsUnitFactory::canInterpret(const std::string &unitString) const {
boost::regex pattern("(Angstrom\\^-1)");
boost::regex pattern2("A\\^-1");
boost::smatch match; // Unused.
auto isFullAngstrom = boost::regex_search(unitString, match, pattern);
auto isPartialAngstrom = boost::regex_search(unitString, match, pattern2);
return isFullAngstrom || isPartialAngstrom;
}
ReciprocalLatticeUnit *ReciprocalLatticeUnitFactory::createRaw(const std::string &unitString) const {
return new ReciprocalLatticeUnit(UnitLabel(unitString));
}
bool ReciprocalLatticeUnitFactory::canInterpret(const std::string &unitString) const {
auto isRLU = unitString == Units::Symbol::RLU.ascii();
// In addition to having RLU we can encounter units of type "in 6.28 A^-1"
// We need to account for the latter here
boost::regex pattern("in.*A.*\\^-1");
auto isHoraceStyle = boost::regex_match(unitString, pattern);
return isRLU || isHoraceStyle;
}
MDUnitFactory_uptr makeMDUnitFactoryChain() {
MDUnitFactory_uptr first = std::make_unique<ReciprocalLatticeUnitFactory>();
first
->setSuccessor(std::make_unique<InverseAngstromsUnitFactory>())
// Add more factories here!
// Make sure that LabelUnitFactory is the last in the chain to give a fall
// through
.setSuccessor(std::make_unique<LabelUnitFactory>());
return first;
}
} // namespace Kernel
} // namespace Mantid