mirror of
https://github.com/libSDL2pp/libSDL2pp.git
synced 2025-08-03 10:55:57 -04:00
Allow ContainerRWops to work with both const and non-const containers
Analogus to how StreamRWops work. Also add more tests for ContainerRWops.
This commit is contained in:
parent
53aa26dec5
commit
d7987b33da
@ -24,16 +24,36 @@
|
||||
|
||||
#include <SDL2pp/RWops.hh>
|
||||
|
||||
#include <type_traits>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace SDL2pp {
|
||||
|
||||
template<typename C>
|
||||
template <class C>
|
||||
class ContainerRWops : public CustomRWops {
|
||||
protected:
|
||||
C& container_;
|
||||
size_t position_;
|
||||
|
||||
private:
|
||||
template <class CC>
|
||||
typename std::enable_if<!std::is_const<CC>::value, size_t>::type WriteHelper(const void* ptr, size_t size, size_t maxnum) {
|
||||
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;
|
||||
}
|
||||
|
||||
template <class CC>
|
||||
typename std::enable_if<std::is_const<CC>::value, size_t>::type WriteHelper(const void*, size_t, size_t) {
|
||||
SDL_SetError("Can't write to read-only container");
|
||||
return 0;
|
||||
}
|
||||
|
||||
public:
|
||||
ContainerRWops(C& container) : container_(container), position_(0) {
|
||||
}
|
||||
@ -74,69 +94,7 @@ public:
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename C>
|
||||
class ConstContainerRWops : public CustomRWops {
|
||||
protected:
|
||||
const C& container_;
|
||||
size_t position_;
|
||||
|
||||
public:
|
||||
ConstContainerRWops(const C& container) : container_(container), position_(0) {
|
||||
}
|
||||
|
||||
ConstContainerRWops(const ConstContainerRWops<C>&) = default;
|
||||
ConstContainerRWops& operator=(const ConstContainerRWops<C>&) = delete;
|
||||
ConstContainerRWops(ConstContainerRWops<C>&&) noexcept = default;
|
||||
ConstContainerRWops& operator=(ConstContainerRWops<C>&&) = delete;
|
||||
|
||||
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 std::logic_error("Unexpected whence value for ConstContainerRWops::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*, size_t, size_t) override {
|
||||
SDL_SetError("Can't write to read-only container");
|
||||
return 0;
|
||||
return WriteHelper<C>(ptr, size, maxnum);
|
||||
}
|
||||
|
||||
virtual int Close() override {
|
||||
|
@ -119,7 +119,7 @@ BEGIN_TEST()
|
||||
{
|
||||
const std::vector<char> buffer = { 'a', 'b', 'c', 'd' };
|
||||
|
||||
RWops rw((ConstContainerRWops<std::vector<char>>(buffer)));
|
||||
RWops rw((ContainerRWops<const std::vector<char>>(buffer)));
|
||||
|
||||
{
|
||||
// Read via C++
|
||||
@ -134,12 +134,30 @@ BEGIN_TEST()
|
||||
}
|
||||
|
||||
{
|
||||
// Write
|
||||
// Write to const container fails
|
||||
char buf[4] = {0};
|
||||
|
||||
EXPECT_TRUE(rw.Write(buf, 1, 4) == 0);
|
||||
EXPECT_TRUE(rw.Write(buf, 4, 1) == 0);
|
||||
}
|
||||
|
||||
{
|
||||
// Write to non-const container
|
||||
std::vector<char> vec;
|
||||
|
||||
RWops rw((ContainerRWops<std::vector<char>>(vec)));
|
||||
|
||||
char buf[4] = {'a', 'b', 'c', 'd'};
|
||||
|
||||
EXPECT_TRUE(rw.Write(buf, 1, 4) == 4);
|
||||
EXPECT_TRUE(rw.Write(buf, 4, 1) == 1);
|
||||
|
||||
EXPECT_TRUE(rw.Seek(2, SEEK_SET) == 2);
|
||||
EXPECT_TRUE(rw.Write(buf, 2, 2) == 2);
|
||||
|
||||
EXPECT_TRUE(vec.size() == 8);
|
||||
EXPECT_TRUE(std::string(vec.data(), 8) == "ababcdcd");
|
||||
}
|
||||
}
|
||||
|
||||
// Test for StreamRWops
|
||||
|
Loading…
x
Reference in New Issue
Block a user