feat(example): currency and unmanned_aerial_vehicle examples added

This commit is contained in:
Mateusz Pusz
2023-05-14 10:38:08 +02:00
parent c33e3a584d
commit c892889901
3 changed files with 154 additions and 0 deletions

View File

@@ -48,6 +48,7 @@ add_example(
mp-units::usc mp-units::usc
) )
add_example(conversion_factor mp-units::core-fmt mp-units::core-io mp-units::si) 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(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(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) 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 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(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) find_package(wg21_linear_algebra CONFIG REQUIRED)
add_example(linear_algebra mp-units::core-fmt mp-units::core-io mp-units::si) add_example(linear_algebra mp-units::core-fmt mp-units::core-io mp-units::si)

84
example/currency.cpp Normal file
View File

@@ -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 <mp_units/iostream.h>
#include <mp_units/quantity.h>
#include <iostream>
#include <map>
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<currency>> {} euro;
inline constexpr struct us_dollar : named_unit<"USD", kind_of<currency>> {} us_dollar;
inline constexpr struct great_british_pound : named_unit<"GBP", kind_of<currency>> {} great_british_pound;
inline constexpr struct japanese_jen : named_unit<"JPY", kind_of<currency>> {} japanese_jen;
// clang-format on
static_assert(!std::equality_comparable_with<quantity<euro, int>, quantity<us_dollar, int>>);
#if 0
// if you have only a few currencies to handle
template<Unit auto From, Unit auto To>
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<Unit auto From, Unit auto To>
double exchange_rate()
{
static std::map<std::pair<std::string_view, std::string_view>, 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<ReferenceOf<currency> auto To>
quantity<To, int> exchange_to(QuantityOf<currency> auto q)
{
return static_cast<int>(exchange_rate<q.unit, get_unit(To)>() * q.number()) * euro;
}
int main()
{
auto price_usd = 100 * us_dollar;
auto price_euro = exchange_to<euro>(price_usd);
std::cout << price_usd << " -> " << price_euro << "\n";
// std::cout << price_usd + price_euro << "\n"; // does not compile
}

View File

@@ -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 <mp_units/iostream.h>
#include <mp_units/quantity_point.h>
#include <mp_units/systems/international/international.h>
#include <mp_units/systems/isq/space_and_time.h>
#include <mp_units/systems/si/unit_symbols.h>
#include <iostream>
using namespace mp_units;
// clang-format off
inline constexpr struct mean_sea_level : absolute_point_origin<isq::altitude> {} mean_sea_level;
inline constexpr struct height_above_launch : absolute_point_origin<isq::altitude> {} height_above_launch;
// clang-format on
using msl_altitude = quantity_point<isq::altitude[si::metre], mean_sea_level>;
using hal_altitude = quantity_point<isq::altitude[si::metre], height_above_launch>;
static_assert(!std::equality_comparable_with<msl_altitude, hal_altitude>);
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";
}