roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
work.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4
5#include "roo_flags.h"
6#include "roo_logging.h"
7
8#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
9#include <string>
10#endif
11
12#if defined(ARDUINO)
13#include <Arduino.h>
14#endif
15
16namespace roo_quantity {
17
18/// Representation of work, internally stored as floating-point Joules.
19class Work {
20 public:
21 /// Creates a work object representing an 'unknown' work.
22 Work() : work_(std::nanf("")) {}
23
24 /// Returns the work in kilojoules.
25 float inKiloJoules() const { return work_ / 1000.0f; }
26
27 /// Returns the work in joules.
28 float inJoules() const { return work_; }
29
30 /// Returns the work in millijoules.
31 float inMilliJoules() const { return work_ * 1000.0f; }
32
33 /// Returns the work in microjoules.
34 float inMicroJoules() const { return work_ * 1000000.0f; }
35
36 /// Returns whether the object represents an unknown work.
37 bool isUnknown() const { return std::isnan(work_); }
38
39 bool operator<(const Work& other) const { return work_ < other.work_; }
40
41 bool operator==(const Work& other) const { return work_ == other.work_; }
42
43 bool operator>(const Work& other) const { return other.work_ < work_; }
44
45 bool operator<=(const Work& other) const { return !(other.work_ < work_); }
46
47 bool operator>=(const Work& other) const { return !(work_ < other.work_); }
48
49 bool operator!=(const Work& other) const { return !(work_ == other.work_); }
50
51 inline Work& operator+=(const Work& other) {
52 work_ += other.inJoules();
53 return *this;
54 }
55
56 inline Work& operator-=(const Work& other) {
57 work_ -= other.inJoules();
58 return *this;
59 }
60
61 inline Work& operator*=(float multi) {
62 work_ *= multi;
63 return *this;
64 }
65
66 inline Work& operator/=(float div) {
67 work_ /= div;
68 return *this;
69 }
70
71#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
72 /// Returns the string representation of the work.
73 std::string asString() const;
74#endif
75
76#if defined(ARDUINO)
77 String asArduinoString() const;
78#endif
79
80 private:
81 friend Work UnknownWork();
82
83 friend Work WorkInJoules(float);
84
85 explicit Work(float work) : work_(work) {}
86
87 /// Stored in joules.
88 float work_;
89};
90
91inline Work WorkInJoules(float work);
92
93/// Returns a work object representing an unknown work.
94inline Work UnknownWork() { return Work(); }
95
96/// Returns a work object equivalent to the specified work
97/// expressed in gigaJoules.
98inline Work WorkInGigaJoules(float work) {
99 return WorkInJoules(work * 1000000000.0f);
100}
101
102/// Returns a work object equivalent to the specified work
103/// expressed in megaJoules.
104inline Work WorkInMegaJoules(float work) {
105 return WorkInJoules(work * 1000000.0f);
106}
107
108/// Returns a work object equivalent to the specified work
109/// expressed in kiloJoules.
110inline Work WorkInKiloJoules(float work) {
111 return WorkInJoules(work * 1000.0f);
112}
113
114/// Returns a work object equivalent to the specified work
115/// expressed in Joules.
116inline Work WorkInJoules(float work) { return Work(work); }
117
118/// Returns a work object equivalent to the specified work
119/// expressed in milliJoules.
120inline Work WorkInMilliJoules(float work) {
121 return WorkInJoules(work / 1000.0f);
122}
123
124/// Returns a work object equivalent to the specified work
125/// expressed in microJoules.
126inline Work WorkInMicroJoules(float work) {
127 return WorkInJoules(work / 1000000.0f);
128}
129
130inline Work operator+(Work a, Work b) {
131 return WorkInJoules(a.inJoules() + b.inJoules());
132}
133
134inline Work operator-(Work a, Work b) {
135 return WorkInJoules(a.inJoules() - b.inJoules());
136}
137
138inline Work operator-(Work a) { return WorkInJoules(-a.inJoules()); }
139
140inline Work operator*(Work a, float b) {
141 return WorkInJoules(a.inJoules() * b);
142}
143
144inline Work operator*(float a, Work b) {
145 return WorkInJoules(a * b.inJoules());
146}
147
148inline Work operator/(Work a, float b) {
149 return WorkInJoules(a.inJoules() / b);
150}
151
152inline float operator/(Work a, Work b) { return a.inJoules() / b.inJoules(); }
153
154roo_logging::Stream& operator<<(roo_logging::Stream& os, const Work& val);
155
156} // namespace roo_quantity
Representation of work, internally stored as floating-point Joules.
Definition work.h:19
bool operator!=(const Work &other) const
Definition work.h:49
friend Work WorkInJoules(float)
Returns a work object equivalent to the specified work expressed in Joules.
Definition work.h:116
bool isUnknown() const
Returns whether the object represents an unknown work.
Definition work.h:37
Work & operator*=(float multi)
Definition work.h:61
Work & operator-=(const Work &other)
Definition work.h:56
float inMicroJoules() const
Returns the work in microjoules.
Definition work.h:34
float inKiloJoules() const
Returns the work in kilojoules.
Definition work.h:25
Work & operator/=(float div)
Definition work.h:66
Work()
Creates a work object representing an 'unknown' work.
Definition work.h:22
Work & operator+=(const Work &other)
Definition work.h:51
float inMilliJoules() const
Returns the work in millijoules.
Definition work.h:31
bool operator<(const Work &other) const
Definition work.h:39
bool operator>(const Work &other) const
Definition work.h:43
friend Work UnknownWork()
Returns a work object representing an unknown work.
Definition work.h:94
bool operator>=(const Work &other) const
Definition work.h:47
bool operator<=(const Work &other) const
Definition work.h:45
bool operator==(const Work &other) const
Definition work.h:41
float inJoules() const
Returns the work in joules.
Definition work.h:28
For convenience conversion from roo_time::Duration.
Definition area.cpp:3
roo_logging::Stream & operator<<(roo_logging::Stream &os, const Area &val)
Definition area.cpp:57
Work WorkInGigaJoules(float work)
Returns a work object equivalent to the specified work expressed in gigaJoules.
Definition work.h:98
Area operator+(Area a, Area b)
Definition area.h:171
Work WorkInMicroJoules(float work)
Returns a work object equivalent to the specified work expressed in microJoules.
Definition work.h:126
Work WorkInKiloJoules(float work)
Returns a work object equivalent to the specified work expressed in kiloJoules.
Definition work.h:110
Work UnknownWork()
Returns a work object representing an unknown work.
Definition work.h:94
Area operator-(Area a, Area b)
Definition area.h:175
Work WorkInJoules(float work)
Returns a work object equivalent to the specified work expressed in Joules.
Definition work.h:116
Work WorkInMilliJoules(float work)
Returns a work object equivalent to the specified work expressed in milliJoules.
Definition work.h:120
Work WorkInMegaJoules(float work)
Returns a work object equivalent to the specified work expressed in megaJoules.
Definition work.h:104
Area operator/(Area a, float b)
Definition area.h:191
Area operator*(Area a, float b)
Definition area.h:183