-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathPrintHelper.h
39 lines (33 loc) · 1.25 KB
/
PrintHelper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once
#include <bitset>
#include <iostream>
#include <iomanip>
#include <string>
/*
* NOTE: You don't need to understand the print helpers here.
* Their purpose is to show each expression, the type it evaluates to, and the value it evaluates to.
* Please go back to the main file now. :-)
*/
#ifdef _MSC_VER
std::string demangle(std::string_view input) { return std::string{input}; }
#else
#include <cxxabi.h>
std::string demangle(std::string_view input) {
int status;
return abi::__cxa_demangle(input.data(), NULL, NULL, &status);
}
#endif
// This helper prints type and value of an expression
void printWithTypeInfo(std::string expression, auto const & t, bool useBitset = false) {
const auto & ti = typeid(t);
const std::string realname = demangle(ti.name());
std::cout << std::left << std::setw(30) << expression << " type=" << std::setw(20) << realname << "value=";
if (useBitset) {
std::cout << "0b" << std::bitset<16>(t) << "\n";
} else {
std::cout << std::setprecision(25) << t << "\n";
}
}
// This macro both prints and evaluates an expression:
#define print(A) printWithTypeInfo("Line " + std::to_string(__LINE__) + ": "#A, A);
#define printBinary(A) printWithTypeInfo("Line " + std::to_string(__LINE__) + ": "#A, A, true);