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

172 lines
4.7 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-09-28 07:47:37 -07: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>
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>
struct is_exp : std::false_type {
};
template<typename BaseDim, int Value>
struct is_exp<exp<BaseDim, Value>> : std::true_type {
};
}
template<typename T>
2018-09-28 14:18:43 -07:00
concept bool Exponent = detail::is_exp<T>::value;
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
// dimension
2018-09-28 10:53:21 -07:00
template<Exponent... Es>
2018-09-28 14:18:43 -07:00
struct dimension;
2018-08-22 12:11:19 +02:00
2018-09-28 07:47:37 -07:00
// is_dimension
namespace detail {
template<typename T>
struct is_dimension : std::false_type {};
2018-09-28 10:53:21 -07:00
template<Exponent... Es>
struct is_dimension<dimension<Es...>> : std::bool_constant<(is_exp<Es>::value && ...)> {};
2018-09-28 07:47:37 -07:00
}
template<typename T>
2018-09-28 14:18:43 -07:00
concept bool Dimension = detail::is_dimension<T>::value;
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
2018-09-28 07:47:37 -07:00
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>
struct dimension_multiply<dimension<E1...>, dimension<E2...>> {
using type = 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-08-22 12:11:19 +02:00
using dimension_multiply_t = typename dimension_multiply<D1, D2>::type;
// 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-08-22 12:11:19 +02:00
using dimension_divide_t = typename dimension_divide<D1, D2>::type;
} // namespace units