Document ContainerRWops

This commit is contained in:
Dmitry Marakasov 2015-01-10 23:44:56 +03:00
parent 5c713ce5e2
commit b9173b0588

View File

@ -29,11 +29,28 @@
namespace SDL2pp { namespace SDL2pp {
////////////////////////////////////////////////////////////
/// \brief RWops adaptor which works with verctor-like containers
///
/// \ingroup io
///
/// \headerfile SDL2pp/ContainerRWops.hh
///
/// This class extends functionality of RWops concept onto random
/// access STL containers such as std::vector. With ContainerRWops,
/// you may read from / write to a container as if it were a plain
/// file.
///
/// Note that this template supports both const and mutable containers.
/// Writes to const containers always return 0, writes to mutable
/// containers extend them as needed.
///
////////////////////////////////////////////////////////////
template <class C> template <class C>
class ContainerRWops : public CustomRWops { class ContainerRWops : public CustomRWops {
protected: protected:
C& container_; C& container_; ///< Reference to container
size_t position_; size_t position_; ///< Virtual file pointer position
private: private:
template <class CC> template <class CC>
@ -55,9 +72,28 @@ private:
} }
public: public:
////////////////////////////////////////////////////////////
/// \brief Construct ContainerRWops for specificed container
///
/// \param container Container to use
///
////////////////////////////////////////////////////////////
ContainerRWops(C& container) : container_(container), position_(0) { ContainerRWops(C& container) : container_(container), position_(0) {
} }
////////////////////////////////////////////////////////////
/// \brief Seek within the container
///
/// \param offset Offset in bytes, relative to whence location; can
/// be negative
/// \param whence Any of RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END
///
/// \returns Final offset in the container stream after the seek or -1 on error
///
/// \see SDL2pp::RWops::Seek
/// \see http://wiki.libsdl.org/SDL_RWseek
///
////////////////////////////////////////////////////////////
virtual Sint64 Seek(Sint64 offset, int whence) override { virtual Sint64 Seek(Sint64 offset, int whence) override {
switch (whence) { switch (whence) {
case RW_SEEK_SET: case RW_SEEK_SET:
@ -75,6 +111,19 @@ public:
return position_; return position_;
} }
////////////////////////////////////////////////////////////
/// \brief Read from a container
///
/// \param ptr Pointer to a buffer to read data into
/// \param size Size of each object to read, in bytes
/// \param maxnum Maximum number of objects to be read
///
/// \returns Number of objects read, or 0 at end of file
///
/// \see SDL2pp::RWops::Read
/// \see http://wiki.libsdl.org/SDL_RWread
///
////////////////////////////////////////////////////////////
virtual size_t Read(void* ptr, size_t size, size_t maxnum) override { virtual size_t Read(void* ptr, size_t size, size_t maxnum) override {
if (position_ + size > container_.size()) if (position_ + size > container_.size())
return 0; return 0;
@ -88,10 +137,35 @@ public:
return toread / size; return toread / size;
} }
////////////////////////////////////////////////////////////
/// \brief Write to container
///
/// \param ptr Pointer to a buffer containing data to write
/// \param size Size of each object to write, in bytes
/// \param num Number of objects to be write
///
/// \returns Number of objects written
///
/// \see SDL2pp::RWops::Write
/// \see http://wiki.libsdl.org/SDL_RWwrite
///
////////////////////////////////////////////////////////////
virtual size_t Write(const void* ptr, size_t size, size_t num) override { virtual size_t Write(const void* ptr, size_t size, size_t num) override {
return WriteHelper<C>(ptr, size, num); return WriteHelper<C>(ptr, size, num);
} }
////////////////////////////////////////////////////////////
/// \brief Close data source
///
/// \returns Always returns 0
///
/// This function is a no-op: there's nothing to be done
/// to "close" the container
///
/// \see SDL2pp::RWops::Close
/// \see http://wiki.libsdl.org/SDL_RWclose
///
////////////////////////////////////////////////////////////
virtual int Close() override { virtual int Close() override {
return 0; return 0;
} }