diff --git a/descriptors/json_descriptor.hpp b/descriptors/json_descriptor.hpp index eb5edbf4a..a2a280cbc 100644 --- a/descriptors/json_descriptor.hpp +++ b/descriptors/json_descriptor.hpp @@ -35,6 +35,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "../data_structures/segment_information.hpp" #include "../data_structures/turn_instructions.hpp" #include "../util/bearing.hpp" +#include "../util/cast.hpp" #include "../util/integer_range.hpp" #include "../util/json_renderer.hpp" #include "../util/simple_logger.hpp" @@ -44,6 +45,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include template class JSONDescriptor final : public BaseDescriptor { @@ -327,18 +329,18 @@ template class JSONDescriptor final : public BaseDescriptor< std::string current_turn_instruction; if (TurnInstruction::LeaveRoundAbout == current_instruction) { - temp_instruction = cast::integral_to_string( + temp_instruction = std::to_string( cast::enum_to_underlying(TurnInstruction::EnterRoundAbout)); current_turn_instruction += temp_instruction; current_turn_instruction += "-"; - temp_instruction = cast::integral_to_string(round_about.leave_at_exit + 1); + temp_instruction = std::to_string(round_about.leave_at_exit + 1); current_turn_instruction += temp_instruction; round_about.leave_at_exit = 0; } else { temp_instruction = - cast::integral_to_string(cast::enum_to_underlying(current_instruction)); + std::to_string(cast::enum_to_underlying(current_instruction)); current_turn_instruction += temp_instruction; } json_instruction_row.values.push_back(current_turn_instruction); @@ -348,7 +350,7 @@ template class JSONDescriptor final : public BaseDescriptor< json_instruction_row.values.push_back(necessary_segments_running_index); json_instruction_row.values.push_back(std::round(segment.duration / 10.)); json_instruction_row.values.push_back( - cast::integral_to_string(static_cast(segment.length)) + "m"); + std::to_string(static_cast(segment.length)) + "m"); const double bearing_value = (segment.bearing / 10.); json_instruction_row.values.push_back(bearing::get(bearing_value)); json_instruction_row.values.push_back( @@ -372,8 +374,8 @@ template class JSONDescriptor final : public BaseDescriptor< } osrm::json::Array json_last_instruction_row; - temp_instruction = cast::integral_to_string( - cast::enum_to_underlying(TurnInstruction::ReachedYourDestination)); + temp_instruction = + std::to_string(cast::enum_to_underlying(TurnInstruction::ReachedYourDestination)); json_last_instruction_row.values.push_back(temp_instruction); json_last_instruction_row.values.push_back(""); json_last_instruction_row.values.push_back(0); diff --git a/extractor/extraction_helper_functions.hpp b/extractor/extraction_helper_functions.hpp index e61e54344..69ab456af 100644 --- a/extractor/extraction_helper_functions.hpp +++ b/extractor/extraction_helper_functions.hpp @@ -37,6 +37,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include bool simple_duration_is_valid(const std::string &s) { @@ -89,18 +90,18 @@ unsigned parseDuration(const std::string &s) { if (1 == result.size()) { - minutes = cast::string_to_int(result[0]); + minutes = std::stoul(result[0]); } if (2 == result.size()) { - minutes = cast::string_to_int(result[1]); - hours = cast::string_to_int(result[0]); + minutes = std::stoul(result[1]); + hours = std::stoul(result[0]); } if (3 == result.size()) { - seconds = cast::string_to_int(result[2]); - minutes = cast::string_to_int(result[1]); - hours = cast::string_to_int(result[0]); + seconds = std::stoul(result[2]); + minutes = std::stoul(result[1]); + hours = std::stoul(result[0]); } return (3600 * hours + 60 * minutes + seconds); } diff --git a/plugins/hello_world.hpp b/plugins/hello_world.hpp index faf5b1048..47d5a9e0c 100644 --- a/plugins/hello_world.hpp +++ b/plugins/hello_world.hpp @@ -30,7 +30,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "plugin_base.hpp" -#include "../util/cast.hpp" #include "../util/json_renderer.hpp" #include @@ -53,10 +52,10 @@ class HelloWorldPlugin final : public BasePlugin std::string temp_string; json_result.values["title"] = "Hello World"; - temp_string = cast::integral_to_string(routeParameters.zoom_level); + temp_string = std::to_string(routeParameters.zoom_level); json_result.values["zoom_level"] = temp_string; - temp_string = cast::integral_to_string(routeParameters.check_sum); + temp_string = std::to_string(routeParameters.check_sum); json_result.values["check_sum"] = temp_string; json_result.values["instructions"] = (routeParameters.print_instructions ? "yes" : "no"); json_result.values["geometry"] = (routeParameters.geometry ? "yes" : "no"); @@ -68,7 +67,7 @@ class HelloWorldPlugin final : public BasePlugin (!routeParameters.jsonp_parameter.empty() ? "yes" : "no"); json_result.values["language"] = (!routeParameters.language.empty() ? "yes" : "no"); - temp_string = cast::integral_to_string(routeParameters.coordinates.size()); + temp_string = std::to_string(routeParameters.coordinates.size()); json_result.values["location_count"] = temp_string; osrm::json::Array json_locations; @@ -82,7 +81,7 @@ class HelloWorldPlugin final : public BasePlugin static_cast(coordinate.lat / COORDINATE_PRECISION)); json_coordinates.values.push_back( static_cast(coordinate.lon / COORDINATE_PRECISION)); - json_location.values[cast::integral_to_string(counter)] = json_coordinates; + json_location.values[std::to_string(counter)] = json_coordinates; json_locations.values.push_back(json_location); ++counter; } diff --git a/server/http/reply.cpp b/server/http/reply.cpp index 036d1ae05..a3d3d9624 100644 --- a/server/http/reply.cpp +++ b/server/http/reply.cpp @@ -27,7 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "reply.hpp" -#include "../../util/cast.hpp" +#include namespace http { @@ -48,7 +48,7 @@ void reply::set_size(const std::size_t size) { if ("Content-Length" == h.name) { - h.value = cast::integral_to_string(size); + h.value = std::to_string(size); } } } @@ -95,7 +95,7 @@ reply reply::stock_reply(const reply::status_type status) const std::string status_string = reply.status_to_string(status); reply.content.insert(reply.content.end(), status_string.begin(), status_string.end()); reply.headers.emplace_back("Access-Control-Allow-Origin", "*"); - reply.headers.emplace_back("Content-Length", cast::integral_to_string(reply.content.size())); + reply.headers.emplace_back("Content-Length", std::to_string(reply.content.size())); reply.headers.emplace_back("Content-Type", "text/html"); return reply; } diff --git a/server/request_handler.cpp b/server/request_handler.cpp index d1c6e6265..a3af7f0b3 100644 --- a/server/request_handler.cpp +++ b/server/request_handler.cpp @@ -45,6 +45,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include RequestHandler::RequestHandler() : routing_machine(nullptr) {} @@ -102,7 +103,7 @@ void RequestHandler::handle_request(const http::request ¤t_request, json_result.values["status"] = 400; std::string message = "Query string malformed close to position "; - message += cast::integral_to_string(position); + message += std::to_string(position); json_result.values["status_message"] = message; osrm::json::render(current_reply.content, json_result); return; @@ -135,7 +136,7 @@ void RequestHandler::handle_request(const http::request ¤t_request, // set headers current_reply.headers.emplace_back("Content-Length", - cast::integral_to_string(current_reply.content.size())); + std::to_string(current_reply.content.size())); if ("gpx" == route_parameters.output_format) { // gpx file osrm::json::gpx_render(current_reply.content, json_result.values["route"]); diff --git a/server/server.hpp b/server/server.hpp index 0ec316379..80fae9704 100644 --- a/server/server.hpp +++ b/server/server.hpp @@ -31,7 +31,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "connection.hpp" #include "request_handler.hpp" -#include "../util/cast.hpp" #include "../util/integer_range.hpp" #include "../util/simple_logger.hpp" @@ -44,6 +43,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include class Server { @@ -62,7 +62,7 @@ class Server : thread_pool_size(thread_pool_size), acceptor(io_service), new_connection(std::make_shared(io_service, request_handler)) { - const std::string port_string = cast::integral_to_string(port); + const auto port_string = std::to_string(port); boost::asio::ip::tcp::resolver resolver(io_service); boost::asio::ip::tcp::resolver::query query(address, port_string); diff --git a/util/cast.hpp b/util/cast.hpp index 6d6ff6548..538f8925b 100644 --- a/util/cast.hpp +++ b/util/cast.hpp @@ -28,158 +28,28 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef CAST_HPP #define CAST_HPP -#include -#include - #include +#include +#include #include -struct cast +namespace cast { - // convert scoped enums to integers - template - static auto enum_to_underlying(Enumeration const value) -> - typename std::underlying_type::type - { - return static_cast::type>(value); - } +template +inline auto enum_to_underlying(Enumeration const value) -> + typename std::underlying_type::type +{ + return static_cast::type>(value); +} - template - static typename std::enable_if::value, std::string>::type - integral_to_string(const Number value) - { - std::string output; - std::back_insert_iterator sink(output); +template inline std::string to_string_with_precision(const T x) +{ + static_assert(std::is_arithmetic::value, "integral or floating point type required"); - if (8 == sizeof(Number)) - { - boost::spirit::karma::generate(sink, boost::spirit::karma::long_long, value); - } - else - { - if (std::is_signed::value) - { - boost::spirit::karma::generate(sink, boost::spirit::karma::int_, value); - } - else - { - boost::spirit::karma::generate(sink, boost::spirit::karma::uint_, value); - } - } - return output; - } - - static int string_to_int(const std::string &input) - { - auto first_digit = input.begin(); - // Delete any trailing white-spaces - while (first_digit != input.end() && std::isspace(*first_digit)) - { - ++first_digit; - } - int value = 0; - boost::spirit::qi::parse(first_digit, input.end(), boost::spirit::int_, value); - return value; - } - - static unsigned string_to_uint(const std::string &input) - { - auto first_digit = input.begin(); - // Delete any trailing white-spaces - while (first_digit != input.end() && (std::isspace(*first_digit) || '-' == *first_digit)) - { - ++first_digit; - } - unsigned value = 0; - boost::spirit::qi::parse(first_digit, input.end(), boost::spirit::uint_, value); - return value; - } - - static uint64_t string_to_uint64(const std::string &input) - { - auto first_digit = input.begin(); - // Delete any trailing white-spaces - while (first_digit != input.end() && std::isspace(*first_digit)) - { - ++first_digit; - } - uint64_t value = 0; - boost::spirit::qi::parse(first_digit, input.end(), boost::spirit::long_long, value); - return value; - } - - // source: http://tinodidriksen.com/2011/05/28/cpp-convert-string-to-double-speed/ - static double string_to_double(const char *p) noexcept - { - double r = 0.0; - bool neg = false; - if (*p == '-') - { - neg = true; - ++p; - } - while (*p >= '0' && *p <= '9') - { - r = (r * 10.0) + (*p - '0'); - ++p; - } - if (*p == '.') - { - double f = 0.0; - int n = 0; - ++p; - while (*p >= '0' && *p <= '9') - { - f = (f * 10.0) + (*p - '0'); - ++p; - ++n; - } - r += f / std::pow(10.0, n); - } - if (neg) - { - r = -r; - } - return r; - } - - template struct scientific_policy : boost::spirit::karma::real_policies - { - // we want the numbers always to be in fixed format - static int floatfield(T) { return boost::spirit::karma::real_policies::fmtflags::fixed; } - static unsigned int precision(T) { return 6; } - }; - using science_type = boost::spirit::karma::real_generator>; - - static std::string double_fixed_to_string(const double value) - { - std::string output; - std::back_insert_iterator sink(output); - boost::spirit::karma::generate(sink, science_type(), value); - if (output.size() >= 2 && output[output.size() - 2] == '.' && - output[output.size() - 1] == '0') - { - output.resize(output.size() - 2); - } - return output; - } - - static std::string double_to_string(const double value) - { - std::string output; - std::back_insert_iterator sink(output); - boost::spirit::karma::generate(sink, value); - return output; - } - - static void double_with_two_digits_to_string(const double value, std::string &output) - { - // The largest 32-bit integer is 4294967295, that is 10 chars - // On the safe side, add 1 for sign, and 1 for trailing zero - char buffer[12]; - sprintf(buffer, "%g", value); - output = buffer; - } -}; + std::ostringstream out; + out << std::setprecision(Precision) << x; + return out.str(); +} +} #endif // CAST_HPP diff --git a/util/json_renderer.hpp b/util/json_renderer.hpp index b6b96f66c..cf3318059 100644 --- a/util/json_renderer.hpp +++ b/util/json_renderer.hpp @@ -111,7 +111,7 @@ struct ArrayRenderer : mapbox::util::static_visitor<> void operator()(const Number &number) const { - const std::string number_string = cast::double_fixed_to_string(number.value); + const std::string number_string = cast::to_string_with_precision(number.value); out.insert(out.end(), number_string.begin(), number_string.end()); } diff --git a/util/xml_renderer.hpp b/util/xml_renderer.hpp index f59810075..44f7c2d9f 100644 --- a/util/xml_renderer.hpp +++ b/util/xml_renderer.hpp @@ -50,7 +50,7 @@ struct XMLToArrayRenderer : mapbox::util::static_visitor<> void operator()(const Number &number) const { - const std::string number_string = cast::double_fixed_to_string(number.value); + const std::string number_string = cast::to_string_with_precision(number.value); out.insert(out.end(), number_string.begin(), number_string.end()); }