Skip to content

Instantly share code, notes, and snippets.

@iaintshine
Created February 24, 2013 14:50
Show Gist options
  • Save iaintshine/6217d915ce6d6731d13f to your computer and use it in GitHub Desktop.
Save iaintshine/6217d915ce6d6731d13f to your computer and use it in GitHub Desktop.
Cross platform shit
#pragma once
//
// Abstract interface
//
class IFile
{
public:
virtual ~IFile() = 0;
virtual std::error_code read_async( std::int8_t*, std::size_t, std::function<void(std::error_code)> callback ) = 0;
virtual std::error_code close( ) = 0;
};
//
// OS Implementation
//
class ConcreteFile : public IFile
{
virtual std::error_code close( );
};
//
// Abstract Factory ( cross ) or Factor method
//
class FileManager
{
IFile* Open( const path&, ios::ios_base::openmode, std::error_code& )
{
return new ConcreteFile( ... );
}
};
#pragma once
enum os
{
windows,
osx
};
template
<
os osid
>
class File
{
//
// If you want them stack allocated :P
//
File( File&& );
std::error_code close( );
};
template<>
class File<windows>
{
File( File&& );
std::error_code close( );
private:
HANDLE m_hFile;
};
template<>
std::error_code File<windows>::close()
{
//
// Do something
//
}
class FileManager
{
File Open( const path&, iost::ios_base::openmode, std::error_code& )
{
#ifdef _WINDOWS
return File<windows>( ... );
#end
}
} ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment