replace boost::lexical_cast with an equivalent based on std::stringstream.

This commit is contained in:
Luca Schlecker 2022-06-06 17:14:37 +02:00 committed by Farook Al-Sammarraie
parent 61d27e3b82
commit df92d6d639
8 changed files with 42 additions and 25 deletions

View File

@ -1,7 +1,5 @@
#include "crow.h"
#include <sstream>
class ExampleLogHandler : public crow::ILogHandler
{
public:
@ -179,7 +177,7 @@ int main()
// To see in action submit something like '/params?pew=42'
if (req.url_params.get("pew") != nullptr)
{
double countD = boost::lexical_cast<double>(req.url_params.get("pew"));
double countD = crow::utility::lexical_cast<double>(req.url_params.get("pew"));
os << "The value of 'pew' is " << countD << '\n';
}

View File

@ -1,7 +1,5 @@
#include "crow.h"
#include <sstream>
class ExampleLogHandler : public crow::ILogHandler
{
public:
@ -110,7 +108,7 @@ int main()
os << "The key 'foo' was " << (req.url_params.get("foo") == nullptr ? "not " : "") << "found.\n";
if (req.url_params.get("pew") != nullptr)
{
double countD = boost::lexical_cast<double>(req.url_params.get("pew"));
double countD = crow::utility::lexical_cast<double>(req.url_params.get("pew"));
os << "The value of 'pew' is " << countD << '\n';
}
auto count = req.url_params.get_list("count");

View File

@ -1,7 +1,5 @@
#include "crow_all.h"
#include <sstream>
class ExampleLogHandler : public crow::ILogHandler
{
public:
@ -100,7 +98,7 @@ int main()
os << "The key 'foo' was " << (req.url_params.get("foo") == nullptr ? "not " : "") << "found.\n";
if (req.url_params.get("pew") != nullptr)
{
double countD = boost::lexical_cast<double>(req.url_params.get("pew"));
double countD = crow::utility::lexical_cast<double>(req.url_params.get("pew"));
os << "The value of 'pew' is " << countD << '\n';
}
auto count = req.url_params.get_list("count");

View File

@ -2,7 +2,6 @@
#define ASIO_STANDALONE
#include <asio.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/array.hpp>
#include <atomic>
#include <chrono>
@ -19,6 +18,7 @@
#include "crow/middleware.h"
#include "crow/socket_adaptors.h"
#include "crow/compression.h"
#include "crow/utility.h"
namespace crow
{
@ -144,7 +144,7 @@ namespace crow
}
}
CROW_LOG_INFO << "Request: " << boost::lexical_cast<std::string>(adaptor_.remote_endpoint()) << " " << this << " HTTP/" << (char)(req.http_ver_major + '0') << "." << (char)(req.http_ver_minor + '0') << ' ' << method_name(req.method) << " " << req.url;
CROW_LOG_INFO << "Request: " << utility::lexical_cast<std::string>(adaptor_.remote_endpoint()) << " " << this << " HTTP/" << (char)(req.http_ver_major + '0') << "." << (char)(req.http_ver_minor + '0') << ' ' << method_name(req.method) << " " << req.url;
need_to_call_after_handlers_ = false;

View File

@ -12,7 +12,6 @@
#include <iostream>
#include <algorithm>
#include <memory>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/operators.hpp>
#include <vector>
@ -349,13 +348,13 @@ namespace crow
{
case type::Number:
case type::String:
return boost::lexical_cast<int64_t>(start_, end_ - start_);
return utility::lexical_cast<int64_t>(start_, end_ - start_);
default:
const std::string msg = "expected number, got: " + std::string(get_type_str(t()));
throw std::runtime_error(msg);
}
#endif
return boost::lexical_cast<int64_t>(start_, end_ - start_);
return utility::lexical_cast<int64_t>(start_, end_ - start_);
}
/// The unsigned integer value.
@ -366,12 +365,12 @@ namespace crow
{
case type::Number:
case type::String:
return boost::lexical_cast<uint64_t>(start_, end_ - start_);
return utility::lexical_cast<uint64_t>(start_, end_ - start_);
default:
throw std::runtime_error(std::string("expected number, got: ") + get_type_str(t()));
}
#endif
return boost::lexical_cast<uint64_t>(start_, end_ - start_);
return utility::lexical_cast<uint64_t>(start_, end_ - start_);
}
/// The double precision floating-point number value.
@ -381,7 +380,7 @@ namespace crow
if (t() != type::Number)
throw std::runtime_error("value is not number");
#endif
return boost::lexical_cast<double>(start_, end_ - start_);
return utility::lexical_cast<double>(start_, end_ - start_);
}
/// The boolean value.

View File

@ -258,7 +258,7 @@ namespace crow
}
break;
default:
throw std::runtime_error("not implemented tag type" + boost::lexical_cast<std::string>(static_cast<int>(ctx.t())));
throw std::runtime_error("not implemented tag type" + utility::lexical_cast<std::string>(static_cast<int>(ctx.t())));
}
}
break;
@ -324,7 +324,7 @@ namespace crow
current = action.pos;
break;
default:
throw std::runtime_error("{{#: not implemented context type: " + boost::lexical_cast<std::string>(static_cast<int>(ctx.t())));
throw std::runtime_error("{{#: not implemented context type: " + utility::lexical_cast<std::string>(static_cast<int>(ctx.t())));
break;
}
break;
@ -333,7 +333,7 @@ namespace crow
stack.pop_back();
break;
default:
throw std::runtime_error("not implemented " + boost::lexical_cast<std::string>(static_cast<int>(action.t)));
throw std::runtime_error("not implemented " + utility::lexical_cast<std::string>(static_cast<int>(action.t)));
}
current++;
}

View File

@ -8,6 +8,7 @@
#include <cctype>
#include <functional>
#include <string>
#include <sstream>
#include <unordered_map>
#include "crow/settings.h"
@ -501,7 +502,6 @@ namespace crow
{
static constexpr auto value = get_index_of_element_from_tuple_by_type_impl<T, N + 1, Args...>::value;
};
} // namespace detail
namespace utility
@ -803,5 +803,29 @@ namespace crow
return equal;
}
template<typename T, typename U>
T lexical_cast(const U& v)
{
std::stringstream stream;
T res;
stream << v;
stream >> res;
return res;
}
template<typename T>
T lexical_cast(const char* v, size_t count)
{
std::stringstream stream;
T res;
stream.write(v, count);
stream >> res;
return res;
}
} // namespace utility
} // namespace crow

View File

@ -759,7 +759,7 @@ TEST_CASE("json_read_real")
for (auto x : v)
{
CROW_LOG_DEBUG << x;
CHECK(json::load(x).d() == boost::lexical_cast<double>(x));
CHECK(json::load(x).d() == utility::lexical_cast<double>(x));
}
auto ret = json::load(
@ -1936,10 +1936,10 @@ TEST_CASE("simple_url_params")
c.receive(asio::buffer(buf, 2048));
c.close();
CHECK(boost::lexical_cast<int>(last_url_params.get("int")) == 100);
CHECK(boost::lexical_cast<double>(last_url_params.get("double")) ==
CHECK(utility::lexical_cast<int>(last_url_params.get("int")) == 100);
CHECK(utility::lexical_cast<double>(last_url_params.get("double")) ==
123.45);
CHECK(boost::lexical_cast<bool>(last_url_params.get("boolean")));
CHECK(utility::lexical_cast<bool>(last_url_params.get("boolean")));
}
// check single array value
sendmsg = "GET /params?tmnt[]=leonardo\r\n\r\n";