]> git.donarmstrong.com Git - bamtools.git/blob - src/api/IBamIODevice.h
99454b21be0f2e837dcb9a15333cae5b9353ef84
[bamtools.git] / src / api / IBamIODevice.h
1 #ifndef IBAMIODEVICE_H
2 #define IBAMIODEVICE_H
3
4 #include <api/api_global.h>
5 #include <string>
6
7 namespace BamTools {
8
9 class API_EXPORT IBamIODevice {
10
11     // enums
12     public: enum OpenMode { NotOpen = 0
13                           , ReadOnly
14                           , WriteOnly
15                           };
16
17     // ctor & dtor
18     public:
19         IBamIODevice(void);
20         virtual ~IBamIODevice(void);
21
22     // IBamIODevice interface
23     public:
24
25         // pure virtuals
26         virtual void Close(void) =0;
27         virtual bool IsRandomAccess(void) const =0;
28         virtual bool Open(const OpenMode mode) =0;
29         virtual size_t Read(char* data, const unsigned int numBytes) =0;
30         virtual bool Seek(const int64_t& position) =0;
31         virtual int64_t Tell(void) const =0;
32         virtual size_t Write(const char* data, const unsigned int numBytes) =0;
33
34         // default implementation provided
35         virtual std::string ErrorString(void);
36         virtual bool IsOpen(void) const;
37         virtual OpenMode Mode(void) const;
38
39     // internal methods
40     protected:
41         void SetErrorString(const std::string& errorString);
42
43     // data members
44     protected:
45         OpenMode    m_mode;
46         std::string m_errorString;
47 };
48
49 inline
50 IBamIODevice::IBamIODevice(void)
51     : m_mode(IBamIODevice::NotOpen)
52 { }
53
54 inline
55 IBamIODevice::~IBamIODevice(void) { }
56
57 inline
58 std::string IBamIODevice::ErrorString(void) {
59     std::string e = m_errorString;
60     m_errorString.clear();
61     return e;
62 }
63
64 inline
65 bool IBamIODevice::IsOpen(void) const {
66     return ( m_mode != IBamIODevice::NotOpen );
67 }
68
69 inline
70 IBamIODevice::OpenMode IBamIODevice::Mode(void) const {
71     return m_mode;
72 }
73
74 inline
75 void IBamIODevice::SetErrorString(const std::string& errorString) {
76     m_errorString = errorString;
77 }
78
79 } // namespace BamTools
80
81 #endif // IBAMIODEVICE_H