Mantid
Loading...
Searching...
No Matches
Unit.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 +
7#include "MantidKernel/Unit.h"
9
10#include <boost/python/class.hpp>
11#include <boost/python/enum.hpp>
12#include <boost/python/register_ptr_to_python.hpp>
13#include <boost/python/tuple.hpp>
14
17using namespace boost::python;
18
20
21namespace {
26const std::string deprecatedName(const Unit &self) {
27 if (PyErr_Warn(PyExc_DeprecationWarning, "'name' is deprecated, use 'caption' instead.") < 0)
28 throw boost::python::error_already_set();
29 return self.caption();
30}
31
36const std::string deprecatedLabel(const Unit &self) {
37 if (PyErr_Warn(PyExc_DeprecationWarning, "'unit.label()' is deprecated, use 'str(unit.symbol())' instead.") < 0)
38 throw boost::python::error_already_set();
39 return self.label().ascii();
40}
41
42template <class T> tuple quickConversionWrapper(const Unit &self, const T &destUnitName) {
43 double wavelengthFactor = 0;
44 double wavelengthPower = 0;
45 bool converted = self.quickConversion(destUnitName, wavelengthFactor, wavelengthPower);
46 if (!converted) {
47 throw std::runtime_error("Quick conversion is not possible from unit:" + std::string(self.unitID()) +
48 " to the desired unit.");
49 }
50 return boost::python::make_tuple<double>(wavelengthFactor, wavelengthPower);
51}
52} // namespace
53
55
56 register_ptr_to_python<std::shared_ptr<Unit>>();
57
58 enum_<Mantid::Kernel::UnitParams>("UnitParams")
60 .value("twoTheta", Mantid::Kernel::UnitParams::twoTheta)
65 .value("tzero", Mantid::Kernel::UnitParams::tzero);
66
67 class_<Unit, boost::noncopyable>("Unit", no_init)
68 .def("name", &deprecatedName, arg("self"), "Return the full name of the unit (deprecated, use caption)")
69 .def("caption", &Unit::caption, arg("self"), "Return the full name of the unit")
70 .def("label", &deprecatedLabel, arg("self"),
71 "Returns a plain-text label to be used "
72 "as the symbol for the unit (deprecated, "
73 "use symbol())")
74 .def("symbol", &Unit::label, arg("self"),
75 "Returns a UnitLabel object that holds "
76 "information on the symbol to use for unit")
77 .def("unitID", &Unit::unitID, arg("self"),
78 "Returns the string ID of the unit. This may/may not match "
79 "its name")
80 .def("quickConversion", &quickConversionWrapper<Unit>, (arg("self"), arg("destUnitName")),
81 "Check whether the unit can be converted to another via a "
82 "simple factor")
83 .def("quickConversion", &quickConversionWrapper<std::string>, (arg("self"), arg("destination")),
84 "Check whether the unit can be converted to another via a "
85 "simple factor");
86}
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition GetPointer.h:17
void export_Unit()
Definition Unit.cpp:54
const AsciiString & ascii() const
Return an ascii label for unit.
The base units (abstract) class.
Definition Unit.h:40
virtual const UnitLabel label() const =0
A label for the unit to be printed on axes,.
virtual const std::string caption() const =0
The full name of the unit.
virtual const std::string unitID() const =0
The name of the unit.
bool quickConversion(const Unit &destination, double &factor, double &power) const
Is conversion by constant multiplication possible?
Definition Unit.cpp:63
std::shared_ptr< Unit > Unit_sptr
Shared pointer to the Unit base class.
Definition Unit_fwd.h:17