Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::buffered_istream Class Reference

Reads data from an unbuffered stream and enables peek functionality. More...

#include <buffered_iostream.hpp>

Inheritance diagram for fc::buffered_istream:
Collaboration diagram for fc::buffered_istream:

Public Member Functions

 buffered_istream (istream_ptr is)
 
 buffered_istream (buffered_istream &&o)
 
buffered_istreamoperator= (buffered_istream &&i)
 
virtual ~buffered_istream ()
 
virtual std::size_t readsome (char *buf, std::size_t len)
 
virtual size_t readsome (const std::shared_ptr< char > &buf, size_t len, size_t offset)
 
virtual char peek () const
 

Detailed Description

Definition at line 16 of file buffered_iostream.hpp.

Constructor & Destructor Documentation

◆ buffered_istream() [1/2]

fc::buffered_istream::buffered_istream ( istream_ptr is)

Definition at line 32 of file buffered_iostream.cpp.

33 :my( new detail::buffered_istream_impl( fc::move(is) ) )
34 {
35 FC_ASSERT( my->_istr != nullptr, " this shouldn't be null" );
36 }
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.

◆ buffered_istream() [2/2]

fc::buffered_istream::buffered_istream ( buffered_istream && o)

Definition at line 38 of file buffered_iostream.cpp.

39 :my( fc::move(o.my) ){}

◆ ~buffered_istream()

fc::buffered_istream::~buffered_istream ( )
virtual

Definition at line 47 of file buffered_iostream.cpp.

47{}

Member Function Documentation

◆ operator=()

buffered_istream & fc::buffered_istream::operator= ( buffered_istream && i)

Definition at line 41 of file buffered_iostream.cpp.

42 {
43 my = fc::move(i.my);
44 return *this;
45 }

◆ peek()

char fc::buffered_istream::peek ( ) const
virtual

This method may block until at least 1 character is available.

Definition at line 112 of file buffered_iostream.cpp.

113 {
114 if( my->_rdbuf.size() )
115 {
116 return my->_rdbuf.sgetc();
117 }
118
119 char tmp[detail::minimum_read_size];
120 size_t bytes_read = my->_istr->readsome( tmp, detail::minimum_read_size );
121 my->_rdbuf.sputn( tmp, bytes_read );
122
123 if( my->_rdbuf.size() )
124 {
125 return my->_rdbuf.sgetc();
126 }
127 FC_THROW_EXCEPTION( assert_exception,
128 "at least one byte should be available, or eof should have been thrown" );
129 }
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)

◆ readsome() [1/2]

virtual std::size_t fc::buffered_istream::readsome ( char * buf,
std::size_t len )
virtual

read at least 1 byte or throw, if no data is available this method should block cooperatively until data is available or fc::eof_exception is thrown.

Precondition
len > 0
buf != nullptr
Exceptions
fc::eofif at least 1 byte cannot be read

◆ readsome() [2/2]

size_t fc::buffered_istream::readsome ( const std::shared_ptr< char > & buf,
size_t len,
size_t offset )
virtual

Definition at line 75 of file buffered_iostream.cpp.

76 {
77 size_t bytes_from_rdbuf = static_cast<size_t>(my->_rdbuf.sgetn(buf.get() + offset, len));
78 if (bytes_from_rdbuf)
79 return bytes_from_rdbuf;
80
81
82 if( len > detail::minimum_read_size )
83 return my->_istr->readsome(buf.get() + offset, len);
84
85#ifndef NDEBUG
86 // This code was written with the assumption that you'd only be making one call to readsome
87 // at a time so it reuses _shared_read_buffer. If you really need to make concurrent calls to
88 // readsome(), you'll need to prevent reusing _shared_read_buffer here
89 struct check_buffer_in_use {
90 bool& _buffer_in_use;
91 check_buffer_in_use(bool& buffer_in_use) : _buffer_in_use(buffer_in_use) { assert(!_buffer_in_use); _buffer_in_use = true; }
92 ~check_buffer_in_use() { assert(_buffer_in_use); _buffer_in_use = false; }
93 } buffer_in_use_checker(my->_shared_read_buffer_in_use);
94#endif
95
96 if (!my->_shared_read_buffer)
97 my->_shared_read_buffer.reset(new char[detail::minimum_read_size], [](char* p){ delete[] p; });
98 size_t bytes_read = my->_istr->readsome( my->_shared_read_buffer, detail::minimum_read_size, 0 );
99
100 size_t bytes_to_deliver_immediately = std::min<size_t>(bytes_read,len);
101
102 memcpy( buf.get() + offset, my->_shared_read_buffer.get(), bytes_to_deliver_immediately );
103
104 if( bytes_read > len )
105 {
106 my->_rdbuf.sputn( my->_shared_read_buffer.get() + len, bytes_read - len );
107 }
108
109 return bytes_to_deliver_immediately;
110 }
const mie::Vuint & p
Definition bn.cpp:27
size_t len
uint8_t buf[2048]
memcpy((char *) pInfo->slotDescription, s, l)
Here is the call graph for this function:

The documentation for this class was generated from the following files: