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)
{
temp_instruction =
IntToString(as_integer(TurnInstruction::EnterRoundAbout));
IntegralToString(as_integer(TurnInstruction::EnterRoundAbout));
current_turn_instruction += temp_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;
round_about.leave_at_exit = 0;
}
else
{
temp_instruction = IntToString(as_integer(current_instruction));
temp_instruction = IntegralToString(as_integer(current_instruction));
current_turn_instruction += temp_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(round(segment.duration / 10));
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.);
json_instruction_row.values.push_back(Azimuth::Get(bearing_value));
json_instruction_row.values.push_back(
@ -375,7 +375,7 @@ template <class DataFacadeT> class JSONDescriptor : public BaseDescriptor<DataFa
}
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("");
json_last_instruction_row.values.push_back(0);

View File

@ -52,10 +52,10 @@ class HelloWorldPlugin : public BasePlugin
std::string temp_string;
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;
temp_string = UintToString(routeParameters.check_sum);
temp_string = IntegralToString(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");
@ -67,7 +67,7 @@ class HelloWorldPlugin : public BasePlugin
(!routeParameters.jsonp_parameter.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::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;
}

View File

@ -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<unsigned>(reply.content.size())));
reply.headers.emplace_back("Content-Length", IntegralToString(reply.content.size()));
reply.headers.emplace_back("Content-Type", "text/html");
return reply;
}

View File

@ -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<unsigned>(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");

View File

@ -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<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);
}
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::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)
{
std::string output;
std::back_insert_iterator<std::string> 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<std::string> sink(output);
boost::spirit::karma::generate(sink, boost::spirit::karma::long_long, value);
if (std::is_signed<Number>::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)