Skip to content

Commit

Permalink
Optional to reduce branching
Browse files Browse the repository at this point in the history
Signed-off-by: Krzysztof Rymski <[email protected]>
  • Loading branch information
Fireronin committed Jul 8, 2024
1 parent 20f4658 commit 4cbb9a8
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions Gems/ROS2/Code/Source/Lidar/ROS2LidarSensorComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,13 @@ namespace ROS2
};
};

auto HandleEntityId = [&message, &lastScanResults]() -> AZStd::function<unsigned char*(
auto HandleEntityId = [&message, &lastScanResults]() -> AZStd::optional<AZStd::function<unsigned char*(
unsigned char* pointBeginPointer,
const size_t pointIdx)>
const size_t pointIdx)>>
{
if (!lastScanResults.m_ids.has_value())
{
return [](unsigned char* pointBeginPointer, const size_t pointIdx)
{
return pointBeginPointer;
};
return {};
}

sensor_msgs::msg::PointField pfId;
Expand All @@ -229,14 +226,11 @@ namespace ROS2
};

auto HandleClassAndClassColor = [&message, &lastScanResults,this](
) -> AZStd::function<unsigned char*(unsigned char* pointBeginPointer, const size_t pointIdx)>
) -> AZStd::optional<AZStd::function<unsigned char*(unsigned char* pointBeginPointer, const size_t pointIdx)>>
{
if (!lastScanResults.m_classes.has_value())
{
return [](unsigned char* pointBeginPointer, const size_t pointIdx)
{
return pointBeginPointer;
};
return {};
}

sensor_msgs::msg::PointField pfClassId;
Expand Down Expand Up @@ -276,8 +270,14 @@ namespace ROS2
};

pointFillers.emplace_back(HandleXYZ());
pointFillers.emplace_back(HandleEntityId());
pointFillers.emplace_back(HandleClassAndClassColor());
if(auto pointFiller = HandleEntityId())
{
pointFillers.emplace_back(pointFiller.value());
}
if (auto pointFiller = HandleClassAndClassColor())
{
pointFillers.emplace_back(pointFiller.value());
}

const auto sizeInBytes = pointCount * message.point_step;
message.data.resize(sizeInBytes);
Expand Down

0 comments on commit 4cbb9a8

Please sign in to comment.