1
0
mirror of synced 2024-12-04 20:17:57 +01:00
ImHex/lib/libimhex/source/data_processor/node.cpp
paxcut c577a42f62
impr: Refactoring of floating point tool to decrease code complexity. (#1275)
I noticed the bad score on code factor so I reorganized it to make it
more readable and maintainable. In order to break down the big function
into it much smaller parts I encapsulated all the variables that the
functions need to access in two classes, one for the imgui related
statics and the other for non-static variables.

When writing the smaller functions I was noticed that there was room to
simplify the existing algorithms by writing functions that could be
called by parts that previously shared no code. I tested the changes the
same way I tested the original and it seems to work the same way but
maybe a bit faster. Although it may be possible to further optimize the
present code code factor no longer flags the function at all.
2023-09-20 10:48:44 +02:00

157 lines
5.3 KiB
C++

#include <hex/data_processor/node.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/api/localization.hpp>
#include <hex/providers/provider.hpp>
namespace hex::dp {
namespace {
int s_idCounter = 1;
}
Node::Node(std::string unlocalizedTitle, std::vector<Attribute> attributes) : m_id(s_idCounter++), m_unlocalizedTitle(std::move(unlocalizedTitle)), m_attributes(std::move(attributes)) {
for (auto &attr : this->m_attributes)
attr.setParentNode(this);
}
const std::vector<u8>& Node::getBufferOnInput(u32 index) {
auto attribute = this->getConnectedInputAttribute(index);
if (attribute == nullptr)
throwNodeError(hex::format("Nothing connected to input '{0}'", LangEntry(this->m_attributes[index].getUnlocalizedName())));
if (attribute->getType() != Attribute::Type::Buffer)
throwNodeError("Tried to read buffer from non-buffer attribute");
markInputProcessed(index);
attribute->getParentNode()->process();
auto &outputData = attribute->getOutputData();
if (outputData.empty())
throwNodeError("No data available at connected attribute");
return outputData;
}
const i128& Node::getIntegerOnInput(u32 index) {
auto attribute = this->getConnectedInputAttribute(index);
auto &outputData = [&] -> std::vector<u8>& {
if (attribute != nullptr) {
if (attribute->getType() != Attribute::Type::Integer)
throwNodeError("Tried to read integer from non-integer attribute");
markInputProcessed(index);
attribute->getParentNode()->process();
return attribute->getOutputData();
} else {
return this->getAttribute(index).getOutputData();
}
}();
if (outputData.empty())
throwNodeError("No data available at connected attribute");
if (outputData.size() < sizeof(i128))
throwNodeError("Not enough data provided for integer");
return *reinterpret_cast<i128 *>(outputData.data());
}
const double& Node::getFloatOnInput(u32 index) {
auto attribute = this->getConnectedInputAttribute(index);
auto &outputData = [&] -> std::vector<u8>& {
if (attribute != nullptr) {
if (attribute->getType() != Attribute::Type::Float)
throwNodeError("Tried to read integer from non-float attribute");
markInputProcessed(index);
attribute->getParentNode()->process();
return attribute->getOutputData();
} else {
return this->getAttribute(index).getOutputData();
}
}();
if (outputData.empty())
throwNodeError("No data available at connected attribute");
if (outputData.size() < sizeof(double))
throwNodeError("Not enough data provided for float");
return *reinterpret_cast<double *>(outputData.data());
}
void Node::setBufferOnOutput(u32 index, std::span<const u8> data) {
if (index >= this->getAttributes().size())
throwNodeError("Attribute index out of bounds!");
auto &attribute = this->getAttributes()[index];
if (attribute.getIOType() != Attribute::IOType::Out)
throwNodeError("Tried to set output data of an input attribute!");
if (attribute.getType() != Attribute::Type::Buffer)
throwNodeError("Tried to set buffer on non-buffer attribute!");
attribute.getOutputData() = { data.begin(), data.end() };
}
void Node::setIntegerOnOutput(u32 index, i128 integer) {
if (index >= this->getAttributes().size())
throwNodeError("Attribute index out of bounds!");
auto &attribute = this->getAttributes()[index];
if (attribute.getIOType() != Attribute::IOType::Out)
throwNodeError("Tried to set output data of an input attribute!");
if (attribute.getType() != Attribute::Type::Integer)
throwNodeError("Tried to set integer on non-integer attribute!");
std::vector<u8> buffer(sizeof(integer), 0);
std::memcpy(buffer.data(), &integer, sizeof(integer));
attribute.getOutputData() = buffer;
}
void Node::setFloatOnOutput(u32 index, double floatingPoint) {
if (index >= this->getAttributes().size())
throwNodeError("Attribute index out of bounds!");
auto &attribute = this->getAttributes()[index];
if (attribute.getIOType() != Attribute::IOType::Out)
throwNodeError("Tried to set output data of an input attribute!");
if (attribute.getType() != Attribute::Type::Float)
throwNodeError("Tried to set float on non-float attribute!");
std::vector<u8> buffer(sizeof(floatingPoint), 0);
std::memcpy(buffer.data(), &floatingPoint, sizeof(floatingPoint));
attribute.getOutputData() = buffer;
}
void Node::setOverlayData(u64 address, const std::vector<u8> &data) {
if (this->m_overlay == nullptr)
throwNodeError("Tried setting overlay data on a node that's not the end of a chain!");
this->m_overlay->setAddress(address);
this->m_overlay->getData() = data;
}
void Node::setIdCounter(int id) {
if (id > s_idCounter)
s_idCounter = id;
}
}