Skip to content

Commit

Permalink
make Number able to be converted to double implicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
SizzinSeal committed Dec 27, 2024
1 parent dbe34e3 commit 7b199f1
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions include/units/units.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ template <isQuantity Q, isQuantity R> constexpr bool operator>(const Q& lhs, con
return (lhs.internal() > rhs.internal());
}

#define NEW_UNIT(Name, suffix, m, l, t, i, a, o, j, n) \
#define NEW_UNIT_FULL(Name, suffix, m, l, t, i, a, o, j, n, extra) \
class Name : public Quantity<std::ratio<m>, std::ratio<l>, std::ratio<t>, std::ratio<i>, std::ratio<a>, \
std::ratio<o>, std::ratio<j>, std::ratio<n>> { \
public: \
Expand All @@ -273,6 +273,7 @@ template <isQuantity Q, isQuantity R> constexpr bool operator>(const Q& lhs, con
value) \
: Quantity<std::ratio<m>, std::ratio<l>, std::ratio<t>, std::ratio<i>, std::ratio<a>, std::ratio<o>, \
std::ratio<j>, std::ratio<n>>(value) {}; \
extra \
}; \
template <> struct LookupName<Quantity<std::ratio<m>, std::ratio<l>, std::ratio<t>, std::ratio<i>, std::ratio<a>, \
std::ratio<o>, std::ratio<j>, std::ratio<n>>> { \
Expand All @@ -294,6 +295,8 @@ template <isQuantity Q, isQuantity R> constexpr bool operator>(const Q& lhs, con
constexpr inline Name from_##suffix(double value) { return Name(value); } \
constexpr inline double to_##suffix(Name quantity) { return quantity.internal(); }

#define NEW_UNIT(Name, suffix, m, l, t, i, a, o, j, n) NEW_UNIT_FULL(Name, suffix, m, l, t, i, a, o, j, n, )

#define NEW_UNIT_LITERAL(Name, suffix, multiple) \
[[maybe_unused]] constexpr Name suffix = multiple; \
constexpr Name operator""_##suffix(long double value) { return static_cast<double>(value) * multiple; } \
Expand All @@ -312,7 +315,7 @@ template <isQuantity Q, isQuantity R> constexpr bool operator>(const Q& lhs, con
NEW_UNIT_LITERAL(Name, u##base, base / 1E6) \
NEW_UNIT_LITERAL(Name, n##base, base / 1E9)

NEW_UNIT(Number, num, 0, 0, 0, 0, 0, 0, 0, 0)
NEW_UNIT_FULL(Number, num, 0, 0, 0, 0, 0, 0, 0, 0, constexpr operator double() { return this->value; })
NEW_UNIT_LITERAL(Number, percent, num / 100.0);

NEW_UNIT(Mass, kg, 1, 0, 0, 0, 0, 0, 0, 0)
Expand Down

0 comments on commit 7b199f1

Please sign in to comment.