61 lines
1.1 KiB
C++
61 lines
1.1 KiB
C++
#ifndef LENGTH_H
|
|
#define LENGTH_H
|
|
|
|
|
|
|
|
|
|
/**
|
|
* unit independent length measurement
|
|
*/
|
|
template <typename T, int mul> struct Length {
|
|
|
|
private:
|
|
|
|
const T val;
|
|
|
|
/** delete empty ctor */
|
|
Length() = delete;
|
|
|
|
/** hidden value ctor */
|
|
Length(const T val) : val(val) {;}
|
|
|
|
public:
|
|
|
|
/** construct from mm */
|
|
static Length mm(const T mm) {return Length(mm * mul / 1000);}
|
|
|
|
/** construct from cm */
|
|
static Length cm(const T cm) {return Length(cm * mul / 100);}
|
|
|
|
/** construct from m */
|
|
static Length m(const T m) {return Length(m * mul / 1);}
|
|
|
|
public:
|
|
|
|
/** get in mm */
|
|
T mm() const {return val * 1000 / mul;}
|
|
|
|
/** get in cm */
|
|
T cm() const {return val * 100 / mul;}
|
|
|
|
/** get in m */
|
|
T m() const {return val * 1 / mul;}
|
|
|
|
public:
|
|
|
|
/** add the given length */
|
|
Length operator + (const Length o) {return Length(m+o.m);}
|
|
|
|
/** subtract the given length */
|
|
Length operator - (const Length o) {return Length(m+o.m);}
|
|
|
|
};
|
|
|
|
/** float. internally stored in meters */
|
|
typedef Length<float, 1> LengthF;
|
|
|
|
/** int. internally stored in millimeters */
|
|
typedef Length<int, 1000> LengthI;
|
|
|
|
#endif // LENGTH_H
|