-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDebuggingTipsSamples.cpp
More file actions
216 lines (173 loc) · 4.18 KB
/
Copy pathDebuggingTipsSamples.cpp
File metadata and controls
216 lines (173 loc) · 4.18 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <iostream>
#include <stdarg.h>
#include <stdio.h>
#include <windows.h>
#include <string>
#include <vector>
#include <memory>
#include <utility>
#include <optional>
#define MY_TRACE(msg, ...) MyTraceImpl(__LINE__, __FILE__, msg, __VA_ARGS__)
// implementation us
void MyTraceImpl(int line, const char *fileName, const char *msg, ...)
{
va_list args;
char buffer[256] = { 0 };
sprintf_s(buffer, "%s(%d) : ", fileName, line);
OutputDebugString(buffer);
// retrieve the variable arguments
va_start(args, msg);
vsprintf_s(buffer, msg, args);
OutputDebugString(buffer);
va_end(args);
}
class SimpleParam
{
public:
SimpleParam(std::string str, int val) : mStr(std::move(str)), mVal(val)
{
Init();
}
void Init() { }
std::string GetStrVal() const { return mStr + std::to_string(mVal); }
private:
std::string mStr;
int mVal{ 0 };
};
void CallBackFunction(SimpleParam p1, SimpleParam p2)
{
std::cout << p1.GetStrVal() << "\n";
std::cout << p2.GetStrVal() << "\n";
}
void MyFunc(const std::string &one, const std::string &two)
{
auto res = one + two;
std::cout << res << "\n";
}
void MethodToFix()
{
static bool bEnableFix = true;
if (bEnableFix)
{
std::cout << "fixed...\n";
}
else
{
std::cout << "Old bug...\n";
}
}
class VertexBase
{
public:
void AddVertex(VertexBase* pVtx) { m_vecNeighbours.push_back(pVtx); }
virtual bool IsMapVertex() const { return false; }
protected:
std::vector<VertexBase*> m_vecNeighbours; // observe only
int m_flags{ 0 };
double m_weight{ 1.0 };
};
class MapVertex : public VertexBase
{
public:
MapVertex() = default;
explicit MapVertex(std::string name) : m_name(std::move(name)) { }
void SetName(const std::string& name) { m_name = name; }
std::string GetName() const { return m_name; }
bool IsMapVertex() const override { return true; }
protected:
double m_range{ 0.0 };
std::string m_name;
};
class MySpecialVertex : public MapVertex
{
public:
MySpecialVertex() { }
MySpecialVertex(const std::string& name) { SetName(name); }
#ifdef _DEBUG
void UpdateSurroundingNames() {
m_vecSurroundingNames.clear();
for (const auto& v : m_vecNeighbours) {
if (v->IsMapVertex())
m_vecSurroundingNames.push_back(static_cast<const MapVertex *>(v)->GetName());
}
}
#endif
private:
int m_val{ 0 };
int m_size{ 0 };
#ifdef _DEBUG
std::vector<std::string> m_vecSurroundingNames;
#endif
};
void ScanRectangle(RECT& r)
{
if (r.right - r.left > 100)
{
r.left = 100;
r.right = 90;
}
}
void ScanForInvalidRects(const std::vector<RECT>& rects)
{
int i = 0;
for (const auto &r : rects)
{
if (r.right < r.left || r.top < r.bottom)
MY_TRACE("invalid rectangle %d! \t - l:%d t:%d r:%d b:%d\n", i, r.left, r.top, r.right, r.bottom);
++i;
}
}
bool CheckRect(const RECT& r)
{
return r.right - r.left > 100;
}
void DebugRectLoop()
{
std::vector<RECT> rects(100);
for (auto &r : rects)
{
r.top = 100;
r.bottom = 0;
r.left = 10;
r.right = 90;
}
// make a few larger
// left, top, right, bottom
rects[7] = { 10, 100, 200, 0 };
rects[17] = { 10, 100, 200, 0 };
rects[37] = { 10, 100, 200, 0 };
ScanForInvalidRects(rects);
for (auto &r : rects)
{
ScanRectangle(r);
}
ScanForInvalidRects(rects);
}
int main()
{
OutputDebugString("DebuggingTipsSamples.cpp(32) : super\n");
MY_TRACE("Hello World %d\n", 5);
MyFunc("Hello ", "World");
int a = 0;
// run method 5 times, try to change `bEnableFix` inside while debugging
for (int i = 0; i < 5; ++i)
MethodToFix();
SimpleParam input{ "number", 42 };
CallBackFunction({ "Hello", 1 }, { "World", 2 });
auto myVertex = std::make_shared<MySpecialVertex>("Cracow");
/*myVertex->AddVertex(std::make_shared<MySpecialVertex>("London"));
myVertex->AddVertex(std::make_shared<MySpecialVertex>("Berlin"));
myVertex->AddVertex(std::make_shared<MySpecialVertex>("Paric"));
myVertex->AddVertex(std::make_shared<MySpecialVertex>("Warsaw"));*/
#ifdef _DEBUG
myVertex->UpdateSurroundingNames();
//myVertex->AddVertex(std::make_shared<MySpecialVertex>("New York"));
#endif // _DEBUG
DebugRectLoop();
std::pair firstpair{ 10, 42 };
std::pair secondpair{ 100.5, 42.5 };
std::optional optOne{ 42 };
std::optional<std::string> optTwo{};
std::vector vec{ 1, 2, 3, 4, 5 };
return 0;
}