From fbf5d046ead0e745644720988d5689ff1ae61730 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 5 Sep 2014 04:48:45 +0400 Subject: [PATCH] Add custom RWops class which works with arbitrary container as a storage --- CMakeLists.txt | 1 + SDL2pp/ExtraRWops.hh | 93 ++++++++++++++++++++++++++++++++++++++++++++ SDL2pp/SDL2pp.hh | 1 + 3 files changed, 95 insertions(+) create mode 100644 SDL2pp/ExtraRWops.hh diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cdf8e9..a2e3ffd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/SDL2pp/ExtraRWops.hh b/SDL2pp/ExtraRWops.hh new file mode 100644 index 0000000..3d682cd --- /dev/null +++ b/SDL2pp/ExtraRWops.hh @@ -0,0 +1,93 @@ +/* + libSDL2pp - C++ wrapper for libSDL2 + Copyright (C) 2014 Dmitry Marakasov + + 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 +#include + +namespace SDL2pp { + +template +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(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(ptr), reinterpret_cast(ptr) + size * maxnum, container_.begin() + position_); + + position_ += size * maxnum; + + return maxnum; + } + + virtual int Close() override { + return 0; + } +}; + +} + +#endif diff --git a/SDL2pp/SDL2pp.hh b/SDL2pp/SDL2pp.hh index f107f94..9f92245 100644 --- a/SDL2pp/SDL2pp.hh +++ b/SDL2pp/SDL2pp.hh @@ -30,5 +30,6 @@ #include #include #include +#include #endif