Files
mp-units/src/include/units/dimension.h
T

186 lines
5.4 KiB
C++
Raw Normal View History

2018-08-22 12:11:19 +02: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
#include "bits/tools.h"
#include "bits/type_list.h"
namespace units {
// dim_id
template<int UniqueValue>
using dim_id = std::integral_constant<int, UniqueValue>;
// dim_id_less
template<typename D1, typename D2>
2018-11-10 17:09:11 -08:00
struct dim_id_less : std::bool_constant<D1::value < D2::value> {
2018-08-22 12:11:19 +02:00
};
// exp
template<typename BaseDimension, int Value> // todo: to be replaced with fixed_string when supported by the compilers
// template<fixed_string BaseDimension, int Value>
2018-08-22 12:11:19 +02:00
struct exp {
using dimension = BaseDimension;
static constexpr int value = Value;
};
2018-09-28 07:47:37 -07:00
// is_exp
namespace detail {
template<typename T>
2018-11-10 17:09:11 -08:00
inline constexpr bool is_exp = false;
2018-09-28 07:47:37 -07:00
template<typename BaseDim, int Value>
2018-11-10 17:09:11 -08:00
inline constexpr bool is_exp<exp<BaseDim, Value>> = true;
} // namespace detail
2018-09-28 07:47:37 -07:00
template<typename T>
2018-11-10 17:09:11 -08:00
concept bool Exponent = detail::is_exp<T>;
2018-09-28 07:47:37 -07:00
2018-08-22 12:11:19 +02:00
// exp_less
2018-09-28 10:53:21 -07:00
template<Exponent E1, Exponent E2>
2018-08-22 12:11:19 +02:00
struct exp_less : dim_id_less<typename E1::dimension, typename E2::dimension> {
};
// exp_invert
2018-09-28 10:53:21 -07:00
template<Exponent E>
2018-08-22 12:11:19 +02:00
struct exp_invert;
template<typename BaseDimension, int Value>
struct exp_invert<exp<BaseDimension, Value>> {
using type = exp<BaseDimension, -Value>;
};
2018-09-28 10:53:21 -07:00
template<Exponent E>
using exp_invert_t = typename exp_invert<E>::type;
2018-08-22 12:11:19 +02:00
2018-11-14 22:14:44 +01:00
// dimension_traits
template<typename T>
struct dimension_traits : std::type_identity<T> {};
template<typename T>
using dimension_traits_t = typename dimension_traits<T>::type;
2018-08-22 12:11:19 +02:00
// dimension
2018-09-28 10:53:21 -07:00
template<Exponent... Es>
2018-11-14 22:14:44 +01:00
struct dimension : std::type_identity<dimension<Es...>> {};
2018-08-22 12:11:19 +02:00
2018-09-28 07:47:37 -07:00
// is_dimension
namespace detail {
template<typename T>
2018-11-10 17:09:11 -08:00
inline constexpr bool is_dimension = false;
2018-09-28 07:47:37 -07:00
2018-09-28 10:53:21 -07:00
template<Exponent... Es>
2018-11-10 17:09:11 -08:00
inline constexpr bool is_dimension<dimension<Es...>> = true;
} // namespace detail
2018-09-28 07:47:37 -07:00
template<typename T>
2018-11-14 22:14:44 +01:00
concept bool Dimension = detail::is_dimension<typename T::type>;
// dim_invert
template<Dimension E>
struct dim_invert;
template<Exponent... Es>
struct dim_invert<dimension<Es...>> : std::type_identity<dimension_traits_t<dimension<exp_invert_t<Es>...>>> {};
template<Dimension D>
using dim_invert_t = typename dim_invert<typename D::type>::type;
2018-09-28 07:47:37 -07:00
2018-08-22 12:11:19 +02:00
// make_dimension
namespace detail {
2018-09-28 07:47:37 -07:00
template<Dimension D>
2018-08-22 12:11:19 +02:00
struct dim_consolidate;
2018-09-28 07:47:37 -07:00
template<Dimension D>
2018-08-22 12:11:19 +02:00
using dim_consolidate_t = typename dim_consolidate<D>::type;
template<>
struct dim_consolidate<dimension<>> {
2018-09-28 07:47:37 -07:00
using type = dimension<>;
2018-08-22 12:11:19 +02:00
};
2018-09-28 10:53:21 -07:00
template<Exponent E>
2018-09-28 07:47:37 -07:00
struct dim_consolidate<dimension<E>> {
using type = dimension<E>;
2018-08-22 12:11:19 +02:00
};
2018-09-28 10:53:21 -07:00
template<Exponent E1, Exponent... ERest>
2018-09-28 07:47:37 -07:00
struct dim_consolidate<dimension<E1, ERest...>> {
using rest = dim_consolidate_t<dimension<ERest...>>;
using type =
std::conditional_t<std::is_same_v<rest, dimension<>>, dimension<E1>, mp::type_list_push_front_t<rest, E1>>;
2018-08-22 12:11:19 +02:00
};
2018-09-28 10:53:21 -07:00
template<typename D, int V1, int V2, Exponent... ERest>
2018-08-22 12:11:19 +02:00
struct dim_consolidate<dimension<exp<D, V1>, exp<D, V2>, ERest...>> {
using type = std::conditional_t<V1 + V2 == 0, dim_consolidate_t<dimension<ERest...>>,
dim_consolidate_t<dimension<exp<D, V1 + V2>, ERest...>>>;
};
} // namespace detail
2018-09-28 10:53:21 -07:00
template<Exponent... Es>
2018-08-22 12:11:19 +02:00
struct make_dimension {
2018-09-28 10:53:21 -07:00
using type = detail::dim_consolidate_t<mp::type_list_sort_t<dimension<Es...>, exp_less>>;
2018-08-22 12:11:19 +02:00
};
2018-09-28 10:53:21 -07:00
template<Exponent... Es>
using make_dimension_t = typename make_dimension<Es...>::type;
2018-08-22 12:11:19 +02:00
// dimension_multiply
2018-09-28 07:47:37 -07:00
template<Dimension D1, Dimension D2>
2018-08-22 12:11:19 +02:00
struct dimension_multiply;
2018-09-28 10:53:21 -07:00
template<Exponent... E1, Exponent... E2>
2018-11-14 22:14:44 +01:00
struct dimension_multiply<dimension<E1...>, dimension<E2...>> : std::type_identity<dimension_traits_t<make_dimension_t<E1..., E2...>>> {};
2018-08-22 12:11:19 +02:00
2018-09-28 07:47:37 -07:00
template<Dimension D1, Dimension D2>
2018-11-14 22:14:44 +01:00
using dimension_multiply_t = typename dimension_multiply<typename D1::type, typename D2::type>::type;
2018-08-22 12:11:19 +02:00
// dimension_divide
2018-09-28 07:47:37 -07:00
template<Dimension D1, Dimension D2>
2018-08-22 12:11:19 +02:00
struct dimension_divide;
2018-09-28 10:53:21 -07:00
template<Exponent... E1, Exponent... E2>
struct dimension_divide<dimension<E1...>, dimension<E2...>>
: dimension_multiply<dimension<E1...>, dimension<exp_invert_t<E2>...>> {
2018-08-22 12:11:19 +02:00
};
2018-09-28 07:47:37 -07:00
template<Dimension D1, Dimension D2>
2018-11-14 22:14:44 +01:00
using dimension_divide_t = typename dimension_divide<typename D1::type, typename D2::type>::type;
2018-08-22 12:11:19 +02:00
} // namespace units