Files
mp-units/example/custom_systems.cpp

118 lines
4.0 KiB
C++
Raw Normal View History

2020-09-10 21:17:43 +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.
#include <units/base_dimension.h>
#include <units/isq/si/prefixes.h>
#include <units/quantity.h>
#include <units/quantity_io.h>
#include <units/unit.h>
2020-09-10 21:17:43 +02:00
#include <iostream>
#include <type_traits>
2020-09-10 21:17:43 +02:00
using namespace units;
namespace fps {
struct foot : named_unit<foot, "ft"> {};
struct yard : named_scaled_unit<yard, "yd", mag<3>(), foot> {};
2020-09-10 21:17:43 +02:00
struct dim_length : base_dimension<"L", foot> {};
template<UnitOf<dim_length> U, Representation Rep = double>
2020-09-10 21:17:43 +02:00
using length = quantity<dim_length, U, Rep>;
} // namespace fps
namespace si {
struct metre : named_unit<metre, "m"> {};
struct kilometre : prefixed_unit<kilometre, units::isq::si::kilo, metre> {};
2020-09-10 21:17:43 +02:00
struct dim_length : base_dimension<"L", metre> {};
template<UnitOf<dim_length> U, Representation Rep = double>
2020-09-10 21:17:43 +02:00
using length = quantity<dim_length, U, Rep>;
namespace fps {
struct foot : named_scaled_unit<foot, "ft", mag<ratio{3'048, 10'000}>(), metre> {};
struct yard : named_scaled_unit<yard, "yd", mag<3>(), foot> {};
2020-09-10 21:17:43 +02:00
struct dim_length : base_dimension<"L", foot> {};
template<UnitOf<dim_length> U, Representation Rep = double>
2020-09-10 21:17:43 +02:00
using length = quantity<dim_length, U, Rep>;
} // namespace fps
} // namespace si
template<typename Q, typename U>
concept castable_to = Quantity<Q> && Unit<U> && requires(Q q) { quantity_cast<U>(q); };
2020-09-10 21:17:43 +02:00
void conversions()
{
// fps::yard is not defined in terms of SI units (or vice-versa)
// so the conversion between FPS and SI is not possible
static_assert(!castable_to<fps::length<fps::yard>, si::kilometre>);
2020-09-10 21:17:43 +02:00
// si::fps::yard is defined in terms of SI units
// so the conversion between FPS and SI is possible
static_assert(castable_to<si::fps::length<si::fps::yard>, si::kilometre>);
2020-09-10 21:17:43 +02:00
constexpr auto si_fps_yard = si::fps::length<si::fps::yard>(1.);
std::cout << quantity_cast<si::kilometre>(si_fps_yard) << "\n";
}
void unknown_dimensions()
{
constexpr auto fps_yard = fps::length<fps::yard>(1.);
2022-08-01 11:59:00 +02:00
constexpr auto fps_area = fps_yard * fps_yard;
2020-09-10 21:17:43 +02:00
std::cout << fps_yard << "\n";
2022-08-01 11:59:00 +02:00
std::cout << quantity_cast<decltype(fps_area)::dimension::coherent_unit>(fps_area) << "\n";
2020-09-10 21:17:43 +02:00
constexpr auto si_fps_yard = si::fps::length<si::fps::yard>(1.);
2022-08-01 11:59:00 +02:00
constexpr auto si_fps_area = si_fps_yard * si_fps_yard;
2020-09-10 21:17:43 +02:00
std::cout << si_fps_yard << "\n";
2022-08-01 11:59:00 +02:00
std::cout << quantity_cast<decltype(si_fps_area)::dimension::coherent_unit>(si_fps_area) << "\n";
2020-09-10 21:17:43 +02:00
}
std::ostream& operator<<(std::ostream& os, const ratio& r) { return os << "ratio{" << r.num << ", " << r.den << "}"; }
2020-09-10 21:17:43 +02:00
template<Unit U>
std::ostream& operator<<(std::ostream& os, const U& u)
2020-09-10 21:17:43 +02:00
{
using unit_type = std::remove_cvref_t<decltype(u)>;
return os << as_ratio(unit_type::mag) << " x " << unit_type::reference::symbol.standard();
2020-09-10 21:17:43 +02:00
}
void what_is_your_ratio()
{
std::cout << "fps: " << fps::yard() << "\n";
std::cout << "si::fps: " << si::fps::yard() << "\n";
}
int main()
{
conversions();
unknown_dimensions();
what_is_your_ratio();
}