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

Go to the SVN repository for this file.

1 #ifndef SRA__READER__SRA__SDK__HPP
2 #define SRA__READER__SRA__SDK__HPP
3 /* $Id: sdk.hpp 75478 2016-11-18 18:25:23Z 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 SRA SDK
32  *
33  */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <corelib/ncbiexpt.hpp>
38 
39 
42 
43 class CSeq_entry;
44 
45 /////////////////////////////////////////////////////////////////////////////
46 // CSraRcFormatter
47 /////////////////////////////////////////////////////////////////////////////
48 
50 {
51 public:
52  explicit CSraRcFormatter(rc_t rc) : m_RC(rc) {}
53 
54  rc_t GetRC(void) const
55  {
56  return m_RC;
57  }
58 private:
60 };
62 ostream& operator<<(ostream& out, const CSraRcFormatter& f);
63 
64 template<class Object>
66 {
67 };
68 
69 #define DECLARE_SRA_REF_TRAITS(T, Const) \
70  template<> \
71  struct CSraRefTraits<Const T> \
72  { \
73  static rc_t x_Release(const T* t); \
74  static rc_t x_AddRef (const T* t); \
75  }
76 #define DEFINE_SRA_REF_TRAITS(T, Const) \
77  rc_t CSraRefTraits<Const T>::x_Release(const T* t) \
78  { return T##Release(t); } \
79  rc_t CSraRefTraits<Const T>::x_AddRef (const T* t) \
80  { return T##AddRef(t); }
81 
82 
83 template<class Object>
84 class CSraRef
85 {
86 protected:
89 public:
90  typedef Object TObject;
91  typedef TObject* TPointer;
92 
93  CSraRef(void)
94  : m_Object(0)
95  {
96  }
97  CSraRef(const TSelf& ref)
98  : m_Object(s_AddRef(ref))
99  {
100  }
101  /// Copy constructor from an existing CRef object,
102  CSraRef(TSelf&& ref)
103  {
104  m_Object = ref.m_Object;
105  ref.m_Object = 0;
106  }
107 
108  TSelf& operator=(const TSelf& ref)
109  {
110  if ( this != &ref ) {
111  Release();
112  m_Object = s_AddRef(ref);
113  }
114  return *this;
115  }
117  {
118 #ifdef NCBI_COMPILER_MSVC
119  // extra check on MSVC that doesn't conform to C++ Standard
120  if ( this == &ref ) {
121  // no-op
122  return *this;
123  }
124 #endif
125  Release();
126  m_Object = ref.m_Object;
127  ref.m_Object = 0;
128  return *this;
129  }
130 
131  ~CSraRef(void)
132  {
133  Release();
134  }
135 
136  void Release(void)
137  {
138  if ( m_Object ) {
139  if ( rc_t rc = TTraits::x_Release(m_Object) ) {
140  CSraException::ReportError("Cannot release ref", rc);
141  }
142  m_Object = 0;
143  }
144  }
145 
146  TObject* GetPointer(void) const
147  {
148  if ( !m_Object ) {
149  NCBI_THROW(CSraException, eNullPtr,
150  "Null SRA pointer");
151  }
152  return m_Object;
153  }
154 
155  operator TObject*(void) const
156  {
157  return m_Object;
158  }
159  TObject* operator->(void) const
160  {
161  return GetPointer();
162  }
163  TObject& operator*(void) const
164  {
165  return *GetPointer();
166  }
167 
168 protected:
169  explicit
171  : m_Object(ptr)
172  {
173  }
174 
176  {
177  Release();
178  return &m_Object;
179  }
180 
181 protected:
182  static TObject* s_AddRef(const TSelf& ref)
183  {
184  TObject* obj = ref.m_Object;
185  if ( obj ) {
186  if ( rc_t rc = TTraits::x_AddRef(obj) ) {
187  NCBI_THROW2(CSraException, eAddRefFailed,
188  "Cannot add ref", rc);
189  }
190  }
191  return obj;
192  }
193 
194 private:
196 };
197 
198 
201 
202 #endif // SRA__READER__SRA__SDK__HPP
uint32_t rc_t
Definition: Seq_entry.hpp:56
static void ReportError(const char *msg, rc_t rc)
Definition: sraread.cpp:218
rc_t GetRC(void) const
Definition: sdk.hpp:54
rc_t m_RC
Definition: sdk.hpp:59
CSraRcFormatter(rc_t rc)
Definition: sdk.hpp:52
Definition: sdk.hpp:85
static TObject * s_AddRef(const TSelf &ref)
Definition: sdk.hpp:182
TSelf & operator=(TSelf &&ref)
Definition: sdk.hpp:116
~CSraRef(void)
Definition: sdk.hpp:131
CSraRef(TPointer ptr)
Definition: sdk.hpp:170
TObject & operator*(void) const
Definition: sdk.hpp:163
TObject ** x_InitPtr(void)
Definition: sdk.hpp:175
TObject * operator->(void) const
Definition: sdk.hpp:159
TSelf & operator=(const TSelf &ref)
Definition: sdk.hpp:108
TObject * m_Object
Definition: sdk.hpp:195
CSraRef(void)
Definition: sdk.hpp:93
CSraRefTraits< Object > TTraits
Definition: sdk.hpp:88
CSraRef(const TSelf &ref)
Definition: sdk.hpp:97
CSraRef< Object > TSelf
Definition: sdk.hpp:87
TObject * TPointer
Definition: sdk.hpp:91
TObject * GetPointer(void) const
Definition: sdk.hpp:146
CSraRef(TSelf &&ref)
Copy constructor from an existing CRef object,.
Definition: sdk.hpp:102
Object TObject
Definition: sdk.hpp:90
void Release(void)
Definition: sdk.hpp:136
Include a standard set of the NCBI C++ Toolkit most basic headers.
std::ofstream out("events_result.xml")
main entry point for tests
#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
#define NCBI_THROW2(exception_class, err_code, message, extra)
Throw exception with extra parameter.
Definition: ncbiexpt.hpp:1754
#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_SRAREAD_EXPORT
Definition: ncbi_export.h:1227
Defines NCBI C++ exception handling.
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
ostream & operator<<(ostream &out, const CSraRcFormatter &f)
Definition: sraread.cpp:194
Modified on Tue Feb 27 05:55:10 2024 by modify_doxy.py rev. 669887