Working with in-memory files
Sometimes it is very convenient to be able to treat some arbitrary in-memory runtime generated data as if it were in a file. As an example, let's consider using a JPEG image downloaded from a photo hosting, as an OpenGL texture. We do not need to save it into the internal storage, as it is a waste of CPU time. We also do not want to write separate code for loading images from memory. Since we have our abstract iIStream
and iRawFile
interfaces, we just implement the latter to support memory blocks as the data source.
Getting ready
In the previous recipes, we already used the Blob
class, which is a simple wrapper around a void*
buffer.
How to do it...
Our
iRawFile
interface consists of two methods:GetFileData()
andGetFileSize()
. We just delegate these calls to an instance ofBlob
:class ManagedMemRawFile: public iRawFile { public: ManagedMemRawFile(): FBlob( NULL ) {} virtual const ubyte* GetFileData() const { return ( const ubyte* )FBlob->GetData(); } ...