Skip to content

Commit

Permalink
Implement formatter specializations for container adaptors (#4825)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephan T. Lavavej <[email protected]>
  • Loading branch information
frederick-vs-ja and StephanTLavavej authored Aug 15, 2024
1 parent 041f584 commit 3c75f79
Show file tree
Hide file tree
Showing 9 changed files with 1,288 additions and 9 deletions.
32 changes: 32 additions & 0 deletions stl/inc/format
Original file line number Diff line number Diff line change
Expand Up @@ -4774,6 +4774,38 @@ public:
private:
_Fill_align_and_width_specs<_CharT> _Specs;
};

// Per LWG-3997, `_CharT` in library-provided `formatter` specializations is
// constrained to character types supported by `format`.
template <class _AdaptorType, _Format_supported_charT _CharT, template <class> class _RefView>
struct _Adaptor_formatter_base {
private:
using _Container = _AdaptorType::container_type;
using _Maybe_const_container = _Fmt_maybe_const<_Container, _CharT>;
using _Maybe_const_adaptor = _Maybe_const<is_const_v<_Maybe_const_container>, _AdaptorType>;

formatter<_RefView<_Maybe_const_container>, _CharT> _Underlying;

public:
template <class _ParseCtx>
constexpr _ParseCtx::iterator parse(_ParseCtx& _Ctx) {
return _Underlying.parse(_Ctx);
}

template <class _FmtCtx>
_FmtCtx::iterator format(_Maybe_const_adaptor& _Adaptor, _FmtCtx& _Ctx) const {
struct _Container_exposer : _AdaptorType {
using _AdaptorType::c;

_Container_exposer(const _Container_exposer&) = delete;
_Container_exposer& operator=(const _Container_exposer&) = delete;
~_Container_exposer() = delete;
};

constexpr auto _Mem_cont_ptr = &_Container_exposer::c;
return _Underlying.format(_Adaptor.*_Mem_cont_ptr, _Ctx);
}
};
#endif // _HAS_CXX23
_STD_END

Expand Down
16 changes: 16 additions & 0 deletions stl/inc/queue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#if _HAS_CXX23
#include <__msvc_ranges_to.hpp>
#include <format>
#include <iterator>
#endif // _HAS_CXX23

Expand Down Expand Up @@ -223,6 +224,14 @@ void swap(queue<_Ty, _Container>& _Left, queue<_Ty, _Container>& _Right) noexcep
template <class _Ty, class _Container, class _Alloc>
struct uses_allocator<queue<_Ty, _Container>, _Alloc> : uses_allocator<_Container, _Alloc>::type {};

#if _HAS_CXX23
// Per LWG-3997, `_CharT` in library-provided `formatter` specializations is
// constrained to character types supported by `format`.
template <_Format_supported_charT _CharT, class _Ty, formattable<_CharT> _Container>
struct formatter<queue<_Ty, _Container>, _CharT>
: _Adaptor_formatter_base<queue<_Ty, _Container>, _CharT, _RANGES ref_view> {};
#endif // _HAS_CXX23

_EXPORT_STD template <class _Ty, class _Container = vector<_Ty>, class _Pr = less<typename _Container::value_type>>
class priority_queue {
public:
Expand Down Expand Up @@ -490,6 +499,13 @@ void swap(priority_queue<_Ty, _Container, _Pr>& _Left, priority_queue<_Ty, _Cont
template <class _Ty, class _Container, class _Pr, class _Alloc>
struct uses_allocator<priority_queue<_Ty, _Container, _Pr>, _Alloc> : uses_allocator<_Container, _Alloc>::type {};

#if _HAS_CXX23
// Per LWG-3997, `_CharT` in library-provided `formatter` specializations is
// constrained to character types supported by `format`.
template <_Format_supported_charT _CharT, class _Ty, formattable<_CharT> _Container, class _Comp>
struct formatter<priority_queue<_Ty, _Container, _Comp>, _CharT>
: _Adaptor_formatter_base<priority_queue<_Ty, _Container, _Comp>, _CharT, _RANGES ref_view> {};
#endif // _HAS_CXX23
_STD_END

#pragma pop_macro("new")
Expand Down
9 changes: 9 additions & 0 deletions stl/inc/stack
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#if _HAS_CXX23
#include <__msvc_ranges_to.hpp>
#include <format>
#include <iterator>
#endif // _HAS_CXX23

Expand Down Expand Up @@ -209,6 +210,14 @@ void swap(stack<_Ty, _Container>& _Left, stack<_Ty, _Container>& _Right) noexcep

template <class _Ty, class _Container, class _Alloc>
struct uses_allocator<stack<_Ty, _Container>, _Alloc> : uses_allocator<_Container, _Alloc>::type {};

#if _HAS_CXX23
// Per LWG-3997, `_CharT` in library-provided `formatter` specializations is
// constrained to character types supported by `format`.
template <_Format_supported_charT _CharT, class _Ty, formattable<_CharT> _Container>
struct formatter<stack<_Ty, _Container>, _CharT>
: _Adaptor_formatter_base<stack<_Ty, _Container>, _CharT, _RANGES ref_view> {};
#endif // _HAS_CXX23
_STD_END

#pragma pop_macro("new")
Expand Down
3 changes: 2 additions & 1 deletion stl/inc/yvals_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@
// P2273R3 constexpr unique_ptr
// P2278R4 cbegin Should Always Return A Constant Iterator
// P2286R8 Formatting Ranges
// (only the '?' format specifier for strings and characters)
// P2291R3 constexpr Integral <charconv>
// P2302R4 ranges::contains, ranges::contains_subrange
// P2321R2 zip
Expand All @@ -384,6 +383,7 @@
// P2539R4 Synchronizing print() With The Underlying Stream
// P2540R1 Empty Product For Certain Views
// P2549R1 unexpected<E>::error()
// P2585R1 Improve Default Container Formatting
// P2599R2 mdspan: index_type, size_type
// P2604R0 mdspan: data_handle_type, data_handle(), exhaustive
// P2613R1 mdspan: empty()
Expand Down Expand Up @@ -1756,6 +1756,7 @@ _EMIT_STL_ERROR(STL1004, "C++98 unexpected() is incompatible with C++23 unexpect
#define __cpp_lib_constexpr_typeinfo 202106L
#define __cpp_lib_containers_ranges 202202L
#define __cpp_lib_expected 202211L
#define __cpp_lib_format_ranges 202207L
#define __cpp_lib_formatters 202302L
#define __cpp_lib_forward_like 202207L
#define __cpp_lib_freestanding_expected 202311L
Expand Down
9 changes: 1 addition & 8 deletions tests/libcxx/expected_results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,6 @@ std/language.support/support.limits/support.limits.general/cstdlib.version.compi
# P2255R2 "Type Traits To Detect References Binding To Temporaries"
std/language.support/support.limits/support.limits.general/type_traits.version.compile.pass.cpp FAIL

# P2286R8 Formatting Ranges
std/containers/container.adaptors/container.adaptors.format/format.functions.format.pass.cpp FAIL
std/containers/container.adaptors/container.adaptors.format/format.functions.vformat.pass.cpp FAIL
std/containers/container.adaptors/container.adaptors.format/format.pass.cpp FAIL
std/containers/container.adaptors/container.adaptors.format/parse.pass.cpp FAIL
std/containers/container.adaptors/container.adaptors.format/types.compile.pass.cpp FAIL
std/utilities/format/format.formattable/concept.formattable.compile.pass.cpp FAIL


# *** MISSING COMPILER FEATURES ***
# P1169R4 static operator()
Expand Down Expand Up @@ -969,6 +961,7 @@ std/input.output/string.streams/stringstream/stringstream.members/str.allocator_
# These formatter tests need to create basic_format_contexts, so test_format_context.h
# says "Please create a vendor specific version of the test functions".
# If we do, some of these tests should be able to work.
std/containers/container.adaptors/container.adaptors.format/format.pass.cpp FAIL
std/containers/sequences/vector.bool/vector.bool.fmt/format.pass.cpp FAIL
std/thread/thread.threads/thread.thread.class/thread.thread.id/format.pass.cpp FAIL
std/utilities/format/format.formatter/format.context/format.context/advance_to.pass.cpp FAIL
Expand Down
1 change: 1 addition & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ tests\P2278R4_const_span
tests\P2278R4_ranges_const_iterator_machinery
tests\P2278R4_ranges_const_range_machinery
tests\P2278R4_views_as_const
tests\P2286R8_text_formatting_container_adaptors
tests\P2286R8_text_formatting_debug_enabled_specializations
tests\P2286R8_text_formatting_escaping
tests\P2286R8_text_formatting_escaping_legacy_text_encoding
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

RUNALL_INCLUDE ..\usual_latest_matrix.lst
Loading

0 comments on commit 3c75f79

Please sign in to comment.