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 {
|
|
|
|
if (destination->getTypeName() == getTypeName()) {
|
2015-09-08 21:52:34 +00:00
|
|
|
((IntNode *)destination)->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");
|
|
|
|
const IntDiff *int_diff = (const IntDiff *)diff;
|
|
|
|
|
|
|
|
double previous = backward ? int_diff->getNewValue() : int_diff->getOldValue();
|
|
|
|
double next = backward ? int_diff->getOldValue() : int_diff->getNewValue();
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
if (value == previous) {
|
2015-09-08 21:52:34 +00:00
|
|
|
value = next;
|
|
|
|
return true;
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2015-12-13 19:08:38 +00:00
|
|
|
Logs::error("Definition") << "Can't apply int diff " << previous << " => " << next << " to " << getName()
|
|
|
|
<< endl;
|
2015-09-08 21:52:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|