WireCellToolkit
Wire Cell Simulation, Signal Process and Reconstruction Toolki for Liquid Argon Detectors
ranges.h
Go to the documentation of this file.
1 // Formatting library for C++ - the core API
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7 //
8 // Copyright (c) 2018 - present, Remotion (Igor Schulz)
9 // All Rights Reserved
10 // {fmt} support for ranges, containers and types tuple interface.
11 
12 #ifndef FMT_RANGES_H_
13 #define FMT_RANGES_H_
14 
15 #include "format.h"
16 #include <type_traits>
17 
18 // output only up to N items from the range.
19 #ifndef FMT_RANGE_OUTPUT_LENGTH_LIMIT
20 # define FMT_RANGE_OUTPUT_LENGTH_LIMIT 256
21 #endif
22 
24 
25 template <typename Char>
27  template <typename ParseContext>
28  FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
29  return ctx.begin();
30  }
31 };
32 
33 template <typename Char, typename Enable = void>
35  static FMT_CONSTEXPR_DECL const std::size_t range_length_limit =
36  FMT_RANGE_OUTPUT_LENGTH_LIMIT; // output only up to N items from the range.
37  Char prefix;
38  Char delimiter;
39  Char postfix;
40  formatting_range() : prefix('{'), delimiter(','), postfix('}') {}
41  static FMT_CONSTEXPR_DECL const bool add_delimiter_spaces = true;
42  static FMT_CONSTEXPR_DECL const bool add_prepostfix_space = false;
43 };
44 
45 template <typename Char, typename Enable = void>
47  Char prefix;
48  Char delimiter;
49  Char postfix;
50  formatting_tuple() : prefix('('), delimiter(','), postfix(')') {}
51  static FMT_CONSTEXPR_DECL const bool add_delimiter_spaces = true;
52  static FMT_CONSTEXPR_DECL const bool add_prepostfix_space = false;
53 };
54 
55 namespace internal {
56 
57 template <typename RangeT, typename OutputIterator>
58 void copy(const RangeT &range, OutputIterator out) {
59  for (auto it = range.begin(), end = range.end(); it != end; ++it)
60  *out++ = *it;
61 }
62 
63 template <typename OutputIterator>
64 void copy(const char *str, OutputIterator out) {
65  const char *p_curr = str;
66  while (*p_curr) {
67  *out++ = *p_curr++;
68  }
69 }
70 
71 template <typename OutputIterator>
72 void copy(char ch, OutputIterator out) {
73  *out++ = ch;
74 }
75 
77 template <typename T>
79  template <typename U>
80  static auto check(U *p) ->
81  decltype(p->find('a'), p->length(), p->data(), int());
82  template <typename>
83  static void check(...);
84 
85  public:
86  static FMT_CONSTEXPR_DECL const bool value =
87  !std::is_void<decltype(check<T>(FMT_NULL))>::value;
88 };
89 
90 template <typename Char>
91 struct is_like_std_string<fmt::basic_string_view<Char>> : std::true_type {};
92 
93 template <typename... Ts>
95 
96 template <typename T, typename _ = void>
97 struct is_range_ : std::false_type {};
98 
99 #if !FMT_MSC_VER || FMT_MSC_VER > 1800
100 template <typename T>
101 struct is_range_<T, typename std::conditional<
102  false,
103  conditional_helper<decltype(internal::declval<T>().begin()),
104  decltype(internal::declval<T>().end())>,
105  void>::type> : std::true_type {};
106 #endif
107 
109 template <typename T>
111  template <typename U>
112  static auto check(U *p) ->
113  decltype(std::tuple_size<U>::value,
115  template <typename>
116  static void check(...);
117 
118  public:
119  static FMT_CONSTEXPR_DECL const bool value =
120  !std::is_void<decltype(check<T>(FMT_NULL))>::value;
121 };
122 
123 // Check for integer_sequence
124 #if defined(__cpp_lib_integer_sequence) || FMT_MSC_VER >= 1900
125 template <typename T, T... N>
126 using integer_sequence = std::integer_sequence<T, N...>;
127 template <std::size_t... N>
128 using index_sequence = std::index_sequence<N...>;
129 template <std::size_t N>
131 #else
132 template <typename T, T... N>
134  typedef T value_type;
135 
136  static FMT_CONSTEXPR std::size_t size() {
137  return sizeof...(N);
138  }
139 };
140 
141 template <std::size_t... N>
142 using index_sequence = integer_sequence<std::size_t, N...>;
143 
144 template <typename T, std::size_t N, T... Ns>
145 struct make_integer_sequence : make_integer_sequence<T, N - 1, N - 1, Ns...> {};
146 template <typename T, T... Ns>
147 struct make_integer_sequence<T, 0, Ns...> : integer_sequence<T, Ns...> {};
148 
149 template <std::size_t N>
151 #endif
152 
153 template <class Tuple, class F, size_t... Is>
154 void for_each(index_sequence<Is...>, Tuple &&tup, F &&f) FMT_NOEXCEPT {
155  using std::get;
156  // using free function get<I>(T) now.
157  const int _[] = {0, ((void)f(get<Is>(tup)), 0)...};
158  (void)_; // blocks warnings
159 }
160 
161 template <class T>
163 get_indexes(T const &) { return {}; }
164 
165 template <class Tuple, class F>
166 void for_each(Tuple &&tup, F &&f) {
167  const auto indexes = get_indexes(tup);
168  for_each(indexes, std::forward<Tuple>(tup), std::forward<F>(f));
169 }
170 
171 template<typename Arg>
172 FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&,
173  typename std::enable_if<
174  !is_like_std_string<typename std::decay<Arg>::type>::value>::type* = nullptr) {
175  return add_space ? " {}" : "{}";
176 }
177 
178 template<typename Arg>
179 FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&,
180  typename std::enable_if<
181  is_like_std_string<typename std::decay<Arg>::type>::value>::type* = nullptr) {
182  return add_space ? " \"{}\"" : "\"{}\"";
183 }
184 
185 FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char*) {
186  return add_space ? " \"{}\"" : "\"{}\"";
187 }
188 FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t*) {
189  return add_space ? L" \"{}\"" : L"\"{}\"";
190 }
191 
192 FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char) {
193  return add_space ? " '{}'" : "'{}'";
194 }
195 FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t) {
196  return add_space ? L" '{}'" : L"'{}'";
197 }
198 
199 } // namespace internal
200 
201 template <typename T>
203  static FMT_CONSTEXPR_DECL const bool value =
205 };
206 
207 template <typename TupleT, typename Char>
208 struct formatter<TupleT, Char,
209  typename std::enable_if<fmt::is_tuple_like<TupleT>::value>::type> {
210 private:
211  // C++11 generic lambda for format()
212  template <typename FormatContext>
213  struct format_each {
214  template <typename T>
215  void operator()(const T& v) {
216  if (i > 0) {
217  if (formatting.add_prepostfix_space) {
218  *out++ = ' ';
219  }
220  internal::copy(formatting.delimiter, out);
221  }
222  format_to(out,
224  (formatting.add_delimiter_spaces && i > 0), v),
225  v);
226  ++i;
227  }
228 
229  formatting_tuple<Char>& formatting;
230  std::size_t& i;
231  typename std::add_lvalue_reference<decltype(std::declval<FormatContext>().out())>::type out;
232  };
233 
234 public:
236 
237  template <typename ParseContext>
238  FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
239  return formatting.parse(ctx);
240  }
241 
242  template <typename FormatContext = format_context>
243  auto format(const TupleT &values, FormatContext &ctx) -> decltype(ctx.out()) {
244  auto out = ctx.out();
245  std::size_t i = 0;
246  internal::copy(formatting.prefix, out);
247 
248  internal::for_each(values, format_each<FormatContext>{formatting, i, out});
249  if (formatting.add_prepostfix_space) {
250  *out++ = ' ';
251  }
252  internal::copy(formatting.postfix, out);
253 
254  return ctx.out();
255  }
256 };
257 
258 template <typename T>
259 struct is_range {
260  static FMT_CONSTEXPR_DECL const bool value =
262 };
263 
264 template <typename RangeT, typename Char>
265 struct formatter<RangeT, Char,
266  typename std::enable_if<fmt::is_range<RangeT>::value>::type> {
267 
269 
270  template <typename ParseContext>
271  FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
272  return formatting.parse(ctx);
273  }
274 
275  template <typename FormatContext>
276  typename FormatContext::iterator format(
277  const RangeT &values, FormatContext &ctx) {
278  auto out = ctx.out();
279  internal::copy(formatting.prefix, out);
280  std::size_t i = 0;
281  for (auto it = values.begin(), end = values.end(); it != end; ++it) {
282  if (i > 0) {
283  if (formatting.add_prepostfix_space) {
284  *out++ = ' ';
285  }
286  internal::copy(formatting.delimiter, out);
287  }
288  format_to(out,
290  (formatting.add_delimiter_spaces && i > 0), *it),
291  *it);
292  if (++i > formatting.range_length_limit) {
293  format_to(out, " ... <other elements>");
294  break;
295  }
296  }
297  if (formatting.add_prepostfix_space) {
298  *out++ = ' ';
299  }
300  internal::copy(formatting.postfix, out);
301  return ctx.out();
302  }
303 };
304 
306 
307 #endif // FMT_RANGES_H_
308 
static FMT_CONSTEXPR_DECL const std::size_t range_length_limit
Definition: ranges.h:35
Char delimiter
Definition: ranges.h:48
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin())
Definition: ranges.h:28
std::shared_ptr< logger > get(const std::string &name)
Definition: spdlog.h:53
auto format(const TupleT &values, FormatContext &ctx) -> decltype(ctx.out())
Definition: ranges.h:243
static FMT_CONSTEXPR_DECL const bool add_prepostfix_space
Definition: ranges.h:52
std::add_rvalue_reference< T >::type declval() FMT_NOEXCEPT
#define FMT_CONSTEXPR_DECL
Definition: core.h:70
If we are still before C++14, supply the fodder for doing the "indices trick".
Definition: format.h:297
void for_each(Tuple &&tup, F &&f)
Definition: ranges.h:166
#define FMT_END_NAMESPACE
Definition: core.h:153
formatting_tuple()
Definition: ranges.h:50
void for_each(index_sequence< Is... >, Tuple &&tup, F &&f) FMT_NOEXCEPT
Definition: ranges.h:154
FMT_CONSTEXPR auto end(const C &c) -> decltype(c.end())
Definition: format.h:257
static FMT_CONSTEXPR_DECL const bool add_prepostfix_space
Definition: ranges.h:42
Char postfix
Definition: ranges.h:39
Return true value if T has std::string interface, like std::string_view.
Definition: ranges.h:78
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin())
Definition: ranges.h:238
static FMT_CONSTEXPR std::size_t size()
Definition: ranges.h:136
std::enable_if< is_contiguous< Container >::value &&internal::is_string< S >::value, std::back_insert_iterator< Container > >::type format_to(std::back_insert_iterator< Container > out, const S &format_str, const Args &... args)
Definition: core.h:1430
tuple_size and tuple_element check.
Definition: ranges.h:110
FormatContext::iterator format(const RangeT &values, FormatContext &ctx)
Definition: ranges.h:276
FMT_CONSTEXPR const wchar_t * format_str_quoted(bool add_space, const wchar_t)
Definition: ranges.h:195
#define FMT_RANGE_OUTPUT_LENGTH_LIMIT
Definition: ranges.h:20
#define FMT_NULL
Definition: core.h:107
FMT_CONSTEXPR const char * format_str_quoted(bool add_space, const Arg &, typename std::enable_if< !is_like_std_string< typename std::decay< Arg >::type >::value >::type *=nullptr)
Definition: ranges.h:172
void copy(const RangeT &range, OutputIterator out)
Definition: ranges.h:58
#define FMT_CONSTEXPR
Definition: core.h:69
static FMT_CONSTEXPR_DECL const bool add_delimiter_spaces
Definition: ranges.h:41
formatting_range()
Definition: ranges.h:40
Char delimiter
Definition: ranges.h:38
FMT_CONSTEXPR make_index_sequence< std::tuple_size< T >::value > get_indexes(T const &)
Definition: ranges.h:163
#define FMT_NOEXCEPT
Definition: core.h:140
Definition: bin_to_hex.h:69
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin())
Definition: ranges.h:271
void copy(char ch, OutputIterator out)
Definition: ranges.h:72
Char postfix
Definition: ranges.h:49
type
Definition: core.h:530