Skip to content
Snippets Groups Projects
MDUnitFactory.cpp 1.94 KiB
Newer Older
#include "MantidKernel/make_unique.h"
#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 &) const {
  return true; // Can always treat a unit as a label unit.
InverseAngstromsUnit *
InverseAngstromsUnitFactory::createRaw(const std::string &) 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 = make_unique<ReciprocalLatticeUnitFactory>();
  first->setSuccessor(make_unique<InverseAngstromsUnitFactory>())
      // Add more factories here!
      // Make sure that LabelUnitFactory is the last in the chain to give a fall
      // through
      .setSuccessor(make_unique<LabelUnitFactory>());
} // namespace Kernel
} // namespace Mantid