1#ifndef PHYTH_QUANTITY_HPP
2#define PHYTH_QUANTITY_HPP
26 template<
typename UnitT>
45 template<
typename OtherUnit>
48 static_assert(std::is_same_v<typename UnitT::DimensionT, typename OtherUnit::DimensionT>,
49 "Cannot convert between different dimensions");
62 template<
typename TargetUnit>
63 constexpr auto As()
const {
64 static_assert(std::is_same_v<
typename UnitT::DimensionT,
65 typename TargetUnit::DimensionT>,
66 "Cannot convert between different dimensions");
76 template<
typename TargetType>
77 constexpr auto To()
const {
88 template<
typename OtherUnit>
90 static_assert(std::is_same_v<typename UnitT::DimensionT, typename OtherUnit::DimensionT>,
91 "Cannot add different dimensions");
103 template<
typename OtherUnit>
105 static_assert(std::is_same_v<typename UnitT::DimensionT, typename OtherUnit::DimensionT>,
106 "Cannot subtract different dimensions");
157 template<
typename UnitT>
181 template<
typename U1,
typename U2>
183 static_assert(std::is_same_v<typename U1::DimensionT, typename U2::DimensionT>,
184 "Cannot compare different dimensions");
185 return a.value ==
b.value * (U2::scale / U1::scale);
201 template<
typename U1,
typename U2>
219 template<
typename U1,
typename U2>
221 static_assert(std::is_same_v<typename U1::DimensionT, typename U2::DimensionT>,
222 "Cannot compare different dimensions");
223 return a.value <
b.value * (U2::scale / U1::scale);
239 template<
typename U1,
typename U2>
258 template<
typename U1,
typename U2>
277 template<
typename U1,
typename U2>
294 template<
typename UnitT>
297 "Cannot compare a dimensionful quantity with a scalar");
313 template<
typename UnitT>
316 "Cannot compare a dimensionful quantity with a scalar");
328 template<
typename UnitT>
331 "Cannot compare a dimensionful quantity with a scalar");
343 template<
typename UnitT>
346 "Cannot compare a dimensionful quantity with a scalar");
358 template<
typename UnitT>
361 "Cannot compare a dimensionful quantity with a scalar");
373 template<
typename UnitT>
376 "Cannot compare a dimensionful quantity with a scalar");
388 template<
typename UnitT>
391 "Cannot compare a dimensionful quantity with a scalar");
403 template<
typename UnitT>
406 "Cannot compare a dimensionful quantity with a scalar");
418 template<
typename UnitT>
421 "Cannot compare a dimensionful quantity with a scalar");
433 template<
typename UnitT>
436 "Cannot compare a dimensionful quantity with a scalar");
448 template<
typename UnitT>
451 "Cannot compare a dimensionful quantity with a scalar");
463 template<
typename UnitT>
466 "Cannot compare a dimensionful quantity with a scalar");
482 template<
typename U1,
typename U2>
484 static_assert(std::is_same_v<typename U1::DimensionT, typename U2::DimensionT>,
485 "Cannot add different dimensions");
501 template<
typename U1,
typename U2>
503 static_assert(std::is_same_v<typename U1::DimensionT, typename U2::DimensionT>,
504 "Cannot subtract different dimensions");
523 template<
typename U1,
typename U2>
543 template<
typename U1,
typename U2>
556 template<
typename UnitT>
559 "Cannot add a dimensionful quantity with a scalar");
571 template<
typename UnitT>
574 "Cannot add a dimensionful quantity with a scalar");
586 template<
typename UnitT>
589 "Cannot subtract a scalar from a dimensionful quantity");
601 template<
typename UnitT>
604 "Cannot subtract a dimensionful quantity from a scalar");
615 template<
typename UnitT>
627 template<
typename UnitT>
639 template<
typename UnitT>
658 template<
typename UnitT>
660 using DimT =
typename UnitT::DimensionT;
672 template<
typename UnitT>
683 template<
typename UnitT>
694 template<
typename UnitT>
705 template<
typename UnitT>
719 std::ostringstream
oss;
737 template<
typename UnitT>
739 using DimT =
typename UnitT::DimensionT;
762 template<
typename UnitT>
782template<
typename U1,
typename U2,
typename =
void>
785template<
typename U1,
typename U2>
786struct common_type_impl<U1, U2, std::enable_if_t<Phyth::is_unit_v<U1> && Phyth::is_unit_v<U2>> > {
790template<
typename U1,
typename U2>
794template<
typename U1,
typename U2>
std::string GetName() const
Get the full name of a unit type.
static UnitRegistry & GetInstance()
Get the singleton instance.
std::string GetSymbol() const
Get the symbol of a unit type.
constexpr bool operator<(Quantity< U1 > a, Quantity< U2 > b)
Less-than comparison between two quantities.
constexpr auto operator/(Quantity< U1 > a, Quantity< U2 > b)
Divide one quantity by another.
std::string UnitSymbol()
Get the symbol string for a unit type (e.g., "m", "kg", "N")
std::ostream & operator<<(std::ostream &os, const Quantity< UnitT > &q)
Stream insertion operator for Quantity.
typename DimDiv< D1, D2 >::type DimDivT
Convenience alias for DimDiv.
constexpr auto operator+(Quantity< U1 > a, Quantity< U2 > b)
Add two quantities of the same dimension.
constexpr bool operator>=(Quantity< U1 > a, Quantity< U2 > b)
Greater-than-or-equal comparison between two quantities.
std::string UnitName()
Get the full name string for a unit type (e.g., "meter", "kilogram")
constexpr auto operator*(Quantity< U1 > a, Quantity< U2 > b)
Multiply two quantities.
constexpr bool operator==(Quantity< U1 > a, Quantity< U2 > b)
Equality comparison between two quantities.
constexpr bool operator>(Quantity< U1 > a, Quantity< U2 > b)
Greater-than comparison between two quantities.
std::string QuantityToString(const Quantity< UnitT > &q)
Convert a Quantity to a human-readable string.
constexpr bool operator!=(Quantity< U1 > a, Quantity< U2 > b)
Inequality comparison between two quantities.
constexpr auto operator-(Quantity< U1 > a, Quantity< U2 > b)
Subtract two quantities of the same dimension.
std::string FormatValue(const double value)
Format a raw numeric value for string output.
constexpr bool operator<=(Quantity< U1 > a, Quantity< U2 > b)
Less-than-or-equal comparison between two quantities.
constexpr bool is_quantity_v
Convenience variable template for is_quantity.
Convert a Dimension to a human-readable string.
Physical quantity with a value and a compile-time unit.
constexpr Quantity operator++(int)
Post-increment: increase value by 1, return old value.
constexpr Quantity & operator++()
Pre-increment: increase value by 1.
constexpr Quantity & operator+=(Quantity< OtherUnit > other)
Add another quantity of the same dimension.
constexpr Quantity & operator-=(Quantity< OtherUnit > other)
Subtract another quantity of the same dimension.
constexpr Quantity(Quantity< OtherUnit > other)
Convert from another unit of the same dimension.
constexpr auto As() const
Convert this quantity to a different unit of the same dimension.
constexpr Quantity & operator/=(const double scalar)
Divide the value by a scalar.
constexpr Quantity(const double v=0.0)
Construct a quantity with a given value (default: 0)
constexpr auto To() const
Extract the numeric value as a different type.
constexpr Quantity & operator*=(const double scalar)
Multiply the value by a scalar.
constexpr Quantity & operator--()
Pre-decrement: decrease value by 1.
constexpr Quantity operator--(int)
Post-decrement: decrease value by 1, return old value.
Physical unit with dimension and scale factor relative to SI base.
Trait: check if a type is a Quantity instantiation.
std::common_type specialization for Quantity