mirror of
https://github.com/fraillt/bitsery.git
synced 2026-06-08 00:03:54 +00:00
basic_ios <=> basic_ostream (#45)
basic_ios changed to basic_ostream and basic_istream
This commit is contained in:
@@ -119,8 +119,8 @@ namespace bitsery {
|
||||
using TConfig = Config;
|
||||
using TValue = TChar;
|
||||
|
||||
BasicOutputStreamAdapter(std::basic_ios<TChar, CharTraits>& ostream)
|
||||
:_ios{std::addressof(ostream)} {}
|
||||
BasicOutputStreamAdapter(std::basic_ostream<TChar, CharTraits>& ostream)
|
||||
:_ostream{std::addressof(ostream)} {}
|
||||
|
||||
void currentWritePos(size_t ) {
|
||||
static_assert(std::is_void<TChar>::value, "setting write position is not supported with StreamAdapter");
|
||||
@@ -132,8 +132,7 @@ namespace bitsery {
|
||||
}
|
||||
|
||||
void flush() {
|
||||
if (auto ostream = dynamic_cast<std::basic_ostream<TChar, CharTraits>*>(_ios))
|
||||
ostream->flush();
|
||||
_ostream->flush();
|
||||
}
|
||||
|
||||
size_t writtenBytesCount() const {
|
||||
@@ -146,14 +145,14 @@ namespace bitsery {
|
||||
|
||||
template <size_t SIZE>
|
||||
void writeInternalValue(const TValue* data) {
|
||||
_ios->rdbuf()->sputn( data , SIZE );
|
||||
_ostream->rdbuf()->sputn( data , SIZE );
|
||||
}
|
||||
|
||||
void writeInternalBuffer(const TValue* data, size_t size) {
|
||||
_ios->rdbuf()->sputn( data , size );
|
||||
_ostream->rdbuf()->sputn( data , size );
|
||||
}
|
||||
|
||||
std::basic_ios<TChar, CharTraits>* _ios;
|
||||
std::basic_ostream<TChar, CharTraits>* _ostream;
|
||||
};
|
||||
|
||||
template <typename TChar, typename Config, typename CharTraits, typename TBuffer = std::array<TChar, 256>>
|
||||
@@ -169,14 +168,14 @@ namespace bitsery {
|
||||
using TValue = TChar;
|
||||
|
||||
//bufferSize is used when buffer is dynamically allocated
|
||||
BasicBufferedOutputStreamAdapter(std::basic_ios<TChar, CharTraits>& ostream, size_t bufferSize = 256)
|
||||
:_ios(std::addressof(ostream)),
|
||||
BasicBufferedOutputStreamAdapter(std::basic_ostream<TChar, CharTraits>& ostream, size_t bufferSize = 256)
|
||||
:_ostream(std::addressof(ostream)),
|
||||
_buf{},
|
||||
_beginIt{std::begin(_buf)},
|
||||
_currOffset{0}
|
||||
{
|
||||
init(bufferSize, TResizable{});
|
||||
// buffer size must be atleast 16, because writeIntervalValue expect that atleast one value fits to buffer.
|
||||
// buffer size must be atleast 16, because writeIntervalValue expect that at least one value fits to buffer.
|
||||
assert(_bufferSize >= 16);
|
||||
}
|
||||
|
||||
@@ -185,7 +184,7 @@ namespace bitsery {
|
||||
BasicBufferedOutputStreamAdapter& operator = (const BasicBufferedOutputStreamAdapter&) = delete;
|
||||
|
||||
BasicBufferedOutputStreamAdapter(BasicBufferedOutputStreamAdapter&& rhs)
|
||||
: _ios{rhs._ios},
|
||||
: _ostream{rhs._ostream},
|
||||
_buf{std::move(rhs._buf)},
|
||||
_beginIt{std::begin(_buf)},
|
||||
_currOffset{rhs._currOffset},
|
||||
@@ -194,7 +193,7 @@ namespace bitsery {
|
||||
};
|
||||
|
||||
BasicBufferedOutputStreamAdapter& operator = (BasicBufferedOutputStreamAdapter&& rhs) {
|
||||
_ios = rhs._ios;
|
||||
_ostream = rhs._ostream;
|
||||
_buf = std::move(rhs._buf);
|
||||
_beginIt = std::begin(_buf);
|
||||
_currOffset = rhs._currOffset;
|
||||
@@ -213,8 +212,7 @@ namespace bitsery {
|
||||
|
||||
void flush() {
|
||||
writeBufferToStream();
|
||||
if (auto ostream = dynamic_cast<std::basic_ostream<TChar, CharTraits>*>(_ios))
|
||||
ostream->flush();
|
||||
_ostream->flush();
|
||||
}
|
||||
|
||||
size_t writtenBytesCount() const {
|
||||
@@ -245,12 +243,12 @@ namespace bitsery {
|
||||
} else {
|
||||
writeBufferToStream();
|
||||
// write buffer directly to stream
|
||||
_ios->rdbuf()->sputn(data, size);
|
||||
_ostream->rdbuf()->sputn(data, size);
|
||||
}
|
||||
}
|
||||
|
||||
void writeBufferToStream() {
|
||||
_ios->rdbuf()->sputn(std::addressof(*_beginIt), _currOffset);
|
||||
_ostream->rdbuf()->sputn(std::addressof(*_beginIt), _currOffset);
|
||||
_currOffset = 0;
|
||||
}
|
||||
|
||||
@@ -265,7 +263,7 @@ namespace bitsery {
|
||||
_bufferSize = traits::ContainerTraits<Buffer>::size(_buf);
|
||||
}
|
||||
|
||||
std::basic_ios<TChar, CharTraits>* _ios;
|
||||
std::basic_ostream<TChar, CharTraits>* _ostream;
|
||||
TBuffer _buf;
|
||||
BufferIt _beginIt;
|
||||
size_t _currOffset;
|
||||
@@ -273,22 +271,24 @@ namespace bitsery {
|
||||
};
|
||||
|
||||
template <typename TChar, typename Config, typename CharTraits>
|
||||
class BasicIOStreamAdapter:public BasicInputStreamAdapter<TChar, Config, CharTraits>, public BasicOutputStreamAdapter<TChar, Config, CharTraits> {
|
||||
class BasicIOStreamAdapter
|
||||
: public BasicInputStreamAdapter<TChar, Config, CharTraits>
|
||||
, public BasicOutputStreamAdapter<TChar, Config, CharTraits>
|
||||
{
|
||||
public:
|
||||
using TValue = TChar;
|
||||
|
||||
//both bases contain reference to same iostream, so no need to do anything
|
||||
BasicIOStreamAdapter(std::basic_ios<TChar, CharTraits>& iostream)
|
||||
:BasicInputStreamAdapter<TChar, Config, CharTraits>{iostream},
|
||||
BasicOutputStreamAdapter<TChar, Config, CharTraits>{iostream} {
|
||||
|
||||
}
|
||||
BasicIOStreamAdapter(std::basic_iostream<TChar, CharTraits>& iostream)
|
||||
:BasicInputStreamAdapter<TChar, Config, CharTraits>{iostream}
|
||||
,BasicOutputStreamAdapter<TChar, Config, CharTraits>{iostream}
|
||||
{}
|
||||
};
|
||||
|
||||
//helper types for most common implementations for std streams
|
||||
using OutputStreamAdapter = BasicOutputStreamAdapter<char, DefaultConfig, std::char_traits<char>>;
|
||||
using InputStreamAdapter = BasicInputStreamAdapter<char, DefaultConfig, std::char_traits<char>>;
|
||||
using IOStreamAdapter = BasicIOStreamAdapter<char, DefaultConfig, std::char_traits<char>>;
|
||||
using InputStreamAdapter = BasicInputStreamAdapter<char, DefaultConfig, std::char_traits<char>>;
|
||||
using IOStreamAdapter = BasicIOStreamAdapter<char, DefaultConfig, std::char_traits<char>>;
|
||||
|
||||
using OutputBufferedStreamAdapter = BasicBufferedOutputStreamAdapter<char, DefaultConfig, std::char_traits<char>>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user