Skip to content

Commit

Permalink
Change qi::lit for qi::symbols for the sides parameter parser.
Browse files Browse the repository at this point in the history
Refactor code :
 - Suppress StartSide Enum
 - Change Side Structure for Enum

Signed-off-by: FILLAU Jean-Maxime <[email protected]>
  • Loading branch information
FILLAU Jean-Maxime committed May 19, 2017
1 parent f8d663b commit 93de1f1
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -714,14 +714,14 @@ class ContiguousInternalMemoryDataFacadeBase : public BaseDataFacade
input_coordinate, max_results, max_distance, bearing, bearing_range);
}

std::pair<PhantomNode, PhantomNode> NearestPhantomNodeWithAlternativeFromBigComponent(
const util::Coordinate input_coordinate,
const engine::SideValue side_value) const override final
std::pair<PhantomNode, PhantomNode>
NearestPhantomNodeWithAlternativeFromBigComponent(const util::Coordinate input_coordinate,
const engine::Side side) const override final
{
BOOST_ASSERT(m_geospatial_query.get());

return m_geospatial_query->NearestPhantomNodeWithAlternativeFromBigComponent(
input_coordinate, side_value);
input_coordinate, side);
}

std::pair<PhantomNode, PhantomNode> NearestPhantomNodeWithAlternativeFromBigComponent(
Expand Down
2 changes: 1 addition & 1 deletion include/engine/datafacade/datafacade_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class BaseDataFacade

virtual std::pair<PhantomNode, PhantomNode>
NearestPhantomNodeWithAlternativeFromBigComponent(const util::Coordinate input_coordinate,
const engine::SideValue side_value) const = 0;
const engine::Side side) const = 0;
virtual std::pair<PhantomNode, PhantomNode>
NearestPhantomNodeWithAlternativeFromBigComponent(const util::Coordinate input_coordinate,
const double max_distance) const = 0;
Expand Down
11 changes: 6 additions & 5 deletions include/engine/geospatial_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,20 @@ template <typename RTreeT, typename DataFacadeT> class GeospatialQuery
// a second phantom node is return that is the nearest coordinate in a big component.
std::pair<PhantomNode, PhantomNode>
NearestPhantomNodeWithAlternativeFromBigComponent(const util::Coordinate input_coordinate,
const engine::SideValue side_value) const
const engine::Side side) const
{
bool has_small_component = false;
bool has_big_component = false;
auto results = rtree.Nearest(
input_coordinate,
[this, &side_value, &input_coordinate, &has_big_component, &has_small_component](const CandidateSegment &segment) {
[this, &side, &input_coordinate, &has_big_component, &has_small_component](
const CandidateSegment &segment) {
auto use_segment =
(!has_small_component || (!has_big_component && !IsTinyComponent(segment)));
auto use_directions = std::make_pair(use_segment, use_segment);
bool isOnewaySegment = !(segment.data.forward_segment_id.enabled &&
segment.data.reverse_segment_id.enabled);
if (!isOnewaySegment && side_value != BOTH)
if (!isOnewaySegment && side != BOTH)
{
// Check the counter clockwise
//
Expand All @@ -241,8 +242,8 @@ template <typename RTreeT, typename DataFacadeT> class GeospatialQuery
// if drive left
// input_coordinate_is_at_right = !input_coordinate_is_at_right

// We reverse goCountrySide if side_value is OPPOSITE
if (side_value == OPPOSITE)
// We reverse goCountrySide if side is OPPOSITE
if (side == OPPOSITE)
input_coordinate_is_at_right = !input_coordinate_is_at_right;

// Apply the side.
Expand Down
8 changes: 4 additions & 4 deletions include/engine/plugins/plugin_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ class BasePlugin
BOOST_ASSERT(parameters.IsValid());
for (const auto i : util::irange<std::size_t>(0UL, parameters.coordinates.size()))
{
SideValue sideValue = engine::SideValue::BOTH;
Side side = engine::Side::BOTH;
// TODO init at SIDE for test
// SideValue sideValue = engine::SideValue::DEFAULT;
// SideValue side = engine::SideValue::DEFAULT;
if (use_sides && parameters.sides[i])
sideValue = parameters.sides[i]->side;
side = parameters.sides[i].get();

if (use_hints && parameters.hints[i] &&
parameters.hints[i]->IsValid(parameters.coordinates[i], facade))
Expand Down Expand Up @@ -277,7 +277,7 @@ class BasePlugin
{
phantom_node_pairs[i] =
facade.NearestPhantomNodeWithAlternativeFromBigComponent(
parameters.coordinates[i], sideValue);
parameters.coordinates[i], side);
}
}

Expand Down
40 changes: 1 addition & 39 deletions include/engine/side.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,56 +28,18 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef OSRM_ENGINE_SIDE_HPP
#define OSRM_ENGINE_SIDE_HPP

#include <string>

namespace osrm
{
namespace engine
{

enum SideValue
enum Side
{
DEFAULT,
OPPOSITE,
BOTH

};

struct Side
{
SideValue side;
static SideValue fromString(const std::string &str)
{
if (str == "d")
return DEFAULT;
else if (str == "o")
return OPPOSITE;
else
return BOTH;
}
static std::string toString(const Side &side)
{
switch(side.side)
{
case(DEFAULT) :
return "0";
case(OPPOSITE) :
return "d";
case(BOTH) :
return "b";
default :
//TODO I don't know what to do here.
return "b";
}
}
};

inline bool operator==(const Side lhs, const Side rhs)
{
return lhs.side == rhs.side;
}
inline bool operator!=(const Side lhs, const Side rhs) { return !(lhs == rhs); }
}
}

#endif
24 changes: 20 additions & 4 deletions include/nodejs/node_osrm_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "osrm/nearest_parameters.hpp"
#include "osrm/osrm.hpp"
#include "osrm/route_parameters.hpp"
#include "osrm/side.hpp"
#include "osrm/status.hpp"
#include "osrm/storage_config.hpp"
#include "osrm/table_parameters.hpp"
Expand Down Expand Up @@ -296,7 +297,7 @@ inline bool argumentsToParameter(const Nan::FunctionCallbackInfo<v8::Value> &arg
Nan::ThrowError("Coordinates must be an array of (lon/lat) pairs");
return false;
}

if (obj->Has(Nan::New("sides").ToLocalChecked()))
{
v8::Local<v8::Value> sides = obj->Get(Nan::New("sides").ToLocalChecked());
Expand Down Expand Up @@ -325,13 +326,28 @@ inline bool argumentsToParameter(const Nan::FunctionCallbackInfo<v8::Value> &arg

if (side->IsString())
{
if (side->ToString()->Length() == 0)
if (!side->ToString().IsOneByte())
{
Nan::ThrowError("Side cannot be an empty string");
Nan::ThrowError("Side can only take 'd' 'b' or 'o' values");
return false;
}

params->sides.push_back(Side{sides});
v8::String::Utf8Value arg(side[0]->ToString());
std::string d_string("d");
Handle<Value> d = ;


if (arg == String::New(string('b')))
params->sides.push_back(osrm::engine::BOTH);
else if (arg == String::New(string('d')))
params->sides.push_back(osrm::engine::DEFAULT);
else if (arg == String::New(string('o')) || side->ToString()->Length() == 0)
params->sides.push_back(osrm::engine::OPPOSITE);
else
{
Nan::ThrowError("Side can only take 'd' 'b' or 'o' values");
return false;
}
}
else if (side->IsNull())
{
Expand Down
38 changes: 38 additions & 0 deletions include/osrm/side.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright (c) 2016, Project OSRM contributors
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef OSRM_SIDE_HPP
#define OSRM_SIDE_HPP

#include "engine/side.hpp"

namespace osrm
{
using engine::Side;
}

#endif
23 changes: 9 additions & 14 deletions include/server/api/base_parameters_grammar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,6 @@ struct BaseParametersGrammar : boost::spirit::qi::grammar<Iterator, Signature>
base_parameters.bearings.push_back(std::move(bearing));
};

const auto add_side = [](engine::api::BaseParameters &base_parameters,
const boost::optional<std::string> &side_string) {
boost::optional<engine::Side> side;
if (side_string)
{
side = engine::Side{engine::Side::fromString(side_string.get())};
}
base_parameters.sides.push_back(std::move(side));
};

polyline_chars = qi::char_("a-zA-Z0-9_.--[]{}@?|\\%~`^");
base64_char = qi::char_("a-zA-Z0-9--_=");
unlimited_rule = qi::lit("unlimited")[qi::_val = std::numeric_limits<double>::infinity()];
Expand Down Expand Up @@ -159,13 +149,16 @@ struct BaseParametersGrammar : boost::spirit::qi::grammar<Iterator, Signature>
qi::lit("bearings=") >
(-(qi::short_ > ',' > qi::short_))[ph::bind(add_bearing, qi::_r1, qi::_1)] % ';';

sides_rule = qi::lit("sides=") >
(-qi::as_string[qi::char_("a-zA")])[ph::bind(add_side, qi::_r1, qi::_1)] % ';';
side_type.add("d", engine::Side::DEFAULT)("o", engine::Side::OPPOSITE)("b", engine::Side::BOTH);
sides_rule =
qi::lit("sides=") >
(-side_type % ';')[ph::bind(&engine::api::BaseParameters::sides, qi::_r1) = qi::_1];

base_rule = radiuses_rule(qi::_r1) //
| hints_rule(qi::_r1) //
| bearings_rule(qi::_r1) //
| generate_hints_rule(qi::_r1) | sides_rule(qi::_r1);
| generate_hints_rule(qi::_r1)
| sides_rule(qi::_r1);
}

protected:
Expand All @@ -176,9 +169,9 @@ struct BaseParametersGrammar : boost::spirit::qi::grammar<Iterator, Signature>
qi::rule<Iterator, Signature> bearings_rule;
qi::rule<Iterator, Signature> radiuses_rule;
qi::rule<Iterator, Signature> hints_rule;
qi::rule<Iterator, Signature> sides_rule;

qi::rule<Iterator, Signature> generate_hints_rule;
qi::rule<Iterator, Signature> sides_rule;

qi::rule<Iterator, osrm::engine::Bearing()> bearing_rule;
qi::rule<Iterator, osrm::util::Coordinate()> location_rule;
Expand All @@ -188,6 +181,8 @@ struct BaseParametersGrammar : boost::spirit::qi::grammar<Iterator, Signature>
qi::rule<Iterator, std::string()> polyline_chars;
qi::rule<Iterator, double()> unlimited_rule;
qi::real_parser<double, json_policy> double_;

qi::symbols<char, engine::Side> side_type;
};
}
}
Expand Down
5 changes: 2 additions & 3 deletions unit_tests/mocks/mock_datafacade.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ class MockBaseDataFacade : public engine::datafacade::BaseDataFacade
}

std::pair<engine::PhantomNode, engine::PhantomNode>
NearestPhantomNodeWithAlternativeFromBigComponent(
const util::Coordinate /*input_coordinate*/,
const engine::SideValue /*side_value*/) const override
NearestPhantomNodeWithAlternativeFromBigComponent(const util::Coordinate /*input_coordinate*/,
const engine::Side /*side*/) const override
{
return {};
}
Expand Down
2 changes: 1 addition & 1 deletion unit_tests/server/parameters_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ inline std::ostream &operator<<(std::ostream &out, Bearing bearing)

inline std::ostream &operator<<(std::ostream &out, Side side)
{
out << Side::toString(side);
out << side;
return out;
}
}
Expand Down
27 changes: 12 additions & 15 deletions unit_tests/server/parameters_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(invalid_route_urls)
BOOST_CHECK_EQUAL(testInvalidOptions<RouteParameters>("1,2;3,4?overview=false&radiuses=foo"),
32UL);
BOOST_CHECK_EQUAL(testInvalidOptions<RouteParameters>("1,2;3,4?overview=false&sides=foo"),
30UL);
29UL);
BOOST_CHECK_EQUAL(testInvalidOptions<RouteParameters>("1,2;3,4?overview=false&hints=foo"),
29UL);
BOOST_CHECK_EQUAL(testInvalidOptions<RouteParameters>("1,2;3,4?overview=false&hints=;;; ;"),
Expand Down Expand Up @@ -400,22 +400,19 @@ BOOST_AUTO_TEST_CASE(valid_route_urls)
BOOST_CHECK_EQUAL(result_17->annotations, true);

std::vector<boost::optional<engine::Side>> sides_18 = {
boost::none,
engine::Side{engine::SideValue::DEFAULT},
engine::Side{engine::SideValue::BOTH},
engine::Side{engine::SideValue::OPPOSITE},
boost::none, engine::Side::DEFAULT, engine::Side::BOTH, engine::Side::OPPOSITE,
};
RouteParameters reference_18{false,
false,
false,
RouteParameters::GeometriesType::Polyline,
RouteParameters::OverviewType::Simplified,
boost::optional<bool>{},
coords_3,
std::vector<boost::optional<engine::Hint>>{},
std::vector<boost::optional<double>>{},
std::vector<boost::optional<engine::Bearing>>{},
sides_18};
false,
false,
RouteParameters::GeometriesType::Polyline,
RouteParameters::OverviewType::Simplified,
boost::optional<bool>{},
coords_3,
std::vector<boost::optional<engine::Hint>>{},
std::vector<boost::optional<double>>{},
std::vector<boost::optional<engine::Bearing>>{},
sides_18};

auto result_18 = parseParameters<RouteParameters>("1,2;3,4;5,6;7,8?steps=false&sides=;d;b;o");
BOOST_CHECK(result_18);
Expand Down

0 comments on commit 93de1f1

Please sign in to comment.