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-09-08 19:11:45 +02:00
|
|
|
#include <units/dimension.h>
|
|
|
|
|
#include <units/unit.h>
|
2021-03-17 15:20:07 -04:00
|
|
|
|
|
|
|
|
namespace units {
|
|
|
|
|
|
2022-09-08 19:11:45 +02:00
|
|
|
// TODO Concept for system reference
|
|
|
|
|
template<auto R, typename Rep = double>
|
|
|
|
|
class quantity {
|
|
|
|
|
public:
|
|
|
|
|
using reference = decltype(R);
|
|
|
|
|
static constexpr auto dimension = reference::dimension;
|
|
|
|
|
static constexpr auto unit = reference::unit;
|
2021-03-18 17:11:54 -04:00
|
|
|
|
2022-09-08 19:11:45 +02:00
|
|
|
quantity(Rep) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename Child, Dimension auto Dim, Unit auto CoU>
|
|
|
|
|
struct system_reference;
|
2021-03-18 13:23:46 -04:00
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
2022-09-08 19:11:45 +02:00
|
|
|
template<typename Child, auto Dim, auto CoU>
|
|
|
|
|
void to_base_specialization_of_system_reference(const volatile system_reference<Child, Dim, CoU>*);
|
2021-03-18 13:23:46 -04:00
|
|
|
|
2022-09-08 19:11:45 +02:00
|
|
|
template<typename T>
|
|
|
|
|
// inline constexpr bool // TODO: Replace with concept when it works with MSVC
|
|
|
|
|
concept is_derived_from_specialization_of_system_reference =
|
|
|
|
|
requires(T* t) { detail::to_base_specialization_of_system_reference(t); };
|
2021-03-18 13:23:46 -04:00
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
2021-03-17 15:20:07 -04:00
|
|
|
/**
|
2021-03-18 14:21:37 -04:00
|
|
|
* @brief The type for quantity references
|
2021-03-17 15:20:07 -04:00
|
|
|
*
|
2021-03-18 14:21:37 -04:00
|
|
|
* This type is intended to be used in the quantity references definition:
|
2021-03-17 15:20:07 -04:00
|
|
|
*
|
|
|
|
|
* @code{.cpp}
|
2021-04-07 11:26:45 +02:00
|
|
|
* namespace length_references {
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-03-17 15:20:07 -04:00
|
|
|
* inline constexpr auto m = reference<dim_length, metre>{};
|
|
|
|
|
* inline constexpr auto km = reference<dim_length, kilometre>{};
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-03-17 15:20:07 -04:00
|
|
|
* }
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-04-07 11:26:45 +02:00
|
|
|
* namespace references {
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-04-07 11:26:45 +02:00
|
|
|
* using namespace length_references;
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-04-07 11:26:45 +02:00
|
|
|
* }
|
2021-03-17 15:20:07 -04:00
|
|
|
* @endcode
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-03-18 14:21:37 -04:00
|
|
|
* Quantity references simplify quantity creation:
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-03-17 15:20:07 -04:00
|
|
|
* @code{.cpp}
|
2021-03-18 14:03:43 -04:00
|
|
|
* using namespace units::isq::si::references;
|
2021-03-17 15:20:07 -04:00
|
|
|
*
|
|
|
|
|
* auto d = 123 * m;
|
2021-03-18 16:09:00 -04:00
|
|
|
* auto v = 70 * (km / h);
|
2021-03-17 15:20:07 -04:00
|
|
|
* @endcode
|
|
|
|
|
*
|
2021-03-18 14:21:37 -04:00
|
|
|
* Also, it is allowed to define custom quantity references from existing ones:
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-03-17 15:20:07 -04:00
|
|
|
* @code{.cpp}
|
|
|
|
|
* constexpr auto Nm = N * m;
|
|
|
|
|
* constexpr auto mph = mi / h;
|
|
|
|
|
* @endcode
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-03-18 18:56:31 -04:00
|
|
|
* The following syntaxes are not allowed:
|
|
|
|
|
* `2 / s`, `km * 3`, `s / 4`, `70 * km / h`.
|
2021-03-17 15:20:07 -04:00
|
|
|
*/
|
2022-09-08 19:11:45 +02:00
|
|
|
template<typename T, Unit U>
|
|
|
|
|
struct reference;
|
2021-03-18 13:23:46 -04:00
|
|
|
|
2022-09-08 19:11:45 +02:00
|
|
|
template<typename R, Unit U>
|
|
|
|
|
requires detail::is_derived_from_specialization_of_system_reference<R>
|
|
|
|
|
struct reference<R, U> {
|
|
|
|
|
using system_reference = R;
|
|
|
|
|
static constexpr auto dimension = R::dimension;
|
|
|
|
|
static constexpr U unit{};
|
|
|
|
|
// static constexpr UNITS_MSVC_WORKAROUND(Magnitude) auto mag = dimension::mag * unit::mag;
|
2021-03-17 15:20:07 -04:00
|
|
|
};
|
|
|
|
|
|
2022-09-08 19:11:45 +02:00
|
|
|
template<DerivedDimension D, Unit U>
|
|
|
|
|
struct reference<D, U> {
|
|
|
|
|
static constexpr D dimension{};
|
|
|
|
|
static constexpr U unit{};
|
|
|
|
|
// static constexpr UNITS_MSVC_WORKAROUND(Magnitude) auto mag = dimension::mag * unit::mag;
|
|
|
|
|
};
|
2021-03-17 15:20:07 -04:00
|
|
|
|
2022-09-08 19:11:45 +02:00
|
|
|
// Reference
|
|
|
|
|
/**
|
|
|
|
|
* @brief A concept matching all references in the library.
|
|
|
|
|
*
|
|
|
|
|
* Satisfied by all specializations of @c reference.
|
|
|
|
|
*/
|
|
|
|
|
template<typename T>
|
|
|
|
|
concept Reference = is_specialization_of<T, reference>;
|
2021-03-17 15:20:07 -04:00
|
|
|
|
2022-09-08 19:11:45 +02:00
|
|
|
template<Reference R1, Reference R2>
|
2022-09-15 14:52:54 -06:00
|
|
|
[[nodiscard]] consteval reference<decltype(R1::dimension * R2::dimension), decltype(R1::unit * R2::unit)> operator*(R1,
|
2022-09-08 19:11:45 +02:00
|
|
|
R2)
|
|
|
|
|
{
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<Reference R1, Reference R2>
|
2022-09-15 14:52:54 -06:00
|
|
|
[[nodiscard]] consteval reference<decltype(R1::dimension / R2::dimension), decltype(R1::unit / R2::unit)> operator/(R1,
|
2022-09-08 19:11:45 +02:00
|
|
|
R2)
|
|
|
|
|
{
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO Update when quantity is done
|
|
|
|
|
// template<Representation Rep>
|
|
|
|
|
// [[nodiscard]] friend constexpr Quantity auto operator*(const Rep& lhs, reference)
|
|
|
|
|
template<typename Rep, Reference R>
|
|
|
|
|
[[nodiscard]] constexpr quantity<R{}, Rep> operator*(const Rep& lhs, R)
|
|
|
|
|
{
|
|
|
|
|
return quantity<R{}, Rep>(lhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// friend void /*Use `q * (1 * r)` rather than `q * r`.*/ operator*(Quantity auto, reference) = delete;
|
|
|
|
|
|
|
|
|
|
// TODO will use deducing this
|
|
|
|
|
template<typename Child, Dimension auto Dim, Unit auto CoU>
|
|
|
|
|
struct system_reference {
|
|
|
|
|
static constexpr auto dimension = Dim;
|
|
|
|
|
static constexpr auto coherent_unit = CoU;
|
|
|
|
|
|
|
|
|
|
template<Unit U>
|
|
|
|
|
// requires same_unit_reference<CoU, U>
|
2022-09-15 14:52:54 -06:00
|
|
|
[[nodiscard]] consteval reference<Child, U> operator[](U) const
|
2022-09-08 19:11:45 +02:00
|
|
|
{
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-03-17 15:20:07 -04:00
|
|
|
|
|
|
|
|
} // namespace units
|