nodes: Allow immediate values on integer and float inputs

Closes #427
This commit is contained in:
WerWolv
2023-02-12 17:33:53 +01:00
parent a59c17aa83
commit d084ec78e9
5 changed files with 161 additions and 111 deletions

View File

@@ -40,7 +40,16 @@ namespace hex::dp {
[[nodiscard]] Node *getParentNode() const { return this->m_parentNode; }
[[nodiscard]] std::optional<std::vector<u8>> &getOutputData() { return this->m_outputData; }
[[nodiscard]] std::vector<u8>& getOutputData() {
if (!this->m_outputData.empty())
return this->m_outputData;
else
return this->m_defaultData;
}
void clearOutputData() { this->m_outputData.clear(); }
[[nodiscard]] std::vector<u8>& getDefaultData() { return this->m_defaultData; }
static void setIdCounter(int id) {
if (id > Attribute::s_idCounter)
@@ -55,7 +64,8 @@ namespace hex::dp {
std::map<int, Attribute *> m_connectedAttributes;
Node *m_parentNode = nullptr;
std::optional<std::vector<u8>> m_outputData;
std::vector<u8> m_outputData;
std::vector<u8> m_defaultData;
friend class Node;
void setParentNode(Node *node) { this->m_parentNode = node; }

View File

@@ -53,7 +53,7 @@ namespace hex::dp {
void resetOutputData() {
for (auto &attribute : this->m_attributes)
attribute.getOutputData().reset();
attribute.clearOutputData();
}
void resetProcessedInputs() {
@@ -73,9 +73,9 @@ namespace hex::dp {
Node::s_idCounter = id;
}
std::vector<u8> getBufferOnInput(u32 index);
i128 getIntegerOnInput(u32 index);
long double getFloatOnInput(u32 index);
const std::vector<u8>& getBufferOnInput(u32 index);
const i128& getIntegerOnInput(u32 index);
const long double& getFloatOnInput(u32 index);
void setBufferOnOutput(u32 index, const std::vector<u8> &data);
void setIntegerOnOutput(u32 index, i128 integer);
@@ -91,11 +91,15 @@ namespace hex::dp {
static int s_idCounter;
Attribute *getConnectedInputAttribute(u32 index) {
Attribute& getAttribute(u32 index) {
if (index >= this->getAttributes().size())
throw std::runtime_error("Attribute index out of bounds!");
auto &connectedAttribute = this->getAttributes()[index].getConnectedAttributes();
return this->getAttributes()[index];
}
Attribute *getConnectedInputAttribute(u32 index) {
const auto &connectedAttribute = this->getAttribute(index).getConnectedAttributes();
if (connectedAttribute.empty())
return nullptr;

View File

@@ -14,7 +14,7 @@ namespace hex::dp {
attr.setParentNode(this);
}
std::vector<u8> Node::getBufferOnInput(u32 index) {
const std::vector<u8>& Node::getBufferOnInput(u32 index) {
auto attribute = this->getConnectedInputAttribute(index);
if (attribute == nullptr)
@@ -28,58 +28,62 @@ namespace hex::dp {
auto &outputData = attribute->getOutputData();
if (!outputData.has_value())
if (outputData.empty())
throwNodeError("No data available at connected attribute");
return outputData.value();
return outputData;
}
i128 Node::getIntegerOnInput(u32 index) {
const i128& Node::getIntegerOnInput(u32 index) {
auto attribute = this->getConnectedInputAttribute(index);
if (attribute == nullptr)
throwNodeError(hex::format("Nothing connected to input '{0}'", LangEntry(this->m_attributes[index].getUnlocalizedName())));
auto &outputData = [&] -> std::vector<u8>& {
if (attribute != nullptr) {
if (attribute->getType() != Attribute::Type::Integer)
throwNodeError("Tried to read integer from non-integer attribute");
if (attribute->getType() != Attribute::Type::Integer)
throwNodeError("Tried to read integer from non-integer attribute");
markInputProcessed(index);
attribute->getParentNode()->process();
markInputProcessed(index);
attribute->getParentNode()->process();
return attribute->getOutputData();
} else {
return this->getAttribute(index).getOutputData();
}
}();
auto &outputData = attribute->getOutputData();
if (!outputData.has_value())
if (outputData.empty())
throwNodeError("No data available at connected attribute");
if (outputData->size() < sizeof(u64))
if (outputData.size() < sizeof(i128))
throwNodeError("Not enough data provided for integer");
return *reinterpret_cast<i64 *>(outputData->data());
return *reinterpret_cast<i128 *>(outputData.data());
}
long double Node::getFloatOnInput(u32 index) {
const long double& Node::getFloatOnInput(u32 index) {
auto attribute = this->getConnectedInputAttribute(index);
if (attribute == nullptr)
throwNodeError(hex::format("Nothing connected to input '{0}'", LangEntry(this->m_attributes[index].getUnlocalizedName())));
auto &outputData = [&] -> std::vector<u8>& {
if (attribute != nullptr) {
if (attribute->getType() != Attribute::Type::Float)
throwNodeError("Tried to read integer from non-float attribute");
if (attribute->getType() != Attribute::Type::Float)
throwNodeError("Tried to read float from non-float attribute");
markInputProcessed(index);
attribute->getParentNode()->process();
markInputProcessed(index);
attribute->getParentNode()->process();
return attribute->getOutputData();
} else {
return this->getAttribute(index).getOutputData();
}
}();
auto &outputData = attribute->getOutputData();
if (!outputData.has_value())
if (outputData.empty())
throwNodeError("No data available at connected attribute");
if (outputData->size() < sizeof(long double))
if (outputData.size() < sizeof(long double))
throwNodeError("Not enough data provided for float");
long double result = 0;
std::memcpy(&result, outputData->data(), sizeof(long double));
return result;
return *reinterpret_cast<long double *>(outputData.data());
}
void Node::setBufferOnOutput(u32 index, const std::vector<u8> &data) {