Skip to content

Commit 7d2ad02

Browse files
Improve error handling & debug info:
+ New function `debug_info`
1 parent 13a582f commit 7d2ad02

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

src/front_end_utils/include/utils.hxx

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
#include <qstringview.h>
1010
#include <qwidget.h>
1111

12+
#include <algorithm>
1213
#include <initializer_list>
1314
#include <type_traits>
15+
#include <utility>
1416
#include <vector>
1517

1618
#include "error_types.hxx"
1719

1820
using std::abort;
21+
using std::for_each;
22+
using std::forward;
1923
using std::initializer_list;
2024
using std::is_same_v;
2125
using std::stringstream;
@@ -29,15 +33,14 @@ using std::source_location;
2933
#endif
3034

3135
namespace Lazyboard::front_end_utils {
32-
3336
template <typename T>
3437
concept is_qcolor = requires(const T& t) { QColor(t); };
3538

3639
template <is_qcolor... Args>
37-
inline constexpr bool is_valid_hex_color(const Args&... args) noexcept {
40+
inline constexpr bool is_valid_hex_color(Args&&... args) noexcept {
3841
return (
3942
[&]() {
40-
QColor color(args);
43+
QColor color(std::forward<Args>(args));
4144

4245
if (!color.isValid()) {
4346
return false;
@@ -49,13 +52,13 @@ inline constexpr bool is_valid_hex_color(const Args&... args) noexcept {
4952
}
5053

5154
using init_list = initializer_list<uint8_t>;
52-
inline QIcon image_from_bytes(const init_list& data) noexcept {
55+
inline QIcon image_from_bytes(const init_list&& data) noexcept {
5356
QByteArray bytes_array;
5457
bytes_array.reserve(static_cast<int>(data.size()));
5558

56-
for (auto byte : data) {
57-
bytes_array.append(static_cast<char>(byte));
58-
}
59+
for_each(data.begin(), data.end(), [&bytes_array](auto bytes) {
60+
bytes_array.append(static_cast<char>(bytes));
61+
});
5962

6063
QPixmap pixmap;
6164
if (pixmap.loadFromData(bytes_array)) {
@@ -74,20 +77,31 @@ inline void error_dialog_show(QWidget* parent,
7477
}
7578

7679
#if defined(LAZY_DEBUG)
80+
const auto green = "\x1b[32m";
81+
const auto white = "\x1b[37m";
82+
7783
using src_loc = source_location;
7884
template <typename T>
7985
inline void dump_ptr_address(T* t,
8086
const src_loc& location = src_loc::current()) {
81-
auto green = "\x1b[32m";
82-
auto white = "\x1b[37m";
83-
8487
auto file_name = location.file_name();
8588
auto file_line = location.line();
8689

8790
println("{}[MEMORY_DEBUG]{} File: {}", green, white, file_name);
8891
println("{}[MEMORY_DEBUG]{} Line: {}", green, white, file_line);
8992
println("{}[MEMORY_DEBUG]{} {}", green, white, static_cast<void*>(t));
9093
}
94+
95+
template <typename T>
96+
concept string_bound = requires(const T& t) { string(t); };
97+
98+
template <typename string_bound>
99+
inline void debug_info(string_bound&& information) {
100+
auto info = forward<string_bound>(information);
101+
102+
println("{}[INFO_DEBUG]{} {}", green, white, info);
103+
}
104+
91105
#endif
92106

93107
} // namespace Lazyboard::front_end_utils

0 commit comments

Comments
 (0)