libSDL2pp/SDL2pp/StreamRWops.hh
Dmitry Marakasov 53aa26dec5 Make StreamRWops work with istream/ostream
Streams do not generally work well with RWops because
* streams have separate read and write pointers
* ostream doesn't allow you to determine how many bytes were actually written
* istream and ostream have separate set of functions

Try my best to support streams in RWops though, engaging some template
magic:
* provide separate template implementations of all operations which
  depend on whether stream is an istream or ostream. This allows to
  e.g. return 0 immediately for an attempt to write() to istream.
* disallow StreamRWops for classes which are both istream and ostream
  to not run into ambiguity of separate read/write pointers
* for read failure, but partially read object back to the stream to not
  lose data for following read (not sure that e.g. fread behaves so
  though; I'll anyway expect user to Seek() after read or write failure)
* for write failure, there's no way to avoid leaking partial data to the
  stream

In general, it is best to use this container as read-only.

Also add tests for StreamRWops
2014-12-18 16:23:22 +03:00

160 lines
4.9 KiB
C++

/*
libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SDL2PP_STREAMRWOPS_HH
#define SDL2PP_STREAMRWOPS_HH
#include <SDL2pp/RWops.hh>
#include <stdexcept>
#include <iostream>
#include <type_traits>
namespace SDL2pp {
template <class S>
class StreamRWops : public CustomRWops {
// since STL streams have different pointers for reading and writing,
// supporting both at the same time is impossible
static_assert(!(std::is_base_of<std::istream, S>::value && std::is_base_of<std::ostream, S>::value), "StreamRWops does not support reading and writing at the same time");
protected:
S& stream_;
private:
template <class SS>
typename std::enable_if<std::is_base_of<std::istream, SS>::value && !std::is_base_of<std::ostream, SS>::value, void>::type SeekHelper(off_t off, std::ios_base::seekdir dir) {
stream_.seekg(off, dir);
}
template <class SS>
typename std::enable_if<!std::is_base_of<std::istream, SS>::value && std::is_base_of<std::ostream, SS>::value, void>::type SeekHelper(off_t off, std::ios_base::seekdir dir) {
stream_.seekp(off, dir);
}
template <class SS>
typename std::enable_if<std::is_base_of<std::istream, SS>::value && !std::is_base_of<std::ostream, SS>::value, off_t>::type TellHelper() {
return stream_.tellg();
}
template <class SS>
typename std::enable_if<!std::is_base_of<std::istream, SS>::value && std::is_base_of<std::ostream, SS>::value, off_t>::type TellHelper() {
return stream_.tellp();
}
template <class SS>
typename std::enable_if<std::is_base_of<std::istream, SS>::value, size_t>::type ReadHelper(void* ptr, size_t size, size_t maxnum) {
stream_.read(static_cast<char*>(ptr), size * maxnum);
size_t nread = stream_.gcount();
// eof is OK
if (stream_.rdstate() == (std::ios_base::eofbit | std::ios_base::failbit))
stream_.clear();
if (nread != size * maxnum) {
// short read
char* pos = static_cast<char*>(ptr);
pos += nread;
int count = nread % size;
// put partially read object back into the stream
while (--count >= 0)
stream_.putback(*--pos);
}
return nread / size;
}
template <class SS>
typename std::enable_if<!std::is_base_of<std::istream, SS>::value, size_t>::type ReadHelper(void*, size_t, size_t) {
return 0;
}
template <class SS>
typename std::enable_if<std::is_base_of<std::ostream, SS>::value, size_t>::type WriteHelper(const void* ptr, size_t size, size_t maxnum) {
stream_.write(static_cast<const char*>(ptr), size * maxnum);
// XXX: there seem to be no reliable way to tell how much
// was actually written
if (stream_.rdstate() & std::ios_base::badbit)
return 0;
return maxnum;
}
template <class SS>
typename std::enable_if<!std::is_base_of<std::ostream, SS>::value, size_t>::type WriteHelper(const void*, size_t, size_t) {
return 0;
}
template <class SS>
typename std::enable_if<std::is_base_of<std::ostream, SS>::value, int>::type CloseHelper() {
stream_.flush();
return stream_.rdstate() & std::ios_base::badbit;
}
template <class SS>
typename std::enable_if<!std::is_base_of<std::ostream, SS>::value, int>::type CloseHelper() {
return 0;
}
public:
StreamRWops(S& stream) : stream_(stream) {
}
StreamRWops(const StreamRWops<S>&) = default;
StreamRWops& operator=(const StreamRWops<S>&) = delete;
StreamRWops(StreamRWops<S>&&) noexcept = default;
StreamRWops& operator=(StreamRWops<S>&&) = delete;
virtual Sint64 Seek(Sint64 offset, int whence) override {
switch (whence) {
case RW_SEEK_SET:
SeekHelper<S>(offset, std::ios_base::beg);
break;
case RW_SEEK_CUR:
SeekHelper<S>(offset, std::ios_base::cur);
break;
case RW_SEEK_END:
SeekHelper<S>(offset, std::ios_base::end);
break;
default:
throw std::logic_error("Unexpected whence value for StreamRWops::Seek");
}
return TellHelper<S>();
}
virtual size_t Read(void* ptr, size_t size, size_t maxnum) override {
return ReadHelper<S>(ptr, size, maxnum);
}
virtual size_t Write(const void* ptr, size_t size, size_t maxnum) override {
return WriteHelper<S>(ptr, size, maxnum);
}
virtual int Close() override {
return CloseHelper<S>();
}
};
}
#endif