Skip to content

Commit

Permalink
Restore ABI compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jan 11, 2025
1 parent fc1b0f3 commit e0c74fd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
15 changes: 13 additions & 2 deletions include/fmt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,8 @@ struct is_output_iterator<
# define FMT_USE_LOCALE (FMT_OPTIMIZE_SIZE <= 1)
#endif

struct locale_arg;

// A type-erased reference to an std::locale to avoid a heavy <locale> include.
struct locale_ref {
#if FMT_USE_LOCALE
Expand All @@ -2271,15 +2273,24 @@ struct locale_ref {
public:
constexpr locale_ref() : locale_(nullptr) {}

template <typename Locale, FMT_ENABLE_IF(sizeof(Locale::collate) != 0)>
locale_ref(const Locale& loc);
template <typename Locale> locale_ref(const Locale& loc);
locale_ref(locale_arg arg);

inline explicit operator bool() const noexcept { return locale_ != nullptr; }
#endif // FMT_USE_LOCALE

template <typename Locale> auto get() const -> Locale;
};

// DEPRECATED!
struct locale_arg : locale_ref {
locale_arg() = default;
template <typename Locale, FMT_ENABLE_IF(sizeof(Locale::collate) != 0)>
locale_arg(const Locale& loc) : locale_ref(loc) {}
};

inline locale_ref::locale_ref(locale_arg arg) : locale_(arg.locale_) {}

template <typename> constexpr auto encode_types() -> unsigned long long {
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ using std::locale;
using std::numpunct;
using std::use_facet;

template <typename Locale, enable_if_t<(sizeof(Locale::collate) != 0), int>>
template <typename Locale>
locale_ref::locale_ref(const Locale& loc) : locale_(&loc) {
static_assert(std::is_same<Locale, locale>::value, "");
}
Expand Down
20 changes: 10 additions & 10 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,7 @@ template <typename Char> class digit_grouping {
}

public:
explicit digit_grouping(locale_ref loc, bool localized = true) {
explicit digit_grouping(locale_arg loc, bool localized = true) {
if (!localized) return;
auto sep = thousands_sep<Char>(loc);
grouping_ = sep.grouping;
Expand Down Expand Up @@ -2328,7 +2328,7 @@ template <typename Char, typename OutputIt, typename DecimalFP,
typename Grouping = digit_grouping<Char>>
FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f,
const format_specs& specs, sign s,
locale_ref loc) -> OutputIt {
locale_arg loc) -> OutputIt {
auto significand = f.significand;
int significand_size = get_significand_size(f);
const Char zero = static_cast<Char>('0');
Expand Down Expand Up @@ -2447,7 +2447,7 @@ template <typename Char> class fallback_digit_grouping {
template <typename Char, typename OutputIt, typename DecimalFP>
FMT_CONSTEXPR20 auto write_float(OutputIt out, const DecimalFP& f,
const format_specs& specs, sign s,
locale_ref loc) -> OutputIt {
locale_arg loc) -> OutputIt {
if (is_constant_evaluated()) {
return do_write_float<Char, OutputIt, DecimalFP,
fallback_digit_grouping<Char>>(out, f, specs, s, loc);
Expand Down Expand Up @@ -3286,7 +3286,7 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision,

template <typename Char, typename OutputIt, typename T>
FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
locale_ref loc) -> OutputIt {
locale_arg loc) -> OutputIt {
// Use signbit because value < 0 is false for NaN.
sign s = detail::signbit(value) ? sign::minus : specs.sign();

Expand Down Expand Up @@ -3341,7 +3341,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
template <typename Char, typename OutputIt, typename T,
FMT_ENABLE_IF(is_floating_point<T>::value)>
FMT_CONSTEXPR20 auto write(OutputIt out, T value, format_specs specs,
locale_ref loc = {}) -> OutputIt {
locale_arg loc = {}) -> OutputIt {
return specs.localized() && write_loc(out, value, specs, loc)
? out
: write_float<Char>(out, value, specs, loc);
Expand Down Expand Up @@ -4131,22 +4131,22 @@ FMT_API void format_system_error(detail::buffer<char>& out, int error_code,
// Can be used to report errors from destructors.
FMT_API void report_system_error(int error_code, const char* message) noexcept;

inline auto vformat(detail::locale_ref loc, string_view fmt, format_args args)
inline auto vformat(detail::locale_arg loc, string_view fmt, format_args args)
-> std::string {
auto buf = memory_buffer();
detail::vformat_to(buf, fmt, args, loc);
return {buf.data(), buf.size()};
}

template <typename... T>
FMT_INLINE auto format(detail::locale_ref loc, format_string<T...> fmt,
FMT_INLINE auto format(detail::locale_arg loc, format_string<T...> fmt,
T&&... args) -> std::string {
return vformat(loc, fmt.str, vargs<T...>{{args...}});
}

template <typename OutputIt,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
auto vformat_to(OutputIt out, detail::locale_ref loc, string_view fmt,
auto vformat_to(OutputIt out, detail::locale_arg loc, string_view fmt,
format_args args) -> OutputIt {
auto&& buf = detail::get_buffer<char>(out);
detail::vformat_to(buf, fmt, args, loc);
Expand All @@ -4155,13 +4155,13 @@ auto vformat_to(OutputIt out, detail::locale_ref loc, string_view fmt,

template <typename OutputIt, typename... T,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
FMT_INLINE auto format_to(OutputIt out, detail::locale_ref loc,
FMT_INLINE auto format_to(OutputIt out, detail::locale_arg loc,
format_string<T...> fmt, T&&... args) -> OutputIt {
return fmt::vformat_to(out, loc, fmt.str, vargs<T...>{{args...}});
}

template <typename... T>
FMT_NODISCARD FMT_INLINE auto formatted_size(detail::locale_ref loc,
FMT_NODISCARD FMT_INLINE auto formatted_size(detail::locale_arg loc,
format_string<T...> fmt,
T&&... args) -> size_t {
auto buf = detail::counting_buffer<>();
Expand Down
8 changes: 4 additions & 4 deletions include/fmt/xchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ auto format(const S& fmt, T&&... args) -> std::basic_string<Char> {

template <typename S, typename Char = detail::format_string_char_t<S>,
FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
inline auto vformat(detail::locale_ref loc, const S& fmt,
inline auto vformat(detail::locale_arg loc, const S& fmt,
typename detail::vformat_args<Char>::type args)
-> std::basic_string<Char> {
auto buf = basic_memory_buffer<Char>();
Expand All @@ -205,7 +205,7 @@ inline auto vformat(detail::locale_ref loc, const S& fmt,
template <typename S, typename... T,
typename Char = detail::format_string_char_t<S>,
FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
inline auto format(detail::locale_ref loc, const S& fmt, T&&... args)
inline auto format(detail::locale_arg loc, const S& fmt, T&&... args)
-> std::basic_string<Char> {
return vformat(loc, detail::to_string_view(fmt),
fmt::make_format_args<buffered_context<Char>>(args...));
Expand Down Expand Up @@ -236,7 +236,7 @@ template <typename S, typename OutputIt, typename... Args,
typename Char = detail::format_string_char_t<S>,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
detail::is_exotic_char<Char>::value)>
inline auto vformat_to(OutputIt out, detail::locale_ref loc, const S& fmt,
inline auto vformat_to(OutputIt out, detail::locale_arg loc, const S& fmt,
typename detail::vformat_args<Char>::type args)
-> OutputIt {
auto&& buf = detail::get_buffer<Char>(out);
Expand All @@ -248,7 +248,7 @@ template <typename OutputIt, typename S, typename... T,
typename Char = detail::format_string_char_t<S>,
bool enable = detail::is_output_iterator<OutputIt, Char>::value &&
detail::is_exotic_char<Char>::value>
inline auto format_to(OutputIt out, detail::locale_ref loc, const S& fmt,
inline auto format_to(OutputIt out, detail::locale_arg loc, const S& fmt,
T&&... args) ->
typename std::enable_if<enable, OutputIt>::type {
return vformat_to(out, loc, detail::to_string_view(fmt),
Expand Down

0 comments on commit e0c74fd

Please sign in to comment.