From 8dc85e764158390ec6f4ff272b1325ed2f49a1d1 Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Wed, 8 Oct 2014 12:40:56 +0200 Subject: [PATCH] use C++11 type traits to reduce code size in integral->string conversion --- Descriptors/JSONDescriptor.h | 10 +++++----- Plugins/HelloWorldPlugin.h | 8 ++++---- Server/Http/Reply.cpp | 5 ++--- Server/RequestHandler.cpp | 5 ++--- Server/Server.h | 5 +---- Util/StringUtil.h | 31 +++++++++++++++---------------- 6 files changed, 29 insertions(+), 35 deletions(-) diff --git a/Descriptors/JSONDescriptor.h b/Descriptors/JSONDescriptor.h index f81d609eb..c70b0eb5d 100644 --- a/Descriptors/JSONDescriptor.h +++ b/Descriptors/JSONDescriptor.h @@ -330,16 +330,16 @@ template class JSONDescriptor : public BaseDescriptor class JSONDescriptor : public BaseDescriptor(segment.length)) + "m"); + IntegralToString(static_cast(segment.length)) + "m"); const double bearing_value = (segment.bearing / 10.); json_instruction_row.values.push_back(Azimuth::Get(bearing_value)); json_instruction_row.values.push_back( @@ -375,7 +375,7 @@ template class JSONDescriptor : public BaseDescriptor(routeParameters.coordinates.size())); + temp_string = IntegralToString(routeParameters.coordinates.size()); json_result.values["location_count"] = temp_string; JSON::Array json_locations; @@ -79,7 +79,7 @@ class HelloWorldPlugin : public BasePlugin json_coordinates.values.push_back(coordinate.lat / COORDINATE_PRECISION); json_coordinates.values.push_back(coordinate.lon / COORDINATE_PRECISION); - json_location.values[UintToString(counter)] = json_coordinates; + json_location.values[IntegralToString(counter)] = json_coordinates; json_locations.values.push_back(json_location); ++counter; } diff --git a/Server/Http/Reply.cpp b/Server/Http/Reply.cpp index b3247ece8..b95a15459 100644 --- a/Server/Http/Reply.cpp +++ b/Server/Http/Reply.cpp @@ -38,7 +38,7 @@ void Reply::SetSize(const unsigned size) { if ("Content-Length" == h.name) { - h.value = UintToString(size); + h.value = IntegralToString(size); } } } @@ -87,8 +87,7 @@ Reply Reply::StockReply(Reply::status_type status) const std::string status_string = reply.ToString(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", - UintToString(static_cast(reply.content.size()))); + reply.headers.emplace_back("Content-Length", IntegralToString(reply.content.size())); reply.headers.emplace_back("Content-Type", "text/html"); return reply; } diff --git a/Server/RequestHandler.cpp b/Server/RequestHandler.cpp index 40b51649c..53cfbbd4e 100644 --- a/Server/RequestHandler.cpp +++ b/Server/RequestHandler.cpp @@ -92,7 +92,7 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply JSON::Object json_result; json_result.values["status"] = 400; std::string message = "Query string malformed close to position "; - message += UintToString(position); + message += IntegralToString(position); json_result.values["status_message"] = message; JSON::render(reply.content, json_result); return; @@ -113,8 +113,7 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply } // set headers - reply.headers.emplace_back("Content-Length", - UintToString(static_cast(reply.content.size()))); + reply.headers.emplace_back("Content-Length", IntegralToString(reply.content.size())); if ("gpx" == route_parameters.output_format) { // gpx file reply.headers.emplace_back("Content-Type", "application/gpx+xml; charset=UTF-8"); diff --git a/Server/Server.h b/Server/Server.h index a01b8068a..8756cfcbe 100644 --- a/Server/Server.h +++ b/Server/Server.h @@ -62,7 +62,7 @@ class Server : thread_pool_size(thread_pool_size), acceptor(io_service), new_connection(new http::Connection(io_service, request_handler)), request_handler() { - const std::string port_string = IntToString(port); + const std::string port_string = IntegralToString(port); boost::asio::ip::tcp::resolver resolver(io_service); boost::asio::ip::tcp::resolver::query query(address, port_string); @@ -77,9 +77,6 @@ class Server boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error)); } - // Server() = delete; - // Server(const Server &) = delete; - void Run() { std::vector> threads; diff --git a/Util/StringUtil.h b/Util/StringUtil.h index 94705d810..6d274d51b 100644 --- a/Util/StringUtil.h +++ b/Util/StringUtil.h @@ -82,27 +82,26 @@ auto as_integer(Enumeration const value) return static_cast::type>(value); } -static inline std::string IntToString(const int value) +template +static inline typename std::enable_if::value, std::string>::type IntegralToString(const Number value) { std::string output; std::back_insert_iterator sink(output); - boost::spirit::karma::generate(sink, boost::spirit::karma::int_, value); - return output; -} -static inline std::string UintToString(const unsigned value) -{ - std::string output; - std::back_insert_iterator sink(output); - boost::spirit::karma::generate(sink, boost::spirit::karma::uint_, value); - return output; -} + if (8 == sizeof(Number)) + { + boost::spirit::karma::generate(sink, boost::spirit::karma::long_long, value); + } -static inline void int64ToString(const int64_t value, std::string &output) -{ - output.clear(); - std::back_insert_iterator sink(output); - boost::spirit::karma::generate(sink, boost::spirit::karma::long_long, value); + 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 inline int StringToInt(const std::string &input)