-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMass.h
112 lines (90 loc) · 3.56 KB
/
Mass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// MIT License
//
// Copyright (c) 2021 Daniel Robertson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef HX711_MASS_H_2FFE3D59_FB56_4C50_87F6_08F5AD88A303
#define HX711_MASS_H_2FFE3D59_FB56_4C50_87F6_08F5AD88A303
#include <cstdint>
#include <ostream>
#include <string>
#include <unordered_map>
namespace HX711 {
class Mass {
public:
enum class Unit : unsigned char {
UG,
MG,
G,
KG,
TON,
IMP_TON,
US_TON,
ST,
LB,
OZ
};
protected:
/**
* This needs to be a sufficient size to contain a floating point
* number, space, and unit name. eg.
*
* "39823.3801 ton (IMP)"
*/
static const std::size_t _TOSTRING_BUFF_SIZE = 64;
static const std::unordered_map<const Unit, const double> _RATIOS;
static const std::unordered_map<const Unit, const char* const> _UNIT_NAMES;
//deal with mass internally as micrograms
double _ug;
//unit the calling code has chosen to represent this Mass
Unit _u;
public:
Mass(const double amount = 0.0, const Unit u = Unit::UG) noexcept;
Mass(const Mass& m2) noexcept;
Mass& operator=(const Mass& rhs) noexcept;
operator double() const noexcept;
double getValue() const noexcept;
double getValue(const Unit u) const noexcept;
Unit getUnit() const noexcept;
void setUnit(const Unit u) noexcept;
Mass convertTo(const Unit to) const noexcept;
friend Mass operator+(const Mass& lhs, const Mass& rhs) noexcept;
friend Mass operator-(const Mass& lhs, const Mass& rhs) noexcept;
friend Mass operator*(const Mass& lhs, const Mass& rhs) noexcept;
friend Mass operator/(const Mass& lhs, const Mass& rhs);
Mass& operator+=(const Mass& rhs) noexcept;
Mass& operator-=(const Mass& rhs) noexcept;
Mass& operator*=(const Mass& rhs) noexcept;
Mass& operator/=(const Mass& rhs);
friend bool operator==(const Mass& lhs, const Mass& rhs) noexcept;
friend bool operator!=(const Mass& lhs, const Mass& rhs) noexcept;
friend bool operator<(const Mass& lhs, const Mass& rhs) noexcept;
friend bool operator>(const Mass& lhs, const Mass& rhs) noexcept;
friend bool operator<=(const Mass& lhs, const Mass& rhs) noexcept;
friend bool operator>=(const Mass& lhs, const Mass& rhs) noexcept;
std::string toString() const noexcept;
std::string toString(const Unit u) const noexcept;
friend std::ostream& operator<<(std::ostream& os, const Mass& m) noexcept;
static double convert(
const double amount,
const Unit from,
const Unit to = Unit::UG) noexcept;
};
};
#endif