SeqAn3  3.0.0
The Modern C++ library for sequence analysis.
debug_stream.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2019, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2019, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
15 #include <ios>
16 #include <ostream>
17 #include <variant>
18 
26 #include <seqan3/std/ranges>
27 
28 namespace seqan3
29 {
30 
31 // ------------------------------------------------------------------
32 // seqan3::fmtflags2
33 // ------------------------------------------------------------------
34 
38 {
39  none = 0,
40  utf8 = 1,
42  default_ = small_int_as_number
44 };
45 
47 template <>
49 
50 // ------------------------------------------------------------------
51 // seqan3::debug_stream_type
52 // ------------------------------------------------------------------
53 
79 {
80 public:
85  debug_stream_type() = default;
86  debug_stream_type(debug_stream_type const &) = default;
87  debug_stream_type(debug_stream_type &&) = default;
88  debug_stream_type & operator= (debug_stream_type const &) = default;
90  ~debug_stream_type() = default;
91 
93  constexpr explicit debug_stream_type(std::ostream & out) : stream{&out}
94  {}
96 
118  {
119  stream = &out;
120  }
122 
126  template <typename t>
129  {
130  *stream << v;
131  return *this;
132  }
133 
136  {
137  *stream << fp;
138  return *this;
139  }
140 
142  debug_stream_type & operator<<(int8_t const v)
143  {
145  *stream << static_cast<int>(v);
146  else
147  *stream << v;
148  return *this;
149  }
150 
151  debug_stream_type & operator<<(uint8_t const v)
152  {
154  *stream << static_cast<unsigned>(v);
155  else
156  *stream << v;
157  return *this;
158  }
161 
166  std::ios_base::fmtflags flags() const
168  {
169  return stream->flags();
170  }
171 
173  std::ios_base::fmtflags flags(std::ios_base::fmtflags const flgs)
174  {
175  return stream->flags(flgs);
176  }
177 
179  void setf(std::ios_base::fmtflags const flag)
180  {
181  stream->setf(flag);
182  }
183 
185  void unsetf(std::ios_base::fmtflags const flag)
186  {
187  stream->unsetf(flag);
188  }
189 
191  debug_stream_type & operator<<(std::ios_base::fmtflags const flag)
192  {
193  setf(flag);
194  return *this;
195  }
197 
202  fmtflags2 flags2() const
204  {
205  return flgs2;
206  }
207 
210  {
211  flgs2 = flgs;
212  return flgs2;
213  }
214 
216  void setf(fmtflags2 const flag)
217  {
218  flgs2 |= flag;
219  }
220 
222  void unsetf(fmtflags2 const flag)
223  {
224  flgs2 &= ~flag;
225  }
226 
229  {
230  setf(flag);
231  return *this;
232  }
234 
235 private:
237  std::ostream *stream = &std::cerr;
238 
240  fmtflags2 flgs2{fmtflags2::default_};
241 };
242 
243 // ------------------------------------------------------------------
244 // seqan3::debug_stream
245 // ------------------------------------------------------------------
246 
250 
251 // ------------------------------------------------------------------
252 // overloads
253 // ------------------------------------------------------------------
254 
264 template <Alphabet alphabet_t>
265 inline debug_stream_type & operator<<(debug_stream_type & s, alphabet_t const l)
269 {
270  return s << to_char(l);
271 }
272 
273 }
274 
275 namespace seqan3::detail
276 {
277 
279 template<typename tuple_t, std::size_t ...I>
280 void print_tuple(debug_stream_type & s, tuple_t && t, std::index_sequence<I...> const &)
281 {
282  s << '(';
283  ((s << (I == 0 ? "" : ",") << std::get<I>(t)), ...);
284  s << ')';
285 }
286 
287 } // namespace seqan3::detail
288 
289 namespace seqan3
290 {
291 
298 template <typename tuple_t>
301  !Alphabet<tuple_t> && // exclude alphabet_tuple_base
302  TupleLike<remove_cvref_t<tuple_t>>
304 inline debug_stream_type & operator<<(debug_stream_type & s, tuple_t && t)
305 {
306  detail::print_tuple(s, std::forward<tuple_t>(t),
308  return s;
309 }
310 
321 template <typename variant_type>
323  requires detail::is_type_specialisation_of_v<remove_cvref_t<variant_type>, std::variant>
325 inline debug_stream_type & operator<<(debug_stream_type & s, variant_type && v)
326 {
327  if (!v.valueless_by_exception())
328  std::visit([&s] (auto && arg) {s << arg;}, v);
329  else
330  s << "<VALUELESS_VARIANT>";
331  return s;
332 }
333 
340 template <typename optional_type>
342  requires detail::is_type_specialisation_of_v<remove_cvref_t<optional_type>, std::optional>
344 inline debug_stream_type & operator<<(debug_stream_type & s, optional_type && arg)
345 {
346  if (arg.has_value())
347  s << *arg;
348  else
349  s << "<VALUELESS_OPTIONAL>";
350  return s;
351 }
352 
367 template <std::ranges::InputRange rng_t>
370  requires !std::Same<remove_cvref_t<reference_t<rng_t>>, remove_cvref_t<rng_t>> && // prevent recursive instantiation
371  requires (reference_t<rng_t> l) { { debug_stream << l }; } &&
372  // exclude null-terminated strings:
373  !(std::is_pointer_v<std::decay_t<rng_t>> &&
376 {
377  if constexpr (Alphabet<reference_t<rng_t>> &&
378  !detail::is_uint_adaptation_v<remove_cvref_t<reference_t<rng_t>>>)
379  {
380  for (auto && l : r)
381  s << l;
382  }
383  else
384  {
385  s << '[';
386  auto b = begin(r);
387  auto e = end(r);
388  if (b != e)
389  {
390  s << *b;
391  ++b;
392  }
393  while (b != e)
394  {
395  s << ',';
396  s << *b;
397  ++b;
398  }
399  s << ']';
400  }
401 
402  return s;
403 }
404 
406 
407 } // namespace seqan3
Provides seqan3::add_enum_bitwise_operators.
The generic alphabet concept that covers most data types used in ranges.
T setf(T... args)
T visit(T... args)
constexpr debug_stream_type(std::ostream &out)
Construction from an output stream.
Definition: debug_stream.hpp:93
debug_stream_type & operator<<(debug_stream_type &s, variant_type &&v)
A std::variant can be printed by visiting the stream operator for the corresponding type...
Definition: debug_stream.hpp:325
fmtflags2 flags2(fmtflags2 flgs)
Replace the current flags on the stream with the given argument.
Definition: debug_stream.hpp:209
Definition: debug_stream.hpp:41
Enables use of non-ASCII UTF8 characters in formatted output.
Definition: debug_stream.hpp:40
constexpr auto to_char
Return the char representation of an alphabet object.
Definition: concept.hpp:285
~debug_stream_type()=default
Defaulted.
Provides various shortcuts for common std::ranges functions.
void setf(std::ios_base::fmtflags const flag)
Set the format flag(s) on the stream (current flags are ORed with the argument).
Definition: debug_stream.hpp:179
debug_stream_type & operator<<(debug_stream_type &s, alphabet_t const l)
All alphabets can be printed to the seqan3::debug_stream by their char representation.
Definition: debug_stream.hpp:265
debug_stream_type & operator<<(debug_stream_type &s, rng_t &&r)
All input ranges can be printed to the seqan3::debug_stream element-wise (if their elements are print...
Definition: debug_stream.hpp:368
void set_underlying_stream(std::ostream &out)
Change the underlying output stream.
Definition: debug_stream.hpp:117
Provides alphabet adaptations for standard uint types.
No flag is set.
Definition: debug_stream.hpp:39
The main SeqAn3 namespace.
fmtflags2 flags2() const
Retrieve the format flags from the stream.
Definition: debug_stream.hpp:203
debug_stream_type & operator<<(std::ios_base::fmtflags const flag)
Set the format flag(s) on the stream (current flags are ORed with the argument).
Definition: debug_stream.hpp:191
T flags(T... args)
Provides seqan3::TupleLike.
debug_stream_type & operator<<(std::ostream &(*fp)(std::ostream &))
This overloads enables forwarding std::endl and other manipulators.
Definition: debug_stream.hpp:135
debug_stream_type debug_stream
A global instance of seqan3::debug_stream_type.
Definition: debug_stream.hpp:249
Adaptations of concepts from the Ranges TS.
void unsetf(fmtflags2 const flag)
Unset the format flag(s) on the stream.
Definition: debug_stream.hpp:222
std::ios_base::fmtflags flags() const
Retrieve the format flags from the stream.
Definition: debug_stream.hpp:167
::ranges::begin begin
Alias for ranges::begin. Returns an iterator to the beginning of a range.
Definition: ranges:174
debug_stream_type & operator<<(fmtflags2 const flag)
Set the format flag(s) on the stream (current flags are ORed with the argument).
Definition: debug_stream.hpp:228
std::ios_base::fmtflags flags(std::ios_base::fmtflags const flgs)
Replace the current flags on the stream with the given argument.
Definition: debug_stream.hpp:173
T tuple_size_v
void setf(fmtflags2 const flag)
Set the format flag(s) on the stream (current flags are ORed with the argument).
Definition: debug_stream.hpp:216
debug_stream_type & operator<<(t &&v)
Forwards to the underlying stream object.
Definition: debug_stream.hpp:128
fmtflags2
Flags that change the behaviour of the seqan3::debug_stream.
Definition: debug_stream.hpp:37
Definition: aligned_sequence_concept.hpp:35
Concept for output streams.
Stream concepts.
debug_stream_type & operator<<(debug_stream_type &s, tuple_t &&t)
All tuples can be printed by printing their elements separately.
Definition: debug_stream.hpp:304
debug_stream_type()=default
Defaulted.
Specifies requirements of a Range type for which begin returns a type that models std::InputIterator...
constexpr bool add_enum_bitwise_operators< fmtflags2 >
Overload bitwise operators for seqan3::fmtflags2.
Definition: debug_stream.hpp:48
Core alphabet concept and free function/type trait wrappers.
debug_stream_type & operator<<(debug_stream_type &s, optional_type &&arg)
A std::optional can be printed by printing its value or nothing if valueless.
Definition: debug_stream.hpp:344
debug_stream_type & operator=(debug_stream_type const &)=default
Defaulted.
Provides various transformation traits used by the range module.
typename reference< t >::type reference_t
Shortcut for seqan3::reference (TransformationTrait shortcut).
Definition: pre.hpp:77
T unsetf(T... args)
The concept std::Same<T, U> is satisfied if and only if T and U denote the same type.
A "pretty printer" for most SeqAn data structures and related types.
Definition: debug_stream.hpp:78
::ranges::end end
Alias for ranges::end. Returns an iterator to the end of a range.
Definition: ranges:179
void unsetf(std::ios_base::fmtflags const flag)
Unset the format flag(s) on the stream.
Definition: debug_stream.hpp:185