PCB Environment: /home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/Units.hpp Source File

PCB Environment 2

Loading...
Searching...
No Matches
Units.hpp
1#ifndef GYM_PCB_UNITS_H
2#define GYM_PCB_UNITS_H
3
4#include <iostream>
5
6 template<typename T, double M1, double Q1> class Length
7{
8public:
9 Length() { }
10 explicit Length(int32_t i) : m(i) { assert(int32_t(m) == i); }
11 explicit Length(int64_t i) : m(i) { assert(int64_t(m) == i); }
12 explicit Length(T v) : m(v) { }
13 template<double M2, double Q2> Length(const Length<T, M2, Q2> &l) { m = l.value() * ((M2 * Q1) / (M1 * Q2)); }
14 T value() const { return m; }
15 Length<T,M1,Q1> operator+(const Length<T,M1,Q1> &l) const { return Length<T,M1,Q1>(m + l.m); }
16 Length<T,M1,Q1> operator-(const Length<T,M1,Q1> &l) const { return Length<T,M1,Q1>(m - l.m); }
17 Length<T,M1,Q1>& operator+=(const Length<T,M1,Q1> &l) { m += l.m; return *this; }
18 Length<T,M1,Q1>& operator-=(const Length<T,M1,Q1> &l) { m += l.m; return *this; }
19 Length<T,M1,Q1> operator*(T s) const { return Length<T,M1,Q1>(m * s); }
20 Length<T,M1,Q1>& operator*=(T s) { m *= s; return *this; }
21 template<double M2, double Q2> bool operator< (const Length<T,M2,Q2> &l) const { return value() * (M1 * Q2) < l.value() * (Q1 * M2); }
22 template<double M2, double Q2> bool operator<=(const Length<T,M2,Q2> &l) const { return value() * (M1 * Q2) <= l.value() * (Q1 * M2); }
23 template<double M2, double Q2> bool operator==(const Length<T,M2,Q2> &l) const { return value() * (M1 * Q2) == l.value() * (Q1 * M2); }
24private:
25 T m;
26};
27
28template<typename T, double M1, double Q1> inline std::ostream& operator<<(std::ostream &os, const Length<T,M1,Q1> &l)
29{
30 const auto Q = M1 / Q1;
31 const char *s = "?";
32 if (Q == 1e-9) s = "nm";
33 else if (Q == 1e-6) s = "µm";
34 else if (Q == 1) s = "m";
35 return os << l.value() << ' ' << s;
36}
37
38using Meters = Length<double, 1.0, 1.0>;
39using Micrometers = Length<double, 1.0, 1000000.0>;
40using FMicrometers = Length<float, 1.0, 1000000.0>;
41using Nanometers = Length<double, 1.0, 1000000000.0>;
42
43#endif // GYM_PCB_UNITS_H
Length
Definition Units.hpp:7