nngn
Loading...
Searching...
No Matches
debug.h
Go to the documentation of this file.
1#ifndef NNGN_DEBUG_H
2#define NNGN_DEBUG_H
3
4#include <ostream>
5#include <sstream>
6#include <utility>
7#include <vector>
8
9#include "graphics/graphics.h"
10#include "math/mat3.h"
11#include "math/mat4.h"
12
13namespace std {
14
15template<typename T0, typename T1>
16requires(requires (ostream &o, T0 t0, T1 t1) { o << t0; o << t1; })
17ostream &operator<<(ostream &o, const pair<T0, T1> &p) {
18 return o << '{' << p.first << ", " << p.second << '}';
19}
20
21template<typename T, size_t N>
22requires(requires (ostream &o, T t) { o << t; })
23ostream &operator<<(ostream &o, const array<T, N> &v) {
24 if(v.empty())
25 return o << "{}";
26 o << '{' << v[0];
27 for(auto b = cbegin(v) + 1, e = cend(v); b != e; ++b)
28 o << ", " << *b;
29 return o << '}';
30}
31
32template<typename T, typename A>
33requires(requires (ostream &o, T t) { o << t; })
34ostream &operator<<(ostream &o, const vector<T, A> &v) {
35 if(v.empty())
36 return o << "{}";
37 o << '{' << v[0];
38 for(auto b = cbegin(v) + 1, e = cend(v); b != e; ++b)
39 o << ", " << *b;
40 return o << '}';
41}
42
43}
44
45namespace nngn {
46
47struct Vertex;
48
49template<template<typename> typename V, typename T, std::size_t N>
50std::ostream &operator <<(std::ostream &os, const vec<V, T, N> &v);
51std::ostream &operator <<(std::ostream &os, const mat4 &m);
52std::ostream &operator <<(std::ostream &os, const Vertex &v);
53
54}
55
56namespace nngn {
57
58template<template<typename> typename V, typename T, std::size_t N>
59inline std::ostream &operator <<(std::ostream &os, const vec<V, T, N> &v) {
60 os << "{" << v[0];
61 for(std::size_t i = 1; i < N; ++i)
62 os << ", " << v[i];
63 return os << "}";
64}
65
66inline std::ostream &operator <<(std::ostream &os, const mat3 &m) {
67 return os << "{" << m[0] << ", " << m[1] << ", " << m[2] << "}";
68}
69
70inline std::ostream &operator <<(std::ostream &os, const mat4 &m) {
71 return os
72 << "{"
73 << m[0] << ", " << m[1] << ", "
74 << m[2] << ", " << m[3]
75 << "}";
76}
77
78inline std::ostream &operator <<(std::ostream &os, const Vertex &v) {
79 return os << "{" << v.pos << ", " << v.color << "}";
80}
81
82}
83
84template<typename T0, typename T1>
85inline std::string vdiff(const T0 &v0, const T1 &v1);
86
87template<typename T0, typename T1>
88inline std::string vdiff(const T0 &v0, const T1 &v1) {
89 using std::cbegin;
90 using std::cend;
91 const auto b0 = cbegin(v0), e0 = cend(v0);
92 const auto e1 = cend(v1);
93 auto p0 = b0;
94 auto p1 = cbegin(v1);
95 while(p0 != e0 && *p0 == *p1) { ++p0; ++p1; }
96 if(p0 == e0 && p1 == e1)
97 return "";
98 const auto i = p0 - b0;
99 std::stringstream s;
100 const auto fmt = [&s, i](const char *n, auto p, const auto e) {
101 s << "\n" << n << "[" << i << ":]:";
102 while(p != e)
103 s << " " << *p++;
104 };
105 fmt("v0", p0, e0);
106 fmt("v1", p1, e1);
107 s << '\n';
108 return s.str();
109}
110
111#endif
std::string vdiff(const T0 &v0, const T1 &v1)
Definition: debug.h:88
function fmt(name, size, n, max)
Definition: debug.h:45
std::ostream & operator<<(std::ostream &os, const vec< V, T, N > &v)
Definition: debug.h:59
Definition: debug.h:13
m
Definition: input.lua:19
v[1]
Definition: math.lua:19
Definition: graphics.h:97
Definition: mat3.h:12
Definition: vec.h:22
e
Definition: math.lua:4
std::chrono::seconds s
Definition: timing.cpp:6