2021-03-17 15:20:07 -04:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2018 Mateusz Pusz
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2022-12-19 14:55:54 +01:00
|
|
|
#include <units/bits/quantity_concepts.h>
|
2022-12-16 18:15:48 +01:00
|
|
|
#include <units/quantity_spec.h>
|
2022-09-08 19:11:45 +02:00
|
|
|
#include <units/unit.h>
|
2021-03-17 15:20:07 -04:00
|
|
|
|
|
|
|
|
namespace units {
|
|
|
|
|
|
|
|
|
|
/**
|
2022-12-16 18:15:48 +01:00
|
|
|
* @brief Quantity reference type
|
2021-03-17 15:20:07 -04:00
|
|
|
*
|
2022-12-16 18:15:48 +01:00
|
|
|
* Quantity reference describes all the properties of a quantity besides its
|
|
|
|
|
* representation type.
|
|
|
|
|
*
|
|
|
|
|
* In most cases this class template is not explicitly instantiated by the user.
|
|
|
|
|
* It is implicitly instantiated by the library's framework while binding a quantity
|
|
|
|
|
* specification with a compatible unit.
|
2021-03-17 15:20:07 -04:00
|
|
|
*
|
|
|
|
|
* @code{.cpp}
|
2022-12-16 18:15:48 +01:00
|
|
|
* Reference auto kmph = isq::speed[km / h];
|
|
|
|
|
* quantity_of<isq::speed[km / h]> auto speed = 90 * kmph;
|
2021-03-17 15:20:07 -04:00
|
|
|
* @endcode
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-03-18 18:56:31 -04:00
|
|
|
* The following syntaxes are not allowed:
|
2022-12-16 18:15:48 +01:00
|
|
|
* `2 / kmph`, `kmph * 3`, `kmph / 4`, `70 * isq::length[km] / isq:time[h]`.
|
2021-03-17 15:20:07 -04:00
|
|
|
*/
|
2022-12-16 18:15:48 +01:00
|
|
|
template<QuantitySpec auto Q, Unit auto U>
|
2022-10-06 23:43:01 +01:00
|
|
|
struct reference {
|
2022-12-16 18:15:48 +01:00
|
|
|
static constexpr QuantitySpec auto quantity_spec = Q;
|
|
|
|
|
static constexpr Dimension auto dimension = Q.dimension;
|
|
|
|
|
static constexpr Unit auto unit = U;
|
2022-09-08 19:11:45 +02:00
|
|
|
};
|
2021-03-17 15:20:07 -04:00
|
|
|
|
2022-09-08 19:11:45 +02:00
|
|
|
// Reference
|
2022-10-06 23:43:01 +01:00
|
|
|
|
2022-09-08 19:11:45 +02:00
|
|
|
template<Reference R1, Reference R2>
|
2022-12-16 18:15:48 +01:00
|
|
|
[[nodiscard]] consteval reference<R1::quantity_spec * R2::quantity_spec, R1::unit * R2::unit> operator*(R1, R2)
|
2022-09-08 19:11:45 +02:00
|
|
|
{
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<Reference R1, Reference R2>
|
2022-12-16 18:15:48 +01:00
|
|
|
[[nodiscard]] consteval reference<R1::quantity_spec / R2::quantity_spec, R1::unit / R2::unit> operator/(R1, R2)
|
2022-09-08 19:11:45 +02:00
|
|
|
{
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 23:43:01 +01:00
|
|
|
template<Representation Rep, Reference R>
|
2022-09-08 19:11:45 +02:00
|
|
|
[[nodiscard]] constexpr quantity<R{}, Rep> operator*(const Rep& lhs, R)
|
|
|
|
|
{
|
|
|
|
|
return quantity<R{}, Rep>(lhs);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 23:43:01 +01:00
|
|
|
void /*Use `q * (1 * r)` rather than `q * r`.*/ operator*(Quantity auto, Reference auto) = delete;
|
2022-09-08 19:11:45 +02:00
|
|
|
|
2022-11-30 18:00:04 +01:00
|
|
|
template<Reference R1, Reference R2>
|
|
|
|
|
[[nodiscard]] consteval bool operator==(R1, R2)
|
|
|
|
|
{
|
2022-12-16 18:15:48 +01:00
|
|
|
return R1::quantity_spec == R2::quantity_spec && R1::unit == R2::unit;
|
2022-11-30 18:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
2022-10-09 21:32:38 +01:00
|
|
|
template<Reference R1, Reference R2>
|
2022-11-08 10:57:05 -10:00
|
|
|
[[nodiscard]] consteval bool interconvertible(R1, R2)
|
2022-10-09 21:32:38 +01:00
|
|
|
{
|
2022-12-16 18:15:48 +01:00
|
|
|
return interconvertible(R1::quantity_spec, R2::quantity_spec) && interconvertible(R1::unit, R2::unit);
|
2022-10-09 21:32:38 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 10:11:52 +01:00
|
|
|
[[nodiscard]] consteval auto common_reference(Reference auto r1, Reference auto r2, Reference auto... rest)
|
2022-10-09 21:32:38 +01:00
|
|
|
requires requires {
|
2022-11-10 16:46:56 -10:00
|
|
|
{
|
2022-12-16 18:15:48 +01:00
|
|
|
common_quantity_spec(r1.quantity_spec, r2.quantity_spec, rest.quantity_spec...)
|
|
|
|
|
} -> QuantitySpec;
|
2022-11-10 16:46:56 -10:00
|
|
|
{
|
2022-11-30 10:11:52 +01:00
|
|
|
common_unit(r1.unit, r2.unit, rest.unit...)
|
2022-11-10 16:46:56 -10:00
|
|
|
} -> Unit;
|
|
|
|
|
}
|
2022-11-09 18:39:51 -10:00
|
|
|
{
|
2022-12-16 18:15:48 +01:00
|
|
|
return reference<common_quantity_spec(r1.quantity_spec, r2.quantity_spec, rest.quantity_spec...),
|
2022-11-30 10:11:52 +01:00
|
|
|
common_unit(r1.unit, r2.unit, rest.unit...)>{};
|
2022-11-09 18:39:51 -10:00
|
|
|
}
|
2022-10-09 21:32:38 +01:00
|
|
|
|
2022-11-09 18:39:51 -10:00
|
|
|
} // namespace units
|