From c892889901e246cff49bea6c88f2ca268e99cc7a Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sun, 14 May 2023 10:38:08 +0200 Subject: [PATCH] feat(example): `currency` and `unmanned_aerial_vehicle` examples added --- example/CMakeLists.txt | 2 + example/currency.cpp | 84 +++++++++++++++++++++++++++++ example/unmanned_aerial_vehicle.cpp | 68 +++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 example/currency.cpp create mode 100644 example/unmanned_aerial_vehicle.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 65f7f7fd..58d96571 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -48,6 +48,7 @@ add_example( mp-units::usc ) add_example(conversion_factor mp-units::core-fmt mp-units::core-io mp-units::si) +add_example(currency mp-units::core-io) add_example(foot_pound_second mp-units::core-fmt mp-units::international mp-units::imperial) add_example(glide_computer_example mp-units::core-fmt mp-units::international mp-units::utility glide_computer) add_example(hello_units mp-units::core-fmt mp-units::core-io mp-units::si mp-units::usc) @@ -57,6 +58,7 @@ add_example( strong_angular_quantities mp-units::core-fmt mp-units::core-io mp-units::si mp-units::isq_angle mp-units::utility ) add_example(total_energy mp-units::core-io mp-units::si mp-units::natural mp-units::utility) +add_example(unmanned_aerial_vehicle mp-units::core-io mp-units::si mp-units::international) find_package(wg21_linear_algebra CONFIG REQUIRED) add_example(linear_algebra mp-units::core-fmt mp-units::core-io mp-units::si) diff --git a/example/currency.cpp b/example/currency.cpp new file mode 100644 index 00000000..c3a46b01 --- /dev/null +++ b/example/currency.cpp @@ -0,0 +1,84 @@ +// 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 +#include +#include +#include + +using namespace mp_units; + +// clang-format off +inline constexpr struct dim_currency : base_dimension<"$"> {} dim_currency; + +QUANTITY_SPEC(currency, dim_currency); + +inline constexpr struct euro : named_unit<"EUR", kind_of> {} euro; +inline constexpr struct us_dollar : named_unit<"USD", kind_of> {} us_dollar; +inline constexpr struct great_british_pound : named_unit<"GBP", kind_of> {} great_british_pound; +inline constexpr struct japanese_jen : named_unit<"JPY", kind_of> {} japanese_jen; +// clang-format on + +static_assert(!std::equality_comparable_with, quantity>); + +#if 0 + +// if you have only a few currencies to handle +template +double exchange_rate() +{ + if constexpr (From == us_dollar && To == euro) return 0.9215; + else if constexpr (From == euro && To == us_dollar) return 1.0848; + // ... +} + +#else + +std::string_view to_string_view(Unit auto u) { return u.symbol.ascii().c_str(); } + +template +double exchange_rate() +{ + static std::map, double> rates = { + {{"USD", "EUR"}, 0.9215}, {{"EUR", "USD"}, 1.0848}, + // ... + }; + + return rates[std::make_pair(to_string_view(From), to_string_view(To))]; +} + +#endif + +template auto To> +quantity exchange_to(QuantityOf auto q) +{ + return static_cast(exchange_rate() * q.number()) * euro; +} + +int main() +{ + auto price_usd = 100 * us_dollar; + auto price_euro = exchange_to(price_usd); + + std::cout << price_usd << " -> " << price_euro << "\n"; + // std::cout << price_usd + price_euro << "\n"; // does not compile +} diff --git a/example/unmanned_aerial_vehicle.cpp b/example/unmanned_aerial_vehicle.cpp new file mode 100644 index 00000000..c1e42652 --- /dev/null +++ b/example/unmanned_aerial_vehicle.cpp @@ -0,0 +1,68 @@ +// 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 +#include +#include +#include +#include +#include + +using namespace mp_units; + +// clang-format off +inline constexpr struct mean_sea_level : absolute_point_origin {} mean_sea_level; +inline constexpr struct height_above_launch : absolute_point_origin {} height_above_launch; +// clang-format on + +using msl_altitude = quantity_point; +using hal_altitude = quantity_point; + +static_assert(!std::equality_comparable_with); + +class unmanned_aerial_vehicle { + msl_altitude current_ = 0 * si::metre; + msl_altitude launch_ = current_; +public: + void take_off(msl_altitude alt) { launch_ = alt; } + msl_altitude take_off() const { return launch_; } + + void current(msl_altitude alt) { current_ = alt; } + msl_altitude current() const { return current_; } + + hal_altitude hal() const { return current_ - launch_; } +}; + + +int main() +{ + using namespace mp_units::si::unit_symbols; + using namespace mp_units::international::unit_symbols; + + unmanned_aerial_vehicle uav; + uav.take_off(6'000 * ft); + uav.current(10'000 * ft); + std::cout << "hal = " << uav.hal().relative() << "\n"; + + msl_altitude ground_level = 123 * m; + std::cout << "agl = " << uav.current() - ground_level << "\n"; +}