NCBI C++ ToolKit
handle_stack.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: handle_stack.cpp 63610 2014-07-14 19:00:33Z ucko $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Vladimir Soussov
27  *
28  * File Description: Handlers Stack
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <string.h>
34 #include <algorithm>
35 
38 
40 
41 namespace impl
42 {
43 
44 // All methods of CDBHandlerStack() are protected where necessary by mutexes
45 // in calling functions
46 
48 {
49 }
50 
52 {
53 }
54 
56 {
57  CHECK_DRIVER_ERROR(h == NULL, "An attempt to pass NULL instead of "
58  "a valid CDB_UserHandler object", 0);
59 
61  obj(new CUserHandlerWrapper(h, ownership == eNoOwnership));
62 
63  m_Stack.push_back(TContainer::value_type(obj));
64 }
65 
66 namespace
67 {
68  class CFunctor
69  {
70  public:
71  CFunctor(CDB_UserHandler* const h) :
72  m_Handler(h)
73  {
74  }
75 
76  bool operator()(const CDBHandlerStack::TContainer::value_type& hwrapper)
77  {
78  return hwrapper->GetHandler() == m_Handler;
79  }
80 
81  private:
82  CDB_UserHandler* const m_Handler;
83  };
84 }
85 
87 {
88  CHECK_DRIVER_ERROR(h == NULL, "An attempt to pass NULL instead of "
89  "a valid CDB_UserHandler object", 0);
90 
91  if ( last ) {
92  TContainer::reverse_iterator rcit;
93 
94  rcit = find_if(m_Stack.rbegin(), m_Stack.rend(), CFunctor(h));
95 
96  if ( rcit != m_Stack.rend() ) {
97  m_Stack.erase((--rcit.base()), m_Stack.end());
98  }
99  } else {
100  TContainer::iterator cit;
101 
102  cit = find_if(m_Stack.begin(), m_Stack.end(), CFunctor(h));
103 
104  if ( cit != m_Stack.end() ) {
105  m_Stack.erase(cit, m_Stack.end());
106  }
107  }
108 }
109 
110 
112 m_Stack( s.m_Stack )
113 {
114  return;
115 }
116 
117 
119 {
120  if ( this != &s ) {
121  m_Stack = s.m_Stack;
122  }
123 
124  return *this;
125 }
126 
127 
129  const CConnection* conn,
130  const CDBParams* params) const
131 {
132  if (dbg_info != NULL) {
133  ex.ApplyContext(*dbg_info);
134  }
135  if (conn != NULL) {
136  ex.SetFromConnection(*conn);
137  }
138  if (params != NULL && ex.GetParams().Empty()) {
139  ex.SetParams(params);
140  }
141 }
142 
144  const CConnection* connection,
145  const CDBParams* params) const
146 {
147  x_AddDetails(*ex, dbg_info, connection, params);
149  if ( cit->NotNull() && cit->GetNCObject().GetHandler()->HandleIt(ex) )
150  {
151  break;
152  }
153  }
154 }
155 
156 
158  const TDbgInfo* dbg_info,
159  const CConnection* connection,
160  const CDBParams* params) const
161 {
162  typedef CDB_UserHandler::TExceptions TExceptions;
163  TExceptions remaining, still_remaining;
164 
165  ITERATE (TExceptions, it, exceptions) {
166  x_AddDetails(**it, dbg_info, connection, params);
167  remaining.push_back(*it);
168  }
169 
171  if (cit->Empty()) {
172  continue;
173  }
174  CDB_UserHandler& handler = *cit->GetNCObject().GetHandler();
175  if (handler.HandleAll(remaining)) {
176  return true;
177  } else {
178  ITERATE (TExceptions, it, remaining) {
179  if ( !handler.HandleIt(*it) ) {
180  still_remaining.push_back(*it);
181  }
182  }
183  if (still_remaining.empty()) {
184  return true;
185  } else {
186  remaining = still_remaining;
187  still_remaining.clear();
188  }
189  }
190  }
191 
192  return false;
193 }
194 
195 bool CDBHandlerStack::HandleMessage(int severity, int msgnum, const string& message) const
196 {
198  if (cit->NotNull()
199  && cit->GetNCObject().GetHandler()->HandleMessage(severity, msgnum, message))
200  {
201  return true;
202  }
203  }
204 
205  return false;
206 }
207 
208 ////////////////////////////////////////////////////////////////////////////////
211  bool guard
212  ) :
213  m_ObjGuard(guard ? handler : NULL),
214  m_UserHandler(handler)
215 {
216 }
217 
219 {
220 }
221 
223  m_Obj(obj)
224 {
225  if (m_Obj) {
226  m_Obj->AddReference();
227  }
228 }
229 
231  m_Obj(other.m_Obj)
232 {
233  if (m_Obj) {
234  m_Obj->AddReference();
235  }
236 }
237 
239 {
240  if (m_Obj) {
241  // This call doesn't delete m_Obj even if reference
242  // counter is equal to 0. And with this feature CObjGuard
243  // differs from CRef.
244  m_Obj->ReleaseReference();
245  }
246 }
247 
248 
249 }
250 
251 
253 
254 
static void handler(int)
double value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:228
No ownership is assumed.
Definition: ncbi_types.h:135
void SetFromConnection(const impl::CConnection &connection)
Definition: exception.cpp:177
void PostMsg(CDB_Exception *ex, const TDbgInfo *dbg_info=NULL, const CConnection *connection=NULL, const CDBParams *params=NULL) const
void Push(CDB_UserHandler *h, EOwnership ownership=eNoOwnership)
deque< CRef< CUserHandlerWrapper > > TContainer
#define NULL
Definition: ncbistd.hpp:225
void ApplyContext(const SContext &ctx)
Definition: exception.hpp:213
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:795
virtual bool HandleIt(CDB_Exception *ex)=0
Handle the exceptions resulting from a native API call, one-by-one.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
void x_AddDetails(CDB_Exception &ex, const TDbgInfo *dbg_info, const CConnection *conn, const CDBParams *params) const
virtual bool HandleAll(const TExceptions &exceptions)
Handle all of the exceptions resulting from a native API call.
Definition: exception.cpp:631
deque< CDB_Exception * > TExceptions
Exception container type.
Definition: exception.hpp:554
#define CHECK_DRIVER_ERROR(failed, message, err_code)
Definition: exception.hpp:751
bool HandleExceptions(const CDB_UserHandler::TExceptions &exeptions, const TDbgInfo *dbg_info=NULL, const CConnection *connection=NULL, const CDBParams *params=NULL) const
#define REVERSE_ITERATE(Type, Var, Cont)
ITERATE macro to reverse sequence through container elements.
Definition: ncbimisc.hpp:807
void SetParams(const CDBParams *params)
Definition: exception.cpp:195
CDB_Exception –.
Definition: exception.hpp:116
CConstRef< SParams > GetParams(void) const
Definition: exception.hpp:222
CRef –.
Definition: ncbiobj.hpp:616
CObject –.
Definition: ncbiobj.hpp:180
CDBParams.
Definition: interfaces.hpp:153
static DLIST_TYPE *DLIST_NAME() last(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:51
enum ENcbiOwnership EOwnership
Ownership relations between objects.
CDBHandlerStack & operator=(const CDBHandlerStack &s)
void Pop(CDB_UserHandler *h, bool last=true)
static CS_CONNECTION * conn
Definition: ct_dynamic.c:25
void AddReference(void) const
Add reference to object.
Definition: ncbiobj.hpp:490
static std::unique_ptr< CParams > params
Definition: wgs_params.cpp:491
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
bool HandleMessage(int severity, int msgnum, const string &message) const
CUserHandlerWrapper(CDB_UserHandler *handler, bool guard=false)
Modified on Sat Feb 24 14:02:16 2018 by modify_doxy.py rev. 546573