Skip to content

Commit

Permalink
Move to a dedicated char_traits header
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Sep 26, 2023
1 parent 046fa71 commit 0169487
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 71 deletions.
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 ();
}
};
72 changes: 1 addition & 71 deletions nano/lib/stream.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

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

#include <boost/endian/conversion.hpp>
Expand All @@ -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<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 ();
}
};

// We operate on streams of uint8_t by convention
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>;
Expand Down

0 comments on commit 0169487

Please sign in to comment.