Dune Core Modules (unstable)

indent.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_COMMON_INDENT_HH
6#define DUNE_COMMON_INDENT_HH
7
8#include <ostream>
9#include <string>
10
11namespace Dune {
22
52 class Indent
53 {
54 const Indent* parent;
55 std::string basic_indent;
56 unsigned level;
57
58 public:
60
63 inline Indent(const std::string& basic_indent_ = " ", unsigned level_ = 0)
64 : parent(0), basic_indent(basic_indent_), level(level_)
65 { }
66
68 inline Indent(unsigned level_)
69 : parent(0), basic_indent(" "), level(level_)
70 { }
71
73
76 inline Indent(const Indent* parent_,
77 const std::string& basic_indent_ = " ", unsigned level_ = 1)
78 : parent(parent_), basic_indent(basic_indent_), level(level_)
79 { }
80
82 inline Indent(const Indent* parent_, unsigned level_)
83 : parent(parent_), basic_indent(" "), level(level_)
84 { }
85
87 inline Indent operator+(const std::string& newindent) const {
88 return Indent(this, newindent);
89 }
91 inline Indent operator+(unsigned morelevel) const {
92 return Indent(parent, basic_indent, level+morelevel);
93 }
95 inline Indent& operator++() { ++level; return *this; }
97 inline Indent& operator--() { if ( level > 0 ) --level; return *this; }
98
100 friend inline std::ostream& operator<<(std::ostream& s,
101 const Indent& indent);
102 };
103
105 inline std::ostream& operator<<(std::ostream& s, const Indent& indent) {
106 if(indent.parent)
107 s << *indent.parent;
108 for(unsigned i = 0; i < indent.level; ++i)
109 s << indent.basic_indent;
110 return s;
111 }
112
115} // namespace Dune
116
117#endif // DUNE_COMMON_INDENT_HH
Utility class for handling nested indentation in output.
Definition: indent.hh:53
Indent operator+(const std::string &newindent) const
create new indentation object with this one as parent
Definition: indent.hh:87
Indent(unsigned level_)
setup without parent and basic_indentation of two spaces
Definition: indent.hh:68
Indent operator+(unsigned morelevel) const
create a copy of this indentation object with raised level
Definition: indent.hh:91
Indent(const Indent *parent_, unsigned level_)
setup with parent
Definition: indent.hh:82
Indent & operator++()
raise indentation level
Definition: indent.hh:95
Indent(const std::string &basic_indent_=" ", unsigned level_=0)
setup without parent
Definition: indent.hh:63
Indent & operator--()
lower indentation level
Definition: indent.hh:97
friend std::ostream & operator<<(std::ostream &s, const Indent &indent)
write indentation to a stream
Definition: indent.hh:105
Indent(const Indent *parent_, const std::string &basic_indent_=" ", unsigned level_=1)
setup with parent
Definition: indent.hh:76
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)