// -*- C++ -*- //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef _LIBCPP___FORMAT_BUFFER_H #define _LIBCPP___FORMAT_BUFFER_H #include <__algorithm/copy_n.h> #include <__algorithm/fill_n.h> #include <__algorithm/max.h> #include <__algorithm/min.h> #include <__algorithm/ranges_copy_n.h> #include <__algorithm/transform.h> #include <__algorithm/unwrap_iter.h> #include <__concepts/same_as.h> #include <__config> #include <__format/concepts.h> #include <__format/enable_insertable.h> #include <__format/format_to_n_result.h> #include <__iterator/back_insert_iterator.h> #include <__iterator/concepts.h> #include <__iterator/incrementable_traits.h> #include <__iterator/iterator_traits.h> #include <__iterator/wrap_iter.h> #include <__memory/addressof.h> #include <__memory/allocate_at_least.h> #include <__memory/allocator_traits.h> #include <__memory/construct_at.h> #include <__memory/ranges_construct_at.h> #include <__memory/uninitialized_algorithms.h> #include <__type_traits/add_pointer.h> #include <__type_traits/conditional.h> #include <__utility/exception_guard.h> #include <__utility/move.h> #include <cstddef> #include <string_view> #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header #endif _LIBCPP_PUSH_MACROS #include <__undef_macros> _LIBCPP_BEGIN_NAMESPACE_STD #if _LIBCPP_STD_VER >= 20 namespace __format { /// A "buffer" that handles writing to the proper iterator. /// /// This helper is used together with the @ref back_insert_iterator to offer /// type-erasure for the formatting functions. This reduces the number to /// template instantiations. template <__fmt_char_type _CharT> class _LIBCPP_TEMPLATE_VIS __output_buffer { … }; /// A storage using an internal buffer. /// /// This storage is used when writing a single element to the output iterator /// is expensive. template <__fmt_char_type _CharT> class _LIBCPP_TEMPLATE_VIS __internal_storage { … }; /// A storage writing directly to the storage. /// /// This requires the storage to be a contiguous buffer of \a _CharT. /// Since the output is directly written to the underlying storage this class /// is just an empty class. template <__fmt_char_type _CharT> class _LIBCPP_TEMPLATE_VIS __direct_storage { … }; __enable_direct_output; /// Write policy for directly writing to the underlying output. template <class _OutIt, __fmt_char_type _CharT> class _LIBCPP_TEMPLATE_VIS __writer_direct { … }; /// Write policy for copying the buffer to the output. template <class _OutIt, __fmt_char_type _CharT> class _LIBCPP_TEMPLATE_VIS __writer_iterator { … }; /// Concept to see whether a \a _Container is insertable. /// /// The concept is used to validate whether multiple calls to a /// \ref back_insert_iterator can be replace by a call to \c _Container::insert. /// /// \note a \a _Container needs to opt-in to the concept by specializing /// \ref __enable_insertable. __insertable; /// Extract the container type of a \ref back_insert_iterator. template <class _It> struct _LIBCPP_TEMPLATE_VIS __back_insert_iterator_container { … }; __back_insert_iterator_container<back_insert_iterator<_Container>>; /// Write policy for inserting the buffer in a container. template <class _Container> class _LIBCPP_TEMPLATE_VIS __writer_container { … }; /// Selects the type of the writer used for the output iterator. template <class _OutIt, class _CharT> class _LIBCPP_TEMPLATE_VIS __writer_selector { … }; /// The generic formatting buffer. template <class _OutIt, __fmt_char_type _CharT> requires(output_iterator<_OutIt, const _CharT&>) class _LIBCPP_TEMPLATE_VIS __format_buffer { … }; /// A buffer that counts the number of insertions. /// /// Since \ref formatted_size only needs to know the size, the output itself is /// discarded. template <__fmt_char_type _CharT> class _LIBCPP_TEMPLATE_VIS __formatted_size_buffer { … }; /// The base of a buffer that counts and limits the number of insertions. template <class _OutIt, __fmt_char_type _CharT, bool> requires(output_iterator<_OutIt, const _CharT&>) struct _LIBCPP_TEMPLATE_VIS __format_to_n_buffer_base { … }; /// The base of a buffer that counts and limits the number of insertions. /// /// This version is used when \c __enable_direct_output<_OutIt, _CharT> == true. /// /// This class limits the size available to the direct writer so it will not /// exceed the maximum number of code units. __format_to_n_buffer_base<_OutIt, _CharT, true>; /// The buffer that counts and limits the number of insertions. template <class _OutIt, __fmt_char_type _CharT> requires(output_iterator<_OutIt, const _CharT&>) struct _LIBCPP_TEMPLATE_VIS __format_to_n_buffer final : public __format_to_n_buffer_base< _OutIt, _CharT, __enable_direct_output<_OutIt, _CharT>> { … }; // A dynamically growing buffer intended to be used for retargeting a context. // // P2286 Formatting ranges adds range formatting support. It allows the user to // specify the minimum width for the entire formatted range. The width of the // range is not known until the range is formatted. Formatting is done to an // output_iterator so there's no guarantee it would be possible to add the fill // to the front of the output. Instead the range is formatted to a temporary // buffer and that buffer is formatted as a string. // // There is an issue with that approach, the format context used in // std::formatter<T>::format contains the output iterator used as part of its // type. So using this output iterator means there needs to be a new format // context and the format arguments need to be retargeted to the new context. // This retargeting is done by a basic_format_context specialized for the // __iterator of this container. // // This class uses its own buffer management, since using vector // would lead to a circular include with formatter for vector<bool>. template <__fmt_char_type _CharT> class _LIBCPP_TEMPLATE_VIS __retarget_buffer { … }; } // namespace __format #endif // _LIBCPP_STD_VER >= 20 _LIBCPP_END_NAMESPACE_STD _LIBCPP_POP_MACROS #endif // _LIBCPP___FORMAT_BUFFER_H