feat: std::format support for compliant compilers added

This commit is contained in:
Mateusz Pusz
2021-09-20 15:01:58 +02:00
parent 9b842ac879
commit b1bd4cab2d
40 changed files with 1447 additions and 975 deletions
+5 -5
View File
@@ -97,9 +97,9 @@ int main()
const Time auto fill_time_left = (height / fill_level - 1) * fill_time;
std::cout << "mp-units box example...\n";
std::cout << fmt::format("fill height at {} = {} ({} full)\n", fill_time, fill_level, fill_percent);
std::cout << fmt::format("spare_capacity at {} = {}\n", fill_time, spare_capacity);
std::cout << fmt::format("input flow rate after {} = {}\n", fill_time, input_flow_rate);
std::cout << fmt::format("float rise rate = {}\n", float_rise_rate);
std::cout << fmt::format("box full E.T.A. at current flow rate = {}\n", fill_time_left);
std::cout << STD_FMT::format("fill height at {} = {} ({} full)\n", fill_time, fill_level, fill_percent);
std::cout << STD_FMT::format("spare_capacity at {} = {}\n", fill_time, spare_capacity);
std::cout << STD_FMT::format("input flow rate after {} = {}\n", fill_time, input_flow_rate);
std::cout << STD_FMT::format("float rise rate = {}\n", float_rise_rate);
std::cout << STD_FMT::format("box full E.T.A. at current flow rate = {}\n", fill_time_left);
}
+4 -4
View File
@@ -115,7 +115,7 @@ void calcs_comparison()
length::fm<float> L1A = fm<>(2.f);
length::fm<float> L2A = fm<>(3.f);
length::fm<float> LrA = L1A + L2A;
fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, LrA);
std::cout << STD_FMT::format("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, LrA);
std::cout << "The single unit method must convert large\n"
"or small values in other units to the base unit.\n"
@@ -124,17 +124,17 @@ void calcs_comparison()
length::m<float> L1B = L1A;
length::m<float> L2B = L2A;
length::m<float> LrB = L1B + L2B;
fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, LrB);
std::cout << STD_FMT::format("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, LrB);
std::cout << "In multiplication and division:\n\n";
area::fm2<float> ArA = L1A * L2A;
fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, ArA);
std::cout << STD_FMT::format("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, ArA);
std::cout << "similar problems arise\n\n";
area::m2<float> ArB = L1B * L2B;
fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, ArB);
std::cout << STD_FMT::format("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, ArB);
}
} // namespace
+12 -12
View File
@@ -55,24 +55,24 @@ struct Ship {
template<class ...Args, units::Quantity Q>
auto fmt_line(const Q a)
{
return fmt::format("{:22}", a) + (fmt::format(",{:20}", units::quantity_cast<Args>(a)) + ...);
return STD_FMT::format("{:22}", a) + (STD_FMT::format(",{:20}", units::quantity_cast<Args>(a)) + ...);
}
// Print the ship details in the units as defined in the Ship struct, in other si::imperial units, and in SI
void print_details(std::string_view description, const Ship& ship)
{
const auto waterDensity = si::fps::density::lb_per_ft3<>(62.4);
std::cout << fmt::format("{}\n", description);
std::cout << fmt::format("{:20} : {}\n", "length", fmt_line<si::fps::length::yd<>, si::length::m<>>(ship.length))
<< fmt::format("{:20} : {}\n", "draft", fmt_line<si::fps::length::yd<>, si::length::m<>>(ship.draft))
<< fmt::format("{:20} : {}\n", "beam", fmt_line<si::fps::length::yd<>, si::length::m<>>(ship.beam))
<< fmt::format("{:20} : {}\n", "mass", fmt_line<si::fps::mass::lton<>, si::mass::t<>>(ship.mass))
<< fmt::format("{:20} : {}\n", "speed", fmt_line<si::fps::speed::knot<>, si::speed::km_per_h<>>(ship.speed))
<< fmt::format("{:20} : {}\n", "power", fmt_line<si::fps::power::hp<>, si::power::kW<>>(ship.power))
<< fmt::format("{:20} : {}\n", "main guns", fmt_line<si::fps::length::in<>, si::length::mm<>>(ship.mainGuns))
<< fmt::format("{:20} : {}\n", "fire shells weighing",fmt_line<si::fps::mass::lton<>, si::mass::kg<>>(ship.shellMass))
<< fmt::format("{:20} : {}\n", "fire shells at",fmt_line<si::fps::speed::mph<>, si::speed::km_per_h<>>(ship.shellSpeed))
<< fmt::format("{:20} : {}\n", "volume underwater", fmt_line<si::volume::m3<>, si::volume::l<>>(ship.mass / waterDensity));
std::cout << STD_FMT::format("{}\n", description);
std::cout << STD_FMT::format("{:20} : {}\n", "length", fmt_line<si::fps::length::yd<>, si::length::m<>>(ship.length))
<< STD_FMT::format("{:20} : {}\n", "draft", fmt_line<si::fps::length::yd<>, si::length::m<>>(ship.draft))
<< STD_FMT::format("{:20} : {}\n", "beam", fmt_line<si::fps::length::yd<>, si::length::m<>>(ship.beam))
<< STD_FMT::format("{:20} : {}\n", "mass", fmt_line<si::fps::mass::lton<>, si::mass::t<>>(ship.mass))
<< STD_FMT::format("{:20} : {}\n", "speed", fmt_line<si::fps::speed::knot<>, si::speed::km_per_h<>>(ship.speed))
<< STD_FMT::format("{:20} : {}\n", "power", fmt_line<si::fps::power::hp<>, si::power::kW<>>(ship.power))
<< STD_FMT::format("{:20} : {}\n", "main guns", fmt_line<si::fps::length::in<>, si::length::mm<>>(ship.mainGuns))
<< STD_FMT::format("{:20} : {}\n", "fire shells weighing",fmt_line<si::fps::mass::lton<>, si::mass::kg<>>(ship.shellMass))
<< STD_FMT::format("{:20} : {}\n", "fire shells at",fmt_line<si::fps::speed::mph<>, si::speed::km_per_h<>>(ship.shellSpeed))
<< STD_FMT::format("{:20} : {}\n", "volume underwater", fmt_line<si::volume::m3<>, si::volume::l<>>(ship.mass / waterDensity));
}
int main()
+11 -16
View File
@@ -21,16 +21,11 @@
// SOFTWARE.
#include "glide_computer.h"
#include <units/bits/external/hacks.h>
#include <units/bits/fmt_hacks.h>
#include <units/chrono.h>
#include <units/generic/dimensionless.h>
#include <units/isq/si/international/length.h>
UNITS_DIAGNOSTIC_PUSH
UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE
#include <fmt/format.h>
UNITS_DIAGNOSTIC_POP
#include <array>
#include <exception>
#include <iostream>
@@ -90,7 +85,7 @@ void print(const R& gliders)
std::cout << "- Name: " << g.name << "\n";
std::cout << "- Polar:\n";
for (const auto& p : g.polar)
fmt::print(" * {:%.4Q %q} @ {:%.1Q %q} -> {:%.1Q %q}\n", p.climb, p.v, units::quantity_cast<units::one>(glide_ratio(g.polar[0])));
std::cout << STD_FMT::format(" * {:%.4Q %q} @ {:%.1Q %q} -> {:%.1Q %q}\n", p.climb, p.v, units::quantity_cast<units::one>(glide_ratio(g.polar[0])));
std::cout << "\n";
}
}
@@ -104,8 +99,8 @@ void print(const R& conditions)
for (const auto& c : conditions) {
std::cout << "- " << c.first << "\n";
const auto& w = c.second;
std::cout << " * Cloud base: " << fmt::format("{:%.0Q %q}", w.cloud_base) << " AGL\n";
std::cout << " * Thermals strength: " << fmt::format("{:%.1Q %q}", w.thermal_strength) << "\n";
std::cout << " * Cloud base: " << STD_FMT::format("{:%.0Q %q}", w.cloud_base) << " AGL\n";
std::cout << " * Thermals strength: " << STD_FMT::format("{:%.1Q %q}", w.thermal_strength) << "\n";
std::cout << "\n";
}
}
@@ -117,7 +112,7 @@ void print(const R& waypoints)
std::cout << "Waypoints:\n";
std::cout << "==========\n";
for (const auto& w : waypoints)
std::cout << fmt::format("- {}: {} {}, {:%.1Q %q}\n", w.name, w.pos.lat, w.pos.lon, w.alt);
std::cout << STD_FMT::format("- {}: {} {}, {:%.1Q %q}\n", w.name, w.pos.lat, w.pos.lon, w.alt);
std::cout << "\n";
}
@@ -128,11 +123,11 @@ void print(const task& t)
std::cout << "- Start: " << t.get_start().name << "\n";
std::cout << "- Finish: " << t.get_finish().name << "\n";
std::cout << "- Length: " << fmt::format("{:%.1Q %q}", t.get_length()) << "\n";
std::cout << "- Length: " << STD_FMT::format("{:%.1Q %q}", t.get_length()) << "\n";
std::cout << "- Legs: " << "\n";
for (const auto& l : t.get_legs())
std::cout << fmt::format(" * {} -> {} ({:%.1Q %q})\n", l.begin().name, l.end().name, l.get_length());
std::cout << STD_FMT::format(" * {} -> {} ({:%.1Q %q})\n", l.begin().name, l.end().name, l.get_length());
std::cout << "\n";
}
@@ -140,7 +135,7 @@ void print(const safety& s)
{
std::cout << "Safety:\n";
std::cout << "=======\n";
std::cout << "- Min AGL separation: " << fmt::format("{:%.0Q %q}", s.min_agl_height) << "\n";
std::cout << "- Min AGL separation: " << STD_FMT::format("{:%.0Q %q}", s.min_agl_height) << "\n";
std::cout << "\n";
}
@@ -149,8 +144,8 @@ void print(const aircraft_tow& tow)
std::cout << "Tow:\n";
std::cout << "====\n";
std::cout << "- Type: aircraft\n";
std::cout << "- Height: " << fmt::format("{:%.0Q %q}", tow.height_agl) << "\n";
std::cout << "- Performance: " << fmt::format("{:%.1Q %q}", tow.performance) << "\n";
std::cout << "- Height: " << STD_FMT::format("{:%.0Q %q}", tow.height_agl) << "\n";
std::cout << "- Performance: " << STD_FMT::format("{:%.1Q %q}", tow.performance) << "\n";
std::cout << "\n";
}
@@ -179,7 +174,7 @@ void example()
for (const auto& c : weather_conditions) {
std::string txt = "Scenario: Glider = " + g.name + ", Weather = " + c.first;
std::cout << txt << "\n";
fmt::print("{0:=^{1}}\n\n", "", txt.size());
std::cout << STD_FMT::format("{0:=^{1}}\n\n", "", txt.size());
estimate(start_time, g, c.second, t, sfty, tow);
+2 -2
View File
@@ -36,7 +36,7 @@ std::ostream& operator<<(std::ostream& os, const vector<ET, OT>& v)
{
os << "|";
for (auto i = 0U; i < v.size(); ++i) {
os << fmt::format(" {:>9}", v(i));
os << STD_FMT::format(" {:>9}", v(i));
}
os << " |";
return os;
@@ -48,7 +48,7 @@ std::ostream& operator<<(std::ostream& os, const matrix<ET, OT>& v)
for (auto i = 0U; i < v.rows(); ++i) {
os << "|";
for (auto j = 0U; j < v.columns(); ++j) {
os << fmt::format(" {:>9}", v(i, j));
os << STD_FMT::format(" {:>9}", v(i, j));
}
os << (i != v.rows() - 1U ? " |\n" : " |");
}
+4 -4
View File
@@ -49,12 +49,12 @@ int main()
constexpr length<metre> lengthA(2.0);
constexpr length<millimetre> lengthB = lengthA;
std::cout << fmt::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB)
std::cout << STD_FMT::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB)
<< "represent the same length in different units.\n\n";
std::cout << fmt::format("therefore ratio lengthA / lengthB == {}\n\n", lengthA / lengthB);
std::cout << STD_FMT::format("therefore ratio lengthA / lengthB == {}\n\n", lengthA / lengthB);
std::cout << fmt::format("conversion factor from lengthA::unit of {:%q} to lengthB::unit of {:%q}:\n\n", lengthA, lengthB)
<< fmt::format("lengthB.number( {} ) == lengthA.number( {} ) * conversion_factor( {} )\n",
std::cout << STD_FMT::format("conversion factor from lengthA::unit of {:%q} to lengthB::unit of {:%q}:\n\n", lengthA, lengthB)
<< STD_FMT::format("lengthB.number( {} ) == lengthA.number( {} ) * conversion_factor( {} )\n",
lengthB.number(), lengthA.number(), conversion_factor(lengthB, lengthA));
}
+3 -2
View File
@@ -21,6 +21,7 @@
// SOFTWARE.
#include "glide_computer.h"
#include <iostream>
#include <numeric>
#include <string_view>
@@ -136,8 +137,8 @@ namespace glide_computer {
void estimate(timestamp start_ts, const glider& g, const weather& w, const task& t, const safety& s, const aircraft_tow& at)
{
fmt::print("| {:<12} | {:^28} | {:^26} | {:^21} |\n", "Flight phase", "Duration", "Distance", "Height");
fmt::print("|{0:-^14}|{0:-^30}|{0:-^28}|{0:-^23}|\n", "");
std::cout << STD_FMT::format("| {:<12} | {:^28} | {:^26} | {:^21} |\n", "Flight phase", "Duration", "Distance", "Height");
std::cout << STD_FMT::format("|{0:-^14}|{0:-^30}|{0:-^28}|{0:-^23}|\n", "");
// ready to takeoff
flight_point pos = takeoff(start_ts, t);
+5 -10
View File
@@ -22,15 +22,10 @@
#pragma once
#include <units/bits/external/hacks.h>
#include <units/bits/fmt_hacks.h>
#include <units/isq/si/length.h>
#include <units/quantity_kind.h>
UNITS_DIAGNOSTIC_PUSH
UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE
#include <fmt/format.h>
UNITS_DIAGNOSTIC_POP
#include <limits>
#include <ostream>
@@ -106,21 +101,21 @@ class std::numeric_limits<geographic::longitude> : public numeric_limits<geograp
};
template<>
struct fmt::formatter<geographic::latitude> : formatter<geographic::latitude::value_type> {
struct STD_FMT::formatter<geographic::latitude> : formatter<geographic::latitude::value_type> {
template<typename FormatContext>
auto format(geographic::latitude lat, FormatContext& ctx)
{
fmt::format_to(ctx.out(), fmt::runtime(lat.value() > 0 ? "N" : "S"));
STD_FMT::format_to(ctx.out(), FMT_RUNTIME(lat.value() > 0 ? "N" : "S"));
return formatter<geographic::latitude::value_type>::format(lat.value() > 0 ? lat.value() : -lat.value(), ctx);
}
};
template<>
struct fmt::formatter<geographic::longitude> : formatter<geographic::longitude::value_type> {
struct STD_FMT::formatter<geographic::longitude> : formatter<geographic::longitude::value_type> {
template<typename FormatContext>
auto format(geographic::longitude lon, FormatContext& ctx)
{
fmt::format_to(ctx.out(), fmt::runtime(lon.value() > 0 ? "E" : "W"));
STD_FMT::format_to(ctx.out(), FMT_RUNTIME(lon.value() > 0 ? "E" : "W"));
return formatter<geographic::longitude::value_type>::format(lon.value() > 0 ? lon.value() : -lon.value(), ctx);
}
};
@@ -57,7 +57,7 @@
// - flight path exactly on a shortest possible line to destination
template<units::QuantityKind QK>
struct fmt::formatter<QK> : formatter<typename QK::quantity_type> {
struct STD_FMT::formatter<QK> : formatter<typename QK::quantity_type> {
template<typename FormatContext>
auto format(const QK& v, FormatContext& ctx)
{
@@ -105,12 +105,12 @@ std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>&
} // namespace glide_computer
template<>
struct fmt::formatter<glide_computer::altitude> : formatter<units::isq::si::length<units::isq::si::metre>> {
struct STD_FMT::formatter<glide_computer::altitude> : formatter<units::isq::si::length<units::isq::si::metre>> {
template<typename FormatContext>
auto format(glide_computer::altitude a, FormatContext& ctx)
{
formatter<units::isq::si::length<units::isq::si::metre>>::format(a.relative().common(), ctx);
return fmt::format_to(ctx.out(), " AMSL");
return STD_FMT::format_to(ctx.out(), " AMSL");
}
};
+7 -7
View File
@@ -55,11 +55,11 @@ int main()
#endif
constexpr Speed auto v7 = quantity_cast<int>(v6);
std::cout << v1 << '\n'; // 110 km/h
std::cout << v2 << '\n'; // 70 mi/h
std::cout << fmt::format("{}", v3) << '\n'; // 110 km/h
std::cout << fmt::format("{:*^14}", v4) << '\n'; // ***70 mi/h****
std::cout << fmt::format("{:%Q in %q}", v5) << '\n'; // 30.5556 in m/s
std::cout << fmt::format("{0:%Q} in {0:%q}", v6) << '\n'; // 31.2928 in m/s
std::cout << fmt::format("{:%Q}", v7) << '\n'; // 31
std::cout << v1 << '\n'; // 110 km/h
std::cout << v2 << '\n'; // 70 mi/h
std::cout << STD_FMT::format("{}", v3) << '\n'; // 110 km/h
std::cout << STD_FMT::format("{:*^14}", v4) << '\n'; // ***70 mi/h****
std::cout << STD_FMT::format("{:%Q in %q}", v5) << '\n'; // 30.5556 in m/s
std::cout << STD_FMT::format("{0:%Q} in {0:%q}", v6) << '\n'; // 31.2928 in m/s
std::cout << STD_FMT::format("{:%Q}", v7) << '\n'; // 31
}
+25 -27
View File
@@ -22,7 +22,7 @@
#pragma once
#include <units/bits/external/hacks.h>
#include <units/bits/fmt_hacks.h>
#include <units/format.h>
#include <units/generic/dimensionless.h>
#include <units/isq/dimensions/time.h>
@@ -151,52 +151,51 @@ constexpr Q covariance_extrapolation(Q uncertainty, Q process_noise_variance)
} // namespace kalman
template<typename... Qs>
struct fmt::formatter<kalman::state<Qs...>> {
struct STD_FMT::formatter<kalman::state<Qs...>> {
constexpr auto parse(format_parse_context& ctx)
{
detail::dynamic_specs_handler<format_parse_context> handler(specs, ctx);
return detail::parse_format_specs(ctx.begin(), ctx.end(), handler);
units::detail::dynamic_specs_handler handler(specs, ctx);
return units::detail::parse_format_specs(ctx.begin(), ctx.end(), handler);
}
template<typename FormatContext>
auto format(const kalman::state<Qs...>& s, FormatContext& ctx)
{
memory_buffer value_buffer;
std::string value_buffer;
auto to_value_buffer = std::back_inserter(value_buffer);
if (specs.precision != -1) {
if constexpr(sizeof...(Qs) == 1)
fmt::format_to(to_value_buffer, "{1:%.{0}Q %q}", specs.precision, kalman::get<0>(s));
STD_FMT::format_to(to_value_buffer, "{1:%.{0}Q %q}", specs.precision, kalman::get<0>(s));
else if constexpr(sizeof...(Qs) == 2)
fmt::format_to(to_value_buffer, "{{ {1:%.{0}Q %q}, {2:%.{0}Q %q} }}", specs.precision, kalman::get<0>(s), kalman::get<1>(s));
STD_FMT::format_to(to_value_buffer, "{{ {1:%.{0}Q %q}, {2:%.{0}Q %q} }}", specs.precision, kalman::get<0>(s), kalman::get<1>(s));
else
fmt::format_to(to_value_buffer, "{{ {1:%.{0}Q %q}, {2:%.{0}Q %q}, {3:%.{0}Q %q} }}", specs.precision, kalman::get<0>(s), kalman::get<1>(s), kalman::get<2>(s));
STD_FMT::format_to(to_value_buffer, "{{ {1:%.{0}Q %q}, {2:%.{0}Q %q}, {3:%.{0}Q %q} }}", specs.precision, kalman::get<0>(s), kalman::get<1>(s), kalman::get<2>(s));
}
else {
if constexpr(sizeof...(Qs) == 1)
fmt::format_to(to_value_buffer, "{}", kalman::get<0>(s));
STD_FMT::format_to(to_value_buffer, "{}", kalman::get<0>(s));
else if constexpr(sizeof...(Qs) == 2)
fmt::format_to(to_value_buffer, "{{ {}, {} }}", kalman::get<0>(s), kalman::get<1>(s));
STD_FMT::format_to(to_value_buffer, "{{ {}, {} }}", kalman::get<0>(s), kalman::get<1>(s));
else
fmt::format_to(to_value_buffer, "{{ {}, {}, {} }}", kalman::get<0>(s), kalman::get<1>(s), kalman::get<2>(s));
STD_FMT::format_to(to_value_buffer, "{{ {}, {}, {} }}", kalman::get<0>(s), kalman::get<1>(s), kalman::get<2>(s));
}
basic_memory_buffer<char> global_format_buffer;
units::detail::global_format_specs<char> global_specs = { specs.fill, specs.align, specs.width };
std::string global_format_buffer;
units::detail::quantity_global_format_specs<char> global_specs = { specs.fill, specs.align, specs.width };
units::detail::format_global_buffer(std::back_inserter(global_format_buffer), global_specs);
return fmt::format_to(ctx.out(), fmt::runtime(std::string_view(global_format_buffer.data(), global_format_buffer.size())),
std::string_view(value_buffer.data(), value_buffer.size()));
return STD_FMT::format_to(ctx.out(), FMT_RUNTIME(global_format_buffer), value_buffer);
}
private:
detail::dynamic_format_specs<char> specs;
units::detail::dynamic_format_specs<char> specs;
};
template<typename Q>
struct fmt::formatter<kalman::estimation<Q>> {
struct STD_FMT::formatter<kalman::estimation<Q>> {
constexpr auto parse(format_parse_context& ctx)
{
detail::dynamic_specs_handler<format_parse_context> handler(specs, ctx);
return detail::parse_format_specs(ctx.begin(), ctx.end(), handler);
units::detail::dynamic_specs_handler handler(specs, ctx);
return units::detail::parse_format_specs(ctx.begin(), ctx.end(), handler);
}
template<typename FormatContext>
@@ -209,22 +208,21 @@ struct fmt::formatter<kalman::estimation<Q>> {
return t.relative();
}(kalman::get<0>(e.state));
memory_buffer value_buffer;
std::string value_buffer;
auto to_value_buffer = std::back_inserter(value_buffer);
if (specs.precision != -1) {
fmt::format_to(to_value_buffer, "{0:%.{2}Q} ± {1:%.{2}Q} {0:%q}", q, sqrt(e.uncertainty), specs.precision);
STD_FMT::format_to(to_value_buffer, "{0:%.{2}Q} ± {1:%.{2}Q} {0:%q}", q, sqrt(e.uncertainty), specs.precision);
}
else {
fmt::format_to(to_value_buffer, "{0:%Q} ± {1:%Q} {0:%q}", q, sqrt(e.uncertainty));
STD_FMT::format_to(to_value_buffer, "{0:%Q} ± {1:%Q} {0:%q}", q, sqrt(e.uncertainty));
}
basic_memory_buffer<char> global_format_buffer;
units::detail::global_format_specs<char> global_specs = { specs.fill, specs.align, specs.width };
std::string global_format_buffer;
units::detail::quantity_global_format_specs<char> global_specs = { specs.fill, specs.align, specs.width };
units::detail::format_global_buffer(std::back_inserter(global_format_buffer), global_specs);
return fmt::format_to(ctx.out(), fmt::runtime(std::string_view(global_format_buffer.data(), global_format_buffer.size())),
std::string_view(value_buffer.data(), value_buffer.size()));
return STD_FMT::format_to(ctx.out(), FMT_RUNTIME(global_format_buffer), value_buffer);
}
private:
detail::dynamic_format_specs<char> specs;
units::detail::dynamic_format_specs<char> specs;
};
@@ -25,6 +25,7 @@
#include <units/format.h>
#include <units/generic/dimensionless.h>
#include <array>
#include <iostream>
// Based on: https://www.kalmanfilter.net/alphabeta.html#ex1
@@ -32,13 +33,13 @@ using namespace units;
void print_header(const kalman::State auto& initial)
{
fmt::print("Initial: {}\n", initial);
fmt::print("{:>2} | {:>9} | {:>8} | {:>14} | {:>14}\n", "N", "Gain", "Measured", "Curr. Estimate", "Next Estimate");
std::cout << STD_FMT::format("Initial: {}\n", initial);
std::cout << STD_FMT::format("{:>2} | {:>9} | {:>8} | {:>14} | {:>14}\n", "N", "Gain", "Measured", "Curr. Estimate", "Next Estimate");
}
void print(auto iteration, Dimensionless auto gain, Quantity auto measured, const kalman::State auto& current, const kalman::State auto& next)
{
fmt::print("{:2} | {:9} | {:8} | {:14} | {:14}\n", iteration, gain, measured, current, next);
std::cout << STD_FMT::format("{:2} | {:9} | {:8} | {:14} | {:14}\n", iteration, gain, measured, current, next);
}
int main()
@@ -27,6 +27,7 @@
#include <units/format.h>
#include <units/generic/dimensionless.h>
#include <array>
#include <iostream>
// Based on: https://www.kalmanfilter.net/alphabeta.html#ex2
@@ -34,13 +35,13 @@ using namespace units;
void print_header(const kalman::State auto& initial)
{
fmt::print("Initial: {}\n", initial);
fmt::print("{:>2} | {:>8} | {:>23} | {:>23}\n", "N", "Measured", "Curr. Estimate", "Next Estimate");
std::cout << STD_FMT::format("Initial: {}\n", initial);
std::cout << STD_FMT::format("{:>2} | {:>8} | {:>23} | {:>23}\n", "N", "Measured", "Curr. Estimate", "Next Estimate");
}
void print(auto iteration, Quantity auto measured, const kalman::State auto& current, const kalman::State auto& next)
{
fmt::print("{:2} | {:8} | {:.1} | {:.1}\n", iteration, measured, current, next);
std::cout << STD_FMT::format("{:2} | {:8} | {:.1} | {:.1}\n", iteration, measured, current, next);
}
int main()
@@ -27,6 +27,7 @@
#include <units/format.h>
#include <units/generic/dimensionless.h>
#include <array>
#include <iostream>
// Based on: https://www.kalmanfilter.net/alphabeta.html#ex3
@@ -34,13 +35,13 @@ using namespace units;
void print_header(const kalman::State auto& initial)
{
fmt::print("Initial: {}\n", initial);
fmt::print("{:>2} | {:>8} | {:>24} | {:>24}\n", "N", "Measured", "Curr. Estimate", "Next Estimate");
std::cout << STD_FMT::format("Initial: {}\n", initial);
std::cout << STD_FMT::format("{:>2} | {:>8} | {:>24} | {:>24}\n", "N", "Measured", "Curr. Estimate", "Next Estimate");
}
void print(auto iteration, Quantity auto measured, const kalman::State auto& current, const kalman::State auto& next)
{
fmt::print("{:2} | {:8} | {:>24.1} | {:>24.1}\n", iteration, measured, current, next);
std::cout << STD_FMT::format("{:2} | {:8} | {:>24.1} | {:>24.1}\n", iteration, measured, current, next);
}
int main()
@@ -28,6 +28,7 @@
#include <units/format.h>
#include <units/generic/dimensionless.h>
#include <array>
#include <iostream>
// Based on: https://www.kalmanfilter.net/alphabeta.html#ex4
@@ -35,13 +36,13 @@ using namespace units;
void print_header(const kalman::State auto& initial)
{
fmt::print("Initial: {}\n", initial);
fmt::print("{:>2} | {:>8} | {:>35} | {:>35}\n", "N", "Measured", "Curr. Estimate", "Next Estimate");
std::cout << STD_FMT::format("Initial: {}\n", initial);
std::cout << STD_FMT::format("{:>2} | {:>8} | {:>35} | {:>35}\n", "N", "Measured", "Curr. Estimate", "Next Estimate");
}
void print(auto iteration, Quantity auto measured, const kalman::State auto& current, const kalman::State auto& next)
{
fmt::print("{:2} | {:8} | {:>35.1} | {:>35.1}\n", iteration, measured, current, next);
std::cout << STD_FMT::format("{:2} | {:8} | {:>35.1} | {:>35.1}\n", iteration, measured, current, next);
}
int main()
@@ -25,6 +25,7 @@
#include <units/format.h>
#include <units/math.h>
#include <array>
#include <iostream>
// Based on: https://www.kalmanfilter.net/kalman1d.html#ex5
@@ -33,14 +34,14 @@ using namespace units;
template<Quantity Q>
void print_header(kalman::estimation<Q> initial)
{
fmt::print("Initial: {}\n", initial);
fmt::print("{:>2} | {:>5} | {:>8} | {:>16} | {:>16}\n", "N", "Gain", "Measured", "Curr. Estimate", "Next Estimate");
std::cout << STD_FMT::format("Initial: {}\n", initial);
std::cout << STD_FMT::format("{:>2} | {:>5} | {:>8} | {:>16} | {:>16}\n", "N", "Gain", "Measured", "Curr. Estimate", "Next Estimate");
}
template<Quantity Q, Dimensionless K>
void print(auto iteration, K gain, Q measured, kalman::estimation<Q> current, kalman::estimation<Q> next)
{
fmt::print("{:2} | {:5%.2Q} | {:8} | {:>16.2} | {:>16.2}\n", iteration, gain, measured, current, next);
std::cout << STD_FMT::format("{:2} | {:5%.2Q} | {:8} | {:>16.2} | {:>16.2}\n", iteration, gain, measured, current, next);
}
int main()
@@ -27,6 +27,7 @@
#include <units/quantity_point.h>
#include <units/unit.h>
#include <array>
#include <iostream>
// TODO Fix when Celsius is properly supported (#232)
namespace units::isq::si {
@@ -54,14 +55,14 @@ using namespace units;
template<QuantityPoint QP>
void print_header(kalman::estimation<QP> initial)
{
fmt::print("Initial: {}\n", initial);
fmt::print("{:>2} | {:>7} | {:>10} | {:>18} | {:>18}\n", "N", "Gain", "Measured", "Curr. Estimate", "Next Estimate");
std::cout << STD_FMT::format("Initial: {}\n", initial);
std::cout << STD_FMT::format("{:>2} | {:>7} | {:>10} | {:>18} | {:>18}\n", "N", "Gain", "Measured", "Curr. Estimate", "Next Estimate");
}
template<QuantityPoint QP, Dimensionless K>
void print(auto iteration, K gain, QP measured, kalman::estimation<QP> current, kalman::estimation<QP> next)
{
fmt::print("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18.3} | {:>18.3}\n", iteration, gain, measured.relative(), current, next);
std::cout << STD_FMT::format("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18.3} | {:>18.3}\n", iteration, gain, measured.relative(), current, next);
}
int main()
@@ -27,6 +27,7 @@
#include <units/quantity_point.h>
#include <units/unit.h>
#include <array>
#include <iostream>
// TODO Fix when Celsius is properly supported (#232)
namespace units::isq::si {
@@ -54,14 +55,14 @@ using namespace units;
template<QuantityPoint QP>
void print_header(kalman::estimation<QP> initial)
{
fmt::print("Initial: {}\n", initial);
fmt::print("{:>2} | {:>7} | {:>10} | {:>18} | {:>18}\n", "N", "Gain", "Measured", "Curr. Estimate", "Next Estimate");
std::cout << STD_FMT::format("Initial: {}\n", initial);
std::cout << STD_FMT::format("{:>2} | {:>7} | {:>10} | {:>18} | {:>18}\n", "N", "Gain", "Measured", "Curr. Estimate", "Next Estimate");
}
template<QuantityPoint QP, Dimensionless K>
void print(auto iteration, K gain, QP measured, kalman::estimation<QP> current, kalman::estimation<QP> next)
{
fmt::print("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18.3} | {:>18.3}\n", iteration, gain, measured.relative(), current, next);
std::cout << STD_FMT::format("{:2} | {:7%.4Q} | {:10%.3Q %q} | {:>18.3} | {:>18.3}\n", iteration, gain, measured.relative(), current, next);
}
int main()
@@ -27,6 +27,7 @@
#include <units/quantity_point.h>
#include <units/unit.h>
#include <array>
#include <iostream>
// TODO Fix when Celsius is properly supported (#232)
namespace units::isq::si {
@@ -54,14 +55,14 @@ using namespace units;
template<QuantityPoint QP>
void print_header(kalman::estimation<QP> initial)
{
fmt::print("Initial: {}\n", initial);
fmt::print("{:>2} | {:>7} | {:>10} | {:>16} | {:>16}\n", "N", "Gain", "Measured", "Curr. Estimate", "Next Estimate");
std::cout << STD_FMT::format("Initial: {}\n", initial);
std::cout << STD_FMT::format("{:>2} | {:>7} | {:>10} | {:>16} | {:>16}\n", "N", "Gain", "Measured", "Curr. Estimate", "Next Estimate");
}
template<QuantityPoint QP, Dimensionless K>
void print(auto iteration, K gain, QP measured, kalman::estimation<QP> current, kalman::estimation<QP> next)
{
fmt::print("{:2} | {:7%.3Q} | {:10%.3Q %q} | {:>16.2} | {:>16.2}\n", iteration, gain, measured.relative(), current, next);
std::cout << STD_FMT::format("{:2} | {:7%.3Q} | {:10%.3Q %q} | {:>16.2} | {:>16.2}\n", iteration, gain, measured.relative(), current, next);
}
int main()
+5 -5
View File
@@ -105,9 +105,9 @@ int main()
const Time auto fill_time_left = (height / fill_level - 1) * fill_time;
std::cout << "mp-units box example...\n";
std::cout << fmt::format("fill height at {} = {} ({} full)\n", fill_time, fill_level, fill_percent);
std::cout << fmt::format("spare_capacity at {} = {}\n", fill_time, spare_capacity);
std::cout << fmt::format("input flow rate after {} = {}\n", fill_time, input_flow_rate);
std::cout << fmt::format("float rise rate = {}\n", float_rise_rate);
std::cout << fmt::format("box full E.T.A. at current flow rate = {}\n", fill_time_left);
std::cout << STD_FMT::format("fill height at {} = {} ({} full)\n", fill_time, fill_level, fill_percent);
std::cout << STD_FMT::format("spare_capacity at {} = {}\n", fill_time, spare_capacity);
std::cout << STD_FMT::format("input flow rate after {} = {}\n", fill_time, input_flow_rate);
std::cout << STD_FMT::format("float rise rate = {}\n", float_rise_rate);
std::cout << STD_FMT::format("box full E.T.A. at current flow rate = {}\n", fill_time_left);
}
+4 -4
View File
@@ -114,7 +114,7 @@ void calcs_comparison()
const length<femtometre, float> L1A = 2._q_fm;
const length<femtometre, float> L2A = 3._q_fm;
const length<femtometre, float> LrA = L1A + L2A;
fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, LrA);
std::cout << STD_FMT::format("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, LrA);
std::cout << "The single unit method must convert large\n"
"or small values in other units to the base unit.\n"
@@ -123,17 +123,17 @@ void calcs_comparison()
const length<metre, float> L1B = L1A;
const length<metre, float> L2B = L2A;
const length<metre, float> LrB = L1B + L2B;
fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, LrB);
std::cout << STD_FMT::format("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, LrB);
std::cout << "In multiplication and division:\n\n";
const area<square_femtometre, float> ArA = L1A * L2A;
fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, ArA);
std::cout << STD_FMT::format("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, ArA);
std::cout << "similar problems arise\n\n";
const area<square_metre, float> ArB = L1B * L2B;
fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, ArB);
std::cout << STD_FMT::format("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, ArB);
}
} // namespace
+12 -12
View File
@@ -56,7 +56,7 @@ struct Ship {
template<class ...Args, units::Quantity Q>
auto fmt_line(const Q a)
{
return fmt::format("{:22}", a) + (fmt::format(",{:20}", units::quantity_cast<Args>(a)) + ...);
return STD_FMT::format("{:22}", a) + (STD_FMT::format(",{:20}", units::quantity_cast<Args>(a)) + ...);
}
// Print the ship details in the units as defined in the Ship struct, in other si::imperial units, and in SI
@@ -64,17 +64,17 @@ void print_details(std::string_view description, const Ship& ship)
{
using namespace units::isq::si::fps::literals;
const auto waterDensity = 62.4_q_lb_per_ft3;
std::cout << fmt::format("{}\n", description);
std::cout << fmt::format("{:20} : {}\n", "length", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.length))
<< fmt::format("{:20} : {}\n", "draft", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.draft))
<< fmt::format("{:20} : {}\n", "beam", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.beam))
<< fmt::format("{:20} : {}\n", "mass", fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::tonne>>(ship.mass))
<< fmt::format("{:20} : {}\n", "speed", fmt_line<si::fps::speed<si::fps::knot>, si::speed<si::kilometre_per_hour>>(ship.speed))
<< fmt::format("{:20} : {}\n", "power", fmt_line<si::fps::power<si::fps::horse_power>, si::power<si::kilowatt>>(ship.power))
<< fmt::format("{:20} : {}\n", "main guns", fmt_line<si::fps::length<si::fps::inch>, si::length<si::millimetre>>(ship.mainGuns))
<< fmt::format("{:20} : {}\n", "fire shells weighing",fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::kilogram>>(ship.shellMass))
<< fmt::format("{:20} : {}\n", "fire shells at",fmt_line<si::fps::speed<si::fps::mile_per_hour>, si::speed<si::kilometre_per_hour>>(ship.shellSpeed))
<< fmt::format("{:20} : {}\n", "volume underwater", fmt_line<si::volume<si::cubic_metre>, si::volume<si::litre>>(ship.mass / waterDensity));
std::cout << STD_FMT::format("{}\n", description);
std::cout << STD_FMT::format("{:20} : {}\n", "length", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.length))
<< STD_FMT::format("{:20} : {}\n", "draft", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.draft))
<< STD_FMT::format("{:20} : {}\n", "beam", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.beam))
<< STD_FMT::format("{:20} : {}\n", "mass", fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::tonne>>(ship.mass))
<< STD_FMT::format("{:20} : {}\n", "speed", fmt_line<si::fps::speed<si::fps::knot>, si::speed<si::kilometre_per_hour>>(ship.speed))
<< STD_FMT::format("{:20} : {}\n", "power", fmt_line<si::fps::power<si::fps::horse_power>, si::power<si::kilowatt>>(ship.power))
<< STD_FMT::format("{:20} : {}\n", "main guns", fmt_line<si::fps::length<si::fps::inch>, si::length<si::millimetre>>(ship.mainGuns))
<< STD_FMT::format("{:20} : {}\n", "fire shells weighing",fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::kilogram>>(ship.shellMass))
<< STD_FMT::format("{:20} : {}\n", "fire shells at",fmt_line<si::fps::speed<si::fps::mile_per_hour>, si::speed<si::kilometre_per_hour>>(ship.shellSpeed))
<< STD_FMT::format("{:20} : {}\n", "volume underwater", fmt_line<si::volume<si::cubic_metre>, si::volume<si::litre>>(ship.mass / waterDensity));
}
int main()
+11 -16
View File
@@ -21,16 +21,11 @@
// SOFTWARE.
#include "glide_computer.h"
#include <units/bits/external/hacks.h>
#include <units/bits/fmt_hacks.h>
#include <units/chrono.h>
#include <units/generic/dimensionless.h>
#include <units/isq/si/international/length.h>
UNITS_DIAGNOSTIC_PUSH
UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE
#include <fmt/format.h>
UNITS_DIAGNOSTIC_POP
#include <array>
#include <exception>
#include <iostream>
@@ -90,7 +85,7 @@ void print(const R& gliders)
std::cout << "- Name: " << g.name << "\n";
std::cout << "- Polar:\n";
for (const auto& p : g.polar)
fmt::print(" * {:%.4Q %q} @ {:%.1Q %q} -> {:%.1Q %q}\n", p.climb, p.v, units::quantity_cast<units::one>(glide_ratio(g.polar[0])));
std::cout << STD_FMT::format(" * {:%.4Q %q} @ {:%.1Q %q} -> {:%.1Q %q}\n", p.climb, p.v, units::quantity_cast<units::one>(glide_ratio(g.polar[0])));
std::cout << "\n";
}
}
@@ -104,8 +99,8 @@ void print(const R& conditions)
for (const auto& c : conditions) {
std::cout << "- " << c.first << "\n";
const auto& w = c.second;
std::cout << " * Cloud base: " << fmt::format("{:%.0Q %q}", w.cloud_base) << " AGL\n";
std::cout << " * Thermals strength: " << fmt::format("{:%.1Q %q}", w.thermal_strength) << "\n";
std::cout << " * Cloud base: " << STD_FMT::format("{:%.0Q %q}", w.cloud_base) << " AGL\n";
std::cout << " * Thermals strength: " << STD_FMT::format("{:%.1Q %q}", w.thermal_strength) << "\n";
std::cout << "\n";
}
}
@@ -117,7 +112,7 @@ void print(const R& waypoints)
std::cout << "Waypoints:\n";
std::cout << "==========\n";
for (const auto& w : waypoints)
std::cout << fmt::format("- {}: {} {}, {:%.1Q %q}\n", w.name, w.pos.lat, w.pos.lon, w.alt);
std::cout << STD_FMT::format("- {}: {} {}, {:%.1Q %q}\n", w.name, w.pos.lat, w.pos.lon, w.alt);
std::cout << "\n";
}
@@ -128,11 +123,11 @@ void print(const task& t)
std::cout << "- Start: " << t.get_start().name << "\n";
std::cout << "- Finish: " << t.get_finish().name << "\n";
std::cout << "- Length: " << fmt::format("{:%.1Q %q}", t.get_length()) << "\n";
std::cout << "- Length: " << STD_FMT::format("{:%.1Q %q}", t.get_length()) << "\n";
std::cout << "- Legs: " << "\n";
for (const auto& l : t.get_legs())
std::cout << fmt::format(" * {} -> {} ({:%.1Q %q})\n", l.begin().name, l.end().name, l.get_length());
std::cout << STD_FMT::format(" * {} -> {} ({:%.1Q %q})\n", l.begin().name, l.end().name, l.get_length());
std::cout << "\n";
}
@@ -140,7 +135,7 @@ void print(const safety& s)
{
std::cout << "Safety:\n";
std::cout << "=======\n";
std::cout << "- Min AGL separation: " << fmt::format("{:%.0Q %q}", s.min_agl_height) << "\n";
std::cout << "- Min AGL separation: " << STD_FMT::format("{:%.0Q %q}", s.min_agl_height) << "\n";
std::cout << "\n";
}
@@ -149,8 +144,8 @@ void print(const aircraft_tow& tow)
std::cout << "Tow:\n";
std::cout << "====\n";
std::cout << "- Type: aircraft\n";
std::cout << "- Height: " << fmt::format("{:%.0Q %q}", tow.height_agl) << "\n";
std::cout << "- Performance: " << fmt::format("{:%.1Q %q}", tow.performance) << "\n";
std::cout << "- Height: " << STD_FMT::format("{:%.0Q %q}", tow.height_agl) << "\n";
std::cout << "- Performance: " << STD_FMT::format("{:%.1Q %q}", tow.performance) << "\n";
std::cout << "\n";
}
@@ -179,7 +174,7 @@ void example()
for (const auto& c : weather_conditions) {
std::string txt = "Scenario: Glider = " + g.name + ", Weather = " + c.first;
std::cout << txt << "\n";
fmt::print("{0:=^{1}}\n\n", "", txt.size());
std::cout << STD_FMT::format("{0:=^{1}}\n\n", "", txt.size());
estimate(start_time, g, c.second, t, sfty, tow);
+2 -2
View File
@@ -36,7 +36,7 @@ std::ostream& operator<<(std::ostream& os, const vector<ET, OT>& v)
{
os << "|";
for (auto i = 0U; i < v.size(); ++i) {
os << fmt::format(" {:>9}", v(i));
os << STD_FMT::format(" {:>9}", v(i));
}
os << " |";
return os;
@@ -48,7 +48,7 @@ std::ostream& operator<<(std::ostream& os, const matrix<ET, OT>& v)
for (auto i = 0U; i < v.rows(); ++i) {
os << "|";
for (auto j = 0U; j < v.columns(); ++j) {
os << fmt::format(" {:>9}", v(i, j));
os << STD_FMT::format(" {:>9}", v(i, j));
}
os << (i != v.rows() - 1U ? " |\n" : " |");
}
+5 -5
View File
@@ -101,9 +101,9 @@ int main()
const Time auto fill_time_left = (height / fill_level - 1) * fill_time;
std::cout << "mp-units box example...\n";
std::cout << fmt::format("fill height at {} = {} ({} full)\n", fill_time, fill_level, fill_percent);
std::cout << fmt::format("spare_capacity at {} = {}\n", fill_time, spare_capacity);
std::cout << fmt::format("input flow rate after {} = {}\n", fill_time, input_flow_rate);
std::cout << fmt::format("float rise rate = {}\n", float_rise_rate);
std::cout << fmt::format("box full E.T.A. at current flow rate = {}\n", fill_time_left);
std::cout << STD_FMT::format("fill height at {} = {} ({} full)\n", fill_time, fill_level, fill_percent);
std::cout << STD_FMT::format("spare_capacity at {} = {}\n", fill_time, spare_capacity);
std::cout << STD_FMT::format("input flow rate after {} = {}\n", fill_time, input_flow_rate);
std::cout << STD_FMT::format("float rise rate = {}\n", float_rise_rate);
std::cout << STD_FMT::format("box full E.T.A. at current flow rate = {}\n", fill_time_left);
}
+4 -4
View File
@@ -123,7 +123,7 @@ void calcs_comparison()
const length<femtometre, float> L1A = 2.f * fm;
const length<femtometre, float> L2A = 3.f * fm;
const length<femtometre, float> LrA = L1A + L2A;
fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, LrA);
std::cout << STD_FMT::format("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, LrA);
std::cout << "The single unit method must convert large\n"
"or small values in other units to the base unit.\n"
@@ -132,17 +132,17 @@ void calcs_comparison()
const length<metre, float> L1B = L1A;
const length<metre, float> L2B = L2A;
const length<metre, float> LrB = L1B + L2B;
fmt::print("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, LrB);
std::cout << STD_FMT::format("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, LrB);
std::cout << "In multiplication and division:\n\n";
const area<square_femtometre, float> ArA = L1A * L2A;
fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, ArA);
std::cout << STD_FMT::format("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, ArA);
std::cout << "similar problems arise\n\n";
const area<square_metre, float> ArB = L1B * L2B;
fmt::print("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, ArB);
std::cout << STD_FMT::format("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1B, L2B, ArB);
}
} // namespace
+12 -12
View File
@@ -59,7 +59,7 @@ struct Ship {
template<class ...Args, units::Quantity Q>
auto fmt_line(const Q a)
{
return fmt::format("{:22}", a) + (fmt::format(",{:20}", units::quantity_cast<Args>(a)) + ...);
return STD_FMT::format("{:22}", a) + (STD_FMT::format(",{:20}", units::quantity_cast<Args>(a)) + ...);
}
// Print the ship details in the units as defined in the Ship struct, in other si::imperial units, and in SI
@@ -67,17 +67,17 @@ void print_details(std::string_view description, const Ship& ship)
{
using namespace units::isq::si::fps::references;
const auto waterDensity = 62.4 * (lb / ft3);
std::cout << fmt::format("{}\n", description);
std::cout << fmt::format("{:20} : {}\n", "length", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.length))
<< fmt::format("{:20} : {}\n", "draft", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.draft))
<< fmt::format("{:20} : {}\n", "beam", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.beam))
<< fmt::format("{:20} : {}\n", "mass", fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::tonne>>(ship.mass))
<< fmt::format("{:20} : {}\n", "speed", fmt_line<si::fps::speed<si::fps::knot>, si::speed<si::kilometre_per_hour>>(ship.speed))
<< fmt::format("{:20} : {}\n", "power", fmt_line<si::fps::power<si::fps::horse_power>, si::power<si::kilowatt>>(ship.power))
<< fmt::format("{:20} : {}\n", "main guns", fmt_line<si::fps::length<si::fps::inch>, si::length<si::millimetre>>(ship.mainGuns))
<< fmt::format("{:20} : {}\n", "fire shells weighing", fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::kilogram>>(ship.shellMass))
<< fmt::format("{:20} : {}\n", "fire shells at", fmt_line<si::fps::speed<si::fps::mile_per_hour>, si::speed<si::kilometre_per_hour>>(ship.shellSpeed))
<< fmt::format("{:20} : {}\n", "volume underwater", fmt_line<si::volume<si::cubic_metre>, si::volume<si::litre>>(ship.mass / waterDensity));
std::cout << STD_FMT::format("{}\n", description);
std::cout << STD_FMT::format("{:20} : {}\n", "length", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.length))
<< STD_FMT::format("{:20} : {}\n", "draft", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.draft))
<< STD_FMT::format("{:20} : {}\n", "beam", fmt_line<si::fps::length<si::fps::yard>, si::length<si::metre>>(ship.beam))
<< STD_FMT::format("{:20} : {}\n", "mass", fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::tonne>>(ship.mass))
<< STD_FMT::format("{:20} : {}\n", "speed", fmt_line<si::fps::speed<si::fps::knot>, si::speed<si::kilometre_per_hour>>(ship.speed))
<< STD_FMT::format("{:20} : {}\n", "power", fmt_line<si::fps::power<si::fps::horse_power>, si::power<si::kilowatt>>(ship.power))
<< STD_FMT::format("{:20} : {}\n", "main guns", fmt_line<si::fps::length<si::fps::inch>, si::length<si::millimetre>>(ship.mainGuns))
<< STD_FMT::format("{:20} : {}\n", "fire shells weighing", fmt_line<si::fps::mass<si::fps::long_ton>, si::mass<si::kilogram>>(ship.shellMass))
<< STD_FMT::format("{:20} : {}\n", "fire shells at", fmt_line<si::fps::speed<si::fps::mile_per_hour>, si::speed<si::kilometre_per_hour>>(ship.shellSpeed))
<< STD_FMT::format("{:20} : {}\n", "volume underwater", fmt_line<si::volume<si::cubic_metre>, si::volume<si::litre>>(ship.mass / waterDensity));
}
int main()
+11 -16
View File
@@ -21,16 +21,11 @@
// SOFTWARE.
#include "glide_computer.h"
#include <units/bits/external/hacks.h>
#include <units/bits/fmt_hacks.h>
#include <units/chrono.h>
#include <units/generic/dimensionless.h>
#include <units/isq/si/international/length.h>
UNITS_DIAGNOSTIC_PUSH
UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE
#include <fmt/format.h>
UNITS_DIAGNOSTIC_POP
#include <array>
#include <exception>
#include <iostream>
@@ -90,7 +85,7 @@ void print(const R& gliders)
std::cout << "- Name: " << g.name << "\n";
std::cout << "- Polar:\n";
for (const auto& p : g.polar)
fmt::print(" * {:%.4Q %q} @ {:%.1Q %q} -> {:%.1Q %q}\n", p.climb, p.v, units::quantity_cast<units::one>(glide_ratio(g.polar[0])));
std::cout << STD_FMT::format(" * {:%.4Q %q} @ {:%.1Q %q} -> {:%.1Q %q}\n", p.climb, p.v, units::quantity_cast<units::one>(glide_ratio(g.polar[0])));
std::cout << "\n";
}
}
@@ -104,8 +99,8 @@ void print(const R& conditions)
for (const auto& c : conditions) {
std::cout << "- " << c.first << "\n";
const auto& w = c.second;
std::cout << " * Cloud base: " << fmt::format("{:%.0Q %q}", w.cloud_base) << " AGL\n";
std::cout << " * Thermals strength: " << fmt::format("{:%.1Q %q}", w.thermal_strength) << "\n";
std::cout << " * Cloud base: " << STD_FMT::format("{:%.0Q %q}", w.cloud_base) << " AGL\n";
std::cout << " * Thermals strength: " << STD_FMT::format("{:%.1Q %q}", w.thermal_strength) << "\n";
std::cout << "\n";
}
}
@@ -117,7 +112,7 @@ void print(const R& waypoints)
std::cout << "Waypoints:\n";
std::cout << "==========\n";
for (const auto& w : waypoints)
std::cout << fmt::format("- {}: {} {}, {:%.1Q %q}\n", w.name, w.pos.lat, w.pos.lon, w.alt);
std::cout << STD_FMT::format("- {}: {} {}, {:%.1Q %q}\n", w.name, w.pos.lat, w.pos.lon, w.alt);
std::cout << "\n";
}
@@ -128,11 +123,11 @@ void print(const task& t)
std::cout << "- Start: " << t.get_start().name << "\n";
std::cout << "- Finish: " << t.get_finish().name << "\n";
std::cout << "- Length: " << fmt::format("{:%.1Q %q}", t.get_length()) << "\n";
std::cout << "- Length: " << STD_FMT::format("{:%.1Q %q}", t.get_length()) << "\n";
std::cout << "- Legs: " << "\n";
for (const auto& l : t.get_legs())
std::cout << fmt::format(" * {} -> {} ({:%.1Q %q})\n", l.begin().name, l.end().name, l.get_length());
std::cout << STD_FMT::format(" * {} -> {} ({:%.1Q %q})\n", l.begin().name, l.end().name, l.get_length());
std::cout << "\n";
}
@@ -140,7 +135,7 @@ void print(const safety& s)
{
std::cout << "Safety:\n";
std::cout << "=======\n";
std::cout << "- Min AGL separation: " << fmt::format("{:%.0Q %q}", s.min_agl_height) << "\n";
std::cout << "- Min AGL separation: " << STD_FMT::format("{:%.0Q %q}", s.min_agl_height) << "\n";
std::cout << "\n";
}
@@ -149,8 +144,8 @@ void print(const aircraft_tow& tow)
std::cout << "Tow:\n";
std::cout << "====\n";
std::cout << "- Type: aircraft\n";
std::cout << "- Height: " << fmt::format("{:%.0Q %q}", tow.height_agl) << "\n";
std::cout << "- Performance: " << fmt::format("{:%.1Q %q}", tow.performance) << "\n";
std::cout << "- Height: " << STD_FMT::format("{:%.0Q %q}", tow.height_agl) << "\n";
std::cout << "- Performance: " << STD_FMT::format("{:%.1Q %q}", tow.performance) << "\n";
std::cout << "\n";
}
@@ -179,7 +174,7 @@ void example()
for (const auto& c : weather_conditions) {
std::string txt = "Scenario: Glider = " + g.name + ", Weather = " + c.first;
std::cout << txt << "\n";
fmt::print("{0:=^{1}}\n\n", "", txt.size());
std::cout << STD_FMT::format("{0:=^{1}}\n\n", "", txt.size());
estimate(start_time, g, c.second, t, sfty, tow);
+2 -2
View File
@@ -36,7 +36,7 @@ std::ostream& operator<<(std::ostream& os, const vector<ET, OT>& v)
{
os << "|";
for (auto i = 0U; i < v.size(); ++i) {
os << fmt::format(" {:>9}", v(i));
os << STD_FMT::format(" {:>9}", v(i));
}
os << " |";
return os;
@@ -48,7 +48,7 @@ std::ostream& operator<<(std::ostream& os, const matrix<ET, OT>& v)
for (auto i = 0U; i < v.rows(); ++i) {
os << "|";
for (auto j = 0U; j < v.columns(); ++j) {
os << fmt::format(" {:>9}", v(i, j));
os << STD_FMT::format(" {:>9}", v(i, j));
}
os << (i != v.rows() - 1U ? " |\n" : " |");
}