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 3f864a4 commit da3a5b0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
4 changes: 1 addition & 3 deletions include/fmt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -2276,9 +2276,7 @@ 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);

inline explicit operator bool() const noexcept { return locale_ != nullptr; }
#endif // FMT_USE_LOCALE
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
47 changes: 30 additions & 17 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,9 @@ template <typename Char> class digit_grouping {
}

public:
explicit digit_grouping(locale_ref loc, bool localized = true) {
template <typename Locale,
FMT_ENABLE_IF(std::is_same<Locale, locale_ref>::value)>
explicit digit_grouping(Locale loc, bool localized = true) {
if (!localized) return;
auto sep = thousands_sep<Char>(loc);
grouping_ = sep.grouping;
Expand Down Expand Up @@ -3639,6 +3641,12 @@ FMT_CONSTEXPR auto native_formatter<T, Char, TYPE>::format(
return write<Char>(ctx.out(), val, specs, ctx.locale());
}

// DEPRECATED! https://github.com/fmtlib/fmt/issues/4292.
template <typename T, typename Enable = void>
struct is_locale : std::false_type {};
template <typename T>
struct is_locale<T, void_t<decltype(T::classic())>> : std::true_type {};

// DEPRECATED!
template <typename Char = char> struct vformat_args {
using type = basic_format_args<buffered_context<Char>>;
Expand Down Expand Up @@ -4120,41 +4128,46 @@ 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)
template <typename Locale, FMT_ENABLE_IF(detail::is_locale<Locale>::value)>
inline auto vformat(const Locale& loc, string_view fmt, format_args args)
-> std::string {
auto buf = memory_buffer();
detail::vformat_to(buf, fmt, args, loc);
detail::vformat_to(buf, fmt, args, detail::locale_ref(loc));
return {buf.data(), buf.size()};
}

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

template <typename OutputIt,
template <typename OutputIt, typename Locale,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
auto vformat_to(OutputIt out, detail::locale_ref loc, string_view fmt,
format_args args) -> OutputIt {
auto vformat_to(OutputIt out, Locale loc, string_view fmt, format_args args)
-> OutputIt {
auto&& buf = detail::get_buffer<char>(out);
detail::vformat_to(buf, fmt, args, loc);
detail::vformat_to(buf, fmt, args, detail::locale_ref(loc));
return detail::get_iterator(buf, out);
}

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,
format_string<T...> fmt, T&&... args) -> OutputIt {
template <typename OutputIt, typename Locale, typename... T,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value&&
detail::is_locale<Locale>::value)>
FMT_INLINE auto format_to(OutputIt out, Locale 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,
template <typename Locale, typename... T,
FMT_ENABLE_IF(detail::is_locale<Locale>::value)>
FMT_NODISCARD FMT_INLINE auto formatted_size(Locale loc,
format_string<T...> fmt,
T&&... args) -> size_t {
auto buf = detail::counting_buffer<>();
detail::vformat_to(buf, fmt.str, vargs<T...>{{args...}}, loc);
detail::vformat_to(buf, fmt.str, vargs<T...>{{args...}},
detail::locale_ref(loc));
return buf.count();
}

Expand Down
27 changes: 16 additions & 11 deletions include/fmt/xchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,11 @@ auto format(const S& fmt, T&&... args) -> std::basic_string<Char> {
fmt::make_format_args<buffered_context<Char>>(args...));
}

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,
template <typename Locale, typename S,
typename Char = detail::format_string_char_t<S>,
FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
detail::is_exotic_char<Char>::value)>
inline auto vformat(const Locale& loc, const S& fmt,
typename detail::vformat_args<Char>::type args)
-> std::basic_string<Char> {
auto buf = basic_memory_buffer<Char>();
Expand All @@ -202,10 +204,11 @@ inline auto vformat(detail::locale_ref loc, const S& fmt,
return {buf.data(), buf.size()};
}

template <typename S, typename... T,
template <typename Locale, 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)
FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
detail::is_exotic_char<Char>::value)>
inline auto format(const Locale& 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 All @@ -232,23 +235,25 @@ inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
fmt::make_format_args<buffered_context<Char>>(args...));
}

template <typename S, typename OutputIt, typename... Args,
template <typename Locale, 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,
detail::is_locale<Locale>::value&&
detail::is_exotic_char<Char>::value)>
inline auto vformat_to(OutputIt out, const Locale& loc, const S& fmt,
typename detail::vformat_args<Char>::type args)
-> OutputIt {
auto&& buf = detail::get_buffer<Char>(out);
vformat_to(buf, detail::to_string_view(fmt), args, detail::locale_ref(loc));
return detail::get_iterator(buf, out);
}

template <typename OutputIt, typename S, typename... T,
template <typename Locale, 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_locale<Locale>::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, const Locale& 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 da3a5b0

Please sign in to comment.