move casts from/to string into static class
This commit is contained in:
parent
ec8f977ebe
commit
57fab61789
@ -32,7 +32,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#define JSON_CONTAINER_H
|
#define JSON_CONTAINER_H
|
||||||
|
|
||||||
#include "../ThirdParty/variant/variant.hpp"
|
#include "../ThirdParty/variant/variant.hpp"
|
||||||
#include "../Util/StringUtil.h"
|
#include "../Util/cast.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -157,7 +157,7 @@ struct ArrayRenderer : mapbox::util::static_visitor<>
|
|||||||
|
|
||||||
void operator()(const Number &number) const
|
void operator()(const Number &number) const
|
||||||
{
|
{
|
||||||
const std::string number_string = FixedDoubleToString(number.value);
|
const std::string number_string = cast::double_fixed_to_string(number.value);
|
||||||
out.insert(out.end(), number_string.begin(), number_string.end());
|
out.insert(out.end(), number_string.begin(), number_string.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 =
|
||||||
IntegralToString(as_integer(TurnInstruction::EnterRoundAbout));
|
cast::integral_to_string(cast::enum_to_underlying(TurnInstruction::EnterRoundAbout));
|
||||||
current_turn_instruction += temp_instruction;
|
current_turn_instruction += temp_instruction;
|
||||||
current_turn_instruction += "-";
|
current_turn_instruction += "-";
|
||||||
temp_instruction = IntegralToString(round_about.leave_at_exit + 1);
|
temp_instruction = cast::integral_to_string(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 = IntegralToString(as_integer(current_instruction));
|
temp_instruction = cast::integral_to_string(cast::enum_to_underlying(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(
|
||||||
IntegralToString(static_cast<unsigned>(segment.length)) + "m");
|
cast::integral_to_string(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 = IntegralToString(as_integer(TurnInstruction::ReachedYourDestination));
|
temp_instruction = cast::integral_to_string(cast::enum_to_underlying(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);
|
||||||
|
@ -28,10 +28,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#ifndef EXTRACTION_HELPER_FUNCTIONS_H
|
#ifndef EXTRACTION_HELPER_FUNCTIONS_H
|
||||||
#define EXTRACTION_HELPER_FUNCTIONS_H
|
#define EXTRACTION_HELPER_FUNCTIONS_H
|
||||||
|
|
||||||
#include "../Util/StringUtil.h"
|
#include "../Util/cast.hpp"
|
||||||
|
|
||||||
#include <boost/algorithm/string.hpp>
|
#include <boost/algorithm/string.hpp>
|
||||||
#include <boost/algorithm/string_regex.hpp>
|
#include <boost/algorithm/string_regex.hpp>
|
||||||
|
#include <boost/spirit/include/qi.hpp>
|
||||||
#include <boost/regex.hpp>
|
#include <boost/regex.hpp>
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
@ -68,18 +69,18 @@ inline unsigned parseDuration(const std::string &s)
|
|||||||
{
|
{
|
||||||
if (1 == result.size())
|
if (1 == result.size())
|
||||||
{
|
{
|
||||||
minutes = StringToUint(result[0]);
|
minutes = cast::string_to_int(result[0]);
|
||||||
}
|
}
|
||||||
if (2 == result.size())
|
if (2 == result.size())
|
||||||
{
|
{
|
||||||
minutes = StringToUint(result[1]);
|
minutes = cast::string_to_int(result[1]);
|
||||||
hours = StringToUint(result[0]);
|
hours = cast::string_to_int(result[0]);
|
||||||
}
|
}
|
||||||
if (3 == result.size())
|
if (3 == result.size())
|
||||||
{
|
{
|
||||||
seconds = StringToUint(result[2]);
|
seconds = cast::string_to_int(result[2]);
|
||||||
minutes = StringToUint(result[1]);
|
minutes = cast::string_to_int(result[1]);
|
||||||
hours = StringToUint(result[0]);
|
hours = cast::string_to_int(result[0]);
|
||||||
}
|
}
|
||||||
return 10 * (3600 * hours + 60 * minutes + seconds);
|
return 10 * (3600 * hours + 60 * minutes + seconds);
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#include "../DataStructures/ImportNode.h"
|
#include "../DataStructures/ImportNode.h"
|
||||||
#include "../DataStructures/InputReaderFactory.h"
|
#include "../DataStructures/InputReaderFactory.h"
|
||||||
#include "../DataStructures/Restriction.h"
|
#include "../DataStructures/Restriction.h"
|
||||||
|
#include "../Util/cast.hpp"
|
||||||
#include "../Util/SimpleLogger.h"
|
#include "../Util/SimpleLogger.h"
|
||||||
#include "../Util/StringUtil.h"
|
#include "../Util/StringUtil.h"
|
||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
@ -168,17 +169,17 @@ InputRestrictionContainer XMLParser::ReadXMLRestriction()
|
|||||||
if (xmlStrEqual(role, (const xmlChar *)"to") &&
|
if (xmlStrEqual(role, (const xmlChar *)"to") &&
|
||||||
xmlStrEqual(type, (const xmlChar *)"way"))
|
xmlStrEqual(type, (const xmlChar *)"way"))
|
||||||
{
|
{
|
||||||
restriction.toWay = StringToUint((const char *)ref);
|
restriction.toWay = cast::string_to_uint((const char *)ref);
|
||||||
}
|
}
|
||||||
if (xmlStrEqual(role, (const xmlChar *)"from") &&
|
if (xmlStrEqual(role, (const xmlChar *)"from") &&
|
||||||
xmlStrEqual(type, (const xmlChar *)"way"))
|
xmlStrEqual(type, (const xmlChar *)"way"))
|
||||||
{
|
{
|
||||||
restriction.fromWay = StringToUint((const char *)ref);
|
restriction.fromWay = cast::string_to_uint((const char *)ref);
|
||||||
}
|
}
|
||||||
if (xmlStrEqual(role, (const xmlChar *)"via") &&
|
if (xmlStrEqual(role, (const xmlChar *)"via") &&
|
||||||
xmlStrEqual(type, (const xmlChar *)"node"))
|
xmlStrEqual(type, (const xmlChar *)"node"))
|
||||||
{
|
{
|
||||||
restriction.restriction.viaNode = StringToUint((const char *)ref);
|
restriction.restriction.viaNode = cast::string_to_uint((const char *)ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nullptr != type)
|
if (nullptr != type)
|
||||||
@ -231,7 +232,7 @@ ExtractionWay XMLParser::ReadXMLWay()
|
|||||||
xmlStrEqual(child_name, (const xmlChar *)"way") == 1)
|
xmlStrEqual(child_name, (const xmlChar *)"way") == 1)
|
||||||
{
|
{
|
||||||
xmlChar *way_id = xmlTextReaderGetAttribute(inputReader, (const xmlChar *)"id");
|
xmlChar *way_id = xmlTextReaderGetAttribute(inputReader, (const xmlChar *)"id");
|
||||||
way.id = StringToUint((char *)way_id);
|
way.id = cast::string_to_uint((char *)way_id);
|
||||||
xmlFree(way_id);
|
xmlFree(way_id);
|
||||||
xmlFree(child_name);
|
xmlFree(child_name);
|
||||||
break;
|
break;
|
||||||
@ -265,7 +266,7 @@ ExtractionWay XMLParser::ReadXMLWay()
|
|||||||
xmlChar *ref = xmlTextReaderGetAttribute(inputReader, (const xmlChar *)"ref");
|
xmlChar *ref = xmlTextReaderGetAttribute(inputReader, (const xmlChar *)"ref");
|
||||||
if (ref != nullptr)
|
if (ref != nullptr)
|
||||||
{
|
{
|
||||||
way.path.push_back(StringToUint((const char *)ref));
|
way.path.push_back(cast::string_to_uint((const char *)ref));
|
||||||
xmlFree(ref);
|
xmlFree(ref);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -281,19 +282,19 @@ ImportNode XMLParser::ReadXMLNode()
|
|||||||
xmlChar *attribute = xmlTextReaderGetAttribute(inputReader, (const xmlChar *)"lat");
|
xmlChar *attribute = xmlTextReaderGetAttribute(inputReader, (const xmlChar *)"lat");
|
||||||
if (attribute != nullptr)
|
if (attribute != nullptr)
|
||||||
{
|
{
|
||||||
node.lat = static_cast<int>(COORDINATE_PRECISION * StringToDouble((const char *)attribute));
|
node.lat = static_cast<int>(COORDINATE_PRECISION * cast::string_to_double((const char *)attribute));
|
||||||
xmlFree(attribute);
|
xmlFree(attribute);
|
||||||
}
|
}
|
||||||
attribute = xmlTextReaderGetAttribute(inputReader, (const xmlChar *)"lon");
|
attribute = xmlTextReaderGetAttribute(inputReader, (const xmlChar *)"lon");
|
||||||
if (attribute != nullptr)
|
if (attribute != nullptr)
|
||||||
{
|
{
|
||||||
node.lon = static_cast<int>(COORDINATE_PRECISION * StringToDouble((const char *)attribute));
|
node.lon = static_cast<int>(COORDINATE_PRECISION * cast::string_to_double((const char *)attribute));
|
||||||
xmlFree(attribute);
|
xmlFree(attribute);
|
||||||
}
|
}
|
||||||
attribute = xmlTextReaderGetAttribute(inputReader, (const xmlChar *)"id");
|
attribute = xmlTextReaderGetAttribute(inputReader, (const xmlChar *)"id");
|
||||||
if (attribute != nullptr)
|
if (attribute != nullptr)
|
||||||
{
|
{
|
||||||
node.node_id = StringToUint((const char *)attribute);
|
node.node_id = cast::string_to_uint((const char *)attribute);
|
||||||
xmlFree(attribute);
|
xmlFree(attribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
#include "BasePlugin.h"
|
#include "BasePlugin.h"
|
||||||
#include "../DataStructures/JSONContainer.h"
|
#include "../DataStructures/JSONContainer.h"
|
||||||
#include "../Util/StringUtil.h"
|
#include "../Util/cast.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@ -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 = IntegralToString(routeParameters.zoom_level);
|
temp_string = cast::integral_to_string(routeParameters.zoom_level);
|
||||||
json_result.values["zoom_level"] = temp_string;
|
json_result.values["zoom_level"] = temp_string;
|
||||||
|
|
||||||
temp_string = IntegralToString(routeParameters.check_sum);
|
temp_string = cast::integral_to_string(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 = IntegralToString(routeParameters.coordinates.size());
|
temp_string = cast::integral_to_string(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[IntegralToString(counter)] = json_coordinates;
|
json_location.values[cast::integral_to_string(counter)] = json_coordinates;
|
||||||
json_locations.values.push_back(json_location);
|
json_locations.values.push_back(json_location);
|
||||||
++counter;
|
++counter;
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
#include <osrm/Reply.h>
|
#include <osrm/Reply.h>
|
||||||
|
|
||||||
#include "../../Util/StringUtil.h"
|
#include "../../Util/cast.hpp"
|
||||||
|
|
||||||
namespace http
|
namespace http
|
||||||
{
|
{
|
||||||
@ -38,7 +38,7 @@ void Reply::SetSize(const unsigned size)
|
|||||||
{
|
{
|
||||||
if ("Content-Length" == h.name)
|
if ("Content-Length" == h.name)
|
||||||
{
|
{
|
||||||
h.value = IntegralToString(size);
|
h.value = cast::integral_to_string(size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,7 +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", IntegralToString(reply.content.size()));
|
reply.headers.emplace_back("Content-Length", cast::integral_to_string(reply.content.size()));
|
||||||
reply.headers.emplace_back("Content-Type", "text/html");
|
reply.headers.emplace_back("Content-Type", "text/html");
|
||||||
return reply;
|
return reply;
|
||||||
}
|
}
|
||||||
|
@ -88,11 +88,11 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply
|
|||||||
{
|
{
|
||||||
reply = http::Reply::StockReply(http::Reply::badRequest);
|
reply = http::Reply::StockReply(http::Reply::badRequest);
|
||||||
reply.content.clear();
|
reply.content.clear();
|
||||||
const unsigned position = static_cast<unsigned>(std::distance(request.begin(), iter));
|
const auto position = std::distance(request.begin(), iter);
|
||||||
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 += IntegralToString(position);
|
message += cast::integral_to_string(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,7 +113,7 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set headers
|
// set headers
|
||||||
reply.headers.emplace_back("Content-Length", IntegralToString(reply.content.size()));
|
reply.headers.emplace_back("Content-Length", cast::integral_to_string(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");
|
||||||
|
@ -31,9 +31,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#include "Connection.h"
|
#include "Connection.h"
|
||||||
#include "RequestHandler.h"
|
#include "RequestHandler.h"
|
||||||
|
|
||||||
|
#include "../Util/cast.hpp"
|
||||||
#include "../Util/make_unique.hpp"
|
#include "../Util/make_unique.hpp"
|
||||||
#include "../Util/SimpleLogger.h"
|
#include "../Util/SimpleLogger.h"
|
||||||
#include "../Util/StringUtil.h"
|
|
||||||
|
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include <boost/bind.hpp>
|
#include <boost/bind.hpp>
|
||||||
@ -60,9 +60,9 @@ class Server
|
|||||||
|
|
||||||
explicit Server(const std::string &address, const int port, const unsigned thread_pool_size)
|
explicit Server(const std::string &address, const int port, const unsigned thread_pool_size)
|
||||||
: 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(std::make_shared<http::Connection>(io_service, request_handler)), request_handler()
|
||||||
{
|
{
|
||||||
const std::string port_string = IntegralToString(port);
|
const std::string port_string = cast::integral_to_string(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);
|
||||||
@ -86,9 +86,9 @@ class Server
|
|||||||
boost::bind(&boost::asio::io_service::run, &io_service));
|
boost::bind(&boost::asio::io_service::run, &io_service));
|
||||||
threads.push_back(thread);
|
threads.push_back(thread);
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < threads.size(); ++i)
|
for (auto thread : threads)
|
||||||
{
|
{
|
||||||
threads[i]->join();
|
thread->join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +102,7 @@ class Server
|
|||||||
if (!e)
|
if (!e)
|
||||||
{
|
{
|
||||||
new_connection->start();
|
new_connection->start();
|
||||||
new_connection.reset(new http::Connection(io_service, request_handler));
|
new_connection = std::make_shared<http::Connection>(io_service, request_handler);
|
||||||
acceptor.async_accept(
|
acceptor.async_accept(
|
||||||
new_connection->socket(),
|
new_connection->socket(),
|
||||||
boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
|
boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
|
||||||
|
188
Util/cast.hpp
Normal file
188
Util/cast.hpp
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
Copyright (c) 2013, Project OSRM, Dennis Luxen, others
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification,
|
||||||
|
are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
Redistributions of source code must retain the above copyright notice, this list
|
||||||
|
of conditions and the following disclaimer.
|
||||||
|
Redistributions in binary form must reproduce the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer in the documentation and/or
|
||||||
|
other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||||
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||||
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef CAST_HPP
|
||||||
|
#define CAST_HPP
|
||||||
|
|
||||||
|
#include <boost/spirit/include/karma.hpp>
|
||||||
|
#include <boost/spirit/include/qi.hpp>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
struct cast
|
||||||
|
{
|
||||||
|
// convert scoped enums to integers
|
||||||
|
template <typename Enumeration>
|
||||||
|
static auto enum_to_underlying(Enumeration const value) -> typename std::underlying_type<Enumeration>::type
|
||||||
|
{
|
||||||
|
return static_cast<typename std::underlying_type<Enumeration>::type>(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Number>
|
||||||
|
static typename std::enable_if<std::is_integral<Number>::value, std::string>::type
|
||||||
|
integral_to_string(const Number value)
|
||||||
|
{
|
||||||
|
std::string output;
|
||||||
|
std::back_insert_iterator<std::string> sink(output);
|
||||||
|
|
||||||
|
if (8 == sizeof(Number))
|
||||||
|
{
|
||||||
|
boost::spirit::karma::generate(sink, boost::spirit::karma::long_long, value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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 int string_to_int(const std::string &input)
|
||||||
|
{
|
||||||
|
auto first_digit = input.begin();
|
||||||
|
// Delete any trailing white-spaces
|
||||||
|
while (first_digit != input.end() && std::isspace(*first_digit))
|
||||||
|
{
|
||||||
|
++first_digit;
|
||||||
|
}
|
||||||
|
int value = 0;
|
||||||
|
boost::spirit::qi::parse(first_digit, input.end(), boost::spirit::int_, value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned string_to_uint(const std::string &input)
|
||||||
|
{
|
||||||
|
auto first_digit = input.begin();
|
||||||
|
// Delete any trailing white-spaces
|
||||||
|
while (first_digit != input.end() && (std::isspace(*first_digit) || '-' == *first_digit))
|
||||||
|
{
|
||||||
|
++first_digit;
|
||||||
|
}
|
||||||
|
unsigned value = 0;
|
||||||
|
boost::spirit::qi::parse(first_digit, input.end(), boost::spirit::uint_, value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint64_t string_to_uint64(const std::string &input)
|
||||||
|
{
|
||||||
|
auto first_digit = input.begin();
|
||||||
|
// Delete any trailing white-spaces
|
||||||
|
while (first_digit != input.end() && std::isspace(*first_digit))
|
||||||
|
{
|
||||||
|
++first_digit;
|
||||||
|
}
|
||||||
|
uint64_t value = 0;
|
||||||
|
boost::spirit::qi::parse(first_digit, input.end(), boost::spirit::long_long, value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// source: http://tinodidriksen.com/2011/05/28/cpp-convert-string-to-double-speed/
|
||||||
|
static double string_to_double(const char *p)
|
||||||
|
{
|
||||||
|
double r = 0.0;
|
||||||
|
bool neg = false;
|
||||||
|
if (*p == '-')
|
||||||
|
{
|
||||||
|
neg = true;
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
while (*p >= '0' && *p <= '9')
|
||||||
|
{
|
||||||
|
r = (r * 10.0) + (*p - '0');
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
if (*p == '.')
|
||||||
|
{
|
||||||
|
double f = 0.0;
|
||||||
|
int n = 0;
|
||||||
|
++p;
|
||||||
|
while (*p >= '0' && *p <= '9')
|
||||||
|
{
|
||||||
|
f = (f * 10.0) + (*p - '0');
|
||||||
|
++p;
|
||||||
|
++n;
|
||||||
|
}
|
||||||
|
r += f / std::pow(10.0, n);
|
||||||
|
}
|
||||||
|
if (neg)
|
||||||
|
{
|
||||||
|
r = -r;
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> struct scientific_policy : boost::spirit::karma::real_policies<T>
|
||||||
|
{
|
||||||
|
// we want the numbers always to be in fixed format
|
||||||
|
static int floatfield(T n)
|
||||||
|
{
|
||||||
|
return boost::spirit::karma::real_policies<T>::fmtflags::fixed;
|
||||||
|
}
|
||||||
|
static unsigned int precision(T) { return 6; }
|
||||||
|
};
|
||||||
|
typedef boost::spirit::karma::real_generator<double, scientific_policy<double>> science_type;
|
||||||
|
|
||||||
|
static std::string double_fixed_to_string(const double value)
|
||||||
|
{
|
||||||
|
std::string output;
|
||||||
|
std::back_insert_iterator<std::string> sink(output);
|
||||||
|
boost::spirit::karma::generate(sink, science_type(), value);
|
||||||
|
if (output.size() >= 2 && output[output.size() - 2] == '.' &&
|
||||||
|
output[output.size() - 1] == '0')
|
||||||
|
{
|
||||||
|
output.resize(output.size() - 2);
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string double_to_string(const double value)
|
||||||
|
{
|
||||||
|
std::string output;
|
||||||
|
std::back_insert_iterator<std::string> sink(output);
|
||||||
|
boost::spirit::karma::generate(sink, value);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void double_with_two_digits_to_string(const double value,
|
||||||
|
std::string &output)
|
||||||
|
{
|
||||||
|
// The largest 32-bit integer is 4294967295, that is 10 chars
|
||||||
|
// On the safe side, add 1 for sign, and 1 for trailing zero
|
||||||
|
char buffer[12];
|
||||||
|
sprintf(buffer, "%g", value);
|
||||||
|
output = buffer;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // CAST_HPP
|
Loading…
Reference in New Issue
Block a user