From 624b80de20eecfcaa06142c0d5c2fb6ee5c707fb Mon Sep 17 00:00:00 2001 From: Fabian Reister <fabian.reister@kit.edu> Date: Tue, 30 May 2023 13:29:26 +0200 Subject: [PATCH] aron code generation: replacing c-style bool conversion --- .../codewriter/cpp/generator/Generator.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/Generator.cpp b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/Generator.cpp index d3c61320d..0b5a021d1 100644 --- a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/Generator.cpp +++ b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/Generator.cpp @@ -598,12 +598,22 @@ namespace armarx::aron::codegenerator::cpp CppBlockPtr Generator::resolveMaybeEqualsBlock(const CppBlockPtr& block_if_data, const std::string& accessor, const std::string& otherInstanceAccessor) const { const auto& type = getType(); - if (type.getMaybe() == type::Maybe::OPTIONAL || type.getMaybe() == type::Maybe::RAW_PTR || type.getMaybe() == type::Maybe::SHARED_PTR || type.getMaybe() == type::Maybe::UNIQUE_PTR) + if (type.getMaybe() == type::Maybe::RAW_PTR || type.getMaybe() == type::Maybe::SHARED_PTR || type.getMaybe() == type::Maybe::UNIQUE_PTR) + { + CppBlockPtr b = std::make_shared<CppBlock>(); + b->addLine("if (not (static_cast<bool>(" + accessor + ") == static_cast<bool>(" + otherInstanceAccessor + "))) // check if both contain data"); + b->addLineAsBlock("return false;"); + b->addLine("if (static_cast<bool>(" + accessor + ") && static_cast<bool>(" + otherInstanceAccessor + "))"); + b->addBlock(block_if_data); + return b; + } + + if (type.getMaybe() == type::Maybe::OPTIONAL) { CppBlockPtr b = std::make_shared<CppBlock>(); - b->addLine("if (not ((bool) " + accessor + " == (bool) " + otherInstanceAccessor + ")) // check if both contain data"); + b->addLine("if (not ( " + accessor + ".has_value() == " + otherInstanceAccessor + ".has_value())) // check if both contain data"); b->addLineAsBlock("return false;"); - b->addLine("if ((bool) " + accessor + " && (bool) " + otherInstanceAccessor + ")"); + b->addLine("if ( " + accessor + ".has_value() && " + otherInstanceAccessor + ".has_value())"); b->addBlock(block_if_data); return b; } -- GitLab