Skip to content

Commit

Permalink
Merge branch 'refactor/routing-code-deduplication'
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoupey committed Nov 7, 2024
2 parents ae454d7 + 7473e94 commit 60decd5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 83 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Reduce complexity for recreation process (#1155)
- Refactor `SolutionIndicators` (#1169)
- Remove amount consistency checks in `parse` in favor of upstream checks in `Input` (#1086)
- Reduce code duplication in routing wrappers (#1184)

#### CI

Expand Down
26 changes: 21 additions & 5 deletions src/routing/http_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All rights reserved (see LICENSE).

#include "routing/wrapper.h"
#include "structures/typedefs.h"
#include "utils/helpers.h"

namespace vroom::routing {

Expand Down Expand Up @@ -55,20 +56,35 @@ class HttpWrapper : public Wrapper {
Matrices get_matrices(const std::vector<Location>& locs) const override;

virtual bool
duration_value_is_null(const rapidjson::Value& matrix_entry) const = 0;
duration_value_is_null(const rapidjson::Value& matrix_entry) const {
// Same implementation for both OSRM and ORS.
return matrix_entry.IsNull();
}

virtual bool
distance_value_is_null(const rapidjson::Value& matrix_entry) const = 0;
distance_value_is_null(const rapidjson::Value& matrix_entry) const {
// Same implementation for both OSRM and ORS.
return matrix_entry.IsNull();
}

virtual UserDuration
get_duration_value(const rapidjson::Value& matrix_entry) const = 0;
get_duration_value(const rapidjson::Value& matrix_entry) const {
// Same implementation for both OSRM and ORS.
return utils::round<UserDuration>(matrix_entry.GetDouble());
}

virtual UserDistance
get_distance_value(const rapidjson::Value& matrix_entry) const = 0;
get_distance_value(const rapidjson::Value& matrix_entry) const {
// Same implementation for both OSRM and ORS.
return utils::round<UserDistance>(matrix_entry.GetDouble());
}

virtual unsigned get_legs_number(const rapidjson::Value& result) const = 0;

virtual std::string get_geometry(rapidjson::Value& result) const = 0;
virtual std::string get_geometry(rapidjson::Value& result) const {
// Same implementation for both OSRM and ORS.
return result["routes"][0]["geometry"].GetString();
}

void add_geometry(Route& route) const override;
};
Expand Down
25 changes: 0 additions & 25 deletions src/routing/ors_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ All rights reserved (see LICENSE).
*/

#include "routing/ors_wrapper.h"
#include "utils/helpers.h"

namespace vroom::routing {

Expand Down Expand Up @@ -84,32 +83,8 @@ void OrsWrapper::check_response(const rapidjson::Document& json_result,
}
}

bool OrsWrapper::duration_value_is_null(
const rapidjson::Value& matrix_entry) const {
return matrix_entry.IsNull();
}

bool OrsWrapper::distance_value_is_null(
const rapidjson::Value& matrix_entry) const {
return matrix_entry.IsNull();
}

UserDuration
OrsWrapper::get_duration_value(const rapidjson::Value& matrix_entry) const {
return utils::round<UserDuration>(matrix_entry.GetDouble());
}

UserDistance
OrsWrapper::get_distance_value(const rapidjson::Value& matrix_entry) const {
return utils::round<UserDistance>(matrix_entry.GetDouble());
}

unsigned OrsWrapper::get_legs_number(const rapidjson::Value& result) const {
return result["routes"][0]["segments"].Size();
}

std::string OrsWrapper::get_geometry(rapidjson::Value& result) const {
return result["routes"][0]["geometry"].GetString();
}

} // namespace vroom::routing
14 changes: 0 additions & 14 deletions src/routing/ors_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,8 @@ class OrsWrapper : public HttpWrapper {
const std::vector<Location>& locs,
const std::string& service) const override;

bool
duration_value_is_null(const rapidjson::Value& matrix_entry) const override;

bool
distance_value_is_null(const rapidjson::Value& matrix_entry) const override;

UserDuration
get_duration_value(const rapidjson::Value& matrix_entry) const override;

UserDistance
get_distance_value(const rapidjson::Value& matrix_entry) const override;

unsigned get_legs_number(const rapidjson::Value& result) const override;

std::string get_geometry(rapidjson::Value& result) const override;

public:
OrsWrapper(const std::string& profile, const Server& server);
};
Expand Down
25 changes: 0 additions & 25 deletions src/routing/osrm_routed_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ All rights reserved (see LICENSE).
*/

#include "routing/osrm_routed_wrapper.h"
#include "utils/helpers.h"

namespace vroom::routing {

Expand Down Expand Up @@ -86,33 +85,9 @@ void OsrmRoutedWrapper::check_response(const rapidjson::Document& json_result,
}
}

bool OsrmRoutedWrapper::duration_value_is_null(
const rapidjson::Value& matrix_entry) const {
return matrix_entry.IsNull();
}

bool OsrmRoutedWrapper::distance_value_is_null(
const rapidjson::Value& matrix_entry) const {
return matrix_entry.IsNull();
}

UserDuration OsrmRoutedWrapper::get_duration_value(
const rapidjson::Value& matrix_entry) const {
return utils::round<UserDuration>(matrix_entry.GetDouble());
}

UserDistance OsrmRoutedWrapper::get_distance_value(
const rapidjson::Value& matrix_entry) const {
return utils::round<UserDistance>(matrix_entry.GetDouble());
}

unsigned
OsrmRoutedWrapper::get_legs_number(const rapidjson::Value& result) const {
return result["routes"][0]["legs"].Size();
}

std::string OsrmRoutedWrapper::get_geometry(rapidjson::Value& result) const {
return result["routes"][0]["geometry"].GetString();
}

} // namespace vroom::routing
14 changes: 0 additions & 14 deletions src/routing/osrm_routed_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,8 @@ class OsrmRoutedWrapper : public HttpWrapper {
const std::vector<Location>& locs,
const std::string& service) const override;

bool
duration_value_is_null(const rapidjson::Value& matrix_entry) const override;

bool
distance_value_is_null(const rapidjson::Value& matrix_entry) const override;

UserDuration
get_duration_value(const rapidjson::Value& matrix_entry) const override;

UserDistance
get_distance_value(const rapidjson::Value& matrix_entry) const override;

unsigned get_legs_number(const rapidjson::Value& result) const override;

std::string get_geometry(rapidjson::Value& result) const override;

public:
OsrmRoutedWrapper(const std::string& profile, const Server& server);
};
Expand Down

0 comments on commit 60decd5

Please sign in to comment.