1 #pragma once
2 
3 /// \file RecordType.h
4 /// \brief Helper type tracking the number of copies, moves, etc.
5 /// \author Pavel Sevecek (sevecek at sirrah.troja.mff.cuni.cz)
6 /// \date 2016-2021
7 
8 #include "objects/Object.h"
9 #include <ostream>
10 
11 NAMESPACE_SPH_BEGIN
12 
13 struct RecordType {
14     bool wasMoved = false;
15     bool wasMoveConstructed = false;
16     bool wasCopyConstructed = false;
17     bool wasMoveAssigned = false;
18     bool wasCopyAssigned = false;
19     bool wasDefaultConstructed = false;
20     bool wasValueConstructed = false;
21     bool wasSwapped = false;
22 
23     static int constructedNum;
24     static int destructedNum;
25 
resetStatsRecordType26     static void resetStats() {
27         constructedNum = 0;
28         destructedNum = 0;
29     }
30 
existingNumRecordType31     static int existingNum() {
32         return constructedNum - destructedNum;
33     }
34 
35     int value = -1;
36 
RecordTypeRecordType37     RecordType() {
38         wasDefaultConstructed = true;
39         constructedNum++;
40     }
41 
~RecordTypeRecordType42     ~RecordType() {
43         destructedNum++;
44     }
45 
RecordTypeRecordType46     RecordType(const int value)
47         : value(value) {
48         wasValueConstructed = true;
49         constructedNum++;
50     }
51 
RecordTypeRecordType52     RecordType(const RecordType& other) {
53         wasCopyConstructed = true;
54         value = other.value;
55         constructedNum++;
56     }
57 
RecordTypeRecordType58     RecordType(RecordType&& other) {
59         wasMoveConstructed = true;
60         other.wasMoved = true;
61         value = other.value;
62         constructedNum++;
63     }
64 
65     RecordType& operator=(const RecordType& other) {
66         wasCopyAssigned = true;
67         value = other.value;
68         return *this;
69     }
70 
71     RecordType& operator=(RecordType&& other) {
72         wasMoveAssigned = true;
73         other.wasMoved = true;
74         value = other.value;
75         return *this;
76     }
77 
78     bool operator==(const RecordType& other) const {
79         return value == other.value;
80     }
81 
82     bool operator!=(const RecordType& other) const {
83         return value != other.value;
84     }
85 
86     bool operator<(const RecordType& other) const {
87         return value < other.value;
88     }
89 
90     friend std::ostream& operator<<(std::ostream& ofs, const RecordType& r) {
91         ofs << r.value;
92         return ofs;
93     }
94 };
95 
96 template <typename T>
97 struct IsRecordType {
98     static constexpr bool value = false;
99 };
100 template <>
101 struct IsRecordType<RecordType> {
102     static constexpr bool value = true;
103 };
104 
105 NAMESPACE_SPH_END
106 
107 namespace std {
108     template <>
109     inline void swap(Sph::RecordType& r1, Sph::RecordType& r2) {
110         swap(r1.value, r2.value);
111         r1.wasSwapped = true;
112         r2.wasSwapped = true;
113     }
114 } // namespace std
115