refactor macros to start with UNITS_ and fix an MSVC issue with FMT_THROW

This commit is contained in:
Yves Delley committed 2022-12-30 17:22:04 +01:00
1 parent c346ee123a
commit b6da63b2f0
36 files changed
+599 -589

No files matched your search

+21 -21
View File
@@ -55,7 +55,7 @@ public:
constexpr fill_t& operator=(std::basic_string_view<Char> str)
{
auto size = str.size();
if (size > max_size) FMT_THROW(STD_FMT::format_error("invalid fill"));
if (size > max_size) UNITS_THROW(UNITS_STD_FMT::format_error("invalid fill"));
for (size_t i = 0; i < size; ++i) data_[i] = str[i];
size_ = static_cast<unsigned char>(size);
return *this;
@@ -98,11 +98,11 @@ struct width_checker {
{
if constexpr (is_integer<T>) {
if constexpr (std::numeric_limits<T>::is_signed) {
if (value < 0) FMT_THROW(STD_FMT::format_error("negative width"));
if (value < 0) UNITS_THROW(UNITS_STD_FMT::format_error("negative width"));
}
return static_cast<unsigned long long>(value);
} else {
FMT_THROW(STD_FMT::format_error("width is not integer"));
UNITS_THROW(UNITS_STD_FMT::format_error("width is not integer"));
}
}
};
@@ -113,11 +113,11 @@ struct precision_checker {
{
if constexpr (is_integer<T>) {
if constexpr (std::numeric_limits<T>::is_signed) {
if (value < 0) FMT_THROW(STD_FMT::format_error("negative precision"));
if (value < 0) UNITS_THROW(UNITS_STD_FMT::format_error("negative precision"));
}
return static_cast<unsigned long long>(value);
} else {
FMT_THROW(STD_FMT::format_error("precision is not integer"));
UNITS_THROW(UNITS_STD_FMT::format_error("precision is not integer"));
}
}
};
@@ -147,31 +147,31 @@ struct dynamic_format_specs : basic_format_specs<Char> {
[[nodiscard]] constexpr int verify_dynamic_arg_index_in_range(size_t idx)
{
if (idx > static_cast<size_t>(std::numeric_limits<int>::max())) {
FMT_THROW(STD_FMT::format_error("Dynamic width or precision index too large."));
UNITS_THROW(UNITS_STD_FMT::format_error("Dynamic width or precision index too large."));
}
return static_cast<int>(idx);
}
template<typename CharT>
[[nodiscard]] constexpr int on_dynamic_arg(size_t arg_id, STD_FMT::basic_format_parse_context<CharT>& context)
[[nodiscard]] constexpr int on_dynamic_arg(size_t arg_id, UNITS_STD_FMT::basic_format_parse_context<CharT>& context)
{
context.check_arg_id(FMT_TO_ARG_ID(arg_id));
context.check_arg_id(UNITS_FMT_TO_ARG_ID(arg_id));
return verify_dynamic_arg_index_in_range(arg_id);
}
template<typename CharT>
[[nodiscard]] constexpr int on_dynamic_arg(auto_id, STD_FMT::basic_format_parse_context<CharT>& context)
[[nodiscard]] constexpr int on_dynamic_arg(auto_id, UNITS_STD_FMT::basic_format_parse_context<CharT>& context)
{
return verify_dynamic_arg_index_in_range(FMT_FROM_ARG_ID(context.next_arg_id()));
return verify_dynamic_arg_index_in_range(UNITS_FMT_FROM_ARG_ID(context.next_arg_id()));
}
template<class Handler, typename FormatContext>
[[nodiscard]] constexpr int get_dynamic_spec(int index, FormatContext& ctx)
{
const unsigned long long value =
STD_FMT::visit_format_arg(Handler{}, ctx.arg(FMT_TO_ARG_ID(static_cast<size_t>(index))));
UNITS_STD_FMT::visit_format_arg(Handler{}, ctx.arg(UNITS_FMT_TO_ARG_ID(static_cast<size_t>(index))));
if (value > static_cast<unsigned long long>(std::numeric_limits<int>::max())) {
FMT_THROW(STD_FMT::format_error("number is too big"));
UNITS_THROW(UNITS_STD_FMT::format_error("number is too big"));
}
return static_cast<int>(value);
}
@@ -195,7 +195,7 @@ template<std::input_iterator It, std::sentinel_for<It> S>
++begin;
} while (begin != end && '0' <= *begin && *begin <= '9');
if (value > max_int) FMT_THROW(STD_FMT::format_error("Number is too big"));
if (value > max_int) UNITS_THROW(UNITS_STD_FMT::format_error("Number is too big"));
return begin;
}
@@ -222,12 +222,12 @@ template<std::input_iterator It, std::sentinel_for<It> S, typename IDHandler>
else
++begin;
if (begin == end || (*begin != '}' && *begin != ':'))
FMT_THROW(STD_FMT::format_error("invalid format string"));
UNITS_THROW(UNITS_STD_FMT::format_error("invalid format string"));
else
handler(index);
return begin;
}
FMT_THROW(STD_FMT::format_error("invalid format string"));
UNITS_THROW(UNITS_STD_FMT::format_error("invalid format string"));
}
template<std::input_iterator It, std::sentinel_for<It> S, typename IDHandler>
@@ -278,11 +278,11 @@ template<std::input_iterator It, std::sentinel_for<It> S, typename Handler>
if (width != -1)
handler.on_width(width);
else
FMT_THROW(STD_FMT::format_error("number is too big"));
UNITS_THROW(UNITS_STD_FMT::format_error("number is too big"));
} else if (*begin == '{') {
++begin;
if (begin != end) begin = parse_arg_id(begin, end, width_adapter{handler});
if (begin == end || *begin != '}') FMT_THROW(STD_FMT::format_error("invalid format string"));
if (begin == end || *begin != '}') UNITS_THROW(UNITS_STD_FMT::format_error("invalid format string"));
++begin;
}
return begin;
@@ -305,13 +305,13 @@ template<std::input_iterator It, std::sentinel_for<It> S, typename Handler>
if (precision != -1)
handler.on_precision(precision);
else
FMT_THROW(STD_FMT::format_error("number is too big"));
UNITS_THROW(UNITS_STD_FMT::format_error("number is too big"));
} else if (c == '{') {
++begin;
if (begin != end) begin = parse_arg_id(begin, end, precision_adapter{handler});
if (begin == end || *begin++ != '}') FMT_THROW(STD_FMT::format_error("invalid format string"));
if (begin == end || *begin++ != '}') UNITS_THROW(UNITS_STD_FMT::format_error("invalid format string"));
} else {
FMT_THROW(STD_FMT::format_error("missing precision specifier"));
UNITS_THROW(UNITS_STD_FMT::format_error("missing precision specifier"));
}
return begin;
}
@@ -355,7 +355,7 @@ template<std::input_iterator It, std::sentinel_for<It> S, typename Handler>
if (align != fmt_align::none) {
if (p != begin) {
auto c = *begin;
if (c == '{') FMT_THROW(STD_FMT::format_error("invalid fill character '{'"));
if (c == '{') UNITS_THROW(UNITS_STD_FMT::format_error("invalid fill character '{'"));
handler.on_fill(std::basic_string_view<std::iter_value_t<It>>(&*begin, static_cast<size_t>(p - begin)));
begin = p + 1;
} else
+18 -9
View File
@@ -41,10 +41,19 @@ UNITS_DIAGNOSTIC_IGNORE_SHADOW
#include <fmt/format.h>
UNITS_DIAGNOSTIC_POP
#define STD_FMT fmt
#define FMT_LOCALE(loc) (loc).template get<std::locale>()
#define FMT_TO_ARG_ID(arg) static_cast<int>(arg)
#define FMT_FROM_ARG_ID(arg) static_cast<size_t>(arg)
#define UNITS_STD_FMT fmt
#define UNITS_FMT_LOCALE(loc) (loc).template get<std::locale>()
#define UNITS_FMT_TO_ARG_ID(arg) static_cast<int>(arg)
#define UNITS_FMT_FROM_ARG_ID(arg) static_cast<size_t>(arg)
// just reuse FMT_THROW - we're anyway only throwing from within string formatting code
#if FMT_EXCEPTIONS && (FMT_MSC_VERSION || defined(__NVCC__))
// work around FMT_THROW being defined without a fully qualified namespace
#define UNITS_THROW(x) ::fmt::detail::do_throw(x)
#else
#define UNITS_THROW(x) FMT_THROW(x)
#endif
#else
@@ -54,10 +63,10 @@ UNITS_DIAGNOSTIC_POP
#include <format>
#define STD_FMT std
#define FMT_LOCALE(loc) loc
#define FMT_TO_ARG_ID(arg) arg
#define FMT_FROM_ARG_ID(arg) arg
#define FMT_THROW(arg) throw arg
#define UNITS_STD_FMT std
#define UNITS_FMT_LOCALE(loc) loc
#define UNITS_FMT_TO_ARG_ID(arg) arg
#define UNITS_FMT_FROM_ARG_ID(arg) arg
#define UNITS_THROW(arg) throw arg
#endif
+40 -39
View File
@@ -54,24 +54,24 @@
// - Add the new symbol in the `units_types` variable in the `parse_units_format` function
// - Add a new case in the `if` following the format_error in `parse_units_format` function;
// this should invoke `handler.on_[...]`
// - Edit `STD_FMT::formatter`:
// - Edit `UNITS_STD_FMT::formatter`:
// - Add a new field for the flag/specs
// - Add to the `STD_FMT::formatter::spec_handler` a `on_[...]` function that set the flag/specs if needed
// - Add to the `UNITS_STD_FMT::formatter::spec_handler` a `on_[...]` function that set the flag/specs if needed
// - Edit `quantity_formatter`:
// - Add a new field for the flag/specs
// - write a `on_[...]` function that writes to the `out` iterator the correct output
//
// If you want to add a new `units-rep-type`:
// - Add the new symbol in the `valid_rep_types` variable (which is in the
// STD_FMT::formatter::spec_handler::on_type member function)
// UNITS_STD_FMT::formatter::spec_handler::on_type member function)
// NB: currently this function forward the modifier to the value that must be formatted;
// if the symbol has no meaning for STD_FMT::formatter<Rep>, this behavior should be disabled manually
// if the symbol has no meaning for UNITS_STD_FMT::formatter<Rep>, this behavior should be disabled manually
// (as is done for '\0')
// - Implement the effect of the new flag in `format_units_quantity_value`
//
// If you want to add a new `units-unit-modifier`:
// - Add the new symbol in the `valid_modifiers` variable (which is in the
// STD_FMT::formatter::spec_handler::on_unit_modifier member function)
// UNITS_STD_FMT::formatter::spec_handler::on_unit_modifier member function)
// - Implement the effect of the new flag in the `quantity_formatter::on_quantity_unit` member function
namespace units::detail {
@@ -126,7 +126,7 @@ constexpr const It parse_units_rep(It begin, S end, Handler&& handler, bool trea
if (treat_as_floating_point) {
begin = parse_precision(begin, end, handler);
} else
FMT_THROW(STD_FMT::format_error("precision not allowed for integral quantity representation"));
UNITS_THROW(UNITS_STD_FMT::format_error("precision not allowed for integral quantity representation"));
if (begin == end) return begin;
}
@@ -156,7 +156,7 @@ constexpr It parse_units_format(It begin, S end, Handler&& handler)
}
if (begin != ptr) handler.on_text(begin, ptr);
begin = ++ptr; // consume '%'
if (ptr == end) FMT_THROW(STD_FMT::format_error("invalid format"));
if (ptr == end) UNITS_THROW(UNITS_STD_FMT::format_error("invalid format"));
c = *ptr++;
switch (c) {
@@ -177,7 +177,7 @@ constexpr It parse_units_format(It begin, S end, Handler&& handler)
default:
constexpr auto units_types = std::string_view{"Qq"};
const auto new_end = std::find_first_of(begin, end, units_types.begin(), units_types.end());
if (new_end == end) FMT_THROW(STD_FMT::format_error("invalid format"));
if (new_end == end) UNITS_THROW(UNITS_STD_FMT::format_error("invalid format"));
if (*new_end == 'Q') {
handler.on_quantity_value(begin, new_end); // Edit `on_quantity_value` to add rep modifiers
} else {
@@ -199,43 +199,43 @@ template<typename CharT, typename Rep, typename OutputIt, typename Locale>
std::basic_string<CharT> buffer;
auto to_buffer = std::back_inserter(buffer);
STD_FMT::format_to(to_buffer, "{{:");
UNITS_STD_FMT::format_to(to_buffer, "{{:");
switch (rep_specs.sign) {
case fmt_sign::none:
break;
case fmt_sign::plus:
STD_FMT::format_to(to_buffer, "+");
UNITS_STD_FMT::format_to(to_buffer, "+");
break;
case fmt_sign::minus:
STD_FMT::format_to(to_buffer, "-");
UNITS_STD_FMT::format_to(to_buffer, "-");
break;
case fmt_sign::space:
STD_FMT::format_to(to_buffer, " ");
UNITS_STD_FMT::format_to(to_buffer, " ");
break;
}
if (rep_specs.alt) {
STD_FMT::format_to(to_buffer, "#");
UNITS_STD_FMT::format_to(to_buffer, "#");
}
auto type = rep_specs.type;
if (auto precision = rep_specs.precision; precision >= 0) {
STD_FMT::format_to(to_buffer, ".{}{}", precision, type == '\0' ? 'f' : type);
UNITS_STD_FMT::format_to(to_buffer, ".{}{}", precision, type == '\0' ? 'f' : type);
} else if constexpr (treat_as_floating_point<Rep>) {
STD_FMT::format_to(to_buffer, "{}", type == '\0' ? 'g' : type);
UNITS_STD_FMT::format_to(to_buffer, "{}", type == '\0' ? 'g' : type);
} else {
if (type != '\0') {
STD_FMT::format_to(to_buffer, "{}", type);
UNITS_STD_FMT::format_to(to_buffer, "{}", type);
}
}
if (rep_specs.localized) {
STD_FMT::format_to(to_buffer, "L");
UNITS_STD_FMT::format_to(to_buffer, "L");
}
STD_FMT::format_to(to_buffer, "}}");
UNITS_STD_FMT::format_to(to_buffer, "}}");
if (rep_specs.localized) {
return STD_FMT::vformat_to(out, FMT_LOCALE(loc), buffer, STD_FMT::make_format_args(val));
return UNITS_STD_FMT::vformat_to(out, UNITS_FMT_LOCALE(loc), buffer, UNITS_STD_FMT::make_format_args(val));
}
return STD_FMT::vformat_to(out, buffer, STD_FMT::make_format_args(val));
return UNITS_STD_FMT::vformat_to(out, buffer, UNITS_STD_FMT::make_format_args(val));
}
// Creates a global format string
@@ -243,25 +243,25 @@ template<typename CharT, typename Rep, typename OutputIt, typename Locale>
template<typename CharT, typename OutputIt>
OutputIt format_global_buffer(OutputIt out, const quantity_global_format_specs<CharT>& specs)
{
STD_FMT::format_to(out, "{{:");
UNITS_STD_FMT::format_to(out, "{{:");
if (specs.fill.size() != 1 || specs.fill[0] != ' ') {
STD_FMT::format_to(out, "{}", specs.fill.data());
UNITS_STD_FMT::format_to(out, "{}", specs.fill.data());
}
switch (specs.align) {
case fmt_align::left:
STD_FMT::format_to(out, "<");
UNITS_STD_FMT::format_to(out, "<");
break;
case fmt_align::right:
STD_FMT::format_to(out, ">");
UNITS_STD_FMT::format_to(out, ">");
break;
case fmt_align::center:
STD_FMT::format_to(out, "^");
UNITS_STD_FMT::format_to(out, "^");
break;
default:
break;
}
if (specs.width >= 1) STD_FMT::format_to(out, "{}", specs.width);
return STD_FMT::format_to(out, "}}");
if (specs.width >= 1) UNITS_STD_FMT::format_to(out, "{}", specs.width);
return UNITS_STD_FMT::format_to(out, "}}");
}
template<typename Dimension, typename Unit, typename Rep, typename Locale, typename CharT, typename OutputIt>
@@ -293,9 +293,9 @@ struct quantity_formatter {
{
auto txt = unit_text<Dimension, Unit>();
if (specs.unit.ascii_only) {
STD_FMT::format_to(out, "{}", txt.ascii().c_str());
UNITS_STD_FMT::format_to(out, "{}", txt.ascii().c_str());
} else {
STD_FMT::format_to(out, "{}", txt.standard().c_str());
UNITS_STD_FMT::format_to(out, "{}", txt.standard().c_str());
}
}
};
@@ -303,10 +303,10 @@ struct quantity_formatter {
} // namespace units::detail
template<typename Dimension, typename Unit, typename Rep, typename CharT>
struct STD_FMT::formatter<units::quantity<Dimension, Unit, Rep>, CharT> {
struct UNITS_STD_FMT::formatter<units::quantity<Dimension, Unit, Rep>, CharT> {
private:
using quantity = units::quantity<Dimension, Unit, Rep>;
using iterator = TYPENAME STD_FMT::basic_format_parse_context<CharT>::iterator;
using iterator = TYPENAME UNITS_STD_FMT::basic_format_parse_context<CharT>::iterator;
bool quantity_value = false;
bool quantity_unit = false;
@@ -315,7 +315,7 @@ private:
struct spec_handler {
formatter& f;
STD_FMT::basic_format_parse_context<CharT>& context;
UNITS_STD_FMT::basic_format_parse_context<CharT>& context;
constexpr void on_fill(std::basic_string_view<CharT> fill) { f.specs.global.fill = fill; }
constexpr void on_align(units::detail::fmt_align align) { f.specs.global.align = align; }
@@ -331,7 +331,7 @@ private:
if (valid_rep_types.find(type) != std::string_view::npos) {
f.specs.rep.type = type;
} else {
FMT_THROW(STD_FMT::format_error("invalid quantity type specifier"));
UNITS_THROW(UNITS_STD_FMT::format_error("invalid quantity type specifier"));
}
}
@@ -341,7 +341,7 @@ private:
if (valid_modifiers.find(mod) != std::string_view::npos) {
f.specs.unit.ascii_only = true;
} else {
FMT_THROW(STD_FMT::format_error("invalid unit modifier specified"));
UNITS_THROW(UNITS_STD_FMT::format_error("invalid unit modifier specified"));
}
}
@@ -376,7 +376,7 @@ private:
}
};
[[nodiscard]] constexpr std::pair<iterator, iterator> do_parse(STD_FMT::basic_format_parse_context<CharT>& ctx)
[[nodiscard]] constexpr std::pair<iterator, iterator> do_parse(UNITS_STD_FMT::basic_format_parse_context<CharT>& ctx)
{
auto begin = ctx.begin();
auto end = ctx.end();
@@ -416,7 +416,7 @@ private:
constexpr auto symbol = units::detail::unit_text<Dimension, Unit>();
if constexpr (symbol.standard().size() > 0) {
*out++ = CharT(' ');
STD_FMT::format_to(out, "{}", symbol.standard().c_str());
UNITS_STD_FMT::format_to(out, "{}", symbol.standard().c_str());
}
} else {
// user provided format
@@ -427,7 +427,7 @@ private:
}
public:
[[nodiscard]] constexpr auto parse(STD_FMT::basic_format_parse_context<CharT>& ctx)
[[nodiscard]] constexpr auto parse(UNITS_STD_FMT::basic_format_parse_context<CharT>& ctx)
{
auto range = do_parse(ctx);
if (range.first != range.second)
@@ -464,8 +464,9 @@ public:
units::detail::format_global_buffer<CharT>(std::back_inserter(global_format_buffer), specs.global);
// Format the `quantity buffer` using specs from `global_format_buffer`
// In the example, equivalent to STD_FMT::format("{:*^10}", "1.2_m")
return STD_FMT::vformat_to(ctx.out(), global_format_buffer, STD_FMT::make_format_args(quantity_buffer));
// In the example, equivalent to UNITS_STD_FMT::format("{:*^10}", "1.2_m")
return UNITS_STD_FMT::vformat_to(ctx.out(), global_format_buffer,
UNITS_STD_FMT::make_format_args(quantity_buffer));
}
}
};