-
Notifications
You must be signed in to change notification settings - Fork 0
/
nomov.cpp
88 lines (74 loc) · 3.01 KB
/
nomov.cpp
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
#include "nomov.h"
void NoMov::init()
{
mSelfAddr = reinterpret_cast<uintptr_t>(this);
mObjID = mSelfAddr & 0x3ff; // Generates a 10-bit ID for this object
// Creates id string as S-031 for instance.
// The 2nd arg below formats number as fieldwidth 3, decimal, padded with 0.
mObjIDString = QString("%1-%2").arg(AddrClassifier::classify(mSelfAddr)).arg(mObjID, 3, 10, QLatin1Char('0'));
mpAllocMem = new uint32_t[ALLOC_SIZE]; // Allocate mem
mAllocAddr = reinterpret_cast<uintptr_t>(mpAllocMem);
}
NoMov::NoMov()
{
init();
std::fill_n(mpAllocMem, ALLOC_SIZE, mObjID); // Fill array with our id
qDebug("NoMov %s CTOR: Obj addr = %s; Alloc addr = %s, contents %d",
objStr(), selfStr(), allocStr(), mpAllocMem[0]);
}
NoMov::~NoMov()
{
qDebug("NoMov %s DTOR: Obj addr = %s; Alloc contents %d",
objStr(), selfStr(), mpAllocMem[0]);
delete [] mpAllocMem;
}
// Copy Ctor
NoMov::NoMov(const NoMov &o)
{
init();
// Deep copy from o (will get o's id!)
std::copy_n(o.mpAllocMem, ALLOC_SIZE, this->mpAllocMem);
qDebug("NoMov %s CC : Obj addr = %s; Alloc addr = %s, contents %d",
objStr(), selfStr(), allocStr(), mpAllocMem[0]);
}
// Copy Assignment Operator
NoMov &NoMov::operator=(const NoMov &that)
{
if (this == &that) {
qDebug("NoMov %s CAO : Obj addr = %s; Alloc addr = %s, contents %d",
objStr(), selfStr(), allocStr(), mpAllocMem[0]);
return *this; // Assigning to self
}
// This sequence copied from https://en.cppreference.com/w/cpp/language/rule_of_three
// Not really needed here since ALLOC_SIZE is constant in this contrived case,
// but relevant when new alloc size is different from old alloc size (e.g. in
// a string class).
uint32_t* newAlloc = new uint32_t[ALLOC_SIZE]; // Allocate new mem
std::copy_n(that.mpAllocMem, ALLOC_SIZE, newAlloc); // Copy from that (will get that's id!)
delete [] mpAllocMem; // Release original mem
mpAllocMem = newAlloc; // Reassign to new
qDebug("NoMov %s CAO : Obj addr = %s; Alloc addr = %s, contents %d",
objStr(), selfStr(), allocStr(), mpAllocMem[0]);
return *this;
}
// Retval is const to prevent it from being used as an lvalue. e.g.
// cannot say (nm1 + nm2) = nm3.
// https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp7_OperatorOverloading.html
const NoMov NoMov::operator+(const NoMov &rhs) const
{
qDebug("In NoMov::operator+; will add objID %d", rhs.mObjID);
NoMov ret;
// Add contents of rhs allocation --
// Note, iterator to last must be one-past-the-end
// Equivalent to the following:
// for (; first != last; ++first) {
// f(*first);
// }
// In the lambda, must capture vars by ref in order to access
// the rhs object.
std::for_each(ret.mpAllocMem, ret.mpAllocMem+ALLOC_SIZE,
[&](uint32_t& n){ n += rhs.mObjID; }
);
qDebug("NoMov::operator+ ret obj %d now has contents %d", ret.mObjID, ret.mpAllocMem[0]);
return ret;
}