LibSWOC++ 1.5.14
Solid Wall of C++
Loading...
Searching...
No Matches
bwf_ex.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright Apache Software Foundation 2019
7
8#pragma once
9
10#include <array>
11#include <string_view>
12
13#include "swoc/swoc_version.h"
14#include "swoc/bwf_base.h"
15#include "swoc/swoc_meta.h"
16
17namespace swoc { inline namespace SWOC_VERSION_NS {
18namespace bwf {
19using namespace swoc::literals; // enable ""sv
20
24struct Pattern {
25 int _n;
26 std::string_view _text;
27};
28
34struct Errno {
35 int _e;
36
38 explicit Errno(int e = errno) : _e(e) {}
39};
40
45struct Date {
47 static constexpr std::string_view DEFAULT_FORMAT{"%Y %b %d %H:%M:%S"_sv};
48 time_t _epoch;
49 std::string_view _fmt;
50
56 Date(time_t t, std::string_view fmt = DEFAULT_FORMAT) : _epoch(t), _fmt(fmt) {}
57
59 Date(std::string_view fmt = DEFAULT_FORMAT);
60};
61
62namespace detail {
63// Special case conversions - these handle nullptr because the @c std::string_view spec is stupid.
64inline std::string_view
65FirstOfConverter(std::nullptr_t) {
66 return std::string_view{};
67}
68
69inline std::string_view
70FirstOfConverter(char const *s) {
71 return std::string_view{s ? s : ""};
72}
73
74// Otherwise do any compliant conversion.
75template <typename T>
76std::string_view
77FirstOfConverter(T &&t) {
78 return t;
79}
80} // namespace detail
81
84template <typename... Args>
85std::string_view
86FirstOf(Args &&...args) {
87 std::array<std::string_view, sizeof...(args)> strings{{detail::FirstOfConverter(args)...}};
88 for (auto &s : strings) {
89 if (!s.empty())
90 return s;
91 }
92 return std::string_view{};
93}
94
99template <typename... Args> struct SubText {
100 using arg_pack = std::tuple<Args...>;
103
105 SubText(TextView const &fmt, arg_pack const &args) : _fmt(fmt), _args(args){};
106
108 bool operator!() const;
109
111 explicit operator bool() const;
112};
113
114template <typename... Args> SubText<Args...>::operator bool() const {
115 return !_fmt.empty();
116}
117
118template <typename... Args>
119bool
121 return _fmt.empty();
122}
123
144template <typename... Args>
145SubText<Args...>
146If(bool flag, TextView const &fmt, Args &&...args) {
147 return SubText<Args...>(flag ? fmt : TextView{}, std::forward_as_tuple(args...));
148}
149
150namespace detail {
151// @a T has the @c empty() method.
152template <typename T>
153auto
154Optional(meta::CaseTag<2>, TextView fmt, T &&t) -> decltype(void(t.empty()), meta::TypeFunc<SubText<T>>()) {
155 return SubText<T>(t.empty() ? TextView{} : fmt, std::forward_as_tuple(t));
156}
157
158// @a T is convertible to @c bool.
159template <typename T>
160auto
161Optional(meta::CaseTag<1>, TextView fmt, T &&t) -> decltype(bool(t), meta::TypeFunc<SubText<T>>()) {
162 return SubText<T>(bool(t) ? fmt : TextView{}, std::forward_as_tuple(t));
163}
164
165// @a T is not optional, always print.
166template <typename T>
167auto
168Optional(meta::CaseTag<0>, TextView fmt, T &&t) -> SubText<T> {
169 return SubText<T>(fmt, std::forward_as_tuple(t));
170}
171} // namespace detail
172
204template <typename ARG>
205SubText<ARG>
206Optional(TextView fmt, ARG &&arg) {
207 return detail::Optional(meta::CaseArg, fmt, std::forward<ARG>(arg));
208}
209
222struct UnHex {
223 UnHex(MemSpan<void const> const &span) : _span(span) {}
225};
226} // namespace bwf
227
237BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, bwf::Pattern const &pattern);
238
246BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, bwf::Errno const &e);
247
255BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, bwf::Date const &date);
256
257BufferWriter &bwformat(BufferWriter &w, bwf::Spec const &spec, bwf::UnHex const &obj);
258
275template <typename... Args>
277bwformat(BufferWriter &w, bwf::Spec const &, bwf::SubText<Args...> const &subtext) {
278 if (!subtext._fmt.empty()) {
279 w.print_v(subtext._fmt, subtext._args);
280 }
281 return w;
282}
283
284}} // namespace swoc::SWOC_VERSION_NS
SubText< ARG > Optional(TextView fmt, ARG &&arg)
Definition bwf_ex.h:206
std::string_view FirstOf(Args &&...args)
Definition bwf_ex.h:86
SubText< Args... > If(bool flag, TextView const &fmt, Args &&...args)
Definition bwf_ex.h:146
BufferWriter & print_v(const TextView &fmt, const std::tuple< Args... > &args)
Definition bwf_base.h:939
For template deduction guides.
Definition ArenaWriter.cc:9
BufferWriter & bwformat(BufferWriter &w, bwf::Spec const &spec, std::string_view sv)
Definition bw_format.cc:649
static constexpr std::string_view DEFAULT_FORMAT
Default format.
Definition bwf_ex.h:47
Date(time_t t, std::string_view fmt=DEFAULT_FORMAT)
Definition bwf_ex.h:56
time_t _epoch
The time.
Definition bwf_ex.h:48
std::string_view _fmt
Data format.
Definition bwf_ex.h:49
int _e
Errno value.
Definition bwf_ex.h:35
Errno(int e=errno)
Construct wrapper, default to current errno.
Definition bwf_ex.h:38
std::string_view _text
output text.
Definition bwf_ex.h:26
arg_pack _args
Arguments to format string.
Definition bwf_ex.h:102
bool operator!() const
Check for output not enabled.
Definition bwf_ex.h:120
std::tuple< Args... > arg_pack
The pack of arguments for format string.
Definition bwf_ex.h:100
TextView _fmt
Format string. If empty, do not generate output.
Definition bwf_ex.h:101
SubText(TextView const &fmt, arg_pack const &args)
Construct with a specific fmt and args.
Definition bwf_ex.h:105
MemSpan< void const > _span
Source span.
Definition bwf_ex.h:224
Case hierarchy.
Definition swoc_meta.h:66