GCC Code Coverage Report


Directory: gridformat/
File: gridformat/common/indentation.hpp
Date: 2024-11-10 16:24:00
Exec Total Coverage
Lines: 15 15 100.0%
Functions: 10 10 100.0%
Branches: 5 10 50.0%

Line Branch Exec Source
1 // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2 // SPDX-License-Identifier: MIT
3 /*!
4 * \file
5 * \ingroup Common
6 * \copydoc GridFormat::Indentation.
7 */
8 #ifndef GRIDFORMAT_COMMON_INDENTATION_HPP_
9 #define GRIDFORMAT_COMMON_INDENTATION_HPP_
10
11 #include <string>
12 #include <ostream>
13
14 namespace GridFormat {
15
16 /*!
17 * \ingroup Common
18 * \brief Helper class for formatting output with indentations.
19 */
20 class Indentation {
21 public:
22 struct Options {
23 unsigned int width = 4;
24 unsigned int level = 0;
25 };
26
27 1 Indentation() : Indentation(Options{}) {}
28 9909 explicit Indentation(Options opts)
29
1/2
✓ Branch 1 taken 9909 times.
✗ Branch 2 not taken.
19818 : _width{std::string(opts.width, ' ')}
30
1/2
✓ Branch 1 taken 9909 times.
✗ Branch 2 not taken.
19818 , _indent{std::string(opts.width*opts.level, ' ')}
31 9909 {}
32
33 42 const std::string& get() const { return _indent; }
34
35 88478 Indentation& operator++() { push_(); return *this; }
36 88478 Indentation& operator--() { pop_(); return *this; }
37
38
1/2
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 Indentation operator++(int) { auto cpy = *this; ++(*this); return cpy; }
39
1/2
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 Indentation operator--(int) { auto cpy = *this; --(*this); return cpy; }
40
41 306035 friend std::ostream& operator<<(std::ostream& s, const Indentation& i) {
42 306035 s << i._indent;
43 306035 return s;
44 }
45
46 private:
47 88478 void push_() { _indent += _width; }
48
1/2
✓ Branch 4 taken 88478 times.
✗ Branch 5 not taken.
88478 void pop_() { _indent.erase(std::max(_indent.length() - _width.length(), std::size_t{0})); }
49
50 std::string _width;
51 std::string _indent;
52 };
53
54 } // end namespace GridFormat
55
56 #endif // GRIDFORMAT_COMMON_INDENTATION_HPP_
57