mirror of
https://github.com/libSDL2pp/libSDL2pp.git
synced 2025-08-04 03:15:59 -04:00
Add custom RWops for use with constant containers
This commit is contained in:
parent
1993fd3ec6
commit
e3ad1a4ad7
@ -88,6 +88,61 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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&) = default;
|
||||||
|
ConstContainerRWops& operator=(const ConstContainerRWops&) = default;
|
||||||
|
ConstContainerRWops(ConstContainerRWops&&) = default;
|
||||||
|
ConstContainerRWops& operator=(ConstContainerRWops&&) = 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*, size_t, size_t) override {
|
||||||
|
SDL_SetError("Can't write to read-only container");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual int Close() override {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -114,6 +114,33 @@ BEGIN_TEST()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Short test for ContainerRWops
|
||||||
|
{
|
||||||
|
const std::vector<char> buffer = { 'a', 'b', 'c', 'd' };
|
||||||
|
|
||||||
|
RWops rw((ConstContainerRWops<std::vector<char>>(buffer)));
|
||||||
|
|
||||||
|
{
|
||||||
|
// Read via C++
|
||||||
|
EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0);
|
||||||
|
|
||||||
|
char buf[4] = {0};
|
||||||
|
EXPECT_TRUE(rw.Read(buf, 1, 4) == 4);
|
||||||
|
EXPECT_TRUE(buf[0] == 'a' && buf[3] == 'd');
|
||||||
|
|
||||||
|
// Position after read
|
||||||
|
EXPECT_TRUE(rw.Tell() == 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Write
|
||||||
|
char buf[4] = {0};
|
||||||
|
|
||||||
|
EXPECT_TRUE(rw.Write(buf, 1, 4) == 0);
|
||||||
|
EXPECT_TRUE(rw.Write(buf, 4, 1) == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SDL file read test
|
// SDL file read test
|
||||||
{
|
{
|
||||||
RWops rw = RWops::FromFile(TESTDATA_DIR "/test.txt");
|
RWops rw = RWops::FromFile(TESTDATA_DIR "/test.txt");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user