IBamIODevice.h

Go to the documentation of this file.
00001 // ***************************************************************************
00002 // IBamIODevice.h (c) 2011 Derek Barnett
00003 // Marth Lab, Department of Biology, Boston College
00004 // ---------------------------------------------------------------------------
00005 // Last modified: 10 November 2011 (DB)
00006 // ---------------------------------------------------------------------------
00007 // Base class for all BAM I/O devices (e.g. local file, pipe, HTTP, FTP, etc.)
00008 //
00009 // Derived classes should provide protocol-specific implementations for
00010 // reading/writing plain bytes, as well as other I/O-related behaviors.
00011 //
00012 // Since IBamIODevices may be defined in client code, the internal
00013 // BamExceptions are NOT allowed to be thrown from devices, including the
00014 // built-in ones. This keeps a consistent interface at the BgzfStream for
00015 // handling any device type. Use the error string for relaying error messages.
00016 // ***************************************************************************
00017 
00018 #ifndef IBAMIODEVICE_H
00019 #define IBAMIODEVICE_H
00020 
00021 #include "api/api_global.h"
00022 #include <cstdio>
00023 #include <string>
00024 
00025 namespace BamTools {
00026 
00027 class API_EXPORT IBamIODevice {
00028 
00029     // enums
00030     public: enum OpenMode { NotOpen   = 0x0000
00031                           , ReadOnly  = 0x0001
00032                           , WriteOnly = 0x0002
00033                           , ReadWrite = ReadOnly | WriteOnly
00034                           };
00035 
00036     // ctor & dtor
00037     public:
00038         virtual ~IBamIODevice(void) { }
00039 
00040     // IBamIODevice interface
00041     public:
00042 
00043         // TODO: add seek(pos, *from*)
00044 
00045         // pure virtuals
00046         virtual void Close(void) =0;
00047         virtual bool IsRandomAccess(void) const =0;
00048         virtual bool Open(const OpenMode mode) =0;
00049         virtual int64_t Read(char* data, const unsigned int numBytes) =0;
00050         virtual bool Seek(const int64_t& position, const int origin = SEEK_SET) =0;
00051         virtual int64_t Tell(void) const =0;
00052         virtual int64_t Write(const char* data, const unsigned int numBytes) =0;
00053 
00054         // default implementation provided
00055         virtual std::string GetErrorString(void);
00056         virtual bool IsOpen(void) const;
00057         virtual OpenMode Mode(void) const;
00058 
00059     // internal methods
00060     protected:
00061         IBamIODevice(void); // hidden ctor
00062         void SetErrorString(const std::string& where, const std::string& what);
00063 
00064     // data members
00065     protected:
00066         OpenMode    m_mode;
00067         std::string m_errorString;
00068 };
00069 
00070 inline
00071 IBamIODevice::IBamIODevice(void)
00072     : m_mode(IBamIODevice::NotOpen)
00073 { }
00074 
00075 inline
00076 std::string IBamIODevice::GetErrorString(void) {
00077     return m_errorString;
00078 }
00079 
00080 inline
00081 bool IBamIODevice::IsOpen(void) const {
00082     return ( m_mode != IBamIODevice::NotOpen );
00083 }
00084 
00085 inline
00086 IBamIODevice::OpenMode IBamIODevice::Mode(void) const {
00087     return m_mode;
00088 }
00089 
00090 inline
00091 void IBamIODevice::SetErrorString(const std::string& where, const std::string& what) {
00092     static const std::string SEPARATOR = ": ";
00093     m_errorString = where + SEPARATOR + what;
00094 }
00095 
00096 } // namespace BamTools
00097 
00098 #endif // IBAMIODEVICE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Aug 29 17:43:46 2012 for BamTools by  doxygen 1.6.3