CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
system_stream_wrappers.h
1
2#pragma once
3
4#include <iostream>
5#include <utility>
6#include "system/io/stream.h"
7#include "system/bit_converter.h"
8
9namespace System { namespace IO {
10
13{
16 Binary,
20};
21
24template <typename Elem, typename Traits = std::char_traits<Elem>>
25class BasicSystemIOStreamBuf : public std::basic_streambuf<Elem, Traits>
26{
27public:
28 using char_type = Elem;
29 using traits_type = Traits;
30 using Mysb = std::basic_streambuf<char_type, traits_type>;
31
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;
35
37 virtual ~BasicSystemIOStreamBuf() override
38 {
39 Tidy();
40 }
41
44 {
45 Tidy();
46 }
47
54 const std::locale& locale = std::locale())
55 : m_stream(str), m_wrapping_mode(mode), m_elem_size(sizeof(char_type))
56 {}
57
60
64 {
65 AssignRV(std::move(right));
66 }
67
70
75 {
76 AssignRV(std::move(right));
77 return *this;
78 }
79
83 {
84 if (this != std::addressof(right))
85 {
86 Tidy();
87 this->swap(right);
88 }
89 }
90
94 {
95 if (this != std::addressof(right))
96 {
97 Mysb::swap(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);
101 }
102 }
103
104protected:
108 virtual int_type overflow(int_type meta = traits_type::eof()) override
109 {
110 try
111 {
112 if (traits_type::eq_int_type(traits_type::eof(), meta))
113 {
114 return traits_type::not_eof(meta);
115 }
116
117 if (!m_stream->get_CanWrite())
118 {
119 return traits_type::eof();
120 }
121
122 if (m_wrapping_mode == SystemIOStreamWrappingMode::Conversion || m_elem_size == 1)
123 {
124 m_stream->WriteByte(uint8_t(meta));
125 }
126 else if (m_wrapping_mode == SystemIOStreamWrappingMode::Binary)
127 {
128 auto decoded_elem = BitConverter::GetBytes(meta);
129 for (int i = 0; i < decoded_elem->get_Length(); ++i)
130 {
131 m_stream->WriteByte(decoded_elem[i]);
132 }
133 }
134 else
135 throw NotSupportedException(u"Wrapper does not support current wrapping mode.");
136
137 return meta;
138 }
139 catch (const Exception&)
140 {
141 return traits_type::eof();
142 }
143 }
144
147 virtual int_type underflow() override
148 {
149 try
150 {
151 if (!m_stream->get_CanRead())
152 {
153 return traits_type::eof();
154 }
155
156 pos_type pos = m_stream->get_Position();
157 int64_t offset = pos;
158 int_type meta;
159 if (m_wrapping_mode == SystemIOStreamWrappingMode::Conversion || m_elem_size == 1)
160 {
161 offset = 1;
162 int byte = m_stream->ReadByte();
163 if (byte == -1)
164 {
165 return traits_type::eof();
166 }
167
168 meta = int_type(byte);
169 }
170 else if (m_wrapping_mode == SystemIOStreamWrappingMode::Binary)
171 {
172 offset = m_elem_size;
173 if (m_stream->get_Length() - m_stream->get_Position() < m_elem_size)
174 {
175 return traits_type::eof();
176 }
177
178 auto dst = (uint8_t*)&meta;
180 {
181 for (int i = 0; i < m_elem_size; ++i)
182 {
183 dst[i] = uint8_t(m_stream->ReadByte());
184 }
185 }
186 else
187 {
188 for (int i = m_elem_size - 1; i >= 0; --i)
189 {
190 dst[i] = uint8_t(m_stream->ReadByte());
191 }
192 }
193 }
194 else
195 throw NotSupportedException(u"Wrapper does not support current wrapping mode.");
196
197 if (pos != m_stream->get_Position())
198 { // if the stream is not a wrapper over std::cin or std::wcin
199 m_stream->Seek(-offset, SeekOrigin::Current);
200 }
201
202 return meta;
203 }
204 catch (const Exception&)
205 {
206 return traits_type::eof();
207 }
208 }
209
212 virtual int_type uflow() override
213 {
214 try
215 {
216 if (!m_stream->get_CanRead())
217 {
218 return traits_type::eof();
219 }
220
221 int_type meta;
222 if (m_wrapping_mode == SystemIOStreamWrappingMode::Conversion || m_elem_size == 1)
223 {
224 int byte = m_stream->ReadByte();
225 if (byte == -1)
226 {
227 return traits_type::eof();
228 }
229
230 meta = int_type(byte);
231 }
232 else if (m_wrapping_mode == SystemIOStreamWrappingMode::Binary)
233 {
234 if (m_stream->get_Length() - m_stream->get_Position() < m_elem_size)
235 {
236 return traits_type::eof();
237 }
238
239 auto dst = (uint8_t*)&meta;
241 {
242 for (int i = 0; i < m_elem_size; ++i)
243 {
244 dst[i] = uint8_t(m_stream->ReadByte());
245 }
246 }
247 else
248 {
249 for (int i = m_elem_size - 1; i >= 0; --i)
250 {
251 dst[i] = uint8_t(m_stream->ReadByte());
252 }
253 }
254 }
255 else
256 throw NotSupportedException(u"Wrapper does not support current wrapping mode.");
257
258 return meta;
259 }
260 catch (const Exception&)
261 {
262 return traits_type::eof();
263 }
264 }
265
269 virtual int_type pbackfail(int_type meta = traits_type::eof()) override
270 {
271 try
272 {
273 if (traits_type::eq_int_type(traits_type::eof(), meta))
274 {
275 return traits_type::not_eof(meta);
276 }
277
278 int offset;
279 if (m_wrapping_mode == SystemIOStreamWrappingMode::Conversion)
280 {
281 offset = 1;
282 }
283 else if (m_wrapping_mode == SystemIOStreamWrappingMode::Binary)
284 {
285 offset = m_elem_size;
286 }
287 else
288 throw NotSupportedException(u"Wrapper does not support current wrapping mode.");
289
290 if (!m_stream->get_CanSeek() || !(m_stream->get_CanWrite() || m_stream->get_CanRead()) ||
291 m_stream->Seek(-offset, SeekOrigin::Current) == -1)
292 {
293 return traits_type::eof();
294 }
295
296 if (m_stream->get_CanWrite())
297 {
298 overflow(meta);
299 m_stream->Seek(-offset, SeekOrigin::Current);
300 }
301 else if (m_stream->get_CanRead())
302 {
303 int_type cur_meta = uflow();
304
305 if (traits_type::eq_int_type(cur_meta, meta))
306 {
307 m_stream->Seek(-offset, SeekOrigin::Current);
308 }
309 else
310 {
311 return traits_type::eof();
312 }
313 }
314
315 return traits_type::not_eof(meta);
316 }
317 catch (const Exception&)
318 {
319 return traits_type::eof();
320 }
321 }
322
329 virtual pos_type seekoff(off_type off, std::ios_base::seekdir way,
330 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) override
331 {
332 try
333 {
334 if (!m_stream->get_CanSeek())
335 {
336 return pos_type(off_type(-1));
337 }
338
339 int64_t offset;
340 if (m_wrapping_mode == SystemIOStreamWrappingMode::Conversion)
341 {
342 offset = off;
343 }
344 else if (m_wrapping_mode == SystemIOStreamWrappingMode::Binary)
345 {
346 offset = off * m_elem_size;
347 }
348 else
349 throw NotSupportedException(u"Wrapper does not support current wrapping mode.");
350
352 if (way == std::ios_base::beg)
353 {
354 origin = SeekOrigin::Begin;
355 }
356 else if (way == std::ios_base::cur)
357 {
358 origin = SeekOrigin::Current;
359 }
360 else if (way == std::ios_base::end)
361 {
362 origin = SeekOrigin::End;
363 }
364
365 return m_stream->Seek(offset, origin);
366 }
367 catch (const Exception&)
368 {
369 return pos_type(off_type(-1));
370 }
371 }
372
379 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) override
380 {
381 try
382 {
383 if (!m_stream->get_CanSeek())
384 {
385 return pos_type(off_type(-1));
386 }
387
388 int64_t value;
389 if (m_wrapping_mode == SystemIOStreamWrappingMode::Conversion)
390 {
391 value = int64_t(pos);
392 }
393 else if (m_wrapping_mode == SystemIOStreamWrappingMode::Binary)
394 {
395 value = int64_t(pos * m_elem_size);
396 }
397 else
398 throw NotSupportedException(u"Wrapper does not support current wrapping mode.");
399
400 m_stream->set_Position(value);
401 return m_stream->get_Position();
402 }
403 catch (const Exception&)
404 {
405 return pos_type(off_type(-1));
406 }
407 }
408
410 virtual std::streamsize showmanyc() override
411 {
412 try
413 {
414 if (!m_stream->get_CanSeek() || !m_stream->get_CanRead())
415 {
416 return traits_type::eof();
417 }
418
419 pos_type cur = m_stream->get_Position();
420 off_type length = m_stream->get_Length();
421
422 if (m_wrapping_mode == SystemIOStreamWrappingMode::Conversion)
423 {
424 return length - cur;
425 }
426 else if (m_wrapping_mode == SystemIOStreamWrappingMode::Binary)
427 {
428 return (length - cur) / m_elem_size;
429 }
430 else
431 throw NotSupportedException(u"Wrapper does not support current wrapping mode.");
432 }
433 catch (const Exception&)
434 {
435 return traits_type::eof();
436 }
437 }
438
440 void Tidy()
441 {
442 Mysb::setg(nullptr, nullptr, nullptr);
443 Mysb::setp(nullptr, nullptr);
444 m_stream.reset();
445 }
446
447private:
449 SharedPtr<Stream> m_stream;
451 SystemIOStreamWrappingMode m_wrapping_mode;
453 uint8_t m_elem_size;
454};
455
457template <typename Elem, typename Traits = std::char_traits<Elem>>
458class BasicSystemIStreamWrapper : public std::basic_istream<Elem, Traits>
459{
460public:
461 using char_type = Elem;
462 using traits_type = Traits;
463 using Mybase = std::basic_istream<char_type, traits_type>;
465
471 : Mybase(std::addressof(m_stream_buffer)), m_stream_buffer(str, mode, std::ios_base::getloc())
472 {}
473
476
479 BasicSystemIStreamWrapper(BasicSystemIStreamWrapper&& right) noexcept : Mybase(std::addressof(m_stream_buffer))
480 {
481 AssignRV(std::move(right));
482 }
483
486
491 {
492 AssignRV(std::move(right));
493 return *this;
494 }
495
499 {
500 if (this != std::addressof(right))
501 {
502 this->swap(right);
503 }
504 }
505
509 {
510 if (this != std::addressof(right))
511 {
512 Mybase::swap(right);
513 m_stream_buffer.swap(right.m_stream_buffer);
514 }
515 }
516
517private:
519 Mysb m_stream_buffer;
520};
521
523template <typename Elem, typename Traits = std::char_traits<Elem>>
524class BasicSystemOStreamWrapper : public std::basic_ostream<Elem, Traits>
525{
526public:
527 using char_type = Elem;
528 using traits_type = Traits;
529 using Mybase = std::basic_ostream<char_type, traits_type>;
531
537 : Mybase(std::addressof(m_stream_buffer)), m_stream_buffer(str, mode, std::ios_base::getloc())
538 {}
539
542
545 BasicSystemOStreamWrapper(BasicSystemOStreamWrapper&& right) noexcept : Mybase(std::addressof(m_stream_buffer))
546 {
547 AssignRV(std::move(right));
548 }
549
552
557 {
558 AssignRV(std::move(right));
559 return *this;
560 }
561
565 {
566 if (this != std::addressof(right))
567 {
568 this->swap(right);
569 }
570 }
571
575 {
576 if (this != std::addressof(right))
577 {
578 Mybase::swap(right);
579 m_stream_buffer.swap(right.m_stream_buffer);
580 }
581 }
582
583private:
585 Mysb m_stream_buffer;
586};
587
589template <typename Elem, typename Traits = std::char_traits<Elem>>
590class BasicSystemIOStreamWrapper : public std::basic_iostream<Elem, Traits>
591{
592public:
593 using char_type = Elem;
594 using traits_type = Traits;
595 using Mybase = std::basic_iostream<char_type, traits_type>;
597
603 : Mybase(std::addressof(m_stream_buffer)), m_stream_buffer(str, mode, std::ios_base::getloc())
604 {}
605
608
611 BasicSystemIOStreamWrapper(BasicSystemIOStreamWrapper&& right) noexcept : Mybase(std::addressof(m_stream_buffer))
612 {
613 AssignRV(std::move(right));
614 }
615
618
623 {
624 AssignRV(std::move(right));
625 return *this;
626 }
627
631 {
632 if (this != std::addressof(right))
633 {
634 this->swap(right);
635 }
636 }
637
641 {
642 if (this != std::addressof(right))
643 {
644 Mybase::swap(right);
645 m_stream_buffer.swap(right.m_stream_buffer);
646 }
647 }
648
649private:
651 Mysb m_stream_buffer;
652};
653
666
667}} // namespace System::IO
668
669namespace std {
673template <typename char_type, typename traits_type>
676{
677 left.swap(right);
678}
679
683template <typename char_type, typename traits_type>
686{
687 left.swap(right);
688}
689
693template <typename char_type, typename traits_type>
696{
697 left.swap(right);
698}
699
703template <typename char_type, typename traits_type>
706{
707 left.swap(right);
708}
709} // namespace std
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.
@ End
End of the stream.
Definition: db_command.h:9