SeqAn3
debug_stream.hpp
Go to the documentation of this file.
1 // ============================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ============================================================================
4 //
5 // Copyright (c) 2006-2018, Knut Reinert & Freie Universitaet Berlin
6 // Copyright (c) 2016-2018, Knut Reinert & MPI Molekulare Genetik
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // * Neither the name of Knut Reinert or the FU Berlin nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
25 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 //
33 // ============================================================================
34 
40 #pragma once
41 
42 #include <ios>
43 #include <ostream>
44 
52 #include <seqan3/std/ranges>
53 
54 namespace seqan3
55 {
56 
57 // ------------------------------------------------------------------
58 // seqan3::fmtflags2
59 // ------------------------------------------------------------------
60 
64 {
65  none = 0,
66  utf8 = 1,
68  default_ = small_int_as_number
70 };
71 
73 template <>
75 
76 // ------------------------------------------------------------------
77 // seqan3::debug_stream_type
78 // ------------------------------------------------------------------
79 
105 {
106 public:
111  debug_stream_type() = default;
112  debug_stream_type(debug_stream_type const &) = default;
113  debug_stream_type(debug_stream_type &&) = default;
114  debug_stream_type & operator= (debug_stream_type const &) = default;
115  debug_stream_type & operator= (debug_stream_type &&) = default;
116  ~debug_stream_type() = default;
117 
119  constexpr explicit debug_stream_type(std::ostream & out) : stream{&out}
120  {}
122 
143  void set_underlying_stream(std::ostream & out)
144  {
145  stream = &out;
146  }
148 
152  template <typename t>
155  {
156  *stream << v;
157  return *this;
158  }
159 
161  debug_stream_type & operator<<(std::ostream&(*fp)(std::ostream&))
162  {
163  *stream << fp;
164  return *this;
165  }
166 
168  debug_stream_type & operator<<(int8_t const v)
169  {
171  *stream << static_cast<int>(v);
172  else
173  *stream << v;
174  return *this;
175  }
176 
177  debug_stream_type & operator<<(uint8_t const v)
178  {
180  *stream << static_cast<unsigned>(v);
181  else
182  *stream << v;
183  return *this;
184  }
187 
192  std::ios_base::fmtflags flags() const
194  {
195  return stream->flags();
196  }
197 
199  std::ios_base::fmtflags flags(std::ios_base::fmtflags const flgs)
200  {
201  return stream->flags(flgs);
202  }
203 
205  void setf(std::ios_base::fmtflags const flag)
206  {
207  stream->setf(flag);
208  }
209 
211  void unsetf(std::ios_base::fmtflags const flag)
212  {
213  stream->unsetf(flag);
214  }
215 
217  debug_stream_type & operator<<(std::ios_base::fmtflags const flag)
218  {
219  setf(flag);
220  return *this;
221  }
223 
228  fmtflags2 flags2() const
230  {
231  return flgs2;
232  }
233 
236  {
237  flgs2 = flgs;
238  return flgs2;
239  }
240 
242  void setf(fmtflags2 const flag)
243  {
244  flgs2 |= flag;
245  }
246 
248  void unsetf(fmtflags2 const flag)
249  {
250  flgs2 &= ~flag;
251  }
252 
255  {
256  setf(flag);
257  return *this;
258  }
260 
261 private:
263  std::ostream *stream = &std::cerr;
264 
266  fmtflags2 flgs2{fmtflags2::default_};
267 };
268 
269 // ------------------------------------------------------------------
270 // seqan3::debug_stream
271 // ------------------------------------------------------------------
272 
276 
277 // ------------------------------------------------------------------
278 // overloads
279 // ------------------------------------------------------------------
280 
290 template <alphabet_concept alphabet_t>
291 inline debug_stream_type & operator<<(debug_stream_type & s, alphabet_t const l)
295 {
296  return s << to_char(l);
297 }
298 
299 }
300 
301 namespace seqan3::detail
302 {
303 
305 template<typename tuple_t, std::size_t ...I>
306 void print_tuple(debug_stream_type & s, tuple_t && t, std::index_sequence<I...> const &)
307 {
308  s << '(';
309  ((s << (I == 0 ? "" : ",") << std::get<I>(t)), ...);
310  s << ')';
311 }
312 
313 } // namespace seqan3::detail
314 
315 namespace seqan3
316 {
317 
324 template <typename tuple_t>
327  !alphabet_concept<remove_cvref_t<tuple_t>> && // exclude cartesian_composition
330 inline debug_stream_type & operator<<(debug_stream_type & s, tuple_t && t)
331 {
332  detail::print_tuple(s, std::forward<tuple_t>(t),
333  std::make_index_sequence<std::tuple_size_v<remove_cvref_t<tuple_t>>>{});
334  return s;
335 }
336 
351 template <std::ranges::InputRange rng_t>
354  requires !std::Same<remove_cvref_t<reference_t<rng_t>>, remove_cvref_t<rng_t>> && // prevent recursive instantiation
355  requires (reference_t<rng_t> l) { { debug_stream << l }; } &&
356  // exclude null-terminated strings:
357  !(std::is_pointer_v<std::decay_t<rng_t>> &&
360 {
362  !detail::is_uint_adaptation_v<remove_cvref_t<reference_t<rng_t>>>)
363  {
364  for (auto && l : r)
365  s << l;
366  }
367  else
368  {
369  s << '[';
370  auto b = begin(r);
371  auto e = end(r);
372  if (b != e)
373  {
374  s << *b;
375  ++b;
376  }
377  while (b != e)
378  {
379  s << ',';
380  s << *b;
381  ++b;
382  }
383  s << ']';
384  }
385 
386  return s;
387 }
388 
390 
391 } // namespace seqan3
Provides seqan3::add_enum_bitwise_operators.
constexpr debug_stream_type(std::ostream &out)
Construction from an output stream.
Definition: debug_stream.hpp:119
fmtflags2 flags2(fmtflags2 flgs)
Replace the current flags on the stream with the given argument.
Definition: debug_stream.hpp:235
Definition: debug_stream.hpp:67
Enables use of non-ASCII UTF8 characters in formatted output.
Definition: debug_stream.hpp:66
Contains 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:205
Specifies requirements of a Range type for which begin returns a type that models std::InputIterator...
Whether a type behaves like a tuple.
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:291
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:352
void set_underlying_stream(std::ostream &out)
Change the underlying output stream.
Definition: debug_stream.hpp:143
Provides alphabet adaptations for standard uint types.
No flag is set.
Definition: debug_stream.hpp:65
The generic alphabet concept that covers most data types used in ranges.This is the core alphabet con...
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:58
fmtflags2 flags2() const
Retrieve the format flags from the stream.
Definition: debug_stream.hpp:229
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:217
Provides seqan3::tuple_like_concept.
debug_stream_type & operator<<(std::ostream &(*fp)(std::ostream &))
This overloads enables forwarding std::endl and other manipulators.
Definition: debug_stream.hpp:161
debug_stream_type debug_stream
A global instance of seqan3::debug_stream_type.
Definition: debug_stream.hpp:275
std::remove_cv_t< std::remove_reference_t< t > > remove_cvref_t
Return the input type with const, volatile and references removed [Type metafunction].
Definition: basic.hpp:64
Adaptations of concepts from the Ranges TS.
void unsetf(fmtflags2 const flag)
Unset the format flag(s) on the stream.
Definition: debug_stream.hpp:248
std::ios_base::fmtflags flags() const
Retrieve the format flags from the stream.
Definition: debug_stream.hpp:193
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:254
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:199
void setf(fmtflags2 const flag)
Set the format flag(s) on the stream (current flags are ORed with the argument).
Definition: debug_stream.hpp:242
The concept std::Same<T, U> is satisfied if and only if T and U denote the same type.
debug_stream_type & operator<<(t &&v)
Forwards to the underlying stream object.
Definition: debug_stream.hpp:154
fmtflags2
Flags that change the behaviour of the seqan3::debug_stream.
Definition: debug_stream.hpp:63
Definition: aligned_sequence_concept.hpp:288
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:330
constexpr bool add_enum_bitwise_operators< fmtflags2 >
Overload bitwise operators for seqan3::fmtflags2.
Definition: debug_stream.hpp:74
typename reference< t >::type reference_t
Type metafunction shortcut for seqan3::reference.
Definition: pre.hpp:98
Core alphabet concept and free function/metafunction wrappers.
Provides various metafunctions used by the range module.
Concept for output streams.
A "pretty printer" for most SeqAn data structures and related types.
Definition: debug_stream.hpp:104
constexpr underlying_char_t< alphabet_type > to_char(alphabet_type const alph) requires requires(alphabet_type alph)
Implementation of seqan3::alphabet_concept::to_char() that delegates to a member function.
Definition: member_exposure.hpp:165
void unsetf(std::ios_base::fmtflags const flag)
Unset the format flag(s) on the stream.
Definition: debug_stream.hpp:211