osrm-backend/include/server/api/route_parameters_grammar.hpp
2017-02-10 16:11:24 +00:00

100 lines
3.8 KiB
C++

#ifndef ROUTE_PARAMETERS_GRAMMAR_HPP
#define ROUTE_PARAMETERS_GRAMMAR_HPP
#include "server/api/base_parameters_grammar.hpp"
#include "engine/api/route_parameters.hpp"
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
namespace osrm
{
namespace server
{
namespace api
{
namespace
{
namespace ph = boost::phoenix;
namespace qi = boost::spirit::qi;
}
template <typename Iterator = std::string::iterator,
typename Signature = void(engine::api::RouteParameters &)>
struct RouteParametersGrammar : public BaseParametersGrammar<Iterator, Signature>
{
using BaseGrammar = BaseParametersGrammar<Iterator, Signature>;
RouteParametersGrammar() : RouteParametersGrammar(root_rule)
{
route_rule =
(qi::lit("alternatives=") >
qi::bool_[ph::bind(&engine::api::RouteParameters::alternatives, qi::_r1) = qi::_1]) |
(qi::lit("continue_straight=") >
(qi::lit("default") |
qi::bool_[ph::bind(&engine::api::RouteParameters::continue_straight, qi::_r1) =
qi::_1]));
root_rule = query_rule(qi::_r1) > -qi::lit(".json") >
-('?' > (route_rule(qi::_r1) | base_rule(qi::_r1)) % '&');
}
RouteParametersGrammar(qi::rule<Iterator, Signature> &root_rule_) : BaseGrammar(root_rule_)
{
using AnnotationsType = engine::api::RouteParameters::AnnotationsType;
const auto add_annotation = [](engine::api::RouteParameters &route_parameters,
AnnotationsType route_param) {
route_parameters.annotations_type = route_parameters.annotations_type | route_param;
route_parameters.annotations =
route_parameters.annotations_type != AnnotationsType::None;
};
geometries_type.add("geojson", engine::api::RouteParameters::GeometriesType::GeoJSON)(
"polyline", engine::api::RouteParameters::GeometriesType::Polyline)(
"polyline6", engine::api::RouteParameters::GeometriesType::Polyline6);
overview_type.add("simplified", engine::api::RouteParameters::OverviewType::Simplified)(
"full", engine::api::RouteParameters::OverviewType::Full)(
"false", engine::api::RouteParameters::OverviewType::False);
annotations_type.add("duration", AnnotationsType::Duration)(
"nodes", AnnotationsType::Nodes)("distance", AnnotationsType::Distance)(
"weight", AnnotationsType::Weight)("datasources", AnnotationsType::Datasources);
base_rule =
BaseGrammar::base_rule(qi::_r1) |
(qi::lit("steps=") >
qi::bool_[ph::bind(&engine::api::RouteParameters::steps, qi::_r1) = qi::_1]) |
(qi::lit("geometries=") >
geometries_type[ph::bind(&engine::api::RouteParameters::geometries, qi::_r1) =
qi::_1]) |
(qi::lit("overview=") >
overview_type[ph::bind(&engine::api::RouteParameters::overview, qi::_r1) = qi::_1]) |
(qi::lit("annotations=") >
(qi::lit("true")[ph::bind(add_annotation, qi::_r1, AnnotationsType::All)] |
qi::lit("false")[ph::bind(add_annotation, qi::_r1, AnnotationsType::None)] |
(annotations_type[ph::bind(add_annotation, qi::_r1, qi::_1)] % ',')));
query_rule = BaseGrammar::query_rule(qi::_r1);
}
protected:
qi::rule<Iterator, Signature> base_rule;
qi::rule<Iterator, Signature> query_rule;
private:
qi::rule<Iterator, Signature> root_rule;
qi::rule<Iterator, Signature> route_rule;
qi::symbols<char, engine::api::RouteParameters::GeometriesType> geometries_type;
qi::symbols<char, engine::api::RouteParameters::OverviewType> overview_type;
qi::symbols<char, engine::api::RouteParameters::AnnotationsType> annotations_type;
};
}
}
}
#endif