Game Development Reference
In-Depth Information
6.
The correct deinitialization function closes all the handles:
bool RawFile::Close()
{
#ifdef OS_WINDOWS
if ( FFileData ) UnmapViewOfFile( FFileData );
if ( FMapHandle ) CloseHandle( (HANDLE)FMapHandle );
CloseHandle( (HANDLE)FMapFile );
#else
if ( FFileData ) munmap( (void*)FFileData, FSize );
#endif
return true;
}
7.
The main functions of the
iRawFile
interface,
GetFileData
and
GetFileSize
,
have trivial implementation here:
virtual const ubyte* GetFileData() { return FFileData; }
virtual uint64 GetFileSize() { return FSize; }
How it works...
To use the
RawFile
class we create an instance and wrap it into a
FileMapper
class instance:
clPtr<RawFile> F = new RawFile();
F->Open("SomeFileName");
clPtr<FileMapper> FM = new FileMapper(F);
The
FM
object can be used with any function supporting the
iIStream
interface. The hierarchy
of all our
iRawFile
implementations looks like what is shown in the following igure:
Implementing ile writers
Quite frequently, our application might want to store some of its data on the disk. Another
typical use case we have already encountered is the downloading of some ile from the
network into a memory buffer. Here, we implement two variations of the
iOStream
interface
for the ordinary and in-memory iles.