Skip to content

Commit

Permalink
Add UnitLabel classes for each defined unique Unit class label.
Browse files Browse the repository at this point in the history
Refs #9252
  • Loading branch information
martyngigg committed Apr 4, 2014
1 parent a764967 commit 0cbe00a
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 105 deletions.
49 changes: 17 additions & 32 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/Unit.h
Expand Up @@ -14,6 +14,10 @@ namespace Mantid
{
namespace Kernel
{
//----------------------------------------------------------------------
// Forward declarations
//----------------------------------------------------------------------
class UnitLabel;

/** The base units (abstract) class. All concrete units should inherit from
this class and provide implementations of the caption(), label(),
Expand Down Expand Up @@ -49,6 +53,8 @@ class MANTID_KERNEL_DLL Unit

/// (Empty) Constructor
Unit();
/// Constructor providing a unit label
Unit(const UnitLabel * label);
/// Virtual destructor
virtual ~Unit();
/// Copy Constructor
Expand All @@ -67,11 +73,11 @@ class MANTID_KERNEL_DLL Unit
/// The full name of the unit
/// @return The unit caption
virtual const std::string caption() const = 0;

virtual const std::wstring utf8Label() const;
/// A label for the unit to be printed on axes, @see UnitLabel
/// @return The unit label
virtual const std::string label() const = 0;
/// A label string that can contain utf-8 character encodings.
virtual const std::wstring utf8Label() const;
virtual const UnitLabel & label() const;

// Check whether the unit can be converted to another via a simple factor
bool quickConversion(const Unit& destination, double& factor, double& power) const;
Expand Down Expand Up @@ -178,6 +184,9 @@ class MANTID_KERNEL_DLL Unit
/// Removes all registered 'quick conversions'
void clearConversions() const;

/// Set a new label
void setLabel(const UnitLabel * label);

/// The unit values have been initialized
bool initialized;
/// l1 :: The source-sample distance (in metres)
Expand All @@ -194,6 +203,8 @@ class MANTID_KERNEL_DLL Unit
double delta;

private:
/// Label
const UnitLabel *m_label;
/// A 'quick conversion' requires the constant by which to multiply the input and the power to which to raise it
typedef std::pair< double, double > ConstantAndPower;
/// Lists, for a given starting unit, the units to which a 'quick conversion' can be made
Expand Down Expand Up @@ -225,7 +236,6 @@ class MANTID_KERNEL_DLL Empty : public Unit
public:
const std::string unitID() const; ///< "Empty"
const std::string caption() const { return ""; }
const std::string label() const {return ""; }

virtual double singleToTOF(const double x) const;
virtual double singleFromTOF(const double tof) const;
Expand All @@ -248,23 +258,17 @@ class MANTID_KERNEL_DLL Label : public Empty
public:
const std::string unitID() const; ///< "Label"
const std::string caption() const { return m_caption; }
const std::string label() const {return m_label; }

Label();
Label(const std::string& caption, const std::string& label);
void setLabel(const std::string& cpt, const std::string& lbl = "");
virtual Unit * clone() const;

virtual double conversionTOFMin() const;
virtual double conversionTOFMax() const;

/// Destructor
~Label() {}
private:
/// Caption
std::string m_caption;
/// Label
std::string m_label;
};

//=================================================================================================
Expand All @@ -274,23 +278,17 @@ class MANTID_KERNEL_DLL TOF : public Unit
public:
const std::string unitID() const; ///< "TOF"
const std::string caption() const { return "Time-of-flight"; }
const std::string label() const {return "microsecond"; }
const std::wstring utf8Label() const { return L"\u03bcs"; }

TOF();
virtual void init();
virtual double singleToTOF(const double x) const;
virtual double singleFromTOF(const double tof) const;
virtual void init();
virtual Unit * clone() const;
///@return -DBL_MAX as ToF convetanble to TOF for in any time range
virtual double conversionTOFMin()const;
///@return DBL_MAX as ToF convetanble to TOF for in any time range
virtual double conversionTOFMax()const;


/// Constructor
TOF() : Unit() {}
/// Destructor
~TOF() {}
};

//=================================================================================================
Expand All @@ -300,7 +298,6 @@ class MANTID_KERNEL_DLL Wavelength : public Unit
public:
const std::string unitID() const; ///< "Wavelength"
const std::string caption() const { return "Wavelength"; }
const std::string label() const {return "Angstrom"; }
const std::wstring utf8Label() const { return L"\u212b"; }

virtual double singleToTOF(const double x) const;
Expand Down Expand Up @@ -331,7 +328,6 @@ class MANTID_KERNEL_DLL Energy : public Unit
public:
const std::string unitID() const; ///< "Energy"
const std::string caption() const { return "Energy"; }
const std::string label() const {return "meV"; }

virtual double singleToTOF(const double x) const;
virtual double singleFromTOF(const double tof) const;
Expand All @@ -358,7 +354,6 @@ class MANTID_KERNEL_DLL Energy_inWavenumber : public Unit
public:
const std::string unitID() const; ///< "Energy_inWavenumber"
const std::string caption() const { return "Energy"; }
const std::string label() const {return "1/cm"; }
const std::wstring utf8Label() const { return L"cm\u207b\u00b9"; }

virtual double singleToTOF(const double x) const;
Expand All @@ -385,7 +380,6 @@ class MANTID_KERNEL_DLL dSpacing : public Unit
public:
const std::string unitID() const; ///< "dSpacing"
const std::string caption() const { return "d-Spacing"; }
const std::string label() const {return "Angstrom"; }
const std::wstring utf8Label() const { return L"\u212b"; }

virtual double singleToTOF(const double x) const;
Expand All @@ -412,7 +406,6 @@ class MANTID_KERNEL_DLL MomentumTransfer : public Unit
public:
const std::string unitID() const; ///< "MomentumTransfer"
const std::string caption() const { return "q"; }
const std::string label() const {return "1/Angstrom"; }
const std::wstring utf8Label() const { return L"\u212b\u207b\u00b9"; }

virtual double singleToTOF(const double x) const;
Expand All @@ -438,7 +431,6 @@ class MANTID_KERNEL_DLL QSquared : public Unit
public:
const std::string unitID() const; ///< "QSquared"
const std::string caption() const { return "Q2"; }
const std::string label() const {return "Angstrom^-2"; }
const std::wstring utf8Label() const { return L"\u212b\u207b\u00b2"; }

virtual double singleToTOF(const double x) const;
Expand All @@ -465,7 +457,6 @@ class MANTID_KERNEL_DLL DeltaE : public Unit
public:
virtual const std::string unitID() const; ///< "DeltaE"
virtual const std::string caption() const { return "Energy transfer"; }
virtual const std::string label() const {return "meV"; }

virtual double singleToTOF(const double x) const;
virtual double singleFromTOF(const double tof) const;
Expand Down Expand Up @@ -495,7 +486,6 @@ class MANTID_KERNEL_DLL DeltaE_inWavenumber : public DeltaE
public:
const std::string unitID() const; ///< "DeltaE_inWavenumber"
const std::string caption() const { return "Energy transfer"; }
const std::string label() const {return "1/cm"; }
const std::wstring utf8Label() const { return L"cm\u207b\u00b9"; }


Expand All @@ -517,7 +507,6 @@ class MANTID_KERNEL_DLL Momentum : public Unit
public:
const std::string unitID() const; ///< "Momentum"
const std::string caption() const { return "Momentum"; }
const std::string label() const {return "Angstrom^-1"; }
const std::wstring utf8Label() const { return L"\u212b\u207b\u00b9"; }

virtual double singleToTOF(const double x) const;
Expand Down Expand Up @@ -547,7 +536,6 @@ class MANTID_KERNEL_DLL SpinEchoLength : public Wavelength
public:
const std::string unitID() const; ///< "SpinEchoLength"
const std::string caption() const { return "Spin Echo Length"; }
const std::string label() const {return "nm"; }
const std::wstring utf8Label() const { return L"nm"; }

virtual double singleToTOF(const double x) const;
Expand All @@ -571,7 +559,6 @@ class MANTID_KERNEL_DLL SpinEchoTime : public Wavelength
public:
const std::string unitID() const; ///< "SpinEchoTime"
const std::string caption() const { return "Spin Echo Time"; }
const std::string label() const {return "ns"; }
const std::wstring utf8Label() const { return L"ns"; }

virtual double singleToTOF(const double x) const;
Expand All @@ -596,8 +583,6 @@ class MANTID_KERNEL_DLL Time : public Unit
public:
const std::string unitID() const; ///< "Time"
const std::string caption() const { return "t"; }
const std::string label() const {return "Second"; }
const std::wstring utf8Label() const { return L"s"; }

virtual double singleToTOF(const double x) const;
virtual double singleFromTOF(const double tof) const;
Expand All @@ -620,9 +605,9 @@ class MANTID_KERNEL_DLL Time : public Unit
/// Degrees that has degrees as unit at "Scattering angle" as title
class Degrees : public Empty
{
Degrees();
const std::string unitID() const { return ""; }
virtual const std::string caption() const { return "Scattering angle"; }
const std::string label() const { return "degrees"; }

virtual Unit * clone() const { return new Degrees(*this); }
};
Expand Down
11 changes: 10 additions & 1 deletion Code/Mantid/Framework/Kernel/inc/MantidKernel/UnitLabel.h
Expand Up @@ -39,14 +39,23 @@ namespace Mantid
public:
/// Virtual destructor
virtual ~UnitLabel();
/// "Virtual" copy constructor
virtual UnitLabel * clone() const = 0;

/// Equality operator with other label
bool operator==(const UnitLabel & rhs) const;
/// Equality operator with std::string
bool operator==(const std::string & rhs) const;
/// Equality operator with std::wtring
bool operator==(const std::wstring & rhs) const;

/// Return an ascii label for unit
virtual const std::string ascii() const = 0;
/// Return a utf-8 encoded label for unit
virtual const std::wstring utf8() const = 0;

/// Implicit conversion to std::string
operator std::string();
operator std::string() const;
};

} // namespace Kernel
Expand Down
63 changes: 53 additions & 10 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/UnitLabelTypes.h
Expand Up @@ -23,30 +23,73 @@
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
#include "MantidKernel/UnitLabel.h"
#include "MantidKernel/ClassMacros.h"

namespace Mantid { namespace Kernel
{
namespace Units
{
/// Empty label
class MANTID_KERNEL_DLL EmptyLabel : public UnitLabel
{
public:
/// Return an ascii label for unit
const std::string ascii() const;
/// Return a utf-8 encoded label for unit
const std::wstring utf8() const;
// Easily declare UnitLabels
#define DECLARE_UNIT_LABEL(ClassName) \
class MANTID_KERNEL_DLL ClassName : public UnitLabel\
{\
public:\
ClassName * clone() const;\
const std::string ascii() const;\
const std::wstring utf8() const;\
};

/// Concrete label type for microseconds
class MANTID_KERNEL_DLL Microseconds : public UnitLabel
/// Empty label
DECLARE_UNIT_LABEL(EmptyLabel);
/// Second
DECLARE_UNIT_LABEL(Second);
/// Microsecond
DECLARE_UNIT_LABEL(Microsecond);
/// Nanosecond
DECLARE_UNIT_LABEL(Nanosecond);
/// Angstrom
DECLARE_UNIT_LABEL(Angstrom);
/// InverseAngstrom
DECLARE_UNIT_LABEL(InverseAngstrom);
/// InverseAngstromSq
DECLARE_UNIT_LABEL(InverseAngstromSq);
/// MilliElectronVolts
DECLARE_UNIT_LABEL(MilliElectronVolts);
/// Metre
DECLARE_UNIT_LABEL(Metre);
/// Nanometre
DECLARE_UNIT_LABEL(Nanometre);
/// Inverse centimeters
DECLARE_UNIT_LABEL(InverseCM);


//-------------------------------------------------------------------------

/// Free text label with a different non-default constructor
class MANTID_KERNEL_DLL TextLabel : public UnitLabel
{
public:
/// Constructor
TextLabel(const std::string & ascii, const std::wstring & utf8);
/// Return a new object of this type
TextLabel * clone() const;

/// Return an ascii label for unit
const std::string ascii() const;
/// Return a utf-8 encoded label for unit
const std::wstring utf8() const;

private:
DISABLE_DEFAULT_CONSTRUCT(TextLabel);
/// plain-text label
std::string m_ascii;
/// utf8 label
std::wstring m_utf8;
};

// tidy up macro
#undef DECLARE_UNIT_LABEL

} // namespace Units
}} // namespace Mantid::Kernel

Expand Down

0 comments on commit 0cbe00a

Please sign in to comment.