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.
This commit is contained in:
parent
ad69ac84b1
commit
c577a42f62
@ -73,11 +73,11 @@ namespace hex::dp {
|
||||
|
||||
const std::vector<u8>& getBufferOnInput(u32 index);
|
||||
const i128& getIntegerOnInput(u32 index);
|
||||
const long double& getFloatOnInput(u32 index);
|
||||
const double& getFloatOnInput(u32 index);
|
||||
|
||||
void setBufferOnOutput(u32 index, std::span<const u8> data);
|
||||
void setIntegerOnOutput(u32 index, i128 integer);
|
||||
void setFloatOnOutput(u32 index, long double floatingPoint);
|
||||
void setFloatOnOutput(u32 index, double floatingPoint);
|
||||
|
||||
private:
|
||||
int m_id;
|
||||
|
@ -64,7 +64,7 @@ namespace hex::dp {
|
||||
return *reinterpret_cast<i128 *>(outputData.data());
|
||||
}
|
||||
|
||||
const long double& Node::getFloatOnInput(u32 index) {
|
||||
const double& Node::getFloatOnInput(u32 index) {
|
||||
auto attribute = this->getConnectedInputAttribute(index);
|
||||
|
||||
auto &outputData = [&] -> std::vector<u8>& {
|
||||
@ -84,10 +84,10 @@ namespace hex::dp {
|
||||
if (outputData.empty())
|
||||
throwNodeError("No data available at connected attribute");
|
||||
|
||||
if (outputData.size() < sizeof(long double))
|
||||
if (outputData.size() < sizeof(double))
|
||||
throwNodeError("Not enough data provided for float");
|
||||
|
||||
return *reinterpret_cast<long double *>(outputData.data());
|
||||
return *reinterpret_cast<double *>(outputData.data());
|
||||
}
|
||||
|
||||
void Node::setBufferOnOutput(u32 index, std::span<const u8> data) {
|
||||
@ -123,7 +123,7 @@ namespace hex::dp {
|
||||
attribute.getOutputData() = buffer;
|
||||
}
|
||||
|
||||
void Node::setFloatOnOutput(u32 index, long double floatingPoint) {
|
||||
void Node::setFloatOnOutput(u32 index, double floatingPoint) {
|
||||
if (index >= this->getAttributes().size())
|
||||
throwNodeError("Attribute index out of bounds!");
|
||||
|
||||
|
@ -563,7 +563,7 @@ namespace hex::plugin::builtin {
|
||||
void process() override {
|
||||
const auto &input = this->getBufferOnInput(0);
|
||||
|
||||
float output = 0;
|
||||
double output = 0;
|
||||
if (input.empty() || input.size() != sizeof(output))
|
||||
throwNodeError("Buffer is empty or not the right size to fit a float");
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user