Skip to content

Commit

Permalink
Change simple stream enums to static constexpr, add streamer tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed May 2, 2023
1 parent 1bba7b9 commit eab84dd
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 146 deletions.
4 changes: 2 additions & 2 deletions include/bitcoin/system/stream/simple/iostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class iostream
base::setstate(state);
}

inline void clear(typename base::iostate state=
base::iostate::goodbit) NOEXCEPT override
inline void clear(
typename base::iostate state=base::goodbit) NOEXCEPT override
{
base::clear(state);
}
Expand Down
42 changes: 19 additions & 23 deletions include/bitcoin/system/stream/simple/istream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,24 @@ class istream
using off_type = typename std::basic_ios<char_type>::off_type;
using pos_type = typename std::basic_ios<char_type>::pos_type;
using failure = typename std::ios_base::failure;
enum iostate
{
goodbit = 0,
eofbit = 1,
failbit = 2,
badbit = 4
};
enum seekdir
{
beg = 0,
cur = 1,
end = 2
};

using iostate = uint8_t;
static constexpr iostate goodbit = 0;
static constexpr iostate eofbit = 1;
static constexpr iostate failbit = 2;
static constexpr iostate badbit = 4;

using seekdir = uint8_t;
static constexpr seekdir beg = 0;
static constexpr seekdir cur = 1;
static constexpr seekdir end = 2;

/// Construct the object.
inline istream(const Source& source) NOEXCEPT
: position_(source.data()),
begin_(position_),
end_(begin_ + source.size()),
state_(iostate::goodbit)
state_(goodbit)
{
}

Expand All @@ -74,13 +72,13 @@ class istream
/// Set the relative input position indicator (zero-based).
virtual inline istream& seekg(off_type offset, seekdir direction) NOEXCEPT
{
if (state_ != iostate::goodbit)
if (state_ != goodbit)
return *this;

using namespace system;
switch (direction)
{
case seekdir::beg:
case beg:
{
if (is_negative(offset) || (offset > (end_ - begin_)))
{
Expand All @@ -91,7 +89,7 @@ class istream
position_ = begin_ + offset;
break;
}
case seekdir::cur:
case cur:
{
if ((is_negative(offset) && (offset < (begin_ - position_))) ||
(is_positive(offset) && (offset > (end_ - position_))))
Expand All @@ -103,7 +101,7 @@ class istream
position_ = position_ + offset;
break;
}
case seekdir::end:
case end:
{
if (is_positive(offset) || (offset < (begin_ - end_)))
{
Expand Down Expand Up @@ -133,13 +131,11 @@ class istream
/// Set the stream error flags state in addition to currently set flags.
virtual inline void setstate(iostate state) NOEXCEPT
{
state_ = static_cast<iostate>(
static_cast<std::underlying_type_t<iostate>>(state_) |
static_cast<std::underlying_type_t<iostate>>(state));
state_ |= state;
}

/// Set the stream error state flags by assigning the state value.
virtual inline void clear(iostate state=iostate::goodbit) NOEXCEPT
virtual inline void clear(iostate state=goodbit) NOEXCEPT
{
state_ = state;
}
Expand Down Expand Up @@ -183,7 +179,7 @@ class istream

inline bool is_overflow(pos_type size) const NOEXCEPT
{
return (state_ != iostate::goodbit) || (size > (end_ - position_));
return (state_ != goodbit) || (size > (end_ - position_));
}

const uint8_t* position_;
Expand Down
23 changes: 10 additions & 13 deletions include/bitcoin/system/stream/simple/ostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,19 @@ class ostream
using char_type = Character;
using pos_type = typename std::basic_ios<char_type>::pos_type;
using failure = typename std::ios_base::failure;
enum iostate
{
goodbit = 0,
eofbit = 1,
failbit = 2,
badbit = 4
};

using iostate = uint8_t;
static constexpr iostate goodbit = 0;
static constexpr iostate eofbit = 1;
static constexpr iostate failbit = 2;
static constexpr iostate badbit = 4;

/// Construct the object.
inline ostream(Sink& sink) NOEXCEPT
: position_(sink.data()),
begin_(position_),
end_(begin_ + sink.size()),
state_(iostate::goodbit)
state_(goodbit)
{
}

Expand All @@ -71,13 +70,11 @@ class ostream
/// Set the stream error flags state in addition to currently set flags.
virtual inline void setstate(iostate state) NOEXCEPT
{
state_ = static_cast<iostate>(
static_cast<std::underlying_type_t<iostate>>(state_) |
static_cast<std::underlying_type_t<iostate>>(state));
state_ |= state;
}

/// Set the stream error state flags by assigning the state value.
virtual void clear(iostate state=iostate::goodbit) NOEXCEPT
virtual void clear(iostate state=goodbit) NOEXCEPT
{
state_ = state;
}
Expand Down Expand Up @@ -107,7 +104,7 @@ class ostream
private:
inline bool is_overflow(pos_type size) const NOEXCEPT
{
return (state_ != iostate::goodbit) || (size > (end_ - position_));
return (state_ != goodbit) || (size > (end_ - position_));
}

uint8_t* position_;
Expand Down
6 changes: 3 additions & 3 deletions test/stream/simple/iostream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ auto chunk = base16_chunk("01020304050607080900");
BOOST_AUTO_TEST_CASE(iostream__setstate__goodbit__goodbit)
{
iostream_chunk stream{ {} };
stream.setstate(iostate::goodbit);
BOOST_REQUIRE(stream.rdstate() == iostate::goodbit);
stream.setstate(iostream_chunk::goodbit);
BOOST_REQUIRE(stream.rdstate() == iostream_chunk::goodbit);
}

BOOST_AUTO_TEST_CASE(iostream__tellg__initial__zero_goodbit)
{
const iostream_chunk stream{ chunk };
BOOST_REQUIRE(is_zero(stream.tellg()));
BOOST_REQUIRE(stream.rdstate() == iostate::goodbit);
BOOST_REQUIRE(stream.rdstate() == iostream_chunk::goodbit);
}

BOOST_AUTO_TEST_SUITE_END()
Loading

0 comments on commit eab84dd

Please sign in to comment.