Add custom RWops class which works with arbitrary container as a storage

This commit is contained in:
Dmitry Marakasov 2014-09-05 04:48:45 +04:00
parent aaea702856
commit fbf5d046ea
3 changed files with 95 additions and 0 deletions

View File

@ -34,6 +34,7 @@ SET(LIBRARY_SOURCES
SET(LIBRARY_HEADERS
SDL2pp/Exception.hh
SDL2pp/ExtraRWops.hh
SDL2pp/Point.hh
SDL2pp/RWops.hh
SDL2pp/Rect.hh

93
SDL2pp/ExtraRWops.hh Normal file
View File

@ -0,0 +1,93 @@
/*
libSDL2pp - C++ wrapper for libSDL2
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_EXTRARWOPS_HH
#define SDL2PP_EXTRARWOPS_HH
#include <SDL2pp/RWops.hh>
#include <SDL2pp/Exception.hh>
namespace SDL2pp {
template<typename C>
class ContainerRWops : public CustomRWops {
protected:
C& container_;
size_t position_;
public:
ContainerRWops(C& container) : container_(container), position_(0) {
}
ContainerRWops(const ContainerRWops&) = default;
ContainerRWops& operator=(const ContainerRWops&) = default;
ContainerRWops(ContainerRWops&&) = default;
ContainerRWops& operator=(ContainerRWops&&) = default;
virtual Sint64 Seek(Sint64 offset, int whence) override {
switch (whence) {
case RW_SEEK_SET:
position_ = offset;
break;
case RW_SEEK_CUR:
position_ = position_ + offset;
break;
case RW_SEEK_END:
position_ = container_.size() + offset;
break;
default:
throw Exception("Unexpected whence value for WritableMemRWops::Seek");
}
return position_;
}
virtual size_t Read(void* ptr, size_t size, size_t maxnum) override {
if (position_ + size > container_.size())
return 0;
int toread = std::min((container_.size() - position_), maxnum * size);
std::copy(container_.begin() + position_, container_.begin() + position_ + toread, reinterpret_cast<unsigned char*>(ptr));
position_ += toread;
return toread / size;
}
virtual size_t Write(const void* ptr, size_t size, size_t maxnum) override {
if (position_ + size * maxnum > container_.size())
container_.resize(position_ + size * maxnum);
std::copy(reinterpret_cast<const unsigned char*>(ptr), reinterpret_cast<const unsigned char*>(ptr) + size * maxnum, container_.begin() + position_);
position_ += size * maxnum;
return maxnum;
}
virtual int Close() override {
return 0;
}
};
}
#endif

View File

@ -30,5 +30,6 @@
#include <SDL2pp/Rect.hh>
#include <SDL2pp/Point.hh>
#include <SDL2pp/RWops.hh>
#include <SDL2pp/ExtraRWops.hh>
#endif