2019-12-04 17:46:19 +01: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
|
|
|
|
|
|
2021-01-04 19:37:38 +01:00
|
|
|
#include <units/bits/external/hacks.h>
|
2019-12-12 13:17:31 +01:00
|
|
|
#include <limits>
|
2019-12-04 17:46:19 +01:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
|
|
namespace units {
|
|
|
|
|
|
2019-12-12 13:17:31 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Specifies if a value of a type should be treated as a floating-point value
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2019-12-12 13:17:31 +01:00
|
|
|
* This type trait should be specialized for a custom representation type to specify
|
|
|
|
|
* that values fo this type should be treated by the library as a floating-point ones
|
|
|
|
|
* which will enable implicit conversions between quantities.
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2019-12-12 13:17:31 +01:00
|
|
|
* @tparam Rep a representation type for which a type trait is defined
|
|
|
|
|
*/
|
2020-10-06 18:17:52 +02:00
|
|
|
template<typename Rep>
|
2019-12-12 13:17:31 +01:00
|
|
|
inline constexpr bool treat_as_floating_point = std::is_floating_point_v<Rep>;
|
2019-12-04 17:46:19 +01:00
|
|
|
|
2020-10-06 18:17:52 +02:00
|
|
|
template<typename T>
|
|
|
|
|
requires requires { typename T::value_type; }
|
|
|
|
|
inline constexpr bool treat_as_floating_point<T> = treat_as_floating_point<typename T::value_type>;
|
|
|
|
|
|
2019-12-12 13:17:31 +01:00
|
|
|
/**
|
|
|
|
|
* @brief A type trait that defines zero, one, min, and max for a representation type
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2019-12-12 13:17:31 +01:00
|
|
|
* The zero, one, min, and max member functions in units::quantity forward their work to
|
|
|
|
|
* these methods. This type can be specialized if the representation Rep requires a specific
|
|
|
|
|
* implementation to return these quantity objects.
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2019-12-12 13:17:31 +01:00
|
|
|
* @tparam Rep a representation type for which a type trait is defined
|
|
|
|
|
*/
|
2020-10-06 18:17:52 +02:00
|
|
|
template<typename Rep>
|
2019-12-12 13:17:31 +01:00
|
|
|
struct quantity_values {
|
2021-01-04 19:37:38 +01:00
|
|
|
static constexpr Rep zero() noexcept
|
|
|
|
|
requires std::constructible_from<Rep, int>
|
2022-03-17 23:59:48 +01:00
|
|
|
{
|
|
|
|
|
return Rep(0);
|
|
|
|
|
}
|
2021-01-04 19:37:38 +01:00
|
|
|
|
|
|
|
|
static constexpr Rep one() noexcept
|
|
|
|
|
requires std::constructible_from<Rep, int>
|
2022-03-17 23:59:48 +01:00
|
|
|
{
|
|
|
|
|
return Rep(1);
|
|
|
|
|
}
|
2021-01-04 19:37:38 +01:00
|
|
|
|
|
|
|
|
static constexpr Rep min() noexcept
|
2022-03-17 23:59:48 +01:00
|
|
|
requires requires {
|
|
|
|
|
{
|
|
|
|
|
std::numeric_limits<Rep>::lowest()
|
|
|
|
|
} -> std::same_as<Rep>;
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
return std::numeric_limits<Rep>::lowest();
|
|
|
|
|
}
|
2021-01-04 19:37:38 +01:00
|
|
|
|
|
|
|
|
static constexpr Rep max() noexcept
|
2022-03-17 23:59:48 +01:00
|
|
|
requires requires {
|
|
|
|
|
{
|
|
|
|
|
std::numeric_limits<Rep>::max()
|
|
|
|
|
} -> std::same_as<Rep>;
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
return std::numeric_limits<Rep>::max();
|
|
|
|
|
}
|
2019-12-12 13:17:31 +01:00
|
|
|
};
|
2019-12-04 17:46:19 +01:00
|
|
|
|
2020-10-07 12:02:08 +02:00
|
|
|
/**
|
|
|
|
|
* @brief Provides support for external quantity-like types
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2020-10-07 12:02:08 +02:00
|
|
|
* The type trait should provide the following nested type aliases: @c dimension, @c unit, @c rep,
|
2021-03-19 06:47:37 +01:00
|
|
|
* and a static member function @c number(T) that will return the raw value of the quantity.
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-02-16 16:19:57 +01:00
|
|
|
* Usage example can be found in @c units/chrono.h header file.
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2020-10-07 12:02:08 +02:00
|
|
|
* @tparam T the type to provide support for
|
|
|
|
|
*/
|
|
|
|
|
template<typename T>
|
2020-10-07 14:29:12 +02:00
|
|
|
struct quantity_like_traits;
|
2020-10-07 12:02:08 +02:00
|
|
|
|
2021-02-17 02:57:59 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Provides support for external quantity point-like types
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-06-27 18:35:21 -04:00
|
|
|
* The type trait should provide the following nested type aliases: @c origin, @c unit, @c rep,
|
2021-02-17 02:57:59 -04:00
|
|
|
* and a static member function @c relative(T) that will return the quantity-like value of the quantity point.
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-02-17 02:57:59 -04:00
|
|
|
* Usage example can be found in @c units/chrono.h header file.
|
2022-03-17 23:59:48 +01:00
|
|
|
*
|
2021-02-17 02:57:59 -04:00
|
|
|
* @tparam T the type to provide support for
|
|
|
|
|
*/
|
|
|
|
|
template<typename T>
|
|
|
|
|
struct quantity_point_like_traits;
|
|
|
|
|
|
2022-03-17 23:59:48 +01:00
|
|
|
} // namespace units
|