]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/api/IBamIODevice.h
Cleaned up intra-API includes & moved version numbers to 2.0.0
[bamtools.git] / src / api / IBamIODevice.h
index c59bb4a9a88bb0060c25b25b1cc0ea7c8ceb1d5f..b34e449a04ed966756c8c1bc3cb71a60f7bd970f 100644 (file)
@@ -1,7 +1,24 @@
+// ***************************************************************************
+// IBamIODevice.h (c) 2011 Derek Barnett
+// Marth Lab, Department of Biology, Boston College
+// ---------------------------------------------------------------------------
+// Last modified: 10 October 2011 (DB)
+// ---------------------------------------------------------------------------
+// Base class for all BAM I/O devices (e.g. local file, pipe, HTTP, FTP, etc.)
+//
+// Derived classes should provide protocol-specific implementations for
+// reading/writing plain bytes, as well as other I/O-related behaviors.
+//
+// Since IBamIODevices may be defined in client code, the internal
+// BamExceptions are NOT allowed to be thrown from devices, including the
+// built-in ones. This keeps a consistent interface at the BgzfStream for
+// handling any device type. Use the error string for relaying error messages.
+// ***************************************************************************
+
 #ifndef IBAMIODEVICE_H
 #define IBAMIODEVICE_H
 
-#include <api/api_global.h>
+#include "api/api_global.h"
 #include <string>
 
 namespace BamTools {
@@ -16,11 +33,12 @@ class API_EXPORT IBamIODevice {
 
     // ctor & dtor
     public:
-        IBamIODevice(void);
-        virtual ~IBamIODevice(void);
+        virtual ~IBamIODevice(void) { }
 
     // IBamIODevice interface
     public:
+
+        // pure virtuals
         virtual void Close(void) =0;
         virtual bool IsRandomAccess(void) const =0;
         virtual bool Open(const OpenMode mode) =0;
@@ -28,14 +46,16 @@ class API_EXPORT IBamIODevice {
         virtual bool Seek(const int64_t& position) =0;
         virtual int64_t Tell(void) const =0;
         virtual size_t Write(const char* data, const unsigned int numBytes) =0;
-    public:
-        virtual std::string ErrorString(void);
+
+        // default implementation provided
+        virtual std::string GetErrorString(void);
         virtual bool IsOpen(void) const;
         virtual OpenMode Mode(void) const;
 
     // internal methods
     protected:
-        void SetErrorString(const std::string& errorString);
+        IBamIODevice(void); // hidden ctor
+        void SetErrorString(const std::string& where, const std::string& what);
 
     // data members
     protected:
@@ -49,13 +69,8 @@ IBamIODevice::IBamIODevice(void)
 { }
 
 inline
-IBamIODevice::~IBamIODevice(void) { }
-
-inline
-std::string IBamIODevice::ErrorString(void) {
-    std::string e = m_errorString;
-    m_errorString.clear();
-    return e;
+std::string IBamIODevice::GetErrorString(void) {
+    return m_errorString;
 }
 
 inline
@@ -69,8 +84,9 @@ IBamIODevice::OpenMode IBamIODevice::Mode(void) const {
 }
 
 inline
-void IBamIODevice::SetErrorString(const std::string& errorString) {
-    m_errorString = errorString;
+void IBamIODevice::SetErrorString(const std::string& where, const std::string& what) {
+    static const std::string SEPARATOR = ": ";
+    m_errorString = where + SEPARATOR + what;
 }
 
 } // namespace BamTools