Skip to content

Commit

Permalink
Fix deprecation warning about using char_traits<uint8_t> (#4298)
Browse files Browse the repository at this point in the history
Create an implementation of char_traits to replace deprecated equivalent in the standard library.
  • Loading branch information
pwojcikdev authored Sep 28, 2023
1 parent a1cc7a5 commit daf5b1e
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 28 deletions.
2 changes: 1 addition & 1 deletion nano/core_test/block.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <nano/lib/stream.hpp>
#include <nano/node/common.hpp>
#include <nano/secure/buffer.hpp>
#include <nano/test_common/testutil.hpp>

#include <gtest/gtest.h>
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/message.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <nano/crypto_lib/random_pool.hpp>
#include <nano/lib/stream.hpp>
#include <nano/node/common.hpp>
#include <nano/node/network.hpp>
#include <nano/secure/buffer.hpp>
#include <nano/test_common/testutil.hpp>

#include <gtest/gtest.h>
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/network_filter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <nano/lib/stream.hpp>
#include <nano/node/common.hpp>
#include <nano/secure/buffer.hpp>
#include <nano/secure/common.hpp>
#include <nano/secure/network_filter.hpp>
#include <nano/test_common/testutil.hpp>
Expand Down
1 change: 1 addition & 0 deletions nano/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_library(
blockbuilders.cpp
blocks.hpp
blocks.cpp
char_traits.hpp
cli.hpp
cli.cpp
config.hpp
Expand Down
75 changes: 75 additions & 0 deletions nano/lib/char_traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#pragma once

#include <cstdint>
#include <string>

/**
* A traits class to allow us to use uint8_t as a char type for streams
* Using `char_traits<uint8_t>` directly is not specified by the standard, is deprecated and scheduled for removal
* Based on implementation from clang
*/
struct uint8_char_traits
{
using char_type = uint8_t;
using int_type = std::char_traits<char>::int_type;
using off_type = std::char_traits<char>::off_type;
using pos_type = std::char_traits<char>::pos_type;
using state_type = std::char_traits<char>::state_type;

static inline void assign (char_type & a, const char_type & b) noexcept
{
a = b;
}
static inline bool eq (char_type a, char_type b) noexcept
{
return a == b;
}
static inline bool lt (char_type a, char_type b) noexcept
{
return a < b;
}
static int compare (const char_type * a, const char_type * b, size_t size)
{
return std::char_traits<char>::compare (reinterpret_cast<char const *> (a), reinterpret_cast<char const *> (b), size);
}
static inline size_t length (const char_type * a)
{
return std::char_traits<char>::length (reinterpret_cast<char const *> (a));
}
static inline const char_type * find (const char_type * a, size_t size, const char_type & b)
{
return reinterpret_cast<const char_type *> (std::char_traits<char>::find (reinterpret_cast<char const *> (a), size, reinterpret_cast<char const &> (b)));
}
static char_type * move (char_type * a, const char_type * b, size_t size)
{
return reinterpret_cast<char_type *> (std::char_traits<char>::move (reinterpret_cast<char *> (a), reinterpret_cast<char const *> (b), size));
}
static char_type * copy (char_type * a, const char_type * b, size_t size)
{
return reinterpret_cast<char_type *> (std::char_traits<char>::copy (reinterpret_cast<char *> (a), reinterpret_cast<char const *> (b), size));
}
static char_type * assign (char_type * a, size_t size, char_type b)
{
return reinterpret_cast<char_type *> (std::char_traits<char>::assign (reinterpret_cast<char *> (a), size, reinterpret_cast<char const &> (b)));
}
static inline int_type not_eof (int_type v) noexcept
{
return std::char_traits<char>::not_eof (v);
}
static inline char_type to_char_type (int_type v) noexcept
{
return char_type (v);
}
static inline int_type to_int_type (char_type v) noexcept
{
return int_type (v);
}
static inline bool eq_int_type (int_type a, int_type b) noexcept
{
return a == b;
}
static inline int_type eof () noexcept
{
return std::char_traits<char>::eof ();
}
};
10 changes: 9 additions & 1 deletion nano/lib/stream.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
#pragma once

#include <nano/lib/char_traits.hpp>
#include <nano/lib/utility.hpp>

#include <boost/endian/conversion.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/stream_buffer.hpp>

#include <streambuf>
#include <string>
#include <vector>

namespace nano
{
// We operate on streams of uint8_t by convention
using stream = std::basic_streambuf<uint8_t>;
using stream = std::basic_streambuf<uint8_t, uint8_char_traits>;
using bufferstream = boost::iostreams::stream_buffer<boost::iostreams::basic_array_source<uint8_t>, uint8_char_traits>;
using vectorstream = boost::iostreams::stream_buffer<boost::iostreams::back_insert_device<std::vector<uint8_t>>, uint8_char_traits>;

// Read a raw byte stream the size of `T' and fill value. Returns true if there was an error, false otherwise
template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion nano/node/bootstrap/block_deserializer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <nano/lib/blocks.hpp>
#include <nano/lib/stream.hpp>
#include <nano/node/bootstrap/block_deserializer.hpp>
#include <nano/node/transport/socket.hpp>
#include <nano/secure/buffer.hpp>

nano::bootstrap::block_deserializer::block_deserializer () :
read_buffer{ std::make_shared<std::vector<uint8_t>> () }
Expand Down
2 changes: 1 addition & 1 deletion nano/node/common.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <nano/lib/blocks.hpp>
#include <nano/lib/memory.hpp>
#include <nano/lib/stream.hpp>
#include <nano/node/active_transactions.hpp>
#include <nano/node/common.hpp>
#include <nano/node/election.hpp>
#include <nano/node/network.hpp>
#include <nano/node/wallet.hpp>
#include <nano/secure/buffer.hpp>

#include <boost/format.hpp>

Expand Down
1 change: 0 additions & 1 deletion nano/node/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <nano/node/election.hpp>
#include <nano/node/messages.hpp>
#include <nano/node/network.hpp>
#include <nano/secure/buffer.hpp>

#include <boost/asio/ip/address_v6.hpp>
#include <boost/endian/conversion.hpp>
Expand Down
2 changes: 1 addition & 1 deletion nano/node/node.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <nano/lib/stream.hpp>
#include <nano/lib/threading.hpp>
#include <nano/lib/tomlconfig.hpp>
#include <nano/lib/utility.hpp>
Expand All @@ -12,7 +13,6 @@
#include <nano/node/scheduler/priority.hpp>
#include <nano/node/telemetry.hpp>
#include <nano/node/websocket.hpp>
#include <nano/secure/buffer.hpp>
#include <nano/store/component.hpp>
#include <nano/store/rocksdb/rocksdb.hpp>

Expand Down
1 change: 0 additions & 1 deletion nano/secure/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ add_library(
${PLATFORM_SECURE_SOURCE}
${CMAKE_BINARY_DIR}/bootstrap_weights_live.cpp
${CMAKE_BINARY_DIR}/bootstrap_weights_beta.cpp
buffer.hpp
common.hpp
common.cpp
ledger.hpp
Expand Down
13 changes: 0 additions & 13 deletions nano/secure/buffer.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion nano/secure/network_filter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <nano/crypto_lib/random_pool.hpp>
#include <nano/lib/locks.hpp>
#include <nano/secure/buffer.hpp>
#include <nano/lib/stream.hpp>
#include <nano/secure/common.hpp>
#include <nano/secure/network_filter.hpp>

Expand Down
6 changes: 3 additions & 3 deletions nano/store/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <nano/crypto_lib/random_pool.hpp>
#include <nano/lib/memory.hpp>
#include <nano/secure/buffer.hpp>
#include <nano/lib/stream.hpp>
#include <nano/secure/common.hpp>
#include <nano/store/tables.hpp>
#include <nano/store/transaction.hpp>
Expand Down Expand Up @@ -33,8 +33,8 @@ class ledger_cache;
namespace store
{
/**
* Store manager
*/
* Store manager
*/
class component
{
friend class rocksdb_block_store_tombstone_count_Test;
Expand Down
3 changes: 2 additions & 1 deletion nano/store/db_val.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <nano/lib/numbers.hpp>
#include <nano/secure/buffer.hpp>
#include <nano/lib/stream.hpp>
#include <nano/secure/common.hpp>
#include <nano/store/block.hpp>
#include <nano/store/component.hpp>
Expand Down
2 changes: 1 addition & 1 deletion nano/store/lmdb/lmdb.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <nano/lib/stream.hpp>
#include <nano/lib/utility.hpp>
#include <nano/secure/buffer.hpp>
#include <nano/secure/ledger.hpp>
#include <nano/store/lmdb/iterator.hpp>
#include <nano/store/lmdb/lmdb.hpp>
Expand Down

0 comments on commit daf5b1e

Please sign in to comment.