use C++11 type traits to reduce code size in integral->string conversion

This commit is contained in:
Dennis Luxen 2014-10-08 12:40:56 +02:00
parent 4e00ebcd74
commit 8dc85e7641
6 changed files with 29 additions and 35 deletions

View File

@ -330,16 +330,16 @@ template <class DataFacadeT> class JSONDescriptor : public BaseDescriptor<DataFa
if (TurnInstruction::LeaveRoundAbout == current_instruction) if (TurnInstruction::LeaveRoundAbout == current_instruction)
{ {
temp_instruction = temp_instruction =
IntToString(as_integer(TurnInstruction::EnterRoundAbout)); IntegralToString(as_integer(TurnInstruction::EnterRoundAbout));
current_turn_instruction += temp_instruction; current_turn_instruction += temp_instruction;
current_turn_instruction += "-"; current_turn_instruction += "-";
temp_instruction = IntToString(round_about.leave_at_exit + 1); temp_instruction = IntegralToString(round_about.leave_at_exit + 1);
current_turn_instruction += temp_instruction; current_turn_instruction += temp_instruction;
round_about.leave_at_exit = 0; round_about.leave_at_exit = 0;
} }
else else
{ {
temp_instruction = IntToString(as_integer(current_instruction)); temp_instruction = IntegralToString(as_integer(current_instruction));
current_turn_instruction += temp_instruction; current_turn_instruction += temp_instruction;
} }
json_instruction_row.values.push_back(current_turn_instruction); json_instruction_row.values.push_back(current_turn_instruction);
@ -350,7 +350,7 @@ template <class DataFacadeT> class JSONDescriptor : public BaseDescriptor<DataFa
json_instruction_row.values.push_back(necessary_segments_running_index); json_instruction_row.values.push_back(necessary_segments_running_index);
json_instruction_row.values.push_back(round(segment.duration / 10)); json_instruction_row.values.push_back(round(segment.duration / 10));
json_instruction_row.values.push_back( json_instruction_row.values.push_back(
UintToString(static_cast<int>(segment.length)) + "m"); IntegralToString(static_cast<unsigned>(segment.length)) + "m");
const double bearing_value = (segment.bearing / 10.); const double bearing_value = (segment.bearing / 10.);
json_instruction_row.values.push_back(Azimuth::Get(bearing_value)); json_instruction_row.values.push_back(Azimuth::Get(bearing_value));
json_instruction_row.values.push_back( json_instruction_row.values.push_back(
@ -375,7 +375,7 @@ template <class DataFacadeT> class JSONDescriptor : public BaseDescriptor<DataFa
} }
JSON::Array json_last_instruction_row; JSON::Array json_last_instruction_row;
temp_instruction = IntToString(as_integer(TurnInstruction::ReachedYourDestination)); temp_instruction = IntegralToString(as_integer(TurnInstruction::ReachedYourDestination));
json_last_instruction_row.values.push_back(temp_instruction); json_last_instruction_row.values.push_back(temp_instruction);
json_last_instruction_row.values.push_back(""); json_last_instruction_row.values.push_back("");
json_last_instruction_row.values.push_back(0); json_last_instruction_row.values.push_back(0);

View File

@ -52,10 +52,10 @@ class HelloWorldPlugin : public BasePlugin
std::string temp_string; std::string temp_string;
json_result.values["title"] = "Hello World"; json_result.values["title"] = "Hello World";
temp_string = IntToString(routeParameters.zoom_level); temp_string = IntegralToString(routeParameters.zoom_level);
json_result.values["zoom_level"] = temp_string; json_result.values["zoom_level"] = temp_string;
temp_string = UintToString(routeParameters.check_sum); temp_string = IntegralToString(routeParameters.check_sum);
json_result.values["check_sum"] = temp_string; json_result.values["check_sum"] = temp_string;
json_result.values["instructions"] = (routeParameters.print_instructions ? "yes" : "no"); json_result.values["instructions"] = (routeParameters.print_instructions ? "yes" : "no");
json_result.values["geometry"] = (routeParameters.geometry ? "yes" : "no"); json_result.values["geometry"] = (routeParameters.geometry ? "yes" : "no");
@ -67,7 +67,7 @@ class HelloWorldPlugin : public BasePlugin
(!routeParameters.jsonp_parameter.empty() ? "yes" : "no"); (!routeParameters.jsonp_parameter.empty() ? "yes" : "no");
json_result.values["language"] = (!routeParameters.language.empty() ? "yes" : "no"); json_result.values["language"] = (!routeParameters.language.empty() ? "yes" : "no");
temp_string = UintToString(static_cast<unsigned>(routeParameters.coordinates.size())); temp_string = IntegralToString(routeParameters.coordinates.size());
json_result.values["location_count"] = temp_string; json_result.values["location_count"] = temp_string;
JSON::Array json_locations; 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.lat / COORDINATE_PRECISION);
json_coordinates.values.push_back(coordinate.lon / 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); json_locations.values.push_back(json_location);
++counter; ++counter;
} }

View File

@ -38,7 +38,7 @@ void Reply::SetSize(const unsigned size)
{ {
if ("Content-Length" == h.name) 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); const std::string status_string = reply.ToString(status);
reply.content.insert(reply.content.end(), status_string.begin(), status_string.end()); reply.content.insert(reply.content.end(), status_string.begin(), status_string.end());
reply.headers.emplace_back("Access-Control-Allow-Origin", "*"); reply.headers.emplace_back("Access-Control-Allow-Origin", "*");
reply.headers.emplace_back("Content-Length", reply.headers.emplace_back("Content-Length", IntegralToString(reply.content.size()));
UintToString(static_cast<unsigned>(reply.content.size())));
reply.headers.emplace_back("Content-Type", "text/html"); reply.headers.emplace_back("Content-Type", "text/html");
return reply; return reply;
} }

View File

@ -92,7 +92,7 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply
JSON::Object json_result; JSON::Object json_result;
json_result.values["status"] = 400; json_result.values["status"] = 400;
std::string message = "Query string malformed close to position "; std::string message = "Query string malformed close to position ";
message += UintToString(position); message += IntegralToString(position);
json_result.values["status_message"] = message; json_result.values["status_message"] = message;
JSON::render(reply.content, json_result); JSON::render(reply.content, json_result);
return; return;
@ -113,8 +113,7 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply
} }
// set headers // set headers
reply.headers.emplace_back("Content-Length", reply.headers.emplace_back("Content-Length", IntegralToString(reply.content.size()));
UintToString(static_cast<unsigned>(reply.content.size())));
if ("gpx" == route_parameters.output_format) if ("gpx" == route_parameters.output_format)
{ // gpx file { // gpx file
reply.headers.emplace_back("Content-Type", "application/gpx+xml; charset=UTF-8"); reply.headers.emplace_back("Content-Type", "application/gpx+xml; charset=UTF-8");

View File

@ -62,7 +62,7 @@ class Server
: thread_pool_size(thread_pool_size), acceptor(io_service), : thread_pool_size(thread_pool_size), acceptor(io_service),
new_connection(new http::Connection(io_service, request_handler)), request_handler() 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 resolver(io_service);
boost::asio::ip::tcp::resolver::query query(address, port_string); 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)); boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
} }
// Server() = delete;
// Server(const Server &) = delete;
void Run() void Run()
{ {
std::vector<std::shared_ptr<std::thread>> threads; std::vector<std::shared_ptr<std::thread>> threads;

View File

@ -82,27 +82,26 @@ auto as_integer(Enumeration const value)
return static_cast<typename std::underlying_type<Enumeration>::type>(value); return static_cast<typename std::underlying_type<Enumeration>::type>(value);
} }
static inline std::string IntToString(const int value) template<typename Number>
static inline typename std::enable_if<std::is_integral<Number>::value, std::string>::type IntegralToString(const Number value)
{ {
std::string output; std::string output;
std::back_insert_iterator<std::string> sink(output); std::back_insert_iterator<std::string> sink(output);
boost::spirit::karma::generate(sink, boost::spirit::karma::int_, value);
return output;
}
static inline std::string UintToString(const unsigned value) if (8 == sizeof(Number))
{ {
std::string output; boost::spirit::karma::generate(sink, boost::spirit::karma::long_long, value);
std::back_insert_iterator<std::string> sink(output); }
boost::spirit::karma::generate(sink, boost::spirit::karma::uint_, value);
return output;
}
static inline void int64ToString(const int64_t value, std::string &output) if (std::is_signed<Number>::value)
{ {
output.clear(); boost::spirit::karma::generate(sink, boost::spirit::karma::int_, value);
std::back_insert_iterator<std::string> sink(output); }
boost::spirit::karma::generate(sink, boost::spirit::karma::long_long, value); else
{
boost::spirit::karma::generate(sink, boost::spirit::karma::uint_, value);
}
return output;
} }
static inline int StringToInt(const std::string &input) static inline int StringToInt(const std::string &input)