TRIQS/triqs_modest 3.3.0
Brillouin zone summation
Loading...
Searching...
No Matches
streams.hpp
Go to the documentation of this file.
1// Copyright (c) 2025--present, The Simons Foundation
2// This file is part of TRIQS/modest and is licensed under the terms of GPLv3 or later.
3// SPDX-License-Identifier: GPL-3.0-or-later
4// See LICENSE in the root of this distribution for details.
5
6#pragma once
7#include <ostream>
8#include <streambuf>
9#include <string>
10
11namespace triqs {
12
14
29
30 std::ostream *out_;
31 int verbosity_;
32
33 public:
39 ostream_with_verbosity(std::ostream &out, int verbosity = 1) : out_(&out), verbosity_(verbosity) {}
40
46 ostream_with_verbosity operator()(int n) const { return {*out_, verbosity_ - n + 1}; }
47
54 template <typename T> ostream_with_verbosity &operator<<(T const &value) {
55 if (verbosity_ > 0) *out_ << value;
56 return *this;
57 }
58
64 ostream_with_verbosity &operator<<(std::ostream &(*manip)(std::ostream &)) {
65 if (verbosity_ > 0) manip(*out_);
66 return *this;
67 }
68 };
69
70 // =============================================================================
71
88 class indented_ostream : public std::ostream {
89
90 class indented_streambuf : public std::streambuf {
91 std::streambuf *dest;
92 std::string head;
93 bool at_line_start = true;
94
95 public:
96 indented_streambuf(std::streambuf *dest, int indent) : dest(dest), head(indent, ' ') {}
97
98 protected:
99 int_type overflow(int_type c) override {
100 if (c == EOF) return !EOF;
101 if (at_line_start && c != '\n') dest->sputn(head.c_str(), long(head.size()));
102 at_line_start = (c == '\n');
103 return dest->sputc(c); //NOLINT
104 }
105 int sync() override { return dest->pubsync(); }
106 };
107 indented_streambuf buffer;
108
109 public:
116 indented_ostream(std::ostream &os, int indent) : std::ostream(&buffer), buffer(os.rdbuf(), indent) {}
117 };
118
119} // namespace triqs
A custom output stream that automatically indents each new line by a specified number of spaces.
Definition streams.hpp:88
indented_ostream(std::ostream &os, int indent)
Constructor.
Definition streams.hpp:116
FIXME : MERGE : replace triqs::report_steam, -> merge + alias.
Definition streams.hpp:28
ostream_with_verbosity & operator<<(T const &value)
Stream insertion operator for generic types.
Definition streams.hpp:54
ostream_with_verbosity & operator<<(std::ostream &(*manip)(std::ostream &))
Stream insertion for manipulators like std::endl.
Definition streams.hpp:64
ostream_with_verbosity(std::ostream &out, int verbosity=1)
Construct a stream_with_verbosity.
Definition streams.hpp:39
ostream_with_verbosity operator()(int n) const
Create a derived stream with adjusted verbosity.
Definition streams.hpp:46