Skip to content

Commit

Permalink
Use IPP files for simple stream bodies.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed May 2, 2023
1 parent 16403d9 commit 59f6cf3
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 161 deletions.
142 changes: 142 additions & 0 deletions include/bitcoin/system/impl/stream/simple/istream.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,153 @@
#ifndef LIBBITCOIN_SYSTEM_STREAM_SIMPLE_ISTREAM_IPP
#define LIBBITCOIN_SYSTEM_STREAM_SIMPLE_ISTREAM_IPP

#include <algorithm>
#include <type_traits>
#include <bitcoin/system/define.hpp>

namespace libbitcoin {
namespace system {

template <typename Source, typename Character>
inline istream<Source, Character>::istream(const Source& source) NOEXCEPT
: position_(source.data()),
begin_(position_),
end_(begin_ + source.size()),
state_(goodbit)
{
}

template <typename Source, typename Character>
inline typename istream<Source, Character>::pos_type
istream<Source, Character>::tellg() const NOEXCEPT
{
return static_cast<pos_type>(position_ - begin_);
}

template <typename Source, typename Character>
inline istream<Source, Character>&
istream<Source, Character>::seekg(off_type offset, seekdir direction) NOEXCEPT
{
if (state_ != goodbit)
return *this;

using namespace system;
switch (direction)
{
case beg:
{
if (is_negative(offset) || (offset > (end_ - begin_)))
{
setstate(badbit);
break;
}

position_ = begin_ + offset;
break;
}
case cur:
{
if ((is_negative(offset) && (offset < (begin_ - position_))) ||
(is_positive(offset) && (offset > (end_ - position_))))
{
setstate(badbit);
break;
}

position_ = position_ + offset;
break;
}
case end:
{
if (is_positive(offset) || (offset < (begin_ - end_)))
{
setstate(badbit);
break;
}

position_ = end_ + offset;
break;
}
default:
{
setstate(failbit);
break;
}
}

return *this;
}

template <typename Source, typename Character>
inline typename istream<Source, Character>::iostate
istream<Source, Character>::rdstate() const NOEXCEPT
{
return state_;
}

template <typename Source, typename Character>
inline void
istream<Source, Character>::setstate(iostate state) NOEXCEPT
{
state_ |= state;
}

template <typename Source, typename Character>
inline void
istream<Source, Character>::clear(iostate state) NOEXCEPT
{
state_ = state;
}

template <typename Source, typename Character>
inline void
istream<Source, Character>::read(char_type* data, pos_type size) NOEXCEPT
{
if (is_overflow(size))
{
setstate(badbit);
return;
}

BC_PUSH_WARNING(NO_UNSAFE_COPY_N)
std::copy_n(position_, size, data);
BC_POP_WARNING()

position_ += size;
}

template <typename Source, typename Character>
inline typename istream<Source, Character>::int_type
istream<Source, Character>::peek() NOEXCEPT
{
constexpr auto eof = std::char_traits<Character>::eof();

if (is_overflow(1))
{
setstate(badbit);
return eof;
}

const uint8_t value = *position_;
return system::sign_cast<int_type>(value);
}

// private
template <typename Source, typename Character>
constexpr bool
istream<Source, Character>::is_positive(off_type value) NOEXCEPT
{
return !is_zero(value) && !system::is_negative(value);
}

// private
template <typename Source, typename Character>
inline bool
istream<Source, Character>::is_overflow(pos_type size) const NOEXCEPT
{
return (state_ != goodbit) || (size > (end_ - position_));
}

} // namespace system
} // namespace libbitcoin

Expand Down
70 changes: 70 additions & 0 deletions include/bitcoin/system/impl/stream/simple/ostream.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,81 @@
#ifndef LIBBITCOIN_SYSTEM_STREAM_SIMPLE_OSTREAM_IPP
#define LIBBITCOIN_SYSTEM_STREAM_SIMPLE_OSTREAM_IPP

#include <algorithm>
#include <bitcoin/system/define.hpp>

namespace libbitcoin {
namespace system {

template <typename Sink, typename Character>
inline ostream<Sink, Character>::ostream(Sink& sink) NOEXCEPT
: position_(sink.data()),
begin_(position_),
end_(begin_ + sink.size()),
state_(goodbit)
{
}

template <typename Sink, typename Character>
inline typename ostream<Sink, Character>::pos_type
ostream<Sink, Character>::tellp() const NOEXCEPT
{
return static_cast<pos_type>(position_ - begin_);
}

template <typename Sink, typename Character>
inline typename ostream<Sink, Character>::iostate
ostream<Sink, Character>::rdstate() const NOEXCEPT
{
return state_;
}

template <typename Sink, typename Character>
inline void
ostream<Sink, Character>::setstate(iostate state) NOEXCEPT
{
state_ |= state;
}

template <typename Sink, typename Character>
inline void
ostream<Sink, Character>::clear(iostate state) NOEXCEPT
{
state_ = state;
}

template <typename Sink, typename Character>
inline void
ostream<Sink, Character>::write(const char_type* data, pos_type size) NOEXCEPT
{
if (is_overflow(size))
{
setstate(badbit);
return;
}

BC_PUSH_WARNING(NO_UNSAFE_COPY_N)
std::copy_n(data, size, position_);
BC_POP_WARNING()

position_ += size;
}

template <typename Sink, typename Character>
inline void
ostream<Sink, Character>::flush() NOEXCEPT
{
// no-op.
}

// private
template <typename Sink, typename Character>
inline bool
ostream<Sink, Character>::is_overflow(pos_type size) const NOEXCEPT
{
return (state_ != goodbit) || (size > (end_ - position_));
}

} // namespace system
} // namespace libbitcoin

Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/stream/simple/iostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class iostream
{
DEFAULT_COPY_MOVE(iostream);

// Common based for state methods, just pick one.
/// Common based for state methods, just pick one.
using base = istream<Buffer, Character>;

/// Construct the object.
Expand Down
Loading

0 comments on commit 59f6cf3

Please sign in to comment.