From 301a796af82ab9f6e33b6a59862534a0263ffe43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 26 Sep 2023 21:43:04 +0200 Subject: [PATCH 1/3] Reimplement custom `char_traits` template for `uint8_t` --- nano/lib/stream.hpp | 80 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/nano/lib/stream.hpp b/nano/lib/stream.hpp index 77bb86185c..c2fe4737d2 100644 --- a/nano/lib/stream.hpp +++ b/nano/lib/stream.hpp @@ -3,13 +3,91 @@ #include #include +#include +#include +#include #include +#include +#include namespace nano { +/** + * A traits class to allow us to use uint8_t as a char type for streams + * Using `char_traits` 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::int_type; + using off_type = std::char_traits::off_type; + using pos_type = std::char_traits::pos_type; + using state_type = std::char_traits::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::compare (reinterpret_cast (a), reinterpret_cast (b), size); + } + static inline size_t length (const char_type * a) + { + return std::char_traits::length (reinterpret_cast (a)); + } + static inline const char_type * find (const char_type * a, size_t size, const char_type & b) + { + return reinterpret_cast (std::char_traits::find (reinterpret_cast (a), size, reinterpret_cast (b))); + } + static char_type * move (char_type * a, const char_type * b, size_t size) + { + return reinterpret_cast (std::char_traits::move (reinterpret_cast (a), reinterpret_cast (b), size)); + } + static char_type * copy (char_type * a, const char_type * b, size_t size) + { + return reinterpret_cast (std::char_traits::copy (reinterpret_cast (a), reinterpret_cast (b), size)); + } + static char_type * assign (char_type * a, size_t size, char_type b) + { + return reinterpret_cast (std::char_traits::assign (reinterpret_cast (a), size, reinterpret_cast (b))); + } + static inline int_type not_eof (int_type v) noexcept + { + return std::char_traits::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::eof (); + } +}; + // We operate on streams of uint8_t by convention -using stream = std::basic_streambuf; +using stream = std::basic_streambuf; +using bufferstream = boost::iostreams::stream_buffer, uint8_char_traits>; +using vectorstream = boost::iostreams::stream_buffer>, 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 From 046fa71e16537cfec368d901894c9e03447919ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 26 Sep 2023 21:51:18 +0200 Subject: [PATCH 2/3] Remove `secure/buffer.hpp` header --- nano/core_test/block.cpp | 2 +- nano/core_test/message.cpp | 2 +- nano/core_test/network_filter.cpp | 2 +- nano/node/bootstrap/block_deserializer.cpp | 2 +- nano/node/common.cpp | 2 +- nano/node/lmdb/lmdb.cpp | 2 +- nano/node/messages.cpp | 1 - nano/node/node.cpp | 2 +- nano/secure/CMakeLists.txt | 1 - nano/secure/buffer.hpp | 13 ------------- nano/secure/network_filter.cpp | 2 +- nano/secure/store.hpp | 2 +- 12 files changed, 9 insertions(+), 24 deletions(-) delete mode 100644 nano/secure/buffer.hpp diff --git a/nano/core_test/block.cpp b/nano/core_test/block.cpp index 8e1e3c7ea8..ac0450595b 100644 --- a/nano/core_test/block.cpp +++ b/nano/core_test/block.cpp @@ -1,5 +1,5 @@ +#include #include -#include #include #include diff --git a/nano/core_test/message.cpp b/nano/core_test/message.cpp index fcec2417bc..082a600978 100644 --- a/nano/core_test/message.cpp +++ b/nano/core_test/message.cpp @@ -1,7 +1,7 @@ #include +#include #include #include -#include #include #include diff --git a/nano/core_test/network_filter.cpp b/nano/core_test/network_filter.cpp index e21af52cd8..21f6f7ec58 100644 --- a/nano/core_test/network_filter.cpp +++ b/nano/core_test/network_filter.cpp @@ -1,5 +1,5 @@ +#include #include -#include #include #include #include diff --git a/nano/node/bootstrap/block_deserializer.cpp b/nano/node/bootstrap/block_deserializer.cpp index 6eaaf29049..23a4d30d83 100644 --- a/nano/node/bootstrap/block_deserializer.cpp +++ b/nano/node/bootstrap/block_deserializer.cpp @@ -1,7 +1,7 @@ #include +#include #include #include -#include nano::bootstrap::block_deserializer::block_deserializer () : read_buffer{ std::make_shared> () } diff --git a/nano/node/common.cpp b/nano/node/common.cpp index 52b7dce80c..9a4de71724 100644 --- a/nano/node/common.cpp +++ b/nano/node/common.cpp @@ -1,11 +1,11 @@ #include #include +#include #include #include #include #include #include -#include #include diff --git a/nano/node/lmdb/lmdb.cpp b/nano/node/lmdb/lmdb.cpp index 00c60ad345..4c28763d46 100644 --- a/nano/node/lmdb/lmdb.cpp +++ b/nano/node/lmdb/lmdb.cpp @@ -1,9 +1,9 @@ +#include #include #include #include #include #include -#include #include #include diff --git a/nano/node/messages.cpp b/nano/node/messages.cpp index 2417eddf18..0f4699c009 100644 --- a/nano/node/messages.cpp +++ b/nano/node/messages.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 5918036783..002d9791f1 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -12,7 +13,6 @@ #include #include #include -#include #include #include diff --git a/nano/secure/CMakeLists.txt b/nano/secure/CMakeLists.txt index 3ad32ee8de..1948b1238f 100644 --- a/nano/secure/CMakeLists.txt +++ b/nano/secure/CMakeLists.txt @@ -41,7 +41,6 @@ add_library( ${CMAKE_BINARY_DIR}/bootstrap_weights_beta.cpp store.hpp store.cpp - buffer.hpp common.hpp common.cpp ledger.hpp diff --git a/nano/secure/buffer.hpp b/nano/secure/buffer.hpp deleted file mode 100644 index d1a4511789..0000000000 --- a/nano/secure/buffer.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include -#include -#include - -#include - -namespace nano -{ -using bufferstream = boost::iostreams::stream_buffer>; -using vectorstream = boost::iostreams::stream_buffer>>; -} diff --git a/nano/secure/network_filter.cpp b/nano/secure/network_filter.cpp index 4b84f5e334..059cddb30e 100644 --- a/nano/secure/network_filter.cpp +++ b/nano/secure/network_filter.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include diff --git a/nano/secure/store.hpp b/nano/secure/store.hpp index 4e952440b8..57f0ac0492 100644 --- a/nano/secure/store.hpp +++ b/nano/secure/store.hpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include From 0169487c318daff670d2d03208cdccd78701fd8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 26 Sep 2023 22:05:35 +0200 Subject: [PATCH 3/3] Move to a dedicated `char_traits` header --- nano/lib/CMakeLists.txt | 1 + nano/lib/char_traits.hpp | 75 ++++++++++++++++++++++++++++++++++++++++ nano/lib/stream.hpp | 72 +------------------------------------- 3 files changed, 77 insertions(+), 71 deletions(-) create mode 100644 nano/lib/char_traits.hpp diff --git a/nano/lib/CMakeLists.txt b/nano/lib/CMakeLists.txt index 2002406120..8e24640dc9 100644 --- a/nano/lib/CMakeLists.txt +++ b/nano/lib/CMakeLists.txt @@ -25,6 +25,7 @@ add_library( blockbuilders.cpp blocks.hpp blocks.cpp + char_traits.hpp cli.hpp cli.cpp config.hpp diff --git a/nano/lib/char_traits.hpp b/nano/lib/char_traits.hpp new file mode 100644 index 0000000000..b21f2364a0 --- /dev/null +++ b/nano/lib/char_traits.hpp @@ -0,0 +1,75 @@ +#pragma once + +#include +#include + +/** + * A traits class to allow us to use uint8_t as a char type for streams + * Using `char_traits` 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::int_type; + using off_type = std::char_traits::off_type; + using pos_type = std::char_traits::pos_type; + using state_type = std::char_traits::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::compare (reinterpret_cast (a), reinterpret_cast (b), size); + } + static inline size_t length (const char_type * a) + { + return std::char_traits::length (reinterpret_cast (a)); + } + static inline const char_type * find (const char_type * a, size_t size, const char_type & b) + { + return reinterpret_cast (std::char_traits::find (reinterpret_cast (a), size, reinterpret_cast (b))); + } + static char_type * move (char_type * a, const char_type * b, size_t size) + { + return reinterpret_cast (std::char_traits::move (reinterpret_cast (a), reinterpret_cast (b), size)); + } + static char_type * copy (char_type * a, const char_type * b, size_t size) + { + return reinterpret_cast (std::char_traits::copy (reinterpret_cast (a), reinterpret_cast (b), size)); + } + static char_type * assign (char_type * a, size_t size, char_type b) + { + return reinterpret_cast (std::char_traits::assign (reinterpret_cast (a), size, reinterpret_cast (b))); + } + static inline int_type not_eof (int_type v) noexcept + { + return std::char_traits::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::eof (); + } +}; \ No newline at end of file diff --git a/nano/lib/stream.hpp b/nano/lib/stream.hpp index c2fe4737d2..895719e801 100644 --- a/nano/lib/stream.hpp +++ b/nano/lib/stream.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -13,77 +14,6 @@ namespace nano { -/** - * A traits class to allow us to use uint8_t as a char type for streams - * Using `char_traits` 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::int_type; - using off_type = std::char_traits::off_type; - using pos_type = std::char_traits::pos_type; - using state_type = std::char_traits::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::compare (reinterpret_cast (a), reinterpret_cast (b), size); - } - static inline size_t length (const char_type * a) - { - return std::char_traits::length (reinterpret_cast (a)); - } - static inline const char_type * find (const char_type * a, size_t size, const char_type & b) - { - return reinterpret_cast (std::char_traits::find (reinterpret_cast (a), size, reinterpret_cast (b))); - } - static char_type * move (char_type * a, const char_type * b, size_t size) - { - return reinterpret_cast (std::char_traits::move (reinterpret_cast (a), reinterpret_cast (b), size)); - } - static char_type * copy (char_type * a, const char_type * b, size_t size) - { - return reinterpret_cast (std::char_traits::copy (reinterpret_cast (a), reinterpret_cast (b), size)); - } - static char_type * assign (char_type * a, size_t size, char_type b) - { - return reinterpret_cast (std::char_traits::assign (reinterpret_cast (a), size, reinterpret_cast (b))); - } - static inline int_type not_eof (int_type v) noexcept - { - return std::char_traits::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::eof (); - } -}; - // We operate on streams of uint8_t by convention using stream = std::basic_streambuf; using bufferstream = boost::iostreams::stream_buffer, uint8_char_traits>;