libSDL2pp
C++ bindings/wrapper for SDL2
Loading...
Searching...
No Matches
RWops.hh
1/*
2 libSDL2pp - C++ bindings/wrapper for SDL2
3 Copyright (C) 2014-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#ifndef SDL2PP_RWOPS_HH
23#define SDL2PP_RWOPS_HH
24
25#include <string>
26
27#include <SDL_rwops.h>
28#include <SDL2pp/Exception.hh>
29#include <SDL2pp/Export.hh>
30
31namespace SDL2pp {
32
48class SDL2PP_EXPORT CustomRWops {
49public:
54 virtual ~CustomRWops() {}
55
64 virtual Sint64 Size() = 0;
65
78 virtual Sint64 Seek(Sint64 offset, int whence) = 0;
79
92
93 virtual size_t Read(void* ptr, size_t size, size_t maxnum) = 0;
106 virtual size_t Write(const void* ptr, size_t size, size_t num) = 0;
107
116 virtual int Close() = 0;
117};
118
156class SDL2PP_EXPORT RWops {
157protected:
158 SDL_RWops* rwops_;
159
160private:
161 static Sint64 StdSizeFuncWrapper(SDL_RWops* context);
162 static Sint64 StdSeekFuncWrapper(SDL_RWops* context, Sint64 offset, int whence);
163 static size_t StdReadFuncWrapper(SDL_RWops* context, void *ptr, size_t size, size_t maxnum);
164 static size_t StdWriteFuncWrapper(SDL_RWops* context, const void *ptr, size_t size, size_t maxnum);
165 static int StdCloseFuncWrapper(SDL_RWops* context);
166
167 static Sint64 CustomSizeFuncWrapper(SDL_RWops* context);
168 static Sint64 CustomSeekFuncWrapper(SDL_RWops* context, Sint64 offset, int whence);
169 static size_t CustomReadFuncWrapper(SDL_RWops* context, void *ptr, size_t size, size_t maxnum);
170 static size_t CustomWriteFuncWrapper(SDL_RWops* context, const void *ptr, size_t size, size_t maxnum);
171 static int CustomCloseFuncWrapper(SDL_RWops* context);
172
173 static RWops CheckedCreateStandardRWops(SDL_RWops* sdl_rwops, const char* errmsg);
174
175public:
188 static RWops FromFP(FILE* file, bool autoclose);
189
201 static RWops FromConstMem(const void* mem, int size);
202
214 static RWops FromMem(void* mem, int size);
215
227 static RWops FromFile(const std::string& file, const std::string& mode = "rb");
228
235 explicit RWops(SDL_RWops* rwops);
236
243 RWops(RWops&& other) noexcept;
244
253 RWops& operator=(RWops&& other) noexcept;
254
261 RWops(const RWops&) = delete;
262
269 RWops& operator=(const RWops&) = delete;
270
281 template<class C>
282 explicit RWops(C&& custom_rwops) {
283 rwops_ = SDL_AllocRW();
284 if (rwops_ == nullptr)
285 throw Exception("SDL_AllocRW");
286
287 rwops_->size = CustomSizeFuncWrapper;
288 rwops_->seek = CustomSeekFuncWrapper;
289 rwops_->read = CustomReadFuncWrapper;
290 rwops_->write = CustomWriteFuncWrapper;
291 rwops_->close = CustomCloseFuncWrapper;
292 rwops_->type = 0x57524370; // "pCRW" for libSDLp[p] [C]ustom [RW]ops
293 try {
294 rwops_->hidden.unknown.data1 = static_cast<void*>(new C(std::move(custom_rwops)));
295 } catch (...) {
296 SDL_FreeRW(rwops_);
297 throw;
298 }
299 rwops_->hidden.unknown.data2 = static_cast<void*>(this);
300 }
301
306 ~RWops();
307
314 SDL_RWops* Get() const;
315
324 int Close();
325
338 size_t Read(void* ptr, size_t size, size_t maxnum);
339
352 Sint64 Seek(Sint64 offset, int whence);
353
366 size_t Write(const void* ptr, size_t size, size_t num);
367
376 Sint64 Tell();
377
384 Sint64 Size();
385
395 Uint16 ReadBE16();
396
406 Uint32 ReadBE32();
407
417 Uint64 ReadBE64();
418
428 Uint16 ReadLE16();
429
439 Uint32 ReadLE32();
440
450 Uint64 ReadLE64();
451
463 size_t WriteBE16(Uint16 value);
464
476 size_t WriteBE32(Uint32 value);
477
489 size_t WriteBE64(Uint64 value);
490
502 size_t WriteLE16(Uint16 value);
503
515 size_t WriteLE32(Uint32 value);
516
528 size_t WriteLE64(Uint64 value);
529};
530
531}
532
533#endif
Base class for custom RWops.
Definition: RWops.hh:48
virtual int Close()=0
Close data source.
virtual ~CustomRWops()
Destructor.
Definition: RWops.hh:54
virtual Sint64 Seek(Sint64 offset, int whence)=0
Seek within the data stream.
virtual size_t Read(void *ptr, size_t size, size_t maxnum)=0
Read from a data stream.
virtual size_t Write(const void *ptr, size_t size, size_t num)=0
Write to a data stream.
virtual Sint64 Size()=0
Get the size of the data stream.
Exception object representing SDL2 error
Definition: Exception.hh:71
I/O abstraction.
Definition: RWops.hh:156
RWops(const RWops &)=delete
Deleted copy constructor.
RWops & operator=(const RWops &)=delete
Deleted assignment operator.
SDL_RWops * rwops_
Managed SDL_RWops object.
Definition: RWops.hh:158
RWops(C &&custom_rwops)
Create RWops from CustomRWops derived class.
Definition: RWops.hh:282