6#include "system/io/stream.h"
7#include "system/bit_converter.h"
9namespace System {
namespace IO {
24template <
typename Elem,
typename Traits = std::
char_traits<Elem>>
30 using Mysb = std::basic_streambuf<char_type, traits_type>;
32 using int_type =
typename traits_type::int_type;
33 using pos_type =
typename traits_type::pos_type;
34 using off_type =
typename traits_type::off_type;
54 const std::locale& locale = std::locale())
55 : m_stream(str), m_wrapping_mode(mode), m_elem_size(sizeof(
char_type))
84 if (
this != std::addressof(right))
95 if (
this != std::addressof(right))
98 std::swap(m_stream, right.m_stream);
99 std::swap(m_wrapping_mode, right.m_wrapping_mode);
100 std::swap(m_elem_size, right.m_elem_size);
112 if (traits_type::eq_int_type(traits_type::eof(), meta))
114 return traits_type::not_eof(meta);
117 if (!m_stream->get_CanWrite())
119 return traits_type::eof();
124 m_stream->WriteByte(uint8_t(meta));
129 for (
int i = 0; i < decoded_elem->get_Length(); ++i)
131 m_stream->WriteByte(decoded_elem[i]);
135 throw NotSupportedException(u
"Wrapper does not support current wrapping mode.");
141 return traits_type::eof();
151 if (!m_stream->get_CanRead())
153 return traits_type::eof();
156 pos_type pos = m_stream->get_Position();
157 int64_t offset = pos;
162 int byte = m_stream->ReadByte();
165 return traits_type::eof();
172 offset = m_elem_size;
173 if (m_stream->get_Length() - m_stream->get_Position() < m_elem_size)
175 return traits_type::eof();
178 auto dst = (uint8_t*)&meta;
181 for (
int i = 0; i < m_elem_size; ++i)
183 dst[i] = uint8_t(m_stream->ReadByte());
188 for (
int i = m_elem_size - 1; i >= 0; --i)
190 dst[i] = uint8_t(m_stream->ReadByte());
195 throw NotSupportedException(u
"Wrapper does not support current wrapping mode.");
197 if (pos != m_stream->get_Position())
206 return traits_type::eof();
216 if (!m_stream->get_CanRead())
218 return traits_type::eof();
224 int byte = m_stream->ReadByte();
227 return traits_type::eof();
234 if (m_stream->get_Length() - m_stream->get_Position() < m_elem_size)
236 return traits_type::eof();
239 auto dst = (uint8_t*)&meta;
242 for (
int i = 0; i < m_elem_size; ++i)
244 dst[i] = uint8_t(m_stream->ReadByte());
249 for (
int i = m_elem_size - 1; i >= 0; --i)
251 dst[i] = uint8_t(m_stream->ReadByte());
256 throw NotSupportedException(u
"Wrapper does not support current wrapping mode.");
262 return traits_type::eof();
273 if (traits_type::eq_int_type(traits_type::eof(), meta))
275 return traits_type::not_eof(meta);
285 offset = m_elem_size;
288 throw NotSupportedException(u
"Wrapper does not support current wrapping mode.");
290 if (!m_stream->get_CanSeek() || !(m_stream->get_CanWrite() || m_stream->get_CanRead()) ||
293 return traits_type::eof();
296 if (m_stream->get_CanWrite())
301 else if (m_stream->get_CanRead())
305 if (traits_type::eq_int_type(cur_meta, meta))
311 return traits_type::eof();
315 return traits_type::not_eof(meta);
319 return traits_type::eof();
330 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
override
334 if (!m_stream->get_CanSeek())
346 offset = off * m_elem_size;
349 throw NotSupportedException(u
"Wrapper does not support current wrapping mode.");
352 if (way == std::ios_base::beg)
356 else if (way == std::ios_base::cur)
360 else if (way == std::ios_base::end)
365 return m_stream->Seek(offset, origin);
379 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
override
383 if (!m_stream->get_CanSeek())
391 value = int64_t(pos);
395 value = int64_t(pos * m_elem_size);
398 throw NotSupportedException(u
"Wrapper does not support current wrapping mode.");
400 m_stream->set_Position(value);
401 return m_stream->get_Position();
414 if (!m_stream->get_CanSeek() || !m_stream->get_CanRead())
416 return traits_type::eof();
419 pos_type cur = m_stream->get_Position();
420 off_type length = m_stream->get_Length();
428 return (length - cur) / m_elem_size;
431 throw NotSupportedException(u
"Wrapper does not support current wrapping mode.");
435 return traits_type::eof();
442 Mysb::setg(
nullptr,
nullptr,
nullptr);
443 Mysb::setp(
nullptr,
nullptr);
457template <
typename Elem,
typename Traits = std::
char_traits<Elem>>
463 using Mybase = std::basic_istream<char_type, traits_type>;
471 :
Mybase(std::addressof(m_stream_buffer)), m_stream_buffer(str, mode, std::ios_base::getloc())
500 if (
this != std::addressof(right))
510 if (
this != std::addressof(right))
513 m_stream_buffer.
swap(right.m_stream_buffer);
519 Mysb m_stream_buffer;
523template <
typename Elem,
typename Traits = std::
char_traits<Elem>>
529 using Mybase = std::basic_ostream<char_type, traits_type>;
537 :
Mybase(std::addressof(m_stream_buffer)), m_stream_buffer(str, mode, std::ios_base::getloc())
566 if (
this != std::addressof(right))
576 if (
this != std::addressof(right))
579 m_stream_buffer.
swap(right.m_stream_buffer);
585 Mysb m_stream_buffer;
589template <
typename Elem,
typename Traits = std::
char_traits<Elem>>
595 using Mybase = std::basic_iostream<char_type, traits_type>;
603 :
Mybase(std::addressof(m_stream_buffer)), m_stream_buffer(str, mode, std::ios_base::getloc())
632 if (
this != std::addressof(right))
642 if (
this != std::addressof(right))
645 m_stream_buffer.
swap(right.m_stream_buffer);
651 Mysb m_stream_buffer;
673template <
typename char_type,
typename traits_type>
683template <
typename char_type,
typename traits_type>
693template <
typename char_type,
typename traits_type>
703template <
typename char_type,
typename traits_type>
static bool _IsLittleEndian()
Indicates the endianness of the current architecture.
static System::ArrayPtr< uint8_t > GetBytes(bool value)
Converts the specified boolean value into an array of bytes.
Template that represents wrapper of exceptions that are derived from Exception class.
Definition: exception.h:113
Represents a buffer that wraps System::IO::Stream-like streams and allows them to be used as an std::...
Definition: system_stream_wrappers.h:26
virtual int_type pbackfail(int_type meta=traits_type::eof()) override
Put an element to stream on previous position.
Definition: system_stream_wrappers.h:269
typename traits_type::pos_type pos_type
Definition: system_stream_wrappers.h:33
virtual pos_type seekpos(pos_type pos, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out) override
Set position pointer to absolute pos position.
Definition: system_stream_wrappers.h:378
BasicSystemIOStreamBuf(const SharedPtr< Stream > &str, SystemIOStreamWrappingMode mode=SystemIOStreamWrappingMode::Binary, const std::locale &locale=std::locale())
Constructs a new instance of the BasicSystemIOStreamBuf.
Definition: system_stream_wrappers.h:52
BasicSystemIOStreamBuf(const BasicSystemIOStreamBuf &)=delete
Copy constructor. Deleted.
virtual std::streamsize showmanyc() override
Get number of characters available.
Definition: system_stream_wrappers.h:410
Traits traits_type
Definition: system_stream_wrappers.h:29
typename traits_type::off_type off_type
Definition: system_stream_wrappers.h:34
virtual pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out) override
Change position by off, according to way, mode.
Definition: system_stream_wrappers.h:329
virtual ~BasicSystemIOStreamBuf() override
Destructor.
Definition: system_stream_wrappers.h:37
void swap(BasicSystemIOStreamBuf &right)
Call to swap *this and right, if they are not equal.
Definition: system_stream_wrappers.h:93
BasicSystemIOStreamBuf & operator=(BasicSystemIOStreamBuf &&right) noexcept
Move assignment operator.
Definition: system_stream_wrappers.h:74
Elem char_type
Definition: system_stream_wrappers.h:28
void Tidy()
Set all pointers to nullptr.
Definition: system_stream_wrappers.h:440
void AssignRV(BasicSystemIOStreamBuf &&right)
Used in move constructor and move assignment operator to reset pointers and call swap().
Definition: system_stream_wrappers.h:82
virtual int_type uflow() override
Get an element from stream and advances one position forward.
Definition: system_stream_wrappers.h:212
std::basic_streambuf< char_type, traits_type > Mysb
Definition: system_stream_wrappers.h:30
virtual int_type overflow(int_type meta=traits_type::eof()) override
Put a character to the stream and advances one position forward.
Definition: system_stream_wrappers.h:108
BasicSystemIOStreamBuf()
Constructs a new instance of the BasicSystemIOStreamBuf.
Definition: system_stream_wrappers.h:43
virtual int_type underflow() override
Get an element from stream, but don't advances position.
Definition: system_stream_wrappers.h:147
BasicSystemIOStreamBuf & operator=(const BasicSystemIOStreamBuf &)=delete
Copy assignment operator. Deleted.
BasicSystemIOStreamBuf(BasicSystemIOStreamBuf &&right) noexcept
Move constructor.
Definition: system_stream_wrappers.h:63
typename traits_type::int_type int_type
Definition: system_stream_wrappers.h:32
Represents a std::iostream-like wrapper that used BasicSystemIOStreamBuf as internal buffer.
Definition: system_stream_wrappers.h:591
std::basic_iostream< char_type, traits_type > Mybase
Definition: system_stream_wrappers.h:595
void swap(BasicSystemIOStreamWrapper &right)
Call to swap *this and right, if they are not equal.
Definition: system_stream_wrappers.h:640
BasicSystemIOStreamWrapper(SharedPtr< Stream > str, SystemIOStreamWrappingMode mode=SystemIOStreamWrappingMode::Binary)
Constructs a new instance of the BasicSystemIOStreamWrapper.
Definition: system_stream_wrappers.h:601
BasicSystemIOStreamBuf< char_type, traits_type > Mysb
Definition: system_stream_wrappers.h:596
Traits traits_type
Definition: system_stream_wrappers.h:594
Elem char_type
Definition: system_stream_wrappers.h:593
void AssignRV(BasicSystemIOStreamWrapper &&right)
Used in move constructor and move assignment operator to reset pointers and call swap().
Definition: system_stream_wrappers.h:630
BasicSystemIOStreamWrapper(BasicSystemIOStreamWrapper &&right) noexcept
Move constructor.
Definition: system_stream_wrappers.h:611
BasicSystemIOStreamWrapper & operator=(const BasicSystemIOStreamWrapper &)=delete
Copy assignment operator. Deleted.
BasicSystemIOStreamWrapper & operator=(BasicSystemIOStreamWrapper &&right) noexcept
Move assignment operator.
Definition: system_stream_wrappers.h:622
BasicSystemIOStreamWrapper(const BasicSystemIOStreamWrapper &)=delete
Copy constructor. Deleted.
Represents a std::istream-like wrapper that used BasicSystemIOStreamBuf as internal buffer.
Definition: system_stream_wrappers.h:459
BasicSystemIStreamWrapper & operator=(BasicSystemIStreamWrapper &&right) noexcept
Move assignment operator.
Definition: system_stream_wrappers.h:490
BasicSystemIOStreamBuf< char_type, traits_type > Mysb
Definition: system_stream_wrappers.h:464
std::basic_istream< char_type, traits_type > Mybase
Definition: system_stream_wrappers.h:463
void AssignRV(BasicSystemIStreamWrapper &&right)
Used in move constructor and move assignment operator to reset pointers and call swap().
Definition: system_stream_wrappers.h:498
BasicSystemIStreamWrapper & operator=(const BasicSystemIStreamWrapper &)=delete
Copy assignment operator. Deleted.
BasicSystemIStreamWrapper(const BasicSystemIStreamWrapper &)=delete
Copy constructor. Deleted.
Traits traits_type
Definition: system_stream_wrappers.h:462
void swap(BasicSystemIStreamWrapper &right)
Call to swap *this and right, if they are not equal.
Definition: system_stream_wrappers.h:508
Elem char_type
Definition: system_stream_wrappers.h:461
BasicSystemIStreamWrapper(SharedPtr< Stream > str, SystemIOStreamWrappingMode mode=SystemIOStreamWrappingMode::Binary)
Constructs a new instance of the BasicSystemIStreamWrapper.
Definition: system_stream_wrappers.h:469
BasicSystemIStreamWrapper(BasicSystemIStreamWrapper &&right) noexcept
Move constructor.
Definition: system_stream_wrappers.h:479
Represents a std::ostream-like wrapper that used BasicSystemIOStreamBuf as internal buffer.
Definition: system_stream_wrappers.h:525
Traits traits_type
Definition: system_stream_wrappers.h:528
BasicSystemOStreamWrapper(SharedPtr< Stream > str, SystemIOStreamWrappingMode mode=SystemIOStreamWrappingMode::Binary)
Constructs a new instance of the BasicSystemOStreamWrapper.
Definition: system_stream_wrappers.h:535
BasicSystemIOStreamBuf< char_type, traits_type > Mysb
Definition: system_stream_wrappers.h:530
void AssignRV(BasicSystemOStreamWrapper &&right)
Used in move constructor and move assignment operator to reset pointers and call swap().
Definition: system_stream_wrappers.h:564
std::basic_ostream< char_type, traits_type > Mybase
Definition: system_stream_wrappers.h:529
BasicSystemOStreamWrapper & operator=(const BasicSystemOStreamWrapper &)=delete
Copy assignment operator. Deleted.
BasicSystemOStreamWrapper(const BasicSystemOStreamWrapper &)=delete
Copy constructor. Deleted.
BasicSystemOStreamWrapper(BasicSystemOStreamWrapper &&right) noexcept
Move constructor.
Definition: system_stream_wrappers.h:545
BasicSystemOStreamWrapper & operator=(BasicSystemOStreamWrapper &&right) noexcept
Move assignment operator.
Definition: system_stream_wrappers.h:556
void swap(BasicSystemOStreamWrapper &right)
Call to swap *this and right, if they are not equal.
Definition: system_stream_wrappers.h:574
Elem char_type
Definition: system_stream_wrappers.h:527
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
SystemIOStreamWrappingMode
Specifies the mode of I/O operations that wrappers will perform on System::IO::Stream-like streams.
Definition: system_stream_wrappers.h:13
@ Conversion
A mode that allows input operations to convert stream bytes from uint8_t type to char_type type and v...
@ Binary
A mode that allows input operations to encode stream bytes into char_type data and decode char_type d...
@ Conversion
A mode that allows input operations to convert stream data from char_type type to uint8_t type and vi...
@ Binary
A mode that allows input operations to decode stream data of char_type type into bytes,...
SeekOrigin
Specifies the reference position in the stream relative to which the position to seek to is specified...
Definition: seekorigin.h:11
@ Begin
Beginning of the stream.
@ Current
Current stream position.
Definition: db_command.h:9