NCBI C++ ToolKit
bamread_base.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef SRA__READER__BAM__BAMREAD_BASE__HPP
2 #define SRA__READER__BAM__BAMREAD_BASE__HPP
3 /* $Id: bamread_base.hpp 96596 2022-04-18 21:11:33Z vasilche $
4  * ===========================================================================
5  *
6  * PUBLIC DOMAIN NOTICE
7  * National Center for Biotechnology Information
8  *
9  * This software/database is a "United States Government Work" under the
10  * terms of the United States Copyright Act. It was written as part of
11  * the author's official duties as a United States Government employee and
12  * thus cannot be copyrighted. This software/database is freely available
13  * to the public for use. The National Library of Medicine and the U.S.
14  * Government have not placed any restriction on its use or reproduction.
15  *
16  * Although all reasonable efforts have been taken to ensure the accuracy
17  * and reliability of the software and data, the NLM and the U.S.
18  * Government do not and cannot warrant the performance or results that
19  * may be obtained by using this software or data. The NLM and the U.S.
20  * Government disclaim all warranties, express or implied, including
21  * warranties of performance, merchantability or fitness for any particular
22  * purpose.
23  *
24  * Please cite the author in any work or product based on this material.
25  *
26  * ===========================================================================
27  *
28  * Authors: Eugene Vasilchenko
29  *
30  * File Description:
31  * Access to BAM files
32  *
33  */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <corelib/ncbiexpt.hpp>
37 #include <corelib/ncbiobj.hpp>
38 
39 typedef uint32_t rc_t; // from <klib/rc.h>
40 
43 
44 /////////////////////////////////////////////////////////////////////////////
45 // CBamException
46 /////////////////////////////////////////////////////////////////////////////
47 
48 #ifndef NCBI_EXCEPTION3_VAR
49 /// Create an instance of the exception with one additional parameter.
50 # define NCBI_EXCEPTION3_VAR(name, exc_cls, err_code, msg, extra1, extra2) \
51  exc_cls name(DIAG_COMPILE_INFO, 0, exc_cls::err_code, msg, \
52  extra1, extra2)
53 #endif
54 
55 #ifndef NCBI_EXCEPTION3
56 # define NCBI_EXCEPTION3(exc_cls, err_code, msg, extra1, extra2) \
57  NCBI_EXCEPTION3_VAR(NCBI_EXCEPTION_EMPTY_NAME, \
58  exc_cls, err_code, msg, extra1, extra2)
59 #endif
60 
61 #ifndef NCBI_THROW3
62 # define NCBI_THROW3(exc_cls, err_code, msg, extra1, extra2) \
63  throw NCBI_EXCEPTION3(exc_cls, err_code, msg, extra1, extra2)
64 #endif
65 
66 #ifndef NCBI_RETHROW3
67 # define NCBI_RETHROW3(prev_exc, exc_cls, err_code, msg, extra1, extra2) \
68  throw exc_cls(DIAG_COMPILE_INFO, &(prev_exc), exc_cls::err_code, msg, \
69  extra1, extra2)
70 #endif
71 
72 
74 {
75 public:
76  explicit CBamRcFormatter(rc_t rc)
77  : m_RC(rc)
78  {
79  }
80 
81  rc_t GetRC(void) const
82  {
83  return m_RC;
84  }
85 private:
87 };
89 ostream& operator<<(ostream& out, const CBamRcFormatter& f);
90 
93 {
94 public:
95  /// Error types that CBamXxx classes can generate.
96  enum EErrCode {
98  eNullPtr, ///< Null pointer error
99  eAddRefFailed, ///< AddRef failed
100  eInvalidArg, ///< Invalid argument error
101  eInitFailed, ///< Initialization failed
102  eNoData, ///< Data not found
103  eBadCIGAR, ///< Bad CIGAR string
104  eInvalidBAMFormat, ///< Invalid data in BAM file
105  eInvalidBAIFormat, ///< Invalid data in BAM index file
106  eFileNotFound ///< File not found
107  };
108  /// Constructors.
110  const CException* prev_exception,
111  EErrCode err_code,
112  const string& message,
113  EDiagSev severity = eDiag_Error);
115  const CException* prev_exception,
116  EErrCode err_code,
117  const string& message,
118  rc_t rc,
119  EDiagSev severity = eDiag_Error);
121  const CException* prev_exception,
122  EErrCode err_code,
123  const string& message,
124  rc_t rc,
125  const string& param,
126  EDiagSev severity = eDiag_Error);
127  CBamException(const CBamException& other);
128 
129  ~CBamException(void) noexcept;
130 
131  /// Report "non-standard" attributes.
132  virtual void ReportExtra(ostream& out) const;
133 
134  virtual const char* GetType(void) const;
135 
136  /// Translate from the error code value to its string representation.
137  typedef int TErrCode;
138  virtual TErrCode GetErrCode(void) const;
139 
140  /// Translate from the error code value to its string representation.
141  virtual const char* GetErrCodeString(void) const;
142 
143  rc_t GetRC(void) const
144  {
145  return m_RC;
146  }
147 
148  const string& GetParam(void) const
149  {
150  return m_Param;
151  }
152 
153  static void ReportError(const char* msg, rc_t rc);
154 
155 protected:
156  /// Constructor.
157  CBamException(void);
158 
159  /// Helper clone method.
160  virtual const CException* x_Clone(void) const;
161 
162 private:
164  string m_Param;
165 };
166 
167 
168 template<class Object>
170 {
171 };
172 
173 #define SPECIALIZE_BAM_REF_TRAITS(T, Const) \
174  template<> \
175  struct CBamRefTraits<Const T> \
176  { \
177  static rc_t x_Release(const T* t); \
178  static rc_t x_AddRef (const T* t); \
179  }
180 #define DEFINE_BAM_REF_TRAITS(T, Const) \
181  rc_t CBamRefTraits<Const T>::x_Release(const T* t) \
182  { return T##Release(t); } \
183  rc_t CBamRefTraits<Const T>::x_AddRef (const T* t) \
184  { return T##AddRef(t); }
185 
186 template<class Object>
187 class CBamRef
188 {
189 protected:
192 public:
193  typedef Object TObject;
194 
195  CBamRef(void)
196  : m_Object(0)
197  {
198  }
199  CBamRef(const TSelf& ref)
200  : m_Object(s_AddRef(ref))
201  {
202  }
203  TSelf& operator=(const TSelf& ref)
204  {
205  if ( this != &ref ) {
206  Release();
207  m_Object = s_AddRef(ref);
208  }
209  return *this;
210  }
211  ~CBamRef(void)
212  {
213  Release();
214  }
215 
216  void Release(void)
217  {
218  if ( m_Object ) {
219  if ( rc_t rc = TTraits::x_Release(m_Object) ) {
220  CBamException::ReportError("Cannot release ref", rc);
221  }
222  m_Object = 0;
223  }
224  }
225 
226  TObject* GetPointer(void) const
227  {
228  if ( !m_Object ) {
229  NCBI_THROW(CBamException, eNullPtr,
230  "Null BAM pointer");
231  }
232  return m_Object;
233  }
234 
235  operator TObject*(void) const
236  {
237  return m_Object;
238  }
239  TObject* operator->(void) const
240  {
241  return GetPointer();
242  }
243  TObject& operator*(void) const
244  {
245  return *GetPointer();
246  }
247 
248 public:
250  {
251  Release();
252  m_Object = ptr;
253  }
254 
256  {
257  Release();
258  return &m_Object;
259  }
260 
261 protected:
262  static TObject* s_AddRef(const TSelf& ref)
263  {
264  TObject* obj = ref.m_Object;
265  if ( obj ) {
266  if ( rc_t rc = TTraits::x_AddRef(obj) ) {
267  NCBI_THROW2(CBamException, eAddRefFailed,
268  "Cannot add ref", rc);
269  }
270  }
271  return obj;
272  }
273 
274 private:
276 };
277 
278 
281 
282 #endif // SRA__READER__BAM__BAMREAD_BASE__HPP
ostream & operator<<(ostream &out, const CBamRcFormatter &f)
Definition: bamread.cpp:193
uint32_t rc_t
EErrCode
Error types that CBamXxx classes can generate.
@ eAddRefFailed
AddRef failed.
@ eInvalidArg
Invalid argument error.
@ eInitFailed
Initialization failed.
@ eInvalidBAIFormat
Invalid data in BAM index file.
@ eInvalidBAMFormat
Invalid data in BAM file.
@ eBadCIGAR
Bad CIGAR string.
@ eNoData
Data not found.
@ eNullPtr
Null pointer error.
int TErrCode
Translate from the error code value to its string representation.
const string & GetParam(void) const
rc_t GetRC(void) const
static void ReportError(const char *msg, rc_t rc)
Definition: bamread.cpp:217
CBamRcFormatter(rc_t rc)
rc_t GetRC(void) const
CBamRef(const TSelf &ref)
static TObject * s_AddRef(const TSelf &ref)
~CBamRef(void)
CBamRef< Object > TSelf
void SetReferencedPointer(TObject *ptr)
void Release(void)
TSelf & operator=(const TSelf &ref)
TObject ** x_InitPtr(void)
TObject * m_Object
CBamRefTraits< Object > TTraits
TObject * operator->(void) const
TObject & operator*(void) const
TObject * GetPointer(void) const
Object TObject
CBamRef(void)
Incapsulate compile time information such as __FILE__, __LINE__, NCBI_MODULE, current function.
Definition: ncbidiag.hpp:65
Include a standard set of the NCBI C++ Toolkit most basic headers.
std::ofstream out("events_result.xml")
main entry point for tests
EDiagSev
Severity level for the posted diagnostics.
Definition: ncbidiag.hpp:650
@ eDiag_Error
Error message.
Definition: ncbidiag.hpp:653
virtual void ReportExtra(ostream &out) const
Report "non-standard" attributes.
Definition: ncbiexpt.cpp:428
virtual const char * GetType(void) const
Get class name as a string.
Definition: ncbiexpt.cpp:268
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbiexpt.cpp:453
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
virtual const CException * x_Clone(void) const
Helper method for cloning the exception.
Definition: ncbiexpt.cpp:503
#define NCBI_THROW2(exception_class, err_code, message, extra)
Throw exception with extra parameter.
Definition: ncbiexpt.hpp:1754
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
#define EXCEPTION_VIRTUAL_BASE
Do not use virtual base classes in exception declaration at all, because in this case derived class s...
Definition: ncbiexpt.hpp:1388
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
#define NCBI_BAMREAD_EXPORT
Definition: ncbi_export.h:1235
static MDB_envinfo info
Definition: mdb_load.c:37
Defines NCBI C++ exception handling.
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
unsigned int uint32_t
Definition: stdint.h:126
Modified on Sun Feb 25 03:07:00 2024 by modify_doxy.py rev. 669887