]> git.donarmstrong.com Git - bamtools.git/blob - src/api/IBamIODevice.h
8a746969b79a88f3da1c8d5c8fc86be2f85c648c
[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: 10 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 <cstdio>
23 #include <string>
24
25 namespace BamTools {
26
27 class API_EXPORT IBamIODevice {
28
29     // enums
30     public: enum OpenMode { NotOpen   = 0x0000
31                           , ReadOnly  = 0x0001
32                           , WriteOnly = 0x0002
33                           , ReadWrite = ReadOnly | WriteOnly
34                           };
35
36     // ctor & dtor
37     public:
38         virtual ~IBamIODevice(void) { }
39
40     // IBamIODevice interface
41     public:
42
43         // TODO: add seek(pos, *from*)
44
45         // pure virtuals
46         virtual void Close(void) =0;
47         virtual bool IsRandomAccess(void) const =0;
48         virtual bool Open(const OpenMode mode) =0;
49         virtual int64_t Read(char* data, const unsigned int numBytes) =0;
50         virtual bool Seek(const int64_t& position, const int origin = SEEK_SET) =0;
51         virtual int64_t Tell(void) const =0;
52         virtual int64_t Write(const char* data, const unsigned int numBytes) =0;
53
54         // default implementation provided
55         virtual std::string GetErrorString(void);
56         virtual bool IsOpen(void) const;
57         virtual OpenMode Mode(void) const;
58
59     // internal methods
60     protected:
61         IBamIODevice(void); // hidden ctor
62         void SetErrorString(const std::string& where, const std::string& what);
63
64     // data members
65     protected:
66         OpenMode    m_mode;
67         std::string m_errorString;
68 };
69
70 inline
71 IBamIODevice::IBamIODevice(void)
72     : m_mode(IBamIODevice::NotOpen)
73 { }
74
75 inline
76 std::string IBamIODevice::GetErrorString(void) {
77     return m_errorString;
78 }
79
80 inline
81 bool IBamIODevice::IsOpen(void) const {
82     return ( m_mode != IBamIODevice::NotOpen );
83 }
84
85 inline
86 IBamIODevice::OpenMode IBamIODevice::Mode(void) const {
87     return m_mode;
88 }
89
90 inline
91 void IBamIODevice::SetErrorString(const std::string& where, const std::string& what) {
92     static const std::string SEPARATOR = ": ";
93     m_errorString = where + SEPARATOR + what;
94 }
95
96 } // namespace BamTools
97
98 #endif // IBAMIODEVICE_H