Skip to content

Commit cfc47fc

Browse files
authored
Merge pull request #32 from LemLib/feat/implicit-number-double
✨ Convert `double` to `Number` implicitly
2 parents 54e887f + b9b78f3 commit cfc47fc

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

include/units/Angle.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,39 +99,27 @@ static inline Angle constrainAngle180(Angle in) {
9999

100100
// Angle to/from operators
101101
// Standard orientation
102-
constexpr inline Angle from_stRad(double value) { return Angle(value); }
103-
104102
constexpr inline Angle from_stRad(Number value) { return Angle(value.internal()); }
105103

106104
constexpr inline double to_stRad(Angle quantity) { return quantity.internal(); }
107105

108-
constexpr inline Angle from_stDeg(double value) { return value * deg; }
109-
110106
constexpr inline Angle from_stDeg(Number value) { return value * deg; }
111107

112108
constexpr inline double to_stDeg(Angle quantity) { return quantity.convert(deg); }
113109

114-
constexpr inline Angle from_stRot(double value) { return value * rot; }
115-
116110
constexpr inline Angle from_stRot(Number value) { return value * rot; }
117111

118112
constexpr inline double to_stRot(Angle quantity) { return quantity.convert(rot); }
119113

120114
// Compass orientation
121-
constexpr inline Angle from_cRad(double value) { return 90 * deg - Angle(value); }
122-
123115
constexpr inline Angle from_cRad(Number value) { return 90 * deg - Angle(value.internal()); }
124116

125117
constexpr inline double to_cRad(Angle quantity) { return quantity.internal(); }
126118

127-
constexpr inline Angle from_cDeg(double value) { return (90 - value) * deg; }
128-
129119
constexpr inline Angle from_cDeg(Number value) { return (90 - value.internal()) * deg; }
130120

131121
constexpr inline double to_cDeg(Angle quantity) { return (90 * deg - quantity).convert(deg); }
132122

133-
constexpr inline Angle from_cRot(double value) { return (90 - value) * deg; }
134-
135123
constexpr inline Angle from_cRot(Number value) { return (90 - value.internal()) * deg; }
136124

137125
constexpr inline double to_cRot(Angle quantity) { return (90 * deg - quantity).convert(rot); }

include/units/Temperature.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,14 @@ constexpr Temperature operator""_fahrenheit(unsigned long long value) {
4848

4949
namespace units {
5050

51-
constexpr inline Temperature from_kelvin(double value) { return Temperature(value); }
52-
5351
constexpr inline Temperature from_kelvin(Number value) { return Temperature(value.internal()); }
5452

5553
constexpr inline double to_kelvin(Temperature quantity) { return quantity.internal(); }
5654

57-
constexpr inline Temperature from_celsius(double value) { return Temperature(value + 273.15); }
58-
5955
constexpr inline Temperature from_celsius(Number value) { return Temperature(value.internal() + 273.15); }
6056

6157
constexpr inline double to_celsius(Temperature quantity) { return quantity.internal() - 273.15; }
6258

63-
constexpr inline Temperature from_fahrenheit(double value) { return Temperature((value - 32) * (5.0 / 9.0) + 273.15); }
64-
6559
constexpr inline Temperature from_fahrenheit(Number value) {
6660
return Temperature((value.internal() - 32) * (5.0 / 9.0) + 273.15);
6761
}

include/units/units.hpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,13 @@ template <isQuantity Q, isQuantity R> constexpr bool operator>(const Q& lhs, con
292292
return os; \
293293
} \
294294
constexpr inline Name from_##suffix(double value) { return Name(value); } \
295+
constexpr inline Name from_##suffix(Number value) { return Name(value.internal()); } \
295296
constexpr inline double to_##suffix(Name quantity) { return quantity.internal(); }
296297

297298
#define NEW_UNIT_LITERAL(Name, suffix, multiple) \
298299
[[maybe_unused]] constexpr Name suffix = multiple; \
299300
constexpr Name operator""_##suffix(long double value) { return static_cast<double>(value) * multiple; } \
300301
constexpr Name operator""_##suffix(unsigned long long value) { return static_cast<double>(value) * multiple; } \
301-
constexpr inline Name from_##suffix(double value) { return value * multiple; } \
302302
constexpr inline Name from_##suffix(Number value) { return value.internal() * multiple; } \
303303
constexpr inline double to_##suffix(Name quantity) { return quantity.convert(multiple); }
304304

@@ -312,8 +312,48 @@ template <isQuantity Q, isQuantity R> constexpr bool operator>(const Q& lhs, con
312312
NEW_UNIT_LITERAL(Name, u##base, base / 1E6) \
313313
NEW_UNIT_LITERAL(Name, n##base, base / 1E9)
314314

315-
NEW_UNIT(Number, num, 0, 0, 0, 0, 0, 0, 0, 0)
316-
NEW_UNIT_LITERAL(Number, percent, num / 100.0);
315+
/* Number is a special type, because it can be implicitly converted to and from any arithmetic type */
316+
class Number : public Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>,
317+
std::ratio<0>, std::ratio<0>> {
318+
public:
319+
template <typename T> constexpr Number(T value)
320+
: Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>,
321+
std::ratio<0>, std::ratio<0>>(double(value)) {}
322+
323+
constexpr Number(Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>,
324+
std::ratio<0>, std::ratio<0>, std::ratio<0>>
325+
value)
326+
: Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>,
327+
std::ratio<0>, std::ratio<0>>(value) {};
328+
};
329+
330+
template <> struct LookupName<Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>,
331+
std::ratio<0>, std::ratio<0>, std::ratio<0>>> {
332+
using Named = Number;
333+
};
334+
335+
[[maybe_unused]] constexpr Number num = Number(1.0);
336+
337+
constexpr Number operator""_num(long double value) {
338+
return Number(Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>,
339+
std::ratio<0>, std::ratio<0>>(static_cast<double>(value)));
340+
}
341+
342+
constexpr Number operator""_num(unsigned long long value) {
343+
return Number(Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>,
344+
std::ratio<0>, std::ratio<0>>(static_cast<double>(value)));
345+
}
346+
347+
inline std::ostream& operator<<(std::ostream& os, const Number& quantity) {
348+
os << quantity.internal() << " " << num;
349+
return os;
350+
}
351+
352+
constexpr inline Number from_num(double value) { return Number(value); }
353+
354+
constexpr inline double to_num(Number quantity) { return quantity.internal(); }
355+
356+
NEW_UNIT_LITERAL(Number, percent, num / 100)
317357

318358
NEW_UNIT(Mass, kg, 1, 0, 0, 0, 0, 0, 0, 0)
319359
NEW_UNIT_LITERAL(Mass, g, kg / 1000)

0 commit comments

Comments
 (0)