27 #include "pplx/pplxtasks.h"
28 #include "cpprest/astreambuf.h"
29 #include "cpprest/streams.h"
33 #pragma warning(disable : 4250)
49 template<
typename _CharType>
52 typedef concurrency::streams::char_traits<_CharType> traits;
53 typedef typename traits::int_type int_type;
54 typedef typename traits::pos_type pos_type;
55 typedef typename traits::off_type off_type;
79 virtual bool can_seek()
const {
return this->
is_open(); }
80 virtual bool has_size()
const {
return false; }
82 virtual size_t in_avail()
const {
return (
size_t)m_buffer->in_avail(); }
84 virtual size_t buffer_size(std::ios_base::openmode)
const {
return 0; }
85 virtual void set_buffer_size(
size_t, std::ios_base::openmode) {
return; }
87 virtual pplx::task<bool> _sync() {
return pplx::task_from_result(m_buffer->pubsync() == 0); }
89 virtual pplx::task<int_type> _putc(_CharType ch) {
return pplx::task_from_result(m_buffer->sputc(ch)); }
90 virtual pplx::task<size_t> _putn(
const _CharType *ptr,
size_t size) {
return pplx::task_from_result((
size_t)m_buffer->sputn(ptr, size)); }
92 size_t _sgetn(_Out_writes_ (size) _CharType *ptr, _In_
size_t size)
const {
return m_buffer->sgetn(ptr, size); }
93 virtual size_t _scopy(_Out_writes_ (size) _CharType *, _In_
size_t size) { (void)(size);
return (
size_t)-1; }
95 virtual pplx::task<size_t> _getn(_Out_writes_ (size) _CharType *ptr, _In_
size_t size) {
return pplx::task_from_result((
size_t)m_buffer->sgetn(ptr, size)); }
97 virtual int_type _sbumpc() {
return m_buffer->sbumpc(); }
98 virtual int_type _sgetc() {
return m_buffer->sgetc(); }
100 virtual pplx::task<int_type> _bumpc() {
return pplx::task_from_result<int_type>(m_buffer->sbumpc()); }
101 virtual pplx::task<int_type> _getc() {
return pplx::task_from_result<int_type>(m_buffer->sgetc()); }
102 virtual pplx::task<int_type> _nextc() {
return pplx::task_from_result<int_type>(m_buffer->snextc()); }
103 virtual pplx::task<int_type> _ungetc() {
return pplx::task_from_result<int_type>(m_buffer->sungetc()); }
105 virtual pos_type getpos(std::ios_base::openmode mode)
const {
return m_buffer->pubseekoff(0, std::ios_base::cur, mode); }
106 virtual pos_type seekpos(pos_type pos, std::ios_base::openmode mode) {
return m_buffer->pubseekpos(pos, mode); }
107 virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode mode) {
return m_buffer->pubseekoff(off, dir, mode); }
109 virtual _CharType* _alloc(
size_t) {
return nullptr; }
110 virtual void _commit(
size_t) {}
112 virtual bool acquire(_CharType*&,
size_t&) {
return false; }
113 virtual void release(_CharType *,
size_t) { }
115 template<
typename CharType>
friend class concurrency::streams::stdio_ostream;
116 template<
typename CharType>
friend class concurrency::streams::stdio_istream;
118 std::basic_streambuf<_CharType>* m_buffer;
136 template<
typename CharType>
137 class stdio_ostream :
public basic_ostream<CharType>
147 template <
typename AlterCharType>
149 :
basic_ostream<CharType>(streams::streambuf<AlterCharType>(std::shared_ptr<details::basic_stdio_buffer<AlterCharType>>(new details::basic_stdio_buffer<AlterCharType>(stream.rdbuf(), std::ios_base::out))))
180 template<
typename CharType>
181 class stdio_istream :
public basic_istream<CharType>
191 template <
typename AlterCharType>
193 :
basic_istream<CharType>(streams::streambuf<AlterCharType>(std::shared_ptr<details::basic_stdio_buffer<AlterCharType>>(new details::basic_stdio_buffer<AlterCharType>(stream.rdbuf(), std::ios_base::in))))
217 template<
typename CharType>
221 typedef concurrency::streams::char_traits<CharType> traits;
222 typedef typename traits::int_type int_type;
223 typedef typename traits::pos_type pos_type;
224 typedef typename traits::off_type off_type;
242 return m_buffer.putc(CharType(ch)).get();
246 return traits::eof();
257 return m_buffer.getc().get();
261 return traits::eof();
272 return m_buffer.bumpc().get();
276 return traits::eof();
283 std::streamsize
xsgetn(_Out_writes_ (count) CharType* ptr, _In_ std::streamsize count)
285 size_t cnt = size_t(count);
286 size_t read_so_far = 0;
290 while (read_so_far < cnt)
292 size_t rd = m_buffer.getn(ptr+read_so_far, cnt-read_so_far).get();
308 std::streamsize
xsputn(
const CharType* ptr, std::streamsize count)
312 return m_buffer.putn_nocopy(ptr, static_cast<size_t>(count)).get();
327 m_buffer.sync().wait();
339 std::ios_base::seekdir dir,
340 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
344 if ( dir == std::ios_base::cur && offset == 0)
345 return m_buffer.getpos(mode);
346 return m_buffer.seekoff(offset,dir,mode);
350 return (pos_type(-1));
358 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
362 return m_buffer.seekpos(pos, mode);
366 return (pos_type(-1));
371 concurrency::streams::streambuf<CharType> m_buffer;
382 template<
typename CharType>
393 template <
typename AlterCharType>
396 m_strbuf(astream.streambuf())
407 template <
typename AlterCharType>
424 template<
typename CharType>
435 template <
typename AlterCharType>
438 m_strbuf(astream.streambuf())
449 template <
typename AlterCharType>
466 template<
typename CharType>
475 : std::basic_iostream<CharType>(&m_strbuf),
484 #if defined(__cplusplus_winrt)
503 _ASYNCRTIMP
static Windows::Storage::Streams::IInputStream^ __cdecl create_input_stream(
const concurrency::streams::streambuf<uint8_t> &buffer);
515 _ASYNCRTIMP
static Windows::Storage::Streams::IOutputStream^ __cdecl create_output_stream(
const concurrency::streams::streambuf<uint8_t> &buffer);
527 _ASYNCRTIMP
static Windows::Storage::Streams::IRandomAccessStream^ __cdecl create_random_access_stream(
const concurrency::streams::streambuf<uint8_t> &buffer);
stdio_ostream(const stdio_ostream &other)
Copy constructor
Definition: interopstream.h:157
int_type overflow(int_type ch)
Writes one byte to the stream buffer.
Definition: interopstream.h:238
int_type underflow()
Gets one byte from the stream buffer without moving the read position.
Definition: interopstream.h:253
int_type uflow()
Gets one byte from the stream buffer and move the read position one character.
Definition: interopstream.h:268
int sync()
Synchronizes with the underlying medium.
Definition: interopstream.h:323
std::streamsize xsgetn(_Out_writes_(count) CharType *ptr, _In_ std::streamsize count)
Gets a number of characters from the buffer and place it into the provided memory block...
Definition: interopstream.h:283
virtual bool is_open() const
Checks if the stream buffer is open.
Definition: astreambuf.h:390
basic_ostream & operator=(const basic_ostream &other)
Assignment operator
Definition: streams.h:132
Reference-counted stream buffer.
Definition: astreambuf.h:804
async_ostream(const streams::streambuf< AlterCharType > &strbuf)
Constructor
Definition: interopstream.h:408
stdio_istream & operator=(const stdio_istream &other)
Assignment operator
Definition: interopstream.h:208
pos_type seekoff(off_type offset, std::ios_base::seekdir dir, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
Seeks to the given offset relative to the beginning, end, or current position.
Definition: interopstream.h:338
Base interface for all asynchronous output streams.
Definition: astreambuf.h:792
async_istream(const streams::basic_istream< AlterCharType > &astream)
Constructor
Definition: interopstream.h:436
stdio_istream(std::basic_istream< AlterCharType > &stream)
Constructor
Definition: interopstream.h:192
pos_type seekpos(pos_type pos, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
Seeks to the given offset relative to the beginning of the stream.
Definition: interopstream.h:357
async_istream(const streams::streambuf< AlterCharType > &strbuf)
Constructor
Definition: interopstream.h:450
A concrete STL ostream which relies on an asynchronous stream for its I/O.
Definition: interopstream.h:383
virtual utility::size64_t size() const
Gets the size of the stream, if known. Calls to has_size will determine whether the result of size ca...
Definition: astreambuf.h:676
async_ostream(const streams::basic_ostream< AlterCharType > &astream)
Constructor
Definition: interopstream.h:394
A concrete STL istream which relies on an asynchronous stream buffer for its I/O. ...
Definition: interopstream.h:467
stdio_ostream represents an async ostream derived from a standard synchronous stream, as defined by the "std" namespace. It is constructed from a reference to a standard stream, which must be valid for the lifetime of the asynchronous stream.
Definition: interopstream.h:38
virtual pplx::task< void > _close_read()
The real read head close operation, implementation should override it if there is any resource to be ...
Definition: astreambuf.h:711
basic_istream & operator=(const basic_istream &other)
Assignment operator
Definition: streams.h:606
virtual pplx::task< void > _close_write()
The real write head close operation, implementation should override it if there is any resource to be...
Definition: astreambuf.h:720
Definition: astreambuf.h:362
The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed as...
Definition: pplxtasks.h:176
A concrete STL istream which relies on an asynchronous stream for its I/O.
Definition: interopstream.h:425
async_iostream(const streams::streambuf< CharType > &strbuf)
Constructor
Definition: interopstream.h:474
IO streams stream buffer implementation used to interface with an async streambuffer underneath...
Definition: interopstream.h:218
stdio_istream represents an async istream derived from a standard synchronous stream, as defined by the "std" namespace. It is constructed from a reference to a standard stream, which must be valid for the lifetime of the asynchronous stream.
Definition: interopstream.h:39
Base interface for all asynchronous input streams.
Definition: astreambuf.h:791
Definition: astreambuf.h:37
virtual ~basic_stdio_buffer()
Destructor
Definition: interopstream.h:68
The basic_stdio_buffer class serves to support interoperability with STL stream buffers. Sitting atop a std::streambuf, which does all the I/O, instances of this class may read and write data to standard iostreams. The class itself should not be used in application code, it is used by the stream definitions farther down in the header file.
Definition: interopstream.h:50
stdio_ostream(std::basic_ostream< AlterCharType > &stream)
Constructor
Definition: interopstream.h:148
std::streamsize xsputn(const CharType *ptr, std::streamsize count)
Writes a given number of characters from the provided block into the stream buffer.
Definition: interopstream.h:308
stdio_ostream & operator=(const stdio_ostream &other)
Assignment operator
Definition: interopstream.h:164
stdio_istream(const stdio_istream &other)
Copy constructor
Definition: interopstream.h:201