Code cleanups (epee, boost::lexical_cast)

- Remove some useless epee functions, and add deprecated markers to ones
that have good replacements already.
- Don't use boost::lexical_cast when std::to_string or direct stream
output can be used just as well.
- Get rid of dumb epee "include_base_utils.h" header
This commit is contained in:
Jason Rhinelander 2020-10-22 18:31:59 -03:00
parent fdc35d5114
commit 862df3b5fe
13 changed files with 29 additions and 69 deletions

View File

@ -42,6 +42,7 @@
#include <type_traits>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <lokimq/hex.h>
#include "storages/parserse_base_utils.h"
#include "hex.h"
#include "mlocker.h"
@ -61,9 +62,10 @@ using namespace std::literals;
namespace string_tools
{
//----------------------------------------------------------------------------
[[deprecated("use lokimq::to_hex instead")]]
inline std::string buff_to_hex_nodelimer(const std::string& src)
{
return to_hex::string(to_byte_span(to_span(src)));
return lokimq::to_hex(src);
}
//----------------------------------------------------------------------------
inline bool parse_hexstr_to_binbuff(const epee::span<const char> s, epee::span<char>& res)
@ -128,21 +130,6 @@ DISABLE_GCC_WARNING(maybe-uninitialized)
return true;
}
POP_WARNINGS
//----------------------------------------------------------------------------
template<class XType>
inline bool xtype_to_string(const XType& val, std::string& str)
{
try
{
str = boost::lexical_cast<std::string>(val);
}
catch(...)
{
return false;
}
return true;
}
//----------------------------------------------------------------------------
std::string get_ip_string_from_int32(uint32_t ip);
//----------------------------------------------------------------------------
@ -176,14 +163,6 @@ POP_WARNINGS
return true;
}
inline std::string num_to_string_fast(int64_t val)
{
/*
char buff[30] = {0};
i64toa_s(val, buff, sizeof(buff)-1, 10);
return buff;*/
return boost::lexical_cast<std::string>(val);
}
//----------------------------------------------------------------------------
template<typename T>
inline std::string to_string_hex(const T &val)
@ -293,6 +272,7 @@ POP_WARNINGS
}
//----------------------------------------------------------------------------
template<class t_pod_type>
[[deprecated("use tools::type_to_hex instead")]]
std::string pod_to_hex(const t_pod_type& s)
{
static_assert(std::is_standard_layout<t_pod_type>(), "expected standard layout type");
@ -300,6 +280,7 @@ POP_WARNINGS
}
//----------------------------------------------------------------------------
template<class t_pod_type>
[[deprecated("use tools::hex_to_type<T> instead")]]
bool hex_to_pod(const std::string& hex_str, t_pod_type& s)
{
static_assert(std::is_pod<t_pod_type>::value, "expected pod type");
@ -321,6 +302,7 @@ POP_WARNINGS
return hex_to_pod(hex_str, unwrap(s));
}
//----------------------------------------------------------------------------
[[deprecated("use lokimq::is_hex instead")]]
bool validate_hex(uint64_t length, const std::string& str);
}
}

View File

@ -64,6 +64,7 @@ endif()
target_link_libraries(epee
PUBLIC
easylogging
lokimq::lokimq
PRIVATE
lokimq::lokimq
filesystem

View File

@ -1728,7 +1728,7 @@ std::string BlockchainLMDB::get_db_name() const
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
return std::string("lmdb");
return "lmdb"s;
}
void BlockchainLMDB::lock()
@ -2611,7 +2611,7 @@ uint64_t BlockchainLMDB::get_block_timestamp(const uint64_t& height) const
auto get_result = mdb_cursor_get(m_cur_block_info, (MDB_val *)&zerokval, &result, MDB_GET_BOTH);
if (get_result == MDB_NOTFOUND)
{
throw0(BLOCK_DNE(std::string("Attempt to get timestamp from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- timestamp not in db").c_str()));
throw0(BLOCK_DNE(std::string("Attempt to get timestamp from height ").append(std::to_string(height)).append(" failed -- timestamp not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a timestamp from the db"));
@ -2709,7 +2709,7 @@ size_t BlockchainLMDB::get_block_weight(const uint64_t& height) const
auto get_result = mdb_cursor_get(m_cur_block_info, (MDB_val *)&zerokval, &result, MDB_GET_BOTH);
if (get_result == MDB_NOTFOUND)
{
throw0(BLOCK_DNE(std::string("Attempt to get block size from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block size not in db").c_str()));
throw0(BLOCK_DNE(std::string("Attempt to get block size from height ").append(std::to_string(height)).append(" failed -- block size not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a block size from the db"));
@ -2845,7 +2845,7 @@ difficulty_type BlockchainLMDB::get_block_cumulative_difficulty(const uint64_t&
auto get_result = mdb_cursor_get(m_cur_block_info, (MDB_val *)&zerokval, &result, MDB_GET_BOTH);
if (get_result == MDB_NOTFOUND)
{
throw0(BLOCK_DNE(std::string("Attempt to get cumulative difficulty from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- difficulty not in db").c_str()));
throw0(BLOCK_DNE(std::string("Attempt to get cumulative difficulty from height ").append(std::to_string(height)).append(" failed -- difficulty not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a cumulative difficulty from the db"));
@ -2884,7 +2884,7 @@ uint64_t BlockchainLMDB::get_block_already_generated_coins(const uint64_t& heigh
auto get_result = mdb_cursor_get(m_cur_block_info, (MDB_val *)&zerokval, &result, MDB_GET_BOTH);
if (get_result == MDB_NOTFOUND)
{
throw0(BLOCK_DNE(std::string("Attempt to get generated coins from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block size not in db").c_str()));
throw0(BLOCK_DNE(std::string("Attempt to get generated coins from height ").append(std::to_string(height)).append(" failed -- block size not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a total generated coins from the db"));
@ -2906,7 +2906,7 @@ uint64_t BlockchainLMDB::get_block_long_term_weight(const uint64_t& height) cons
auto get_result = mdb_cursor_get(m_cur_block_info, (MDB_val *)&zerokval, &result, MDB_GET_BOTH);
if (get_result == MDB_NOTFOUND)
{
throw0(BLOCK_DNE(std::string("Attempt to get block long term weight from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- block info not in db").c_str()));
throw0(BLOCK_DNE(std::string("Attempt to get block long term weight from height ").append(std::to_string(height)).append(" failed -- block info not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a long term block weight from the db"));
@ -2928,7 +2928,7 @@ crypto::hash BlockchainLMDB::get_block_hash_from_height(const uint64_t& height)
auto get_result = mdb_cursor_get(m_cur_block_info, (MDB_val *)&zerokval, &result, MDB_GET_BOTH);
if (get_result == MDB_NOTFOUND)
{
throw0(BLOCK_DNE(std::string("Attempt to get hash from height ").append(boost::lexical_cast<std::string>(height)).append(" failed -- hash not in db").c_str()));
throw0(BLOCK_DNE(std::string("Attempt to get hash from height ").append(std::to_string(height)).append(" failed -- hash not in db").c_str()));
}
else if (get_result)
throw0(DB_ERROR(lmdb_error("Error attempting to retrieve a block hash from the db: ", get_result).c_str()));
@ -4290,7 +4290,7 @@ void BlockchainLMDB::get_output_key(const epee::span<const uint64_t> &amounts, c
MDEBUG("Partial result: " << outputs.size() << "/" << offsets.size());
break;
}
throw1(OUTPUT_DNE((std::string("Attempting to get output pubkey by global index (amount ") + boost::lexical_cast<std::string>(amount) + ", index " + boost::lexical_cast<std::string>(offsets[i]) + ", count " + boost::lexical_cast<std::string>(get_num_outputs(amount)) + "), but key does not exist (current height " + boost::lexical_cast<std::string>(height()) + ")").c_str()));
throw1(OUTPUT_DNE((std::string("Attempting to get output pubkey by global index (amount ") + std::to_string(amount) + ", index " + std::to_string(offsets[i]) + ", count " + std::to_string(get_num_outputs(amount)) + "), but key does not exist (current height " + std::to_string(height()) + ")").c_str()));
}
else if (get_result)
throw0(DB_ERROR(lmdb_error("Error attempting to retrieve an output pubkey from the db", get_result).c_str()));
@ -4598,7 +4598,7 @@ uint8_t BlockchainLMDB::get_hard_fork_version(uint64_t height) const
MDB_val val_ret;
auto result = mdb_cursor_get(m_cur_hf_versions, &val_key, &val_ret, MDB_SET);
if (result == MDB_NOTFOUND || result)
throw0(DB_ERROR(lmdb_error("Error attempting to retrieve a hard fork version at height " + boost::lexical_cast<std::string>(height) + " from the db: ", result).c_str()));
throw0(DB_ERROR(lmdb_error("Error attempting to retrieve a hard fork version at height " + std::to_string(height) + " from the db: ", result).c_str()));
uint8_t ret = *(const uint8_t*)val_ret.mv_data;
return ret;

View File

@ -30,7 +30,6 @@
#define __STDC_FORMAT_MACROS // NOTE(loki): Explicitly define the PRIu64 macro on Mingw
#endif
#include <boost/algorithm/string.hpp>
#include "common/command_line.h"
#include "serialization/crypto.h"
#include "cryptonote_core/cryptonote_core.h"

View File

@ -65,18 +65,6 @@ namespace cryptonote {
public:
virtual bool alt_block_added(const block &block, const std::vector<transaction>& txs, struct checkpoint_t const *checkpoint) = 0;
};
/************************************************************************/
/* */
/************************************************************************/
template<class t_array>
struct array_hasher: std::unary_function<t_array&, std::size_t>
{
std::size_t operator()(const t_array& val) const
{
return boost::hash_range(&val.data[0], &val.data[sizeof(val.data)]);
}
};
#pragma pack(push, 1)
struct public_address_outer_blob

View File

@ -45,8 +45,6 @@
#include "common/loki_integration_test_hooks.h"
#include <boost/format.hpp>
#include <fstream>
#include <ctime>
#include <string>
@ -138,9 +136,7 @@ namespace {
std::string elapsed = peer.last_seen == 0 ? "never" : epee::misc_utils::get_time_interval_string(now - last_seen);
std::string id_str = epee::string_tools::pad_string(epee::string_tools::to_string_hex(peer.id), 16, '0', true);
std::string port_str;
epee::string_tools::xtype_to_string(peer.port, port_str);
std::string addr_str = peer.host + ":" + port_str;
std::string addr_str = peer.host + ":" + std::to_string(peer.port);
std::string rpc_port = peer.rpc_port ? std::to_string(peer.rpc_port) : "-";
std::string pruning_seed = epee::string_tools::to_string_hex(peer.pruning_seed);
tools::msg_writer() << boost::format("%-10s %-25s %-25s %-5s %-4s %s") % prefix % id_str % addr_str % rpc_port % pruning_seed % elapsed;
@ -149,15 +145,15 @@ namespace {
void print_block_header(block_header_response const & header)
{
tools::success_msg_writer()
<< "timestamp: " << boost::lexical_cast<std::string>(header.timestamp) << " (" << tools::get_human_readable_timestamp(header.timestamp) << ")" << "\n"
<< "timestamp: " << header.timestamp << " (" << tools::get_human_readable_timestamp(header.timestamp) << ")" << "\n"
<< "previous hash: " << header.prev_hash << "\n"
<< "nonce: " << boost::lexical_cast<std::string>(header.nonce) << "\n"
<< "nonce: " << header.nonce << "\n"
<< "is orphan: " << header.orphan_status << "\n"
<< "height: " << boost::lexical_cast<std::string>(header.height) << "\n"
<< "depth: " << boost::lexical_cast<std::string>(header.depth) << "\n"
<< "height: " << header.height << "\n"
<< "depth: " << header.depth << "\n"
<< "hash: " << header.hash << "\n"
<< "difficulty: " << boost::lexical_cast<std::string>(header.difficulty) << "\n"
<< "cumulative_difficulty: " << boost::lexical_cast<std::string>(header.cumulative_difficulty) << "\n"
<< "difficulty: " << header.difficulty << "\n"
<< "cumulative_difficulty: " << header.cumulative_difficulty << "\n"
<< "POW hash: " << header.pow_hash.value_or("N/A") << "\n"
<< "block size: " << header.block_size << "\n"
<< "block weight: " << header.block_weight << "\n"
@ -176,7 +172,7 @@ namespace {
time_t dt = t > now ? t - now : now - t;
std::string s;
if (dt < 90)
s = boost::lexical_cast<std::string>(dt) + (abbreviate ? "sec" : dt == 1 ? " second" : " seconds");
s = std::to_string(dt) + (abbreviate ? "sec" : dt == 1 ? " second" : " seconds");
else if (dt < 90 * 60)
s = (boost::format(abbreviate ? "%.1fmin" : "%.1f minutes") % ((float)dt/60)).str();
else if (dt < 36 * 3600)
@ -937,7 +933,7 @@ static void print_pool(const std::vector<cryptonote::rpc::tx_info> &transactions
/// (we can't back out the individual per_out and per_byte that got used anyway).
<< "fee/byte: " << cryptonote::print_money(tx_info.fee / (double)tx_info.weight) << "\n"
<< "receive_time: " << tx_info.receive_time << " (" << get_human_time_ago(tx_info.receive_time, now) << ")\n"
<< "relayed: " << (tx_info.relayed ? boost::lexical_cast<std::string>(tx_info.last_relayed_time) + " (" + get_human_time_ago(tx_info.last_relayed_time, now) + ")" : "no") << "\n"
<< "relayed: " << (tx_info.relayed ? std::to_string(tx_info.last_relayed_time) + " (" + get_human_time_ago(tx_info.last_relayed_time, now) + ")" : "no") << "\n"
<< std::boolalpha
<< "do_not_relay: " << tx_info.do_not_relay << "\n"
<< "blink: " << tx_info.blink << "\n"
@ -1338,7 +1334,7 @@ bool rpc_command_executor::alt_chain_info(const std::string &tip, size_t above,
continue;
display.push_back(i);
}
tools::msg_writer() << boost::lexical_cast<std::string>(display.size()) << " alternate chains found:";
tools::msg_writer() << display.size() << " alternate chains found:";
for (const size_t idx: display)
{
const auto &chain = chains[idx];

View File

@ -2057,7 +2057,7 @@ namespace nodetool
if(!zone.m_peerlist.is_host_allowed(context.m_remote_address))
return false;
std::string port = epee::string_tools::num_to_string_fast(node_data.my_port);
std::string port = std::to_string(node_data.my_port);
epee::net_utils::network_address address;
if (ipv4_addr)
@ -2814,13 +2814,13 @@ namespace nodetool
{
const epee::net_utils::ipv4_network_address &ipv4 = na.as<const epee::net_utils::ipv4_network_address>();
address = epee::string_tools::get_ip_string_from_int32(ipv4.ip());
port = epee::string_tools::num_to_string_fast(ipv4.port());
port = std::to_string(ipv4.port());
}
else
{
const epee::net_utils::ipv6_network_address &ipv6 = na.as<const epee::net_utils::ipv6_network_address>();
address = ipv6.ip().to_string();
port = epee::string_tools::num_to_string_fast(ipv6.port());
port = std::to_string(ipv6.port());
}
typename net_server::t_connection_context con{};

View File

@ -37,7 +37,6 @@
#include "gtest/gtest.h"
#include "include_base_utils.h"
#include "misc_language.h"
#include "misc_log_ex.h"
#include "storages/levin_abstract_invoke2.h"

View File

@ -35,7 +35,6 @@
#include <boost/asio/io_service.hpp>
#include <boost/uuid/uuid_io.hpp>
#include "include_base_utils.h"
#include "string_tools.h"
#include "net/levin_protocol_handler_async.h"
#include "net/abstract_tcp_server2.h"

View File

@ -31,7 +31,6 @@
#include <thread>
#include <memory>
#include "include_base_utils.h"
#include "misc_log_ex.h"
#include "storages/levin_abstract_invoke2.h"
#include "common/util.h"

View File

@ -28,7 +28,6 @@
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#include "include_base_utils.h"
#include "cryptonote_basic/cryptonote_basic_impl.h"
#include "cryptonote_basic/account.h"
#include "cryptonote_core/cryptonote_tx_utils.h"

View File

@ -34,7 +34,6 @@
#include "gtest/gtest.h"
#include "include_base_utils.h"
#include "string_tools.h"
#include "net/abstract_tcp_server2.h"

View File

@ -32,7 +32,6 @@
#include "gtest/gtest.h"
#include "include_base_utils.h"
#include "string_tools.h"
#include "net/levin_protocol_handler_async.h"
#include "net/net_utils_base.h"