BamTools  2.5.2
IBamIODevice.h
Go to the documentation of this file.
1 // ***************************************************************************
2 // IBamIODevice.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 10 November 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 <cstdio>
22 #include <string>
23 #include "api/api_global.h"
24 
25 namespace BamTools {
26 
27 class API_EXPORT IBamIODevice
28 {
29 
30  // enums
31 public:
32  enum OpenMode
33  {
34  NotOpen = 0x0000,
35  ReadOnly = 0x0001,
36  WriteOnly = 0x0002,
37  ReadWrite = ReadOnly | WriteOnly
38  };
39 
40  // ctor & dtor
41 public:
42  virtual ~IBamIODevice() {}
43 
44  // IBamIODevice interface
45 public:
46  // TODO: add seek(pos, *from*)
47 
48  // pure virtuals
49  virtual void Close() = 0;
50  virtual bool IsRandomAccess() const = 0;
51  virtual bool Open(const OpenMode mode) = 0;
52  virtual int64_t Read(char* data, const unsigned int numBytes) = 0;
53  virtual bool Seek(const int64_t& position, const int origin = SEEK_SET) = 0;
54  virtual int64_t Tell() const = 0;
55  virtual int64_t Write(const char* data, const unsigned int numBytes) = 0;
56 
57  // default implementation provided
58  virtual std::string GetErrorString();
59  virtual bool IsOpen() const;
60  virtual OpenMode Mode() const;
61 
62  // internal methods
63 protected:
64  IBamIODevice(); // hidden ctor
65  void SetErrorString(const std::string& where, const std::string& what);
66 
67  // data members
68 protected:
70  std::string m_errorString;
71 };
72 
74  : m_mode(IBamIODevice::NotOpen)
75 {}
76 
77 inline std::string IBamIODevice::GetErrorString()
78 {
79  return m_errorString;
80 }
81 
82 inline bool IBamIODevice::IsOpen() const
83 {
84  return (m_mode != IBamIODevice::NotOpen);
85 }
86 
88 {
89  return m_mode;
90 }
91 
92 inline void IBamIODevice::SetErrorString(const std::string& where, const std::string& what)
93 {
94  static const std::string SEPARATOR = ": ";
95  m_errorString = where + SEPARATOR + what;
96 }
97 
98 } // namespace BamTools
99 
100 #endif // IBAMIODEVICE_H
Definition: IBamIODevice.h:28
virtual ~IBamIODevice()
Definition: IBamIODevice.h:42
virtual int64_t Tell() const =0
virtual int64_t Write(const char *data, const unsigned int numBytes)=0
virtual OpenMode Mode() const
Definition: IBamIODevice.h:87
std::string m_errorString
Definition: IBamIODevice.h:70
virtual int64_t Read(char *data, const unsigned int numBytes)=0
void SetErrorString(const std::string &where, const std::string &what)
Definition: IBamIODevice.h:92
OpenMode m_mode
Definition: IBamIODevice.h:69
virtual void Close()=0
virtual bool IsOpen() const
Definition: IBamIODevice.h:82
OpenMode
Definition: IBamIODevice.h:33
@ NotOpen
Definition: IBamIODevice.h:34
IBamIODevice()
Definition: IBamIODevice.h:73
virtual bool IsRandomAccess() const =0
virtual bool Seek(const int64_t &position, const int origin=SEEK_SET)=0
virtual std::string GetErrorString()
Definition: IBamIODevice.h:77
virtual bool Open(const OpenMode mode)=0
Contains all BamTools classes & methods.
Definition: Sort.h:24