Game Development Reference
In-Depth Information
How to do it...
1.
Let us derive the
FileWriter
class from the
iOStream
interface. We add the
Open()
and
Close()
member functions on top of the
iOStream
interface and
carefully implement the
Write()
operation. Our output stream implementation does
not use memory-mapped iles and uses ordinary ile descriptors, as shown in the
following code:
class FileWriter: public iOStream
{
public:
FileWriter(): FPosition( 0 ) {}
virtual ~FileWriter() { Close(); }
bool Open( const std::string& FileName )
{
FFileName = FileName;
2. We split Android and Windows-speciic code paths using deines:
#ifdef _WIN32
FMapFile = CreateFile( FFileName.c_str(),
GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL );
return !( FMapFile == ( void* )INVALID_HANDLE_VALUE );
#else
FMapFile = open( FFileName.c_str(), O_WRONLY|O_CREAT );
FPosition = 0;
return !( FMapFile == -1 );
#endif
}
3.
The same technique is used in the other methods. The difference between both
OS systems is is trivial, so we decided to keep everything inside a single class and
separate the code using deines:
void Close()
{
#ifdef _WIN32
CloseHandle( FMapFile );
#else
if ( FMapFile != -1 ) { close( FMapFile ); }
#endif
}