2015-09-08 21:52:34 +00:00
|
|
|
#include "IntNode.h"
|
|
|
|
|
|
|
|
#include "PackStream.h"
|
|
|
|
#include "IntDiff.h"
|
|
|
|
#include "Logs.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include <cassert>
|
|
|
|
|
2015-12-10 23:36:50 +00:00
|
|
|
IntNode::IntNode(DefinitionNode *parent, const string &name, int value)
|
2015-11-09 21:30:46 +00:00
|
|
|
: DefinitionNode(parent, name, "int"), value(value) {
|
2015-09-08 21:52:34 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 23:36:50 +00:00
|
|
|
string IntNode::toString(int indent) const {
|
|
|
|
ostringstream stream;
|
2015-09-08 21:52:34 +00:00
|
|
|
|
|
|
|
stream << DefinitionNode::toString(indent) << " " << value;
|
|
|
|
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void IntNode::save(PackStream *stream) const {
|
2015-09-08 21:52:34 +00:00
|
|
|
stream->write(&value);
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void IntNode::load(PackStream *stream) {
|
2015-09-08 21:52:34 +00:00
|
|
|
stream->read(&value);
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void IntNode::copy(DefinitionNode *destination) const {
|
2015-12-16 00:32:25 +00:00
|
|
|
if (auto tdest = dynamic_cast<IntNode *>(destination)) {
|
|
|
|
tdest->value = value;
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2015-12-13 19:08:38 +00:00
|
|
|
Logs::error("Definition") << "Can't copy from " << getTypeName() << " to " << destination->getTypeName()
|
|
|
|
<< endl;
|
2015-09-08 21:52:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void IntNode::setValue(int new_value) {
|
2015-09-08 21:52:34 +00:00
|
|
|
addDiff(produceDiff(new_value));
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
const IntDiff *IntNode::produceDiff(int new_value) const {
|
2015-09-08 21:52:34 +00:00
|
|
|
return new IntDiff(this, value, new_value);
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:36:50 +00:00
|
|
|
void IntNode::generateInitDiffs(vector<const DefinitionDiff *> *diffs) const {
|
2015-09-08 21:52:34 +00:00
|
|
|
diffs->push_back(produceDiff(value));
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
bool IntNode::applyDiff(const DefinitionDiff *diff, bool backward) {
|
|
|
|
if (!DefinitionNode::applyDiff(diff, backward)) {
|
2015-09-08 21:52:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(diff->getTypeName() == "int");
|
2015-12-16 00:32:25 +00:00
|
|
|
auto int_diff = dynamic_cast<const IntDiff *>(diff);
|
2015-09-08 21:52:34 +00:00
|
|
|
|
2015-12-16 00:32:25 +00:00
|
|
|
if (int_diff) {
|
|
|
|
int previous = backward ? int_diff->getNewValue() : int_diff->getOldValue();
|
|
|
|
int next = backward ? int_diff->getOldValue() : int_diff->getNewValue();
|
2015-09-08 21:52:34 +00:00
|
|
|
|
2015-12-16 00:32:25 +00:00
|
|
|
if (value == previous) {
|
|
|
|
value = next;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
Logs::error("Definition") << "Can't apply int diff " << previous << " => " << next << " to " << getName()
|
|
|
|
<< endl;
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2015-12-16 00:32:25 +00:00
|
|
|
Logs::error("Could not cast DefinitionDiff to IntDiff");
|
2015-09-08 21:52:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|