Flatbuffers is not a boost::optional friendly

This commit is contained in:
Denis Chaplygin 2019-09-30 16:33:32 +03:00 committed by Denis Chaplygin
parent 0205cbc578
commit 016c77a4de
5 changed files with 39 additions and 36 deletions

View File

@ -106,7 +106,7 @@ class BaseAPI
auto name_string = builder->CreateString( auto name_string = builder->CreateString(
facade.GetNameForID(facade.GetNameIndex(phantom.forward_segment_id.id)).to_string()); facade.GetNameForID(facade.GetNameIndex(phantom.forward_segment_id.id)).to_string());
boost::optional<flatbuffers::Offset<flatbuffers::String>> hint_string = boost::none; flatbuffers::Offset<flatbuffers::String> hint_string;
if (parameters.generate_hints) if (parameters.generate_hints)
{ {
hint_string = builder->CreateString(Hint{phantom, facade.GetCheckSum()}.ToBase64()); hint_string = builder->CreateString(Hint{phantom, facade.GetCheckSum()}.ToBase64());
@ -117,9 +117,9 @@ class BaseAPI
waypoint->add_distance(util::coordinate_calculation::fccApproximateDistance( waypoint->add_distance(util::coordinate_calculation::fccApproximateDistance(
phantom.location, phantom.input_location)); phantom.location, phantom.input_location));
waypoint->add_name(name_string); waypoint->add_name(name_string);
if (hint_string) if (parameters.generate_hints)
{ {
waypoint->add_hint(*hint_string); waypoint->add_hint(hint_string);
} }
return waypoint; return waypoint;
} }

View File

@ -50,7 +50,7 @@ class MatchAPI final : public RouteAPI
flatbuffers::FlatBufferBuilder &fb_result) const flatbuffers::FlatBufferBuilder &fb_result) const
{ {
auto data_timestamp = facade.GetTimestamp(); auto data_timestamp = facade.GetTimestamp();
boost::optional<flatbuffers::Offset<flatbuffers::String>> data_version_string = boost::none; flatbuffers::Offset<flatbuffers::String> data_version_string;
if (!data_timestamp.empty()) if (!data_timestamp.empty())
{ {
data_version_string = fb_result.CreateString(data_timestamp); data_version_string = fb_result.CreateString(data_timestamp);
@ -60,9 +60,9 @@ class MatchAPI final : public RouteAPI
return MakeTracepoints(fb_result, sub_matchings); return MakeTracepoints(fb_result, sub_matchings);
}); });
if (data_version_string) if (!data_timestamp.empty())
{ {
response->add_data_version(*data_version_string); response->add_data_version(data_version_string);
} }
fb_result.Finish(response->Finish()); fb_result.Finish(response->Finish());

View File

@ -73,7 +73,7 @@ class RouteAPI : public BaseAPI
{ {
auto data_timestamp = facade.GetTimestamp(); auto data_timestamp = facade.GetTimestamp();
boost::optional<flatbuffers::Offset<flatbuffers::String>> data_version_string = boost::none; flatbuffers::Offset<flatbuffers::String> data_version_string;
if (!data_timestamp.empty()) if (!data_timestamp.empty())
{ {
data_version_string = fb_result.CreateString(data_timestamp); data_version_string = fb_result.CreateString(data_timestamp);
@ -84,9 +84,9 @@ class RouteAPI : public BaseAPI
return BaseAPI::MakeWaypoints(&fb_result, all_start_end_points); return BaseAPI::MakeWaypoints(&fb_result, all_start_end_points);
}); });
if (data_version_string) if (!data_timestamp.empty())
{ {
response->add_data_version(*data_version_string); response->add_data_version(data_version_string);
} }
fb_result.Finish(response->Finish()); fb_result.Finish(response->Finish());
} }
@ -373,8 +373,7 @@ class RouteAPI : public BaseAPI
requested_annotations = RouteParameters::AnnotationsType::All; requested_annotations = RouteParameters::AnnotationsType::All;
} }
boost::optional<flatbuffers::Offset<fbresult::Annotation>> annotation_buffer = flatbuffers::Offset<fbresult::Annotation> annotation_buffer;
boost::none;
if (requested_annotations != RouteParameters::AnnotationsType::None) if (requested_annotations != RouteParameters::AnnotationsType::None)
{ {
annotation_buffer = annotation_buffer =
@ -397,9 +396,9 @@ class RouteAPI : public BaseAPI
} }
legBuilder.add_steps(steps_vector); legBuilder.add_steps(steps_vector);
if (annotation_buffer) if (requested_annotations != RouteParameters::AnnotationsType::None)
{ {
legBuilder.add_annotations(*annotation_buffer); legBuilder.add_annotations(annotation_buffer);
} }
routeLegs.emplace_back(legBuilder.Finish()); routeLegs.emplace_back(legBuilder.Finish());
} }
@ -506,8 +505,9 @@ class RouteAPI : public BaseAPI
} }
auto nodes_vector = fb_result.CreateVector(nodes); auto nodes_vector = fb_result.CreateVector(nodes);
// Add any supporting metadata, if needed // Add any supporting metadata, if needed
boost::optional<flatbuffers::Offset<fbresult::Metadata>> metadata_buffer = boost::none; bool use_metadata = requested_annotations & RouteParameters::AnnotationsType::Datasources;
if (requested_annotations & RouteParameters::AnnotationsType::Datasources) flatbuffers::Offset<fbresult::Metadata> metadata_buffer;
if (use_metadata)
{ {
const auto MAX_DATASOURCE_ID = 255u; const auto MAX_DATASOURCE_ID = 255u;
std::vector<flatbuffers::Offset<flatbuffers::String>> names; std::vector<flatbuffers::Offset<flatbuffers::String>> names;
@ -529,9 +529,9 @@ class RouteAPI : public BaseAPI
annotation.add_weight(weight); annotation.add_weight(weight);
annotation.add_datasources(datasources); annotation.add_datasources(datasources);
annotation.add_nodes(nodes_vector); annotation.add_nodes(nodes_vector);
if (metadata_buffer) if (use_metadata)
{ {
annotation.add_metadata(*metadata_buffer); annotation.add_metadata(metadata_buffer);
} }
return annotation.Finish(); return annotation.Finish();

View File

@ -74,7 +74,7 @@ class TableAPI final : public BaseAPI
auto number_of_destinations = parameters.destinations.size(); auto number_of_destinations = parameters.destinations.size();
auto data_timestamp = facade.GetTimestamp(); auto data_timestamp = facade.GetTimestamp();
boost::optional<flatbuffers::Offset<flatbuffers::String>> data_version_string = boost::none; flatbuffers::Offset<flatbuffers::String> data_version_string;
if (!data_timestamp.empty()) if (!data_timestamp.empty())
{ {
data_version_string = fb_result.CreateString(data_timestamp); data_version_string = fb_result.CreateString(data_timestamp);
@ -116,21 +116,24 @@ class TableAPI final : public BaseAPI
} }
} }
boost::optional<flatbuffers::Offset<flatbuffers::Vector<float>>> durations = boost::none; bool use_durations = parameters.annotations & TableParameters::AnnotationsType::Duration;
if (parameters.annotations & TableParameters::AnnotationsType::Duration) flatbuffers::Offset<flatbuffers::Vector<float>> durations;
if (use_durations)
{ {
durations = MakeDurationTable(fb_result, tables.first); durations = MakeDurationTable(fb_result, tables.first);
} }
boost::optional<flatbuffers::Offset<flatbuffers::Vector<float>>> distances = boost::none; bool use_distances = parameters.annotations & TableParameters::AnnotationsType::Distance;
if (parameters.annotations & TableParameters::AnnotationsType::Distance) flatbuffers::Offset<flatbuffers::Vector<float>> distances;
if (use_distances)
{ {
distances = MakeDistanceTable(fb_result, tables.second); distances = MakeDistanceTable(fb_result, tables.second);
} }
boost::optional<flatbuffers::Offset<flatbuffers::Vector<uint32_t>>> speed_cells = bool have_speed_cells =
boost::none; parameters.fallback_speed != INVALID_FALLBACK_SPEED && parameters.fallback_speed > 0;
if (parameters.fallback_speed != INVALID_FALLBACK_SPEED && parameters.fallback_speed > 0) flatbuffers::Offset<flatbuffers::Vector<uint32_t>> speed_cells;
if (have_speed_cells)
{ {
speed_cells = MakeEstimatesTable(fb_result, fallback_speed_cells); speed_cells = MakeEstimatesTable(fb_result, fallback_speed_cells);
} }
@ -139,24 +142,24 @@ class TableAPI final : public BaseAPI
table.add_destinations(destinations); table.add_destinations(destinations);
table.add_rows(number_of_sources); table.add_rows(number_of_sources);
table.add_cols(number_of_destinations); table.add_cols(number_of_destinations);
if (durations) if (use_durations)
{ {
table.add_durations(*durations); table.add_durations(durations);
} }
if (distances) if (use_distances)
{ {
table.add_distances(*distances); table.add_distances(distances);
} }
if (speed_cells) if (have_speed_cells)
{ {
table.add_fallback_speed_cells(*speed_cells); table.add_fallback_speed_cells(speed_cells);
} }
auto table_buffer = table.Finish(); auto table_buffer = table.Finish();
fbresult::FBResultBuilder response(fb_result); fbresult::FBResultBuilder response(fb_result);
if (data_version_string) if (!data_timestamp.empty())
{ {
response.add_data_version(*data_version_string); response.add_data_version(data_version_string);
} }
response.add_table(table_buffer); response.add_table(table_buffer);
response.add_waypoints(sources); response.add_waypoints(sources);

View File

@ -48,7 +48,7 @@ class TripAPI final : public RouteAPI
flatbuffers::FlatBufferBuilder &fb_result) const flatbuffers::FlatBufferBuilder &fb_result) const
{ {
auto data_timestamp = facade.GetTimestamp(); auto data_timestamp = facade.GetTimestamp();
boost::optional<flatbuffers::Offset<flatbuffers::String>> data_version_string = boost::none; flatbuffers::Offset<flatbuffers::String> data_version_string;
if (!data_timestamp.empty()) if (!data_timestamp.empty())
{ {
data_version_string = fb_result.CreateString(data_timestamp); data_version_string = fb_result.CreateString(data_timestamp);
@ -59,9 +59,9 @@ class TripAPI final : public RouteAPI
return MakeWaypoints(fb_result, sub_trips, phantoms); return MakeWaypoints(fb_result, sub_trips, phantoms);
}); });
if (data_version_string) if (!data_timestamp.empty())
{ {
response->add_data_version(*data_version_string); response->add_data_version(data_version_string);
} }
fb_result.Finish(response->Finish()); fb_result.Finish(response->Finish());
} }