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