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-11-09 21:30:46 +00:00
|
|
|
IntNode::IntNode(DefinitionNode *parent, const std::string &name, int value)
|
|
|
|
: DefinitionNode(parent, name, "int"), value(value) {
|
2015-09-08 21:52:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
std::string IntNode::toString(int indent) const {
|
2015-09-08 21:52:34 +00:00
|
|
|
std::ostringstream stream;
|
|
|
|
|
|
|
|
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-09-08 21:52:34 +00:00
|
|
|
Logs::error() << "Can't copy from " << getTypeName() << " to " << destination->getTypeName() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-09 21:30:46 +00:00
|
|
|
void IntNode::generateInitDiffs(std::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-09-08 21:52:34 +00:00
|
|
|
Logs::error() << "Can't apply int diff " << previous << " => " << next << " to " << getName() << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|