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] 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>;