1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___FORMAT_FORMAT_ARGS_H
11#define _LIBCPP___FORMAT_FORMAT_ARGS_H
12
13#include <__config>
14#include <__cstddef/size_t.h>
15#include <__format/format_arg.h>
16#include <__format/format_arg_store.h>
17#include <__fwd/format.h>
18#include <cstdint>
19
20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21# pragma GCC system_header
22#endif
23
24_LIBCPP_BEGIN_NAMESPACE_STD
25
26#if _LIBCPP_STD_VER >= 20
27
28template <class _Context>
29class basic_format_args {
30public:
31 template <class... _Args>
32 basic_format_args(const __format_arg_store<_Context, _Args...>& __store) noexcept : __size_(sizeof...(_Args)) {
33 if constexpr (sizeof...(_Args) != 0) {
34 if constexpr (__format::__use_packed_format_arg_store(size: sizeof...(_Args))) {
35 __values_ = __store.__storage.__values_;
36 __types_ = __store.__storage.__types_;
37 } else
38 __args_ = __store.__storage.__args_;
39 }
40 }
41
42 [[nodiscard]] basic_format_arg<_Context> get(size_t __id) const noexcept {
43 if (__id >= __size_)
44 return basic_format_arg<_Context>{};
45
46 if (__format::__use_packed_format_arg_store(size: __size_))
47 return basic_format_arg<_Context>{__format::__get_packed_type(types: __types_, __id), __values_[__id]};
48
49 return __args_[__id];
50 }
51
52 size_t __size() const noexcept { return __size_; }
53
54private:
55 size_t __size_{0};
56 // [format.args]/5
57 // [Note 1: Implementations are encouraged to optimize the representation of
58 // basic_format_args for small number of formatting arguments by storing
59 // indices of type alternatives separately from values and packing the
60 // former. - end note]
61 union {
62 struct {
63 const __basic_format_arg_value<_Context>* __values_;
64 uint64_t __types_;
65 };
66 const basic_format_arg<_Context>* __args_;
67 };
68};
69
70template <class _Context, class... _Args>
71basic_format_args(__format_arg_store<_Context, _Args...>) -> basic_format_args<_Context>;
72
73#endif // _LIBCPP_STD_VER >= 20
74
75_LIBCPP_END_NAMESPACE_STD
76
77#endif // _LIBCPP___FORMAT_FORMAT_ARGS_H
78