Mantid
Loading...
Searching...
No Matches
MDUnitFactory.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 +
9
10#include <boost/regex.hpp>
11
12namespace Mantid::Kernel {
13
14LabelUnit *LabelUnitFactory::createRaw(const std::string &unitString) const {
15 return new LabelUnit(UnitLabel(unitString));
16}
17
18bool LabelUnitFactory::canInterpret(const std::string & /*unitString*/) const {
19 return true; // Can always treat a unit as a label unit.
20}
21
22InverseAngstromsUnit *InverseAngstromsUnitFactory::createRaw(const std::string & /*unitString*/) const {
23 return new InverseAngstromsUnit;
24}
25
26bool InverseAngstromsUnitFactory::canInterpret(const std::string &unitString) const {
27 boost::regex pattern("(Angstrom\\^-1)");
28 boost::regex pattern2("A\\^-1");
29 boost::smatch match; // Unused.
30
31 auto isFullAngstrom = boost::regex_search(unitString, match, pattern);
32 auto isPartialAngstrom = boost::regex_search(unitString, match, pattern2);
33
34 return isFullAngstrom || isPartialAngstrom;
35}
36
38 return new ReciprocalLatticeUnit(UnitLabel(unitString));
39}
40
41bool ReciprocalLatticeUnitFactory::canInterpret(const std::string &unitString) const {
42 auto isRLU = unitString == Units::Symbol::RLU.ascii();
43
44 // In addition to having RLU we can encounter units of type "in 6.28 A^-1"
45 // We need to account for the latter here
46 boost::regex pattern("in.*A.*\\^-1");
47 auto isHoraceStyle = boost::regex_match(unitString, pattern);
48 return isRLU || isHoraceStyle;
49}
50
52 MDUnitFactory_uptr first = std::make_unique<ReciprocalLatticeUnitFactory>();
53 first
54 ->setSuccessor(std::make_unique<InverseAngstromsUnitFactory>())
55 // Add more factories here!
56 // Make sure that LabelUnitFactory is the last in the chain to give a fall
57 // through
58 .setSuccessor(std::make_unique<LabelUnitFactory>());
59 return first;
60}
61
62} // namespace Mantid::Kernel
bool canInterpret(const std::string &unitString) const override
Indicate an ability to intepret the string.
InverseAngstromsUnit * createRaw(const std::string &unitString) const override
Create the product.
Inverse Angstroms unit.
Definition: MDUnit.h:51
LabelUnit * createRaw(const std::string &unitString) const override
Create the product.
bool canInterpret(const std::string &unitString) const override
Indicate an ability to intepret the string.
bool canInterpret(const std::string &unitString) const override
Indicate an ability to intepret the string.
ReciprocalLatticeUnit * createRaw(const std::string &unitString) const override
Create the product.
A base-class for the a class that is able to return unit labels in different representations.
Definition: UnitLabel.h:20
const AsciiString & ascii() const
Return an ascii label for unit.
Definition: UnitLabel.cpp:105
static const UnitLabel RLU
Reciprocal lattice units.
std::unique_ptr< MDUnitFactory > MDUnitFactory_uptr
Definition: MDUnitFactory.h:50
MDUnitFactory_uptr MANTID_KERNEL_DLL makeMDUnitFactoryChain()
Convience method. Pre-constructed builder chain.