28 #ifndef _CASA_RAWPTR_STREAMS_H
29 #define _CASA_RAWPTR_STREAMS_H
36 #include "pplx/pplxtasks.h"
37 #include "cpprest/astreambuf.h"
38 #include "cpprest/streams.h"
51 template<
typename _CharType>
55 typedef _CharType char_type;
57 typedef typename basic_streambuf<_CharType>::traits traits;
58 typedef typename basic_streambuf<_CharType>::int_type int_type;
59 typedef typename basic_streambuf<_CharType>::pos_type pos_type;
60 typedef typename basic_streambuf<_CharType>::off_type off_type;
68 m_current_position(0),
98 virtual utility::size64_t
size()
const
100 return utility::size64_t(m_size);
108 virtual size_t buffer_size(std::ios_base::openmode = std::ios_base::in)
const
134 _ASSERTE(m_current_position <= m_size);
138 return (
size_t)(writeend - readhead);
147 if (mode & std::ios_base::in)
152 if (mode & std::ios_base::out)
163 return pplx::task_from_result();
168 return pplx::task_from_result(
true);
173 if (m_current_position >= m_size)
174 return pplx::task_from_result<int_type>(traits::eof());
175 int_type retVal = (this->write(&ch, 1) == 1) ? static_cast<int_type>(ch) : traits::eof();
176 return pplx::task_from_result<int_type>(retVal);
182 if ( newSize > m_size )
183 return pplx::task_from_exception<size_t>(std::make_exception_ptr(std::runtime_error(
"Writing past the end of the buffer")));
184 return pplx::task_from_result<size_t>(this->write(ptr, count));
198 size_t space_left = (size_t)(writeend - readhead);
200 if (space_left < count)
return nullptr;
203 return (_CharType*)(m_data+m_current_position);
213 update_current_position(m_current_position+actual);
229 virtual bool acquire(_Out_ _CharType*& ptr, _Out_
size_t& count)
234 if (!this->
can_read())
return false;
240 ptr = (_CharType*)(m_data+m_current_position);
259 virtual void release(_Out_writes_opt_ (count) _CharType *ptr, _In_
size_t count)
262 update_current_position(m_current_position + count);
265 virtual pplx::task<size_t> _getn(_Out_writes_ (count) _CharType *ptr, _In_
size_t count)
267 return pplx::task_from_result(this->read(ptr, count));
270 size_t _sgetn(_Out_writes_ (count) _CharType *ptr, _In_
size_t count)
272 return this->read(ptr, count);
275 virtual size_t _scopy(_Out_writes_ (count) _CharType *ptr, _In_
size_t count)
277 return this->read(ptr, count,
false);
282 return pplx::task_from_result(this->read_byte(
true));
285 virtual int_type _sbumpc()
287 return this->read_byte(
true);
292 return pplx::task_from_result(this->read_byte(
false));
297 return this->read_byte(
false);
302 if (m_current_position >= m_size-1)
303 return pplx::task_from_result(basic_streambuf<_CharType>::traits::eof());
305 this->read_byte(
true);
306 return pplx::task_from_result(this->read_byte(
false));
311 auto pos =
seekoff(-1, std::ios_base::cur, std::ios_base::in);
312 if ( pos == (pos_type)traits::eof())
313 return pplx::task_from_result(traits::eof());
324 virtual pos_type
getpos(std::ios_base::openmode mode)
const
326 if ( ((mode & std::ios_base::in) && !this->
can_read()) ||
327 ((mode & std::ios_base::out) && !this->
can_write()))
328 return static_cast<pos_type
>(traits::eof());
330 if (mode == std::ios_base::in)
331 return (pos_type)m_current_position;
332 else if (mode == std::ios_base::out)
333 return (pos_type)m_current_position;
335 return (pos_type)traits::eof();
345 virtual pos_type
seekpos(pos_type position, std::ios_base::openmode mode)
348 pos_type end(m_size);
352 auto pos =
static_cast<size_t>(position);
355 if ((mode & std::ios_base::in) && this->
can_read())
360 update_current_position(pos);
361 return static_cast<pos_type
>(m_current_position);
366 if ((mode & std::ios_base::out) && this->
can_write())
369 update_current_position(pos);
371 return static_cast<pos_type
>(m_current_position);
375 return static_cast<pos_type
>(traits::eof());
387 virtual pos_type
seekoff(off_type offset, std::ios_base::seekdir way, std::ios_base::openmode mode)
390 pos_type cur =
static_cast<pos_type
>(m_current_position);
391 pos_type end =
static_cast<pos_type
>(m_size);
395 case std::ios_base::beg:
396 return seekpos(beg + offset, mode);
398 case std::ios_base::cur:
399 return seekpos(cur + offset, mode);
401 case std::ios_base::end:
402 return seekpos(end + offset, mode);
405 return static_cast<pos_type
>(traits::eof());
410 template<
typename _CharType1>
friend class ::concurrency::streams::rawptr_buffer;
419 m_data(const_cast<_CharType*>(data)),
421 m_current_position(0)
423 validate_mode(std::ios_base::in);
433 : streambuf_state_manager<_CharType>(mode),
436 m_current_position(0)
441 static void validate_mode(std::ios_base::openmode mode)
444 if ((mode & std::ios_base::in) && (mode & std::ios_base::out))
445 throw std::invalid_argument(
"this combination of modes on raw pointer stream not supported");
451 bool can_satisfy(
size_t)
const
462 int_type read_byte(
bool advance =
true)
465 auto read_size = this->read(&value, 1, advance);
466 return read_size == 1 ?
static_cast<int_type
>(value) : traits::eof();
474 size_t read(_Out_writes_ (count) _CharType *ptr, _In_
size_t count,
bool advance =
true)
476 if (!can_satisfy(count))
482 size_t newPos = m_current_position + read_size;
484 auto readBegin = m_data + m_current_position;
485 auto readEnd = m_data + newPos;
489 std::copy(readBegin, readEnd, stdext::checked_array_iterator<_CharType *>(ptr, count));
491 std::copy(readBegin, readEnd, ptr);
496 update_current_position(newPos);
499 return (
size_t) read_size;
505 size_t write(
const _CharType *ptr,
size_t count)
507 if (!this->
can_write() || (count == 0))
return 0;
511 if ( newSize > m_size )
512 throw std::runtime_error(
"Writing past the end of the buffer");
517 std::copy(ptr, ptr + count, stdext::checked_array_iterator<_CharType *>(m_data, m_size, m_current_position));
519 std::copy(ptr, ptr + count, m_data+m_current_position);
523 update_current_position(newSize);
531 void update_current_position(
size_t newPos)
534 m_current_position = newPos;
536 _ASSERTE(m_current_position <= m_size);
546 size_t m_current_position;
558 template<
typename _CharType>
559 class rawptr_buffer :
public streambuf<_CharType>
562 typedef _CharType char_type;
570 :
streambuf<char_type>(std::shared_ptr<details::basic_rawptr_buffer<char_type>>(new details::basic_rawptr_buffer<char_type>(data, size)))
580 :
streambuf<char_type>(std::shared_ptr<details::basic_rawptr_buffer<char_type>>(new details::basic_rawptr_buffer<char_type>(data, size, mode)))
599 template<
typename _CharType>
603 typedef _CharType char_type;
612 static concurrency::streams::basic_istream<char_type>
open_istream(
const char_type* data,
size_t size)
614 return concurrency::streams::basic_istream<char_type>(buffer_type(data, size));
623 static concurrency::streams::basic_istream<char_type>
open_istream(char_type* data,
size_t size)
625 return concurrency::streams::basic_istream<char_type>(buffer_type(data, size, std::ios::in));
634 static concurrency::streams::basic_ostream<char_type>
open_ostream(char_type* data,
size_t size)
636 return concurrency::streams::basic_ostream<char_type>(buffer_type(data, size, std::ios::out));
virtual pplx::task< int_type > getc()
Reads a single character from the stream without advancing the read position.
Definition: astreambuf.h:526
The rawptr_stream class is used to create memory-backed streams that support writing or reading seque...
Definition: rawptrstream.h:600
rawptr_buffer(const char_type *data, size_t size)
Create a rawptr_buffer given a pointer to a memory block and the size of the block.
Definition: rawptrstream.h:569
virtual bool is_open() const
Checks if the stream buffer is open.
Definition: astreambuf.h:390
Reference-counted stream buffer.
Definition: astreambuf.h:804
rawptr_buffer(char_type *data, size_t size, std::ios_base::openmode mode=std::ios::out)
Create a rawptr_buffer given a pointer to a memory block and the size of the block.
Definition: rawptrstream.h:579
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: rawptrstream.h:98
static concurrency::streams::basic_istream< char_type > open_istream(char_type *data, size_t size)
Create a rawptr-stream given a pointer to a writable memory block and the size of the block...
Definition: rawptrstream.h:623
rawptr_buffer()
Default constructor.
Definition: rawptrstream.h:587
void get() const
Returns the result this task produced. If the task is not in a terminal state, a call to get will wai...
Definition: pplxtasks.h:4440
virtual bool can_read() const
can_read is used to determine whether a stream buffer will support read operations (get)...
Definition: astreambuf.h:373
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
virtual size_t buffer_size(std::ios_base::openmode=std::ios_base::in) const
Get the stream buffer size, if one has been set.
Definition: rawptrstream.h:108
virtual pplx::task< void > close(std::ios_base::openmode mode)
Closes the stream buffer, preventing further read or write operations.
Definition: rawptrstream.h:145
void _commit(size_t actual)
Submits a block already allocated by the stream buffer.
Definition: rawptrstream.h:210
virtual ~basic_rawptr_buffer()
Destructor
Definition: rawptrstream.h:76
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
The basic_rawptr_buffer class serves as a memory-based steam buffer that supports both writing and re...
Definition: rawptrstream.h:52
virtual bool can_write() const
can_write is used to determine whether a stream buffer will support write operations (put)...
Definition: astreambuf.h:381
static concurrency::streams::basic_ostream< char_type > open_ostream(char_type *data, size_t size)
Create a rawptr-stream given a pointer to a writable memory block and the size of the block...
Definition: rawptrstream.h:634
Definition: astreambuf.h:362
virtual bool can_seek() const
can_seek is used to determine whether a stream buffer supports seeking.
Definition: rawptrstream.h:87
basic_rawptr_buffer()
Constructor
Definition: rawptrstream.h:65
The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed as...
Definition: pplxtasks.h:176
virtual bool acquire(_Out_ _CharType *&ptr, _Out_ size_t &count)
Gets a pointer to the next already allocated contiguous block of data.
Definition: rawptrstream.h:229
virtual pos_type getpos(std::ios_base::openmode mode) const
Gets the current read or write position in the stream.
Definition: rawptrstream.h:324
virtual utility::size64_t size() const
Gets the total number of characters in the stream buffer, if known. Calls to has_size will determine ...
Definition: astreambuf.h:913
The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed as...
Definition: pplxtasks.h:4173
virtual void release(_Out_writes_opt_(count) _CharType *ptr, _In_ size_t count)
Releases a block of data acquired using ::acquire method. This frees the stream buffer to de-allocate...
Definition: rawptrstream.h:259
static concurrency::streams::basic_istream< char_type > open_istream(const char_type *data, size_t size)
Create a rawptr-stream given a pointer to a read-only memory block and the size of the block...
Definition: rawptrstream.h:612
Definition: astreambuf.h:37
The rawptr_buffer class serves as a memory-based stream buffer that supports reading sequences of cha...
Definition: rawptrstream.h:43
_CharType * _alloc(size_t count)
Allocates a contiguous memory block and returns it.
Definition: rawptrstream.h:192
virtual size_t in_avail() const
For any input stream, in_avail returns the number of characters that are immediately available to be ...
Definition: rawptrstream.h:130
virtual pos_type seekoff(off_type offset, std::ios_base::seekdir way, std::ios_base::openmode mode)
Seeks to a position given by a relative offset.
Definition: rawptrstream.h:387
virtual void set_buffer_size(size_t, std::ios_base::openmode=std::ios_base::in)
Set the stream buffer implementation to buffer or not buffer.
Definition: rawptrstream.h:120
virtual bool has_size() const
has_size is used to determine whether a stream buffer supports size().
Definition: rawptrstream.h:92
virtual pos_type seekpos(pos_type position, std::ios_base::openmode mode)
Seeks to the given position.
Definition: rawptrstream.h:345