Skip to content

Conversation

@M393
Copy link
Contributor

@M393 M393 commented Nov 12, 2025

Makes sense to use the enum type as a key without the need to cast to int type.

There are a few more maps using uint8_t as key, but the enum is of type int, not sure this should be changed:

const std::unordered_map<uint8_t, std::string> TripLeg_TravelMode_Strings{
{static_cast<uint8_t>(TravelMode::kDrive), "drive"},
{static_cast<uint8_t>(TravelMode::kPedestrian), "pedestrian"},
{static_cast<uint8_t>(TravelMode::kBicycle), "bicycle"},
{static_cast<uint8_t>(TravelMode::kTransit), "transit"},
};
inline std::string to_string(TravelMode travel_mode) {
auto i = TripLeg_TravelMode_Strings.find(static_cast<uint8_t>(travel_mode));

M393 added 3 commits November 12, 2025 17:32
The datatype for enum and key has already been the same for these.
For some the key type was int and the enum is without type which
should have defaulted to int.
This changes the key type from int to uint8_t.
This changes the key type from uint8_t to int8_t.
{static_cast<int>(Turn::Type::kSharpLeft), "sharp left"},
{static_cast<int>(Turn::Type::kLeft), "left"},
{static_cast<int>(Turn::Type::kSlightLeft), "slight left"}};
const std::unordered_map<Turn::Type, std::string>
Copy link
Member

@chrstnbwnkl chrstnbwnkl Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few examples here (like Turn::Type) which could even be turned into a constexpr std::array<const char*, <number of values>>. Turn type is an uint8_t but in practice we only allow 8 distinct values because we reserve 3 bits for it in DirectedEdge, so the number of values is static anyway.

Copy link
Contributor Author

@M393 M393 Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it can be a switch, returning a const std::string_view.

const std::string_view Turn::GetTypeString(Turn::Type turn_type) {
  switch (turn_type) {
    case Turn::Type::kStraight:
      return "straight";
    case Turn::Type::kSlightRight:
      return "slight right";
    case Turn::Type::kRight:
      return "right";
    case Turn::Type::kSharpRight:
      return "sharp right";
    case Turn::Type::kReverse:
      return "reverse";
    case Turn::Type::kSharpLeft:
      return "sharp left";
    case Turn::Type::kLeft:
      return "left";
    case Turn::Type::kSlightLeft:
      return "slight left";
  }
}

?

Copy link
Member

@chrstnbwnkl chrstnbwnkl Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also possible! We have examples for both the array and switch pattern in the code base, I don't have a strong opinion. Maybe the switch is more idiosyncratic here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants