-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinstrumented.h
69 lines (62 loc) · 1.63 KB
/
instrumented.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
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
#ifndef INSTRUMENTED_H
#define INSTRUMENTED_H
#include <cstddef>
struct instrumented_base
{
enum operations {
n, copy, assignment, destructor, default_constructor, equality, comparison, construction
};
static const size_t number_ops = 8;
static double counts[number_ops];
static const char* counter_names[number_ops];
static void initialize(size_t);
};
template <typename T>
// T is Semiregualr or Regular or TotallyOrdered
struct instrumented : instrumented_base
{
typedef T value_type;
T value;
// Conversions from T and to T:
explicit instrumented(const T& x) : value(x) { ++counts[construction]; }
// Semiregular:
instrumented(const instrumented& x) : value(x.value) {
++counts[copy];
}
instrumented() { ++counts[default_constructor]; }
~instrumented() { ++counts[destructor]; }
instrumented& operator=(const instrumented& x) {
++counts[assignment];
value = x.value;
return *this;
}
// Regular
friend
bool operator==(const instrumented& x, const instrumented& y) {
++counts[equality];
return x.value == y.value;
}
friend
bool operator!=(const instrumented& x, const instrumented& y) {
return !(x == y);
}
// TotallyOrdered
friend
bool operator<(const instrumented& x, const instrumented& y) {
++counts[comparison];
return x.value < y.value;
}
friend
bool operator>(const instrumented& x, const instrumented& y) {
return y < x;
}
friend
bool operator<=(const instrumented& x, const instrumented& y) {
return !(y < x);
}
friend
bool operator>=(const instrumented& x, const instrumented& y) {
return !(x < y);
}
};
#endif