mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[wpimath] Disable iostream support for units and enable fmtlib (#3481)
Supersedes #2497.
This commit is contained in:
@@ -9,22 +9,8 @@
|
||||
#include "units/base.h"
|
||||
|
||||
template <class Units, typename T, template <typename> class NonLinearScale>
|
||||
struct fmt::formatter<units::unit_t<Units, T, NonLinearScale>> {
|
||||
char presentation = 'f';
|
||||
|
||||
constexpr auto parse(fmt::format_parse_context& ctx) {
|
||||
auto it = ctx.begin(), end = ctx.end();
|
||||
if (it != end && (*it == 'f' || *it == 'e')) {
|
||||
presentation = *it++;
|
||||
}
|
||||
|
||||
if (it != end && *it != '}') {
|
||||
throw fmt::format_error("invalid format");
|
||||
}
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
struct fmt::formatter<units::unit_t<Units, T, NonLinearScale>>
|
||||
: fmt::formatter<double> {
|
||||
template <typename FormatContext>
|
||||
auto format(const units::unit_t<Units, T, NonLinearScale>& obj,
|
||||
FormatContext& ctx) {
|
||||
@@ -34,7 +20,8 @@ struct fmt::formatter<units::unit_t<Units, T, NonLinearScale>> {
|
||||
|
||||
auto out = ctx.out();
|
||||
|
||||
out = fmt::format_to(out, "{}", units::convert<Units, BaseUnits>(obj()));
|
||||
out = fmt::formatter<double>::format(
|
||||
units::convert<Units, BaseUnits>(obj()), ctx);
|
||||
|
||||
if constexpr (units::traits::unit_traits<
|
||||
Units>::base_unit_type::meter_ratio::num != 0) {
|
||||
|
||||
@@ -74,36 +74,40 @@
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
#if !defined(UNIT_LIB_DISABLE_IOSTREAM)
|
||||
#if defined(UNIT_LIB_ENABLE_IOSTREAM)
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <locale>
|
||||
|
||||
//------------------------------
|
||||
// STRING FORMATTER
|
||||
//------------------------------
|
||||
|
||||
namespace units
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template <typename T> std::string to_string(const T& t)
|
||||
{
|
||||
std::string str{ std::to_string(t) };
|
||||
int offset{ 1 };
|
||||
|
||||
// remove trailing decimal points for integer value units. Locale aware!
|
||||
struct lconv * lc;
|
||||
lc = localeconv();
|
||||
char decimalPoint = *lc->decimal_point;
|
||||
if (str.find_last_not_of('0') == str.find(decimalPoint)) { offset = 0; }
|
||||
str.erase(str.find_last_not_of('0') + offset, std::string::npos);
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
#include <string>
|
||||
#else
|
||||
#include <locale>
|
||||
#include <string>
|
||||
#include <fmt/format.h>
|
||||
#endif
|
||||
|
||||
//------------------------------
|
||||
// STRING FORMATTER
|
||||
//------------------------------
|
||||
|
||||
namespace units
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template <typename T> std::string to_string(const T& t)
|
||||
{
|
||||
std::string str{ std::to_string(t) };
|
||||
int offset{ 1 };
|
||||
|
||||
// remove trailing decimal points for integer value units. Locale aware!
|
||||
struct lconv * lc;
|
||||
lc = localeconv();
|
||||
char decimalPoint = *lc->decimal_point;
|
||||
if (str.find_last_not_of('0') == str.find(decimalPoint)) { offset = 0; }
|
||||
str.erase(str.find_last_not_of('0') + offset, std::string::npos);
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace units
|
||||
{
|
||||
template<typename T> inline constexpr const char* name(const T&);
|
||||
@@ -172,10 +176,33 @@ namespace units
|
||||
* @param namespaceName namespace in which the new units will be encapsulated.
|
||||
* @param nameSingular singular version of the unit name, e.g. 'meter'
|
||||
* @param abbrev - abbreviated unit name, e.g. 'm'
|
||||
* @note When UNIT_LIB_DISABLE_IOSTREAM is defined, the macro does not generate any code
|
||||
* @note When UNIT_LIB_ENABLE_IOSTREAM isn't defined, the macro does not generate any code
|
||||
*/
|
||||
#if defined(UNIT_LIB_DISABLE_IOSTREAM)
|
||||
#define UNIT_ADD_IO(namespaceName, nameSingular, abbrev)
|
||||
#if !defined(UNIT_LIB_ENABLE_IOSTREAM)
|
||||
#define UNIT_ADD_IO(namespaceName, nameSingular, abbrev)\
|
||||
}\
|
||||
template <>\
|
||||
struct fmt::formatter<units::namespaceName::nameSingular ## _t> \
|
||||
: fmt::formatter<double> \
|
||||
{\
|
||||
template <typename FormatContext>\
|
||||
auto format(const units::namespaceName::nameSingular ## _t& obj,\
|
||||
FormatContext& ctx) -> decltype(ctx.out()) \
|
||||
{\
|
||||
auto out = ctx.out();\
|
||||
out = fmt::formatter<double>::format(obj(), ctx);\
|
||||
return fmt::format_to(out, " " #abbrev);\
|
||||
}\
|
||||
};\
|
||||
namespace units\
|
||||
{\
|
||||
namespace namespaceName\
|
||||
{\
|
||||
inline std::string to_string(const nameSingular ## _t& obj)\
|
||||
{\
|
||||
return units::detail::to_string(obj()) + std::string(" "#abbrev);\
|
||||
}\
|
||||
}
|
||||
#else
|
||||
#define UNIT_ADD_IO(namespaceName, nameSingular, abbrev)\
|
||||
namespace namespaceName\
|
||||
@@ -2180,7 +2207,7 @@ namespace units
|
||||
return UnitType(value);
|
||||
}
|
||||
|
||||
#if !defined(UNIT_LIB_DISABLE_IOSTREAM)
|
||||
#if defined(UNIT_LIB_ENABLE_IOSTREAM)
|
||||
template<class Units, typename T, template<typename> class NonLinearScale>
|
||||
inline std::ostream& operator<<(std::ostream& os, const unit_t<Units, T, NonLinearScale>& obj) noexcept
|
||||
{
|
||||
@@ -2815,11 +2842,31 @@ namespace units
|
||||
namespace dimensionless
|
||||
{
|
||||
typedef unit_t<scalar, UNIT_LIB_DEFAULT_TYPE, decibel_scale> dB_t;
|
||||
#if !defined(UNIT_LIB_DISABLE_IOSTREAM)
|
||||
#if defined(UNIT_LIB_ENABLE_IOSTREAM)
|
||||
inline std::ostream& operator<<(std::ostream& os, const dB_t& obj) { os << obj() << " dB"; return os; }
|
||||
#endif
|
||||
typedef dB_t dBi_t;
|
||||
}
|
||||
#else
|
||||
}
|
||||
}
|
||||
template <>
|
||||
struct fmt::formatter<units::dimensionless::dB_t> : fmt::formatter<double>
|
||||
{
|
||||
template <typename FormatContext>
|
||||
auto format(const units::dimensionless::dB_t& obj,
|
||||
FormatContext& ctx) -> decltype(ctx.out())
|
||||
{
|
||||
auto out = ctx.out();
|
||||
out = fmt::formatter<double>::format(obj(), ctx);
|
||||
return fmt::format_to(out, " dB");
|
||||
}
|
||||
};
|
||||
|
||||
namespace units {
|
||||
namespace dimensionless {
|
||||
typedef dB_t dBi_t;
|
||||
}
|
||||
#endif
|
||||
|
||||
//------------------------------
|
||||
// DECIBEL ARITHMETIC
|
||||
@@ -3365,3 +3412,5 @@ namespace units
|
||||
namespace units::literals {}
|
||||
using namespace units::literals;
|
||||
#endif // UNIT_HAS_LITERAL_SUPPORT
|
||||
|
||||
#include "frc/fmt/Units.h"
|
||||
|
||||
Reference in New Issue
Block a user