]> git.donarmstrong.com Git - bamtools.git/blob - src/api/IBamIODevice.h
5c1856ea4f97ab1b87d92c5fbedac77a5b6fb9a3
[bamtools.git] / src / api / IBamIODevice.h
1 // ***************************************************************************
2 // IBamIODevice.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 7 October 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Base class for all BAM I/O devices (e.g. local file, pipe, HTTP, FTP, etc.)
8 //
9 // Derived classes should provide protocol-specific implementations for
10 // reading/writing plain bytes, as well as other I/O-related behaviors.
11 //
12 // Since IBamIODevices may be defined in client code, the internal
13 // BamExceptions are NOT allowed to be thrown from devices, including the
14 // built-in ones. This keeps a consistent interface at the BgzfStream for
15 // handling any device type. Use the error string for relaying error messages.
16 // ***************************************************************************
17
18 #ifndef IBAMIODEVICE_H
19 #define IBAMIODEVICE_H
20
21 #include <api/api_global.h>
22 #include <string>
23
24 namespace BamTools {
25
26 class API_EXPORT IBamIODevice {
27
28     // enums
29     public: enum OpenMode { NotOpen = 0
30                           , ReadOnly
31                           , WriteOnly
32                           };
33
34     // ctor & dtor
35     public:
36         virtual ~IBamIODevice(void) { }
37
38     // IBamIODevice interface
39     public:
40
41         // pure virtuals
42         virtual void Close(void) =0;
43         virtual bool IsRandomAccess(void) const =0;
44         virtual bool Open(const OpenMode mode) =0;
45         virtual size_t Read(char* data, const unsigned int numBytes) =0;
46         virtual bool Seek(const int64_t& position) =0;
47         virtual int64_t Tell(void) const =0;
48         virtual size_t Write(const char* data, const unsigned int numBytes) =0;
49
50         // default implementation provided
51         virtual std::string GetErrorString(void);
52         virtual bool IsOpen(void) const;
53         virtual OpenMode Mode(void) const;
54
55     // internal methods
56     protected:
57         IBamIODevice(void); // hidden ctor
58         void SetErrorString(const std::string& where, const std::string& what);
59
60     // data members
61     protected:
62         OpenMode    m_mode;
63         std::string m_errorString;
64 };
65
66 inline
67 IBamIODevice::IBamIODevice(void)
68     : m_mode(IBamIODevice::NotOpen)
69 { }
70
71 inline
72 std::string IBamIODevice::GetErrorString(void) {
73     return m_errorString;
74 }
75
76 inline
77 bool IBamIODevice::IsOpen(void) const {
78     return ( m_mode != IBamIODevice::NotOpen );
79 }
80
81 inline
82 IBamIODevice::OpenMode IBamIODevice::Mode(void) const {
83     return m_mode;
84 }
85
86 inline
87 void IBamIODevice::SetErrorString(const std::string& where, const std::string& what) {
88     static const std::string SEPARATOR = ": ";
89     m_errorString = where + SEPARATOR + what;
90 }
91
92 } // namespace BamTools
93
94 #endif // IBAMIODEVICE_H