C++ Rest SDK
The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
astreambuf.h
1 /***
2 * ==++==
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * ==--==
17 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
18 *
19 * Asynchronous I/O: stream buffer. This is an extension to the PPL concurrency features and therefore
20 * lives in the Concurrency namespace.
21 *
22 * For the latest on this and related APIs, please see http://casablanca.codeplex.com.
23 *
24 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
25 ****/
26 #pragma once
27 
28 #include <ios>
29 #include <memory>
30 #include <cstring>
31 #include <math.h>
32 
33 #include "pplx/pplxtasks.h"
34 #include "cpprest/details/basic_types.h"
35 #include "cpprest/asyncrt_utils.h"
36 
37 namespace Concurrency
38 {
40 namespace streams
41 {
49  template<typename _CharType>
50  struct char_traits : std::char_traits<_CharType>
51  {
57  static typename std::char_traits<_CharType>::int_type requires_async() { return std::char_traits<_CharType>::eof()-1; }
58  };
59 #if !defined(_WIN32)
60  template<>
61  struct char_traits<unsigned char> : private std::char_traits<char>
62  {
63  public:
64  typedef unsigned char char_type;
65 
66  using std::char_traits<char>::eof;
67  using std::char_traits<char>::int_type;
68  using std::char_traits<char>::off_type;
69  using std::char_traits<char>::pos_type;
70 
71  static size_t length(const unsigned char* str)
72  {
73  return std::char_traits<char>::length(reinterpret_cast<const char*>(str));
74  }
75 
76  static void assign(unsigned char& left, const unsigned char& right) { left = right; }
77  static unsigned char* assign(unsigned char* left, size_t n, unsigned char value)
78  {
79  return reinterpret_cast<unsigned char*>(std::char_traits<char>::assign(reinterpret_cast<char*>(left), n, static_cast<char>(value)));
80  }
81 
82  static unsigned char* copy(unsigned char* left, const unsigned char* right, size_t n)
83  {
84  return reinterpret_cast<unsigned char*>(std::char_traits<char>::copy(reinterpret_cast<char*>(left), reinterpret_cast<const char*>(right), n));
85  }
86 
87  static unsigned char* move(unsigned char* left, const unsigned char* right, size_t n)
88  {
89  return reinterpret_cast<unsigned char*>(std::char_traits<char>::move(reinterpret_cast<char*>(left), reinterpret_cast<const char*>(right), n));
90  }
91 
92  static int_type requires_async() { return eof() - 1; }
93  };
94 #endif
95 
96  namespace details {
97 
101  template<typename _CharType>
103  {
104  public:
105  typedef _CharType char_type;
106  typedef ::concurrency::streams::char_traits<_CharType> traits;
107 
108  typedef typename traits::int_type int_type;
109  typedef typename traits::pos_type pos_type;
110  typedef typename traits::off_type off_type;
111 
112 
116  virtual ~basic_streambuf() { }
117 
121  virtual bool can_read() const = 0;
122 
126  virtual bool can_write() const = 0;
127 
131  virtual bool can_seek() const = 0;
132 
136  virtual bool has_size() const = 0;
137 
141  virtual bool is_eof() const = 0;
142 
149  virtual size_t buffer_size(std::ios_base::openmode direction = std::ios_base::in) const = 0;
150 
157  virtual void set_buffer_size(size_t size, std::ios_base::openmode direction = std::ios_base::in) = 0;
158 
164  virtual size_t in_avail() const = 0;
165 
170  virtual bool is_open() const = 0;
171 
176  virtual pplx::task<void> close(std::ios_base::openmode mode = (std::ios_base::in | std::ios_base::out)) = 0;
177 
183  virtual pplx::task<void> close(std::ios_base::openmode mode, std::exception_ptr eptr) = 0;
184 
190  virtual pplx::task<int_type> putc(_CharType ch) = 0;
191 
198  virtual pplx::task<size_t> putn(const _CharType *ptr, size_t count) = 0;
199 
207  virtual pplx::task<size_t> putn_nocopy(const _CharType *ptr, size_t count) = 0;
208 
213  virtual pplx::task<int_type> bumpc() = 0;
214 
220  virtual int_type sbumpc() = 0;
221 
226  virtual pplx::task<int_type> getc() = 0;
227 
233  virtual int_type sgetc() = 0;
234 
239  virtual pplx::task<int_type> nextc() = 0;
240 
245  virtual pplx::task<int_type> ungetc() = 0;
246 
253  virtual pplx::task<size_t> getn(_Out_writes_(count) _CharType *ptr, _In_ size_t count) = 0;
254 
262  virtual size_t scopy(_Out_writes_(count) _CharType *ptr, _In_ size_t count) = 0;
263 
271  virtual pos_type getpos(std::ios_base::openmode direction) const = 0;
272 
277  virtual utility::size64_t size() const = 0;
278 
286  virtual pos_type seekpos(pos_type pos, std::ios_base::openmode direction) = 0;
287 
297  virtual pos_type seekoff(off_type offset, std::ios_base::seekdir way, std::ios_base::openmode mode) = 0;
298 
303  virtual pplx::task<void> sync() = 0;
304 
305  //
306  // Efficient read and write.
307  //
308  // The following routines are intended to be used for more efficient, copy-free, reading and
309  // writing of data from/to the stream. Rather than having the caller provide a buffer into which
310  // data is written or from which it is read, the stream buffer provides a pointer directly to the
311  // internal data blocks that it is using. Since not all stream buffers use internal data structures
312  // to copy data, the functions may not be supported by all. An application that wishes to use this
313  // functionality should therefore first try them and check for failure to support. If there is
314  // such failure, the application should fall back on the copying interfaces (putn / getn)
315  //
316 
322  virtual _CharType* alloc(_In_ size_t count) = 0;
323 
328  virtual void commit(_In_ size_t count) = 0;
329 
343  virtual bool acquire(_Out_ _CharType*& ptr, _Out_ size_t& count) = 0;
344 
351  virtual void release(_Out_writes_(count) _CharType *ptr, _In_ size_t count) = 0;
352 
357  virtual std::exception_ptr exception() const = 0;
358  };
359 
360 
361  template<typename _CharType>
362  class streambuf_state_manager : public basic_streambuf<_CharType>, public std::enable_shared_from_this<streambuf_state_manager<_CharType>>
363  {
364  public:
365  typedef typename details::basic_streambuf<_CharType>::traits traits;
366  typedef typename details::basic_streambuf<_CharType>::int_type int_type;
367  typedef typename details::basic_streambuf<_CharType>::pos_type pos_type;
368  typedef typename details::basic_streambuf<_CharType>::off_type off_type;
369 
373  virtual bool can_read() const
374  {
375  return m_stream_can_read;
376  }
377 
381  virtual bool can_write() const
382  {
383  return m_stream_can_write;
384  }
385 
390  virtual bool is_open() const
391  {
392  return can_read() || can_write();
393  }
394 
399  virtual pplx::task<void> close(std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
400  {
401  pplx::task<void> closeOp = pplx::task_from_result();
402 
403  if (mode & std::ios_base::in && can_read()) {
404  closeOp = _close_read();
405  }
406 
407  // After the flush_internal task completed, "this" object may have been destroyed,
408  // accessing the members is invalid, use shared_from_this to avoid access violation exception.
409  auto this_ptr = std::static_pointer_cast<streambuf_state_manager>(this->shared_from_this());
410 
411  if (mode & std::ios_base::out && can_write()) {
412  if (closeOp.is_done())
413  closeOp = closeOp && _close_write().then([this_ptr]{}); // passing down exceptions from closeOp
414  else
415  closeOp = closeOp.then([this_ptr] { return this_ptr->_close_write().then([this_ptr]{}); });
416  }
417 
418  return closeOp;
419  }
420 
426  virtual pplx::task<void> close(std::ios_base::openmode mode, std::exception_ptr eptr)
427  {
428  if (m_currentException == nullptr)
429  m_currentException = eptr;
430  return close(mode);
431  }
432 
436  virtual bool is_eof() const
437  {
438  return m_stream_read_eof;
439  }
440 
446  virtual pplx::task<int_type> putc(_CharType ch)
447  {
448  if (!can_write())
449  return create_exception_checked_value_task<int_type>(traits::eof());
450 
451  return create_exception_checked_task<int_type>(_putc(ch), [](int_type) {
452  return false; // no EOF for write
453  });
454  }
455 
462  CASABLANCA_DEPRECATED("This API in some cases performs a copy. It is deprecated and will be removed in a future release. Use putn_nocopy instead.")
463  virtual pplx::task<size_t> putn(const _CharType *ptr, size_t count)
464  {
465  if (!can_write())
466  return create_exception_checked_value_task<size_t>(0);
467  if (count == 0)
468  return pplx::task_from_result<size_t>(0);
469 
470  return create_exception_checked_task<size_t>(_putn(ptr, count, true), [](size_t) {
471  return false; // no EOF for write
472  });
473  }
474 
482  virtual pplx::task<size_t> putn_nocopy(const _CharType *ptr, size_t count)
483  {
484  if (!can_write())
485  return create_exception_checked_value_task<size_t>(0);
486  if (count == 0)
487  return pplx::task_from_result<size_t>(0);
488 
489  return create_exception_checked_task<size_t>(_putn(ptr, count), [](size_t) {
490  return false; // no EOF for write
491  });
492  }
493 
499  {
500  if (!can_read())
501  return create_exception_checked_value_task<int_type>(streambuf_state_manager<_CharType>::traits::eof());
502 
503  return create_exception_checked_task<int_type>(_bumpc(), [](int_type val) {
505  });
506  }
507 
513  virtual int_type sbumpc()
514  {
515  if ( !(m_currentException == nullptr) )
516  std::rethrow_exception(m_currentException);
517  if (!can_read())
518  return traits::eof();
519  return check_sync_read_eof(_sbumpc());
520  }
521 
527  {
528  if (!can_read())
529  return create_exception_checked_value_task<int_type>(traits::eof());
530 
531  return create_exception_checked_task<int_type>(_getc(), [](int_type val) {
533  });
534  }
535 
541  virtual int_type sgetc()
542  {
543  if ( !(m_currentException == nullptr) )
544  std::rethrow_exception(m_currentException);
545  if (!can_read())
546  return traits::eof();
547  return check_sync_read_eof(_sgetc());
548  }
549 
555  {
556  if (!can_read())
557  return create_exception_checked_value_task<int_type>(traits::eof());
558 
559  return create_exception_checked_task<int_type>(_nextc(), [](int_type val) {
561  });
562  }
563 
569  {
570  if (!can_read())
571  return create_exception_checked_value_task<int_type>(traits::eof());
572 
573  return create_exception_checked_task<int_type>(_ungetc(), [](int_type) {
574  return false;
575  });
576  }
577 
584  virtual pplx::task<size_t> getn(_Out_writes_(count) _CharType *ptr, _In_ size_t count)
585  {
586  if (!can_read())
587  return create_exception_checked_value_task<size_t>(0);
588  if (count == 0)
589  return pplx::task_from_result<size_t>(0);
590 
591  return create_exception_checked_task<size_t>(_getn(ptr, count), [](size_t val) {
592  return val == 0;
593  });
594  }
595 
603  virtual size_t scopy(_Out_writes_(count) _CharType *ptr, _In_ size_t count)
604  {
605  if ( !(m_currentException == nullptr) )
606  std::rethrow_exception(m_currentException);
607  if (!can_read())
608  return 0;
609 
610  return _scopy(ptr, count);
611  }
612 
618  {
619  if (!can_write())
620  {
621  if (m_currentException == nullptr)
622  return pplx::task_from_result();
623  else
624  return pplx::task_from_exception<void>(m_currentException);
625  }
626  return create_exception_checked_task<bool>(_sync(), [](bool) {
627  return false;
628  }).then([](bool){});
629  }
630 
635  virtual std::exception_ptr exception() const
636  {
637  return m_currentException;
638  }
639 
646  _CharType* alloc(size_t count)
647  {
648  if (m_alloced)
649  throw std::logic_error("The buffer is already allocated, this maybe caused by overlap of stream read or write");
650 
651  _CharType* alloc_result = _alloc(count);
652 
653  if (alloc_result)
654  m_alloced = true;
655 
656  return alloc_result;
657  }
658 
664  void commit(size_t count)
665  {
666  if (!m_alloced)
667  throw std::logic_error("The buffer needs to allocate first");
668 
669  _commit(count);
670  m_alloced = false;
671  }
672 
673  public:
674  virtual bool can_seek() const = 0;
675  virtual bool has_size() const = 0;
676  virtual utility::size64_t size() const { return 0; }
677  virtual size_t buffer_size(std::ios_base::openmode direction = std::ios_base::in) const = 0;
678  virtual void set_buffer_size(size_t size, std::ios_base::openmode direction = std::ios_base::in) = 0;
679  virtual size_t in_avail() const = 0;
680  virtual pos_type getpos(std::ios_base::openmode direction) const = 0;
681  virtual pos_type seekpos(pos_type pos, std::ios_base::openmode direction) = 0;
682  virtual pos_type seekoff(off_type offset, std::ios_base::seekdir way, std::ios_base::openmode mode) = 0;
683  virtual bool acquire(_Out_writes_(count) _CharType*& ptr, _In_ size_t& count) = 0;
684  virtual void release(_Out_writes_(count) _CharType *ptr, _In_ size_t count) = 0;
685  protected:
686  virtual pplx::task<int_type> _putc(_CharType ch) = 0;
687 
688  // This API is only needed for file streams and until we remove the deprecated stream buffer putn overload.
689  virtual pplx::task<size_t> _putn(const _CharType *ptr, size_t count, bool)
690  {
691  // Default to no copy, only the file streams API overloads and performs a copy.
692  return _putn(ptr, count);
693  }
694  virtual pplx::task<size_t> _putn(const _CharType *ptr, size_t count) = 0;
695 
696  virtual pplx::task<int_type> _bumpc() = 0;
697  virtual int_type _sbumpc() = 0;
698  virtual pplx::task<int_type> _getc() = 0;
699  virtual int_type _sgetc() = 0;
700  virtual pplx::task<int_type> _nextc() = 0;
701  virtual pplx::task<int_type> _ungetc() = 0;
702  virtual pplx::task<size_t> _getn(_Out_writes_(count) _CharType *ptr, _In_ size_t count) = 0;
703  virtual size_t _scopy(_Out_writes_(count) _CharType *ptr, _In_ size_t count) = 0;
704  virtual pplx::task<bool> _sync() = 0;
705  virtual _CharType* _alloc(size_t count) = 0;
706  virtual void _commit(size_t count) = 0;
707 
712  {
713  m_stream_can_read = false;
714  return pplx::task_from_result();
715  }
716 
721  {
722  m_stream_can_write = false;
723  return pplx::task_from_result();
724  }
725 
726  protected:
727  streambuf_state_manager(std::ios_base::openmode mode)
728  {
729  m_stream_can_read = (mode & std::ios_base::in) != 0;
730  m_stream_can_write = (mode & std::ios_base::out) != 0;
731  m_stream_read_eof = false;
732  m_alloced = false;
733  }
734 
735  std::exception_ptr m_currentException;
736  // The in/out mode for the buffer
737  bool m_stream_can_read, m_stream_can_write, m_stream_read_eof, m_alloced;
738 
739 
740  private:
741  template<typename _CharType1>
742  pplx::task<_CharType1> create_exception_checked_value_task(const _CharType1 &val) const
743  {
744  if (this->exception() == nullptr)
745  return pplx::task_from_result<_CharType1>(static_cast<_CharType1>(val));
746  else
747  return pplx::task_from_exception<_CharType1>(this->exception());
748  }
749 
750  // Set exception and eof states for async read
751  template<typename _CharType1>
752  pplx::task<_CharType1> create_exception_checked_task(pplx::task<_CharType1> result, std::function<bool(_CharType1)> eof_test, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
753  {
754  auto thisPointer = this->shared_from_this();
755 
756  auto func1 = [=](pplx::task<_CharType1> t1) -> pplx::task<_CharType1> {
757  try {
758  thisPointer->m_stream_read_eof = eof_test(t1.get());
759  } catch (...) {
760  thisPointer->close(mode, std::current_exception()).get();
761  return pplx::task_from_exception<_CharType1>(thisPointer->exception(), pplx::task_options());
762  }
763  if (thisPointer->m_stream_read_eof && !(thisPointer->exception() == nullptr))
764  return pplx::task_from_exception<_CharType1>(thisPointer->exception(), pplx::task_options());
765  return t1;
766  };
767 
768  if ( result.is_done() )
769  {
770  // If the data is already available, we should avoid scheduling a continuation, so we do it inline.
771  return func1(result);
772  }
773  else
774  {
775  return result.then(func1);
776  }
777  }
778 
779  // Set eof states for sync read
780  int_type check_sync_read_eof(int_type ch)
781  {
782  m_stream_read_eof = ch == traits::eof();
783  return ch;
784  }
785 
786  };
787 
788  } // namespace details
789 
790  // Forward declarations
791  template<typename _CharType> class basic_istream;
792  template<typename _CharType> class basic_ostream;
793 
803  template<typename _CharType>
804  class streambuf : public details::basic_streambuf<_CharType>
805  {
806  public:
807  typedef typename details::basic_streambuf<_CharType>::traits traits;
808  typedef typename details::basic_streambuf<_CharType>::int_type int_type;
809  typedef typename details::basic_streambuf<_CharType>::pos_type pos_type;
810  typedef typename details::basic_streambuf<_CharType>::off_type off_type;
811  typedef typename details::basic_streambuf<_CharType>::char_type char_type;
812 
813  template <typename _CharType2> friend class streambuf;
814 
819  streambuf(_In_ const std::shared_ptr<details::basic_streambuf<_CharType>> &ptr) : m_buffer(ptr) {}
820 
824  streambuf() { }
825 
833  template <typename AlterCharType>
835  m_buffer(std::static_pointer_cast<details::basic_streambuf<_CharType>>(std::static_pointer_cast<void>(other.m_buffer)))
836  {
837  static_assert(std::is_same<pos_type, typename details::basic_streambuf<AlterCharType>::pos_type>::value
838  && std::is_same<off_type, typename details::basic_streambuf<AlterCharType>::off_type>::value
839  && std::is_integral<_CharType>::value && std::is_integral<AlterCharType>::value
840  && std::is_integral<int_type>::value && std::is_integral<typename details::basic_streambuf<AlterCharType>::int_type>::value
841  && sizeof(_CharType) == sizeof(AlterCharType)
842  && sizeof(int_type) == sizeof(typename details::basic_streambuf<AlterCharType>::int_type),
843  "incompatible stream character types");
844  }
845 
850  concurrency::streams::basic_istream<_CharType> create_istream() const
851  {
852  if (!can_read()) throw std::runtime_error("stream buffer not set up for input of data");
853  return concurrency::streams::basic_istream<_CharType>(*this);
854  }
855 
860  concurrency::streams::basic_ostream<_CharType> create_ostream() const
861  {
862  if (!can_write()) throw std::runtime_error("stream buffer not set up for output of data");
863  return concurrency::streams::basic_ostream<_CharType>(*this);
864  }
865 
869  operator bool() const { return (bool)m_buffer; }
870 
874  virtual ~streambuf() { }
875 
876  const std::shared_ptr<details::basic_streambuf<_CharType>> & get_base() const
877  {
878  if (!m_buffer)
879  {
880  throw std::invalid_argument("Invalid streambuf object");
881  }
882 
883  return m_buffer;
884  }
885 
889  virtual bool can_read() const { return get_base()->can_read(); }
890 
894  virtual bool can_write() const { return get_base()->can_write(); }
895 
900  virtual bool can_seek() const { return get_base()->can_seek(); }
901 
906  virtual bool has_size() const { return get_base()->has_size(); }
907 
913  virtual utility::size64_t size() const { return get_base()->size(); }
914 
921  virtual size_t buffer_size(std::ios_base::openmode direction = std::ios_base::in) const { return get_base()->buffer_size(direction); }
922 
929  virtual void set_buffer_size(size_t size, std::ios_base::openmode direction = std::ios_base::in) { get_base()->set_buffer_size(size,direction); }
930 
937  virtual size_t in_avail() const { return get_base()->in_avail(); }
938 
944  virtual bool is_open() const { return get_base()->is_open(); }
945 
950  virtual bool is_eof() const { return get_base()->is_eof(); }
951 
956  virtual pplx::task<void> close(std::ios_base::openmode mode = (std::ios_base::in | std::ios_base::out))
957  {
958  // We preserve the check here to workaround a Dev10 compiler crash
959  auto buffer = get_base();
960  return buffer ? buffer->close(mode) : pplx::task_from_result();
961  }
962 
968  virtual pplx::task<void> close(std::ios_base::openmode mode, std::exception_ptr eptr)
969  {
970  // We preserve the check here to workaround a Dev10 compiler crash
971  auto buffer = get_base();
972  return buffer ? buffer->close(mode, eptr) : pplx::task_from_result();
973  }
974 
980  virtual pplx::task<int_type> putc(_CharType ch)
981  {
982  return get_base()->putc(ch);
983  }
984 
990  virtual _CharType* alloc(size_t count)
991  {
992  return get_base()->alloc(count);
993  }
994 
999  virtual void commit(size_t count)
1000  {
1001  get_base()->commit(count);
1002  }
1003 
1017  virtual bool acquire(_Out_ _CharType*& ptr, _Out_ size_t& count)
1018  {
1019  ptr = nullptr;
1020  count = 0;
1021  return get_base()->acquire(ptr, count);
1022  }
1023 
1030  virtual void release(_Out_writes_(count) _CharType *ptr, _In_ size_t count)
1031  {
1032  get_base()->release(ptr, count);
1033  }
1034 
1041  CASABLANCA_DEPRECATED("This API in some cases performs a copy. It is deprecated and will be removed in a future release. Use putn_nocopy instead.")
1042  virtual pplx::task<size_t> putn(const _CharType *ptr, size_t count)
1043  {
1044  return get_base()->putn(ptr, count);
1045  }
1046 
1054  virtual pplx::task<size_t> putn_nocopy(const _CharType *ptr, size_t count)
1055  {
1056  return get_base()->putn_nocopy(ptr, count);
1057  }
1058 
1064  {
1065  return get_base()->bumpc();
1066  }
1067 
1073  virtual typename details::basic_streambuf<_CharType>::int_type sbumpc()
1074  {
1075  return get_base()->sbumpc();
1076  }
1077 
1083  {
1084  return get_base()->getc();
1085  }
1086 
1092  virtual typename details::basic_streambuf<_CharType>::int_type sgetc()
1093  {
1094  return get_base()->sgetc();
1095  }
1096 
1102  {
1103  return get_base()->nextc();
1104  }
1105 
1111  {
1112  return get_base()->ungetc();
1113  }
1114 
1121  virtual pplx::task<size_t> getn(_Out_writes_(count) _CharType *ptr, _In_ size_t count)
1122  {
1123  return get_base()->getn(ptr, count);
1124  }
1125 
1133  virtual size_t scopy(_Out_writes_(count) _CharType *ptr, _In_ size_t count)
1134  {
1135  return get_base()->scopy(ptr, count);
1136  }
1137 
1145  virtual typename details::basic_streambuf<_CharType>::pos_type getpos(std::ios_base::openmode direction) const
1146  {
1147  return get_base()->getpos(direction);
1148  }
1149 
1157  virtual typename details::basic_streambuf<_CharType>::pos_type seekpos(typename details::basic_streambuf<_CharType>::pos_type pos, std::ios_base::openmode direction)
1158  {
1159  return get_base()->seekpos(pos, direction);
1160  }
1161 
1171  virtual typename details::basic_streambuf<_CharType>::pos_type seekoff(typename details::basic_streambuf<_CharType>::off_type offset, std::ios_base::seekdir way, std::ios_base::openmode mode)
1172  {
1173  return get_base()->seekoff(offset, way, mode);
1174  }
1175 
1181  {
1182  return get_base()->sync();
1183  }
1184 
1189  virtual std::exception_ptr exception() const
1190  {
1191  return get_base()->exception();
1192  }
1193 
1194  private:
1195  std::shared_ptr<details::basic_streambuf<_CharType>> m_buffer;
1196 
1197  };
1198 
1199 }}
virtual size_t scopy(_Out_writes_(count) _CharType *ptr, _In_ size_t count)
Copies up to a given number of characters from the stream, synchronously.
Definition: astreambuf.h:603
streambuf(const streambuf< AlterCharType > &other)
Converter Constructor.
Definition: astreambuf.h:834
virtual bool can_seek() const =0
can_seek is used to determine whether a stream buffer supports seeking.
virtual bool can_seek() const
can_seek is used to determine whether a stream buffer supports seeking.
Definition: astreambuf.h:900
Stream buffer base class.
Definition: astreambuf.h:102
concurrency::streams::basic_ostream< _CharType > create_ostream() const
Constructs an output stream for this stream buffer.
Definition: astreambuf.h:860
virtual pplx::task< int_type > bumpc()
Reads a single character from the stream and advances the read position.
Definition: astreambuf.h:498
virtual ~streambuf()
Destructor
Definition: astreambuf.h:874
virtual pplx::task< int_type > getc()=0
Reads a single character from the stream without advancing the read position.
virtual pplx::task< int_type > getc()
Reads a single character from the stream without advancing the read position.
Definition: astreambuf.h:526
virtual int_type sbumpc()
Reads a single character from the stream and advances the read position.
Definition: astreambuf.h:513
virtual pplx::task< size_t > putn(const _CharType *ptr, size_t count)
Writes a number of characters to the stream.
Definition: astreambuf.h:463
virtual pos_type seekpos(pos_type pos, std::ios_base::openmode direction)=0
Seeks to the given position.
virtual pplx::task< int_type > ungetc()=0
Retreats the read position, then returns the current character without advancing. ...
virtual pplx::task< int_type > nextc()=0
Advances the read position, then returns the next character without advancing again.
virtual bool acquire(_Out_ _CharType *&ptr, _Out_ size_t &count)=0
Gets a pointer to the next already allocated contiguous block of data.
virtual int_type sgetc()=0
Reads a single character from the stream without advancing the read position.
virtual bool is_open() const
Checks if the stream buffer is open.
Definition: astreambuf.h:944
The pplx namespace provides classes and functions that give you access to the Concurrency Runtime...
Definition: pplx.h:81
virtual pplx::task< void > close(std::ios_base::openmode mode=(std::ios_base::in|std::ios_base::out))
Closes the stream buffer, preventing further read or write operations.
Definition: astreambuf.h:956
virtual bool is_open() const
Checks if the stream buffer is open.
Definition: astreambuf.h:390
virtual details::basic_streambuf< _CharType >::int_type sbumpc()
Reads a single character from the stream and advances the read position.
Definition: astreambuf.h:1073
virtual pplx::task< int_type > putc(_CharType ch)
Writes a single character to the stream.
Definition: astreambuf.h:446
Reference-counted stream buffer.
Definition: astreambuf.h:804
virtual void release(_Out_writes_(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: astreambuf.h:1030
virtual bool has_size() const =0
has_size is used to determine whether a stream buffer supports size().
virtual std::exception_ptr exception() const
Retrieves the stream buffer exception_ptr if it has been set.
Definition: astreambuf.h:1189
virtual bool is_eof() const
is_eof is used to determine whether a read head has reached the end of the buffer.
Definition: astreambuf.h:950
virtual pplx::task< size_t > putn_nocopy(const _CharType *ptr, size_t count)
Writes a number of characters to the stream. Note: callers must make sure the data to be written is v...
Definition: astreambuf.h:482
virtual int_type sbumpc()=0
Reads a single character from the stream and advances the read position.
virtual bool can_seek() const =0
can_seek is used to determine whether a stream buffer supports seeking.
virtual bool can_write() const =0
can_write is used to determine whether a stream buffer will support write operations (put)...
Base interface for all asynchronous output streams.
Definition: astreambuf.h:792
virtual pos_type getpos(std::ios_base::openmode direction) const =0
Gets the current read or write position in the stream.
void commit(size_t count)
Submits a block already allocated by the stream buffer.
Definition: astreambuf.h:664
virtual bool is_open() const =0
Checks if the stream buffer is open.
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
virtual size_t scopy(_Out_writes_(count) _CharType *ptr, _In_ size_t count)=0
Copies up to a given number of characters from the stream, synchronously.
static std::char_traits< _CharType >::int_type requires_async()
Some synchronous functions will return this value if the operation requires an asynchronous call in a...
Definition: astreambuf.h:57
virtual pplx::task< void > close(std::ios_base::openmode mode, std::exception_ptr eptr)
Closes the stream buffer with an exception.
Definition: astreambuf.h:968
virtual pplx::task< int_type > getc()
Reads a single character from the stream without advancing the read position.
Definition: astreambuf.h:1082
virtual details::basic_streambuf< _CharType >::pos_type seekpos(typename details::basic_streambuf< _CharType >::pos_type pos, std::ios_base::openmode direction)
Seeks to the given position.
Definition: astreambuf.h:1157
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 details::basic_streambuf< _CharType >::int_type sgetc()
Reads a single character from the stream without advancing the read position.
Definition: astreambuf.h:1092
virtual pos_type seekoff(off_type offset, std::ios_base::seekdir way, std::ios_base::openmode mode)=0
Seeks to a position given by a relative offset.
virtual pplx::task< size_t > getn(_Out_writes_(count) _CharType *ptr, _In_ size_t count)
Reads up to a given number of characters from the stream.
Definition: astreambuf.h:584
bool is_done() const
Determines if the task is completed.
Definition: pplxtasks.h:3558
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 pplx::task< int_type > bumpc()=0
Reads a single character from the stream and advances the read position.
virtual pplx::task< int_type > putc(_CharType ch)=0
Writes a single character to the stream.
virtual void release(_Out_writes_(count) _CharType *ptr, _In_ size_t count)=0
Releases a block of data acquired using ::acquire method. This frees the stream buffer to de-allocate...
virtual std::exception_ptr exception() const
Retrieves the stream buffer exception_ptr if it has been set.
Definition: astreambuf.h:635
virtual void commit(_In_ size_t count)=0
Submits a block already allocated by the stream buffer.
virtual bool is_eof() const =0
is_eof is used to determine whether a read head has reached the end of the buffer.
virtual void set_buffer_size(size_t size, std::ios_base::openmode direction=std::ios_base::in)=0
Sets the stream buffer implementation to buffer or not buffer.
virtual pplx::task< size_t > getn(_Out_writes_(count) _CharType *ptr, _In_ size_t count)
Reads up to a given number of characters from the stream.
Definition: astreambuf.h:1121
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
virtual size_t buffer_size(std::ios_base::openmode direction=std::ios_base::in) const
Gets the stream buffer size, if one has been set.
Definition: astreambuf.h:921
virtual pplx::task< int_type > bumpc()
Reads a single character from the stream and advances the read position.
Definition: astreambuf.h:1063
virtual bool can_write() const
can_write is used to determine whether a stream buffer will support write operations (put)...
Definition: astreambuf.h:381
virtual pplx::task< void > sync()=0
For output streams, flush any internally buffered data to the underlying medium.
virtual pplx::task< void > sync()
For output streams, flush any internally buffered data to the underlying medium.
Definition: astreambuf.h:617
virtual bool is_eof() const
is_eof is used to determine whether a read head has reached the end of the buffer.
Definition: astreambuf.h:436
pplx::task< int_type > nextc()
Advances the read position, then returns the next character without advancing again.
Definition: astreambuf.h:1101
The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed as...
Definition: pplxtasks.h:176
virtual pplx::task< void > close(std::ios_base::openmode mode, std::exception_ptr eptr)
Closes the stream buffer with an exception.
Definition: astreambuf.h:426
virtual details::basic_streambuf< _CharType >::pos_type getpos(std::ios_base::openmode direction) const
Gets the current read or write position in the stream.
Definition: astreambuf.h:1145
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
Represents the allowed options for creating a task
Definition: pplxtasks.h:1238
virtual pplx::task< void > close(std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
Closes the stream buffer, preventing further read or write operations.
Definition: astreambuf.h:399
streambuf()
Default constructor.
Definition: astreambuf.h:824
virtual utility::size64_t size() const =0
Gets the size of the stream, if known. Calls to has_size will determine whether the result of size ca...
_ReturnType 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:3534
virtual void release(_Out_writes_(count) _CharType *ptr, _In_ size_t count)=0
Releases a block of data acquired using ::acquire method. This frees the stream buffer to de-allocate...
virtual pos_type seekoff(off_type offset, std::ios_base::seekdir way, std::ios_base::openmode mode)=0
Seeks to a position given by a relative offset.
Base interface for all asynchronous input streams.
Definition: astreambuf.h:791
concurrency::streams::basic_istream< _CharType > create_istream() const
Constructs an input stream head for this stream buffer.
Definition: astreambuf.h:850
The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed as...
Definition: pplxtasks.h:4173
virtual pplx::task< size_t > putn(const _CharType *ptr, size_t count)=0
Writes a number of characters to the stream.
virtual pplx::task< size_t > putn(const _CharType *ptr, size_t count)
Writes a number of characters to the stream.
Definition: astreambuf.h:1042
virtual void commit(size_t count)
Submits a block already allocated by the stream buffer.
Definition: astreambuf.h:999
virtual size_t in_avail() const
For any input stream, in_avail returns the number of characters that are immediately available to be ...
Definition: astreambuf.h:937
virtual pplx::task< int_type > ungetc()
Retreats the read position, then returns the current character without advancing. ...
Definition: astreambuf.h:568
_CharType * alloc(size_t count)
Allocates a contiguous memory block and returns it.
Definition: astreambuf.h:646
virtual pplx::task< int_type > putc(_CharType ch)
Writes a single character to the stream.
Definition: astreambuf.h:980
virtual details::basic_streambuf< _CharType >::pos_type seekoff(typename details::basic_streambuf< _CharType >::off_type offset, std::ios_base::seekdir way, std::ios_base::openmode mode)
Seeks to a position given by a relative offset.
Definition: astreambuf.h:1171
Definition: astreambuf.h:37
virtual void set_buffer_size(size_t size, std::ios_base::openmode direction=std::ios_base::in)=0
Sets the stream buffer implementation to buffer or not buffer.
virtual ~basic_streambuf()
Virtual constructor for stream buffers.
Definition: astreambuf.h:116
virtual bool can_read() const =0
can_read is used to determine whether a stream buffer will support read operations (get)...
virtual size_t in_avail() const =0
For any input stream, in_avail returns the number of characters that are immediately available to be ...
virtual pplx::task< int_type > nextc()
Advances the read position, then returns the next character without advancing again.
Definition: astreambuf.h:554
virtual pos_type seekpos(pos_type pos, std::ios_base::openmode direction)=0
Seeks to the given position.
pplx::task< int_type > ungetc()
Retreats the read position, then returns the current character without advancing. ...
Definition: astreambuf.h:1110
virtual void set_buffer_size(size_t size, std::ios_base::openmode direction=std::ios_base::in)
Sets the stream buffer implementation to buffer or not buffer.
Definition: astreambuf.h:929
virtual size_t scopy(_Out_writes_(count) _CharType *ptr, _In_ size_t count)
Copies up to a given number of characters from the stream, synchronously.
Definition: astreambuf.h:1133
virtual pos_type getpos(std::ios_base::openmode direction) const =0
Gets the current read or write position in the stream.
virtual pplx::task< void > sync()
For output streams, flush any internally buffered data to the underlying medium.
Definition: astreambuf.h:1180
virtual size_t buffer_size(std::ios_base::openmode direction=std::ios_base::in) const =0
Gets the stream buffer size, if one has been set.
virtual bool has_size() const
has_size is used to determine whether a stream buffer supports size().
Definition: astreambuf.h:906
virtual size_t in_avail() const =0
For any input stream, in_avail returns the number of characters that are immediately available to be ...
virtual _CharType * alloc(size_t count)
Allocates a contiguous memory block and returns it.
Definition: astreambuf.h:990
virtual bool has_size() const =0
has_size is used to determine whether a stream buffer supports size().
virtual int_type sgetc()
Reads a single character from the stream without advancing the read position.
Definition: astreambuf.h:541
virtual pplx::task< size_t > putn_nocopy(const _CharType *ptr, size_t count)
Writes a number of characters to the stream. Note: callers must make sure the data to be written is v...
Definition: astreambuf.h:1054
virtual bool acquire(_Out_ _CharType *&ptr, _Out_ size_t &count)
Gets a pointer to the next already allocated contiguous block of data.
Definition: astreambuf.h:1017
virtual _CharType * alloc(_In_ size_t count)=0
Allocates a contiguous memory block and returns it.
streambuf(_In_ const std::shared_ptr< details::basic_streambuf< _CharType >> &ptr)
Constructor.
Definition: astreambuf.h:819
Extending the standard char_traits type with one that adds values and types that are unique to "C++ R...
Definition: astreambuf.h:50
virtual std::exception_ptr exception() const =0
Retrieves the stream buffer exception_ptr if it has been set.
virtual bool can_read() const
can_read is used to determine whether a stream buffer will support read operations (get)...
Definition: astreambuf.h:889
virtual pplx::task< size_t > getn(_Out_writes_(count) _CharType *ptr, _In_ size_t count)=0
Reads up to a given number of characters from the stream.
virtual bool can_write() const
can_write is used to determine whether a stream buffer will support write operations (put)...
Definition: astreambuf.h:894
virtual size_t buffer_size(std::ios_base::openmode direction=std::ios_base::in) const =0
Gets the stream buffer size, if one has been set.
virtual pplx::task< void > close(std::ios_base::openmode mode=(std::ios_base::in|std::ios_base::out))=0
Closes the stream buffer, preventing further read or write operations.
virtual pplx::task< size_t > putn_nocopy(const _CharType *ptr, size_t count)=0
Writes a number of characters to the stream. Note: callers must make sure the data to be written is v...
bool is_done() const
Determines if the task is completed.
Definition: pplxtasks.h:4454