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
+4 -5
View File
@@ -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 <osrm/json_container.hpp>
@@ -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<double>(coordinate.lat / COORDINATE_PRECISION));
json_coordinates.values.push_back(
static_cast<double>(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;
}