Remove hand written conversion code and replace with stdlib features.

With C++11 the stdlib gains:

- `std::stoi` function family to convert from `std::string` to integral type

- `std::to_string` to convert from number types to `std::string`

The only reason for hand-writing the conversion code therefore is
performance. I benchmarked an `osrm-extract` with the hand-written code
against one with the stdlib conversion features and could not find any
significant difference (we switch back and forth between C++ and Lua,
shaving off a few us in conversion doesn't gain us much).

Formatting arithmetic types in the default format with given precision
requires streams, but is doable in a few lines of idiomatic stdlib code.

For this, there is now the following function template available:

    template <Arithmetic T, int Precision = 6>
    inline std::string to_string_with_precision(const T);

that requires integral or floating point types and returns a formatted
string in the defaukt format with the given precision applied.

In addition this completely rips out Boost.Spirit from the `casts.hpp`
header, resulting in faster compile times.

Boom!

References:

- http://en.cppreference.com/w/cpp/string/basic_string/stol
- http://en.cppreference.com/w/cpp/string/basic_string/to_string
- http://www.kumobius.com/2013/08/c-string-to-int/
This commit is contained in:
Daniel J. Hofmann
2015-09-09 03:31:03 +02:00
parent 31cf8a8813
commit f9f0ffb64d
9 changed files with 46 additions and 173 deletions
+3 -3
View File
@@ -27,7 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "reply.hpp"
#include "../../util/cast.hpp"
#include <string>
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;
}
+3 -2
View File
@@ -45,6 +45,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <algorithm>
#include <iostream>
#include <string>
RequestHandler::RequestHandler() : routing_machine(nullptr) {}
@@ -102,7 +103,7 @@ void RequestHandler::handle_request(const http::request &current_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 &current_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"]);
+2 -2
View File
@@ -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 <memory>
#include <thread>
#include <vector>
#include <string>
class Server
{
@@ -62,7 +62,7 @@ class Server
: thread_pool_size(thread_pool_size), acceptor(io_service),
new_connection(std::make_shared<http::Connection>(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);