2015-08-13 21:46:50 +00:00
|
|
|
#include "FloatNode.h"
|
|
|
|
|
|
|
|
#include "PackStream.h"
|
2015-08-16 21:01:56 +00:00
|
|
|
#include "FloatDiff.h"
|
|
|
|
#include "Logs.h"
|
2015-08-13 21:46:50 +00:00
|
|
|
#include <sstream>
|
2015-08-16 21:01:56 +00:00
|
|
|
#include <cassert>
|
2015-08-13 21:46:50 +00:00
|
|
|
|
2015-12-10 23:36:50 +00:00
|
|
|
FloatNode::FloatNode(DefinitionNode *parent, const string &name, double value)
|
2015-11-09 21:30:46 +00:00
|
|
|
: DefinitionNode(parent, name, "float"), value(value) {
|
2015-08-13 21:46:50 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 23:36:50 +00:00
|
|
|
string FloatNode::toString(int indent) const {
|
|
|
|
ostringstream stream;
|
2015-08-13 21:46:50 +00:00
|
|
|
|
|
|
|
stream << DefinitionNode::toString(indent) << " " << value;
|
|
|
|
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void FloatNode::save(PackStream *stream) const {
|
2015-08-13 21:46:50 +00:00
|
|
|
stream->write(&value);
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void FloatNode::load(PackStream *stream) {
|
2015-08-13 21:46:50 +00:00
|
|
|
stream->read(&value);
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void FloatNode::copy(DefinitionNode *destination) const {
|
|
|
|
if (destination->getTypeName() == getTypeName()) {
|
2015-08-16 21:01:56 +00:00
|
|
|
((FloatNode *)destination)->value = value;
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2015-12-13 18:56:00 +00:00
|
|
|
Logs::error() << "[Definition] Can't copy from " << getTypeName() << " to " << destination->getTypeName() << endl;
|
2015-08-16 21:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void FloatNode::setValue(double new_value) {
|
2015-08-16 22:29:54 +00:00
|
|
|
addDiff(produceDiff(new_value));
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
const FloatDiff *FloatNode::produceDiff(double new_value) const {
|
2015-08-16 22:29:54 +00:00
|
|
|
return new FloatDiff(this, value, new_value);
|
2015-08-16 21:01:56 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 23:36:50 +00:00
|
|
|
void FloatNode::generateInitDiffs(vector<const DefinitionDiff *> *diffs) const {
|
2015-08-17 20:55:30 +00:00
|
|
|
diffs->push_back(produceDiff(value));
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
bool FloatNode::applyDiff(const DefinitionDiff *diff, bool backward) {
|
|
|
|
if (!DefinitionNode::applyDiff(diff, backward)) {
|
2015-08-16 21:01:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(diff->getTypeName() == "float");
|
|
|
|
const FloatDiff *float_diff = (const FloatDiff *)diff;
|
|
|
|
|
|
|
|
double previous = backward ? float_diff->getNewValue() : float_diff->getOldValue();
|
|
|
|
double next = backward ? float_diff->getOldValue() : float_diff->getNewValue();
|
2015-08-13 21:46:50 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
if (value == previous) {
|
2015-08-16 21:01:56 +00:00
|
|
|
value = next;
|
|
|
|
return true;
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2015-12-13 18:56:00 +00:00
|
|
|
Logs::error() << "[Definition] Can't apply float diff " << previous << " => " << next << " to " << getName() << endl;
|
2015-08-16 21:01:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-13 21:46:50 +00:00
|
|
|
}
|
2015-10-15 15:52:03 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void FloatNode::addValue(double added) {
|
2015-10-15 15:52:03 +00:00
|
|
|
setValue(value + added);
|
|
|
|
}
|