UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Utils_fmt.hh
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*\
2 | |
3 | Copyright (C) 2022 |
4 | |
5 | , __ , __ |
6 | /|/ \ /|/ \ |
7 | | __/ _ ,_ | __/ _ ,_ |
8 | | \|/ / | | | | \|/ / | | | |
9 | |(__/|__/ |_/ \_/|/|(__/|__/ |_/ \_/|/ |
10 | /| /| |
11 | \| \| |
12 | |
13 | Enrico Bertolazzi |
14 | Dipartimento di Ingegneria Industriale |
15 | Università degli Studi di Trento |
16 | email: enrico.bertolazzi@unitn.it |
17 | |
18\*--------------------------------------------------------------------------*/
19
20//
21// file: Utils_fmt.hh
22//
23
24#pragma once
25
26#ifndef UTILS_FMT_dot_HH
27#define UTILS_FMT_dot_HH
28
29#if defined(__llvm__) || defined(__clang__)
30#pragma clang diagnostic ignored "-Wduplicate-enum"
31#endif
32
33#include "Utils.hh"
34
35#ifndef DOXYGEN_SHOULD_SKIP_THIS
36#include "Utils/3rd/fmt/printf.h"
37#include "Utils/3rd/fmt/chrono.h"
38#include "Utils/3rd/fmt/ostream.h"
39#include "Utils/3rd/fmt/color.h"
40#include "Utils/3rd/fmt/std.h"
41#endif
42
43#ifndef DOXYGEN_SHOULD_SKIP_THIS
44
45#include <string.h>
46#ifndef __FILENAME__
47 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr("/" __FILE__, '/') + 1 : __FILE__)
48#endif
49
50#ifndef UTILS_ERROR0
51 #define UTILS_ERROR0(MSG) \
52 throw Utils::Runtime_Error( MSG, __FILENAME__, __LINE__ )
53#endif
54
55#ifndef UTILS_ASSERT0
56 #define UTILS_ASSERT0(COND,MSG) if ( !(COND) ) UTILS_ERROR0( MSG )
57#endif
58
59#ifndef UTILS_WARNING0
60 #define UTILS_WARNING0(COND,MSG) if ( !(COND) ) std::cerr << MSG
61#endif
62
63#ifndef UTILS_ERROR
64 #define UTILS_ERROR(...) \
65 throw Utils::Runtime_Error( fmt::format(__VA_ARGS__), __FILENAME__, __LINE__ )
66#endif
67
68#ifndef UTILS_ASSERT
69 #define UTILS_ASSERT(COND,...) if ( !(COND) ) UTILS_ERROR( __VA_ARGS__ )
70#endif
71
72#ifndef UTILS_WARNING
73 #define UTILS_WARNING(COND,...) if ( !(COND) ) fmt::print( __VA_ARGS__ )
74#endif
75
76#ifdef UTILS_NO_DEBUG
77 #ifndef UTILS_ASSERT0_DEBUG
78 #define UTILS_ASSERT0_DEBUG(COND,MSG)
79 #endif
80 #ifndef UTILS_ASSERT_DEBUG
81 #define UTILS_ASSERT_DEBUG(COND,...)
82 #endif
83#else
84 #ifndef UTILS_ASSERT0_DEBUG
85 #define UTILS_ASSERT0_DEBUG(COND,MSG) UTILS_ASSERT0(COND,MSG)
86 #endif
87 #ifndef UTILS_ASSERT_DEBUG
88 #define UTILS_ASSERT_DEBUG(COND,...) UTILS_ASSERT(COND,__VA_ARGS__)
89 #endif
90#endif
91
92#endif
93
94#include <string>
95#include <string_view>
96
97namespace Utils {
98
99 using std::runtime_error;
100
118
119 class Runtime_Error : public runtime_error {
120 public:
132 explicit
134 string_view reason,
135 string_view file,
136 int line
137 )
138 : std::runtime_error( fmt::format( "\n{}\nOn File:{}:{}\n", reason, file, line ) )
139 { }
140
150 char const * what() const noexcept override;
151 };
152
153 inline string fmt_table_left_top() { return "┌"; };
154 inline string fmt_table_left_middle() { return "├"; };
155 inline string fmt_table_left_bottom() { return "└"; };
156
157 inline string fmt_table_middle_top() { return "┬"; };
158 inline string fmt_table_middle_middle() { return "┼"; };
159 inline string fmt_table_middle_bottom() { return "┴"; };
160
161 inline string fmt_table_right_top() { return "┐"; };
162 inline string fmt_table_right_middle() { return "┤"; };
163 inline string fmt_table_right_bottom() { return "┘"; };
164
165 inline string fmt_table_cross() { return "┼"; };
166
167 inline string fmt_table_bar() { return "─"; }
168 inline string fmt_table_vbar() { return "│"; }
169 inline string fmt_table_dot() { return "•"; }
170 inline string fmt_table_vdots() { return "⋮"; }
171
172 using std::string_view;
173
174 string fmt_table_row( unsigned width, string_view L, string_view R, string_view F, string_view title, string_view align );
175 string fmt_table_row( unsigned width, string_view L, string_view C, string_view R, string_view F, std::initializer_list<string_view> names, string_view align );
176 string fmt_table_row( unsigned width, string_view L, string_view C, string_view R, string_view F, unsigned N );
177
178 inline string fmt_table_row ( unsigned width, string_view title = "", string_view align = "^", string_view fill = " " ) { return fmt_table_row( width, "│", "│\n", fill, title, align ); }
179 inline string fmt_table_top_row ( unsigned width, string_view title = "", string_view align = "^", string_view fill = "─" ) { return fmt_table_row( width, "┌", "┐\n", fill, title, align ); }
180 inline string fmt_table_middle_row ( unsigned width, string_view title = "", string_view align = "^", string_view fill = "─" ) { return fmt_table_row( width, "├", "┤\n", fill, title, align ); }
181 inline string fmt_table_bottom_row ( unsigned width, string_view title = "", string_view align = "^", string_view fill = "─" ) { return fmt_table_row( width, "└", "┘\n", fill, title, align ); }
182
183 inline string fmt_table_row ( unsigned width, std::initializer_list<string_view> names, string_view align = "<", string_view fill = " " ) { return fmt_table_row( width, "│", "│", "│\n", fill, names, align ); }
184 inline string fmt_table_top_row ( unsigned width, std::initializer_list<string_view> names, string_view align = "<", string_view fill = "─" ) { return fmt_table_row( width, "┌", "─", "┐\n", fill, names, align ); }
185 inline string fmt_table_middle_row ( unsigned width, std::initializer_list<string_view> names, string_view align = "<", string_view fill = "─" ) { return fmt_table_row( width, "├", "┼", "┤\n", fill, names, align ); }
186 inline string fmt_table_bottom_row ( unsigned width, std::initializer_list<string_view> names, string_view align = "<", string_view fill = "─" ) { return fmt_table_row( width, "└", "─", "┘\n", fill, names, align ); }
187
188 inline string fmt_table_row ( unsigned width, unsigned N, string_view fill = " " ) { return fmt_table_row( width, "│", "│", "│\n", fill, N ); }
189 inline string fmt_table_top_row ( unsigned width, unsigned N, string_view fill = "─" ) { return fmt_table_row( width, "┌", "┬", "┐\n", fill, N ); }
190 inline string fmt_table_middle_row ( unsigned width, unsigned N, string_view fill = "─" ) { return fmt_table_row( width, "├", "┼", "┤\n", fill, N ); }
191 inline string fmt_table_bottom_row ( unsigned width, unsigned N, string_view fill = "─" ) { return fmt_table_row( width, "└", "┴", "┘\n", fill, N ); }
192
193}
194
195#endif
196
197//
198// EOF: Utils_fmt.hh
199//
Runtime_Error(string_view reason, string_view file, int line)
Constructs a Runtime_Error instance with a given reason.
Definition Utils_fmt.hh:133
char const * what() const noexcept override
Returns a C-style string describing the error.
Definition SystemUtils.cc:39
string fmt_table_bottom_row(unsigned width, string_view title="", string_view align="^", string_view fill="─")
Definition Utils_fmt.hh:181
string fmt_table_vdots()
Definition Utils_fmt.hh:170
string fmt_table_cross()
Definition Utils_fmt.hh:165
string fmt_table_middle_bottom()
Definition Utils_fmt.hh:159
string fmt_table_right_middle()
Definition Utils_fmt.hh:162
string fmt_table_row(unsigned const width, string_view const L, string_view const R, string_view const F, string_view const title, string_view const align)
Definition Utils_fmt.cc:29
string fmt_table_left_bottom()
Definition Utils_fmt.hh:155
string fmt_table_left_top()
Definition Utils_fmt.hh:153
string fmt_table_right_bottom()
Definition Utils_fmt.hh:163
string fmt_table_middle_middle()
Definition Utils_fmt.hh:158
string fmt_table_middle_top()
Definition Utils_fmt.hh:157
string fmt_table_middle_row(unsigned width, string_view title="", string_view align="^", string_view fill="─")
Definition Utils_fmt.hh:180
string fmt_table_right_top()
Definition Utils_fmt.hh:161
string fmt_table_bar()
Definition Utils_fmt.hh:167
string fmt_table_left_middle()
Definition Utils_fmt.hh:154
string fmt_table_top_row(unsigned width, string_view title="", string_view align="^", string_view fill="─")
Definition Utils_fmt.hh:179
string fmt_table_vbar()
Definition Utils_fmt.hh:168
string fmt_table_dot()
Definition Utils_fmt.hh:169