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

Go to the SVN repository for this file.

1 /* $Id: iterator.cpp 80729 2018-01-02 16:45:55Z gouriano $
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: Eugene Vasilchenko
27 *
28 * File Description:
29 * !!! PUT YOUR DESCRIPTION HERE !!!
30 */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbistd.hpp>
34 #include <corelib/ncbiutil.hpp>
35 #include <serial/exception.hpp>
36 #include <serial/objectiter.hpp>
37 #include <serial/iterator.hpp>
38 #include <serial/objistr.hpp>
39 
41 
43  static bool s_ContainsType(const CConstObjectInfo& object,
44  TTypeInfo needType);
45 };
46 
48 {
49 public:
51  : m_Object(object), m_ItemInfo(0)
52  {
53  }
54 
55  bool Valid(void) const
56  {
57  return m_Object;
58  }
59  void Next(void)
60  {
61  m_Object.Reset();
62  }
63  CObjectInfo Get(void) const
64  {
65  return m_Object;
66  }
67  const CItemInfo* GetItemInfo(void) const
68  {
69  return m_ItemInfo;
70  }
71  TMemberIndex GetIndex(void) const
72  {
74  }
76  {
77  return new CTreeLevelIteratorOne(*this);
78  }
79 protected:
80  void SetItemInfo(const CItemInfo* info)
81  {
82  m_ItemInfo = info;
83  }
84 private:
87 };
88 
90 {
91 public:
93  : m_Object(object), m_ItemInfo(0)
94  {
95  }
96 
97  bool Valid(void) const
98  {
99  return m_Object;
100  }
101  void Next(void)
102  {
103  m_Object.Reset();
104  }
105  CConstObjectInfo Get(void) const
106  {
107  return m_Object;
108  }
109  const CItemInfo* GetItemInfo(void) const
110  {
111  return m_ItemInfo;
112  }
113  TMemberIndex GetIndex(void) const
114  {
116  }
118  {
119  return new CConstTreeLevelIteratorOne(*this);
120  }
121 protected:
123  {
124  m_ItemInfo = info;
125  }
126 private:
129 };
130 
131 template<class ChildIterator>
133 {
134 public:
136  : m_Iterator(object)
137  {
138  }
139 
140  bool Valid(void) const
141  {
142  return m_Iterator;
143  }
144  void Next(void)
145  {
146  m_Iterator.Next();
147  }
148  bool CanGet(void) const
149  {
150  return m_Iterator.CanGet();
151  }
152  CObjectInfo Get(void) const
153  {
154  return *m_Iterator;
155  }
156  void Erase(void)
157  {
158  m_Iterator.Erase();
159  }
160  const CItemInfo* GetItemInfo(void) const
161  {
162  return m_Iterator.GetItemInfo();
163  }
164  TMemberIndex GetIndex(void) const
165  {
166  return m_Iterator.GetIndex();
167  }
169  {
170  return new CTreeLevelIteratorMany(*this);
171  }
172 protected:
173  void SetItemInfo(const CItemInfo* /*info*/)
174  {
175  }
176 private:
177  ChildIterator m_Iterator;
178 };
179 
180 template<class ChildIterator>
182 {
183 public:
185  : m_Iterator(object)
186  {
187  }
188 
189  bool Valid(void) const
190  {
191  return m_Iterator;
192  }
193  void Next(void)
194  {
195  m_Iterator.Next();
196  }
197  bool CanGet(void) const
198  {
199  return m_Iterator.CanGet();
200  }
201  CConstObjectInfo Get(void) const
202  {
203  return *m_Iterator;
204  }
205  const CItemInfo* GetItemInfo(void) const
206  {
207  return m_Iterator.GetItemInfo();
208  }
209  TMemberIndex GetIndex(void) const
210  {
211  return m_Iterator.GetIndex();
212  }
214  {
215  return new CConstTreeLevelIteratorMany(*this);
216  }
217 protected:
218  void SetItemInfo(const CItemInfo* /*info*/)
219  {
220  }
221 private:
222  ChildIterator m_Iterator;
223 };
224 
225 
227 {
228 }
229 
232 {
233  return new CConstTreeLevelIteratorOne(object);
234 }
235 
238 {
239  switch ( obj.GetTypeFamily() ) {
240  case eTypeFamilyClass:
244  case eTypeFamilyPointer:
245  return CreateOne(obj.GetPointedObject());
246  case eTypeFamilyChoice:
247  {
249  if ( v ) {
251  it->SetItemInfo(v.GetVariantInfo());
252  return it;
253  }
254  else
255  return 0;
256  }
257  default:
258  return 0;
259  }
260 }
261 
263 {
264  if ( !object )
265  return false;
266  switch ( object.GetTypeFamily() ) {
267  case eTypeFamilyClass:
268  case eTypeFamilyChoice:
269  case eTypeFamilyPointer:
271  return true;
272  default:
273  return false;
274  }
275 }
276 
278  TTypeInfo needType)
279 {
280  if ( object.GetTypeInfo()->IsType(needType) )
281  return true;
282  switch ( object.GetTypeFamily() ) {
283  case eTypeFamilyClass:
284  for ( CConstObjectInfo::CMemberIterator m(object); m; ++m ) {
285  if ( m.GetMemberInfo()->GetTypeInfo()->MayContainType(needType) &&
286  s_ContainsType(*m, needType) )
287  return true;
288  }
289  return false;
290  case eTypeFamilyChoice:
291  {
293  object.GetCurrentChoiceVariant();
294  return v &&
295  v.GetVariantInfo()->GetTypeInfo()->MayContainType(needType) &&
296  s_ContainsType(*v, needType);
297  }
298  case eTypeFamilyPointer:
299  return s_ContainsType(object.GetPointedObject(), needType);
301  for ( CConstObjectInfo::CElementIterator e(object); e; ++e ) {
302  if ( s_ContainsType(*e, needType) )
303  return true;
304  }
305  return false;
306  default:
307  return false;
308  }
309 }
310 
312 {
313 }
314 
317 {
318  return new CTreeLevelIteratorOne(object);
319 }
320 
323 {
324  switch ( obj.GetTypeFamily() ) {
325  case eTypeFamilyClass:
329  case eTypeFamilyPointer:
330  return CreateOne(obj.GetPointedObject());
331  case eTypeFamilyChoice:
332  {
334  if ( v ) {
335  CTreeLevelIterator* it = CreateOne(*v);
336  it->SetItemInfo(v.GetVariantInfo());
337  return it;
338  }
339  else
340  return 0;
341  }
342  default:
343  return 0;
344  }
345 }
346 
348 {
349  NCBI_THROW(CSerialException,eIllegalCall, "cannot erase");
350 }
351 
352 /////////////////
353 
355 {
356  _ASSERT(CheckValid());
357  m_CurrentObject.Reset();
358 
359  _ASSERT(!m_Stack.empty());
360  m_Stack.back()->Erase();
361  Walk();
362 }
363 
364 
365 #if 0
366 // these are obsolete
367 template<class Parent>
368 bool CLeafTypeIteratorBase<Parent>::CanSelect(const CConstObjectInfo& object)
369 {
370  return CParent::CanSelect(object) &&
371  SIteratorFunctions::s_ContainsType(object, this->GetIteratorType());
372 }
373 #endif
374 
375 #if 0
376 // There is an (unconfirmed) opinion that putting these two functions
377 // into header (iterator.hpp) increases the size of an executable.
378 // Still, keeping them here is reported as bug by
379 // Metrowerks Codewarrior 9.0 (Mac OSX)
380 template<class Parent>
382 {
383  if ( !CParent::CanSelect(object) )
384  return false;
385  m_MatchType = 0;
386  TTypeInfo type = object.GetTypeInfo();
387  ITERATE ( TTypeList, i, GetTypeList() ) {
388  if ( type->IsType(*i) ) {
389  m_MatchType = *i;
390  return true;
391  }
392  }
393  return false;
394 }
395 
396 template<class Parent>
398 {
399  if ( !CParent::CanEnter(object) )
400  return false;
401  TTypeInfo type = object.GetTypeInfo();
402  ITERATE ( TTypeList, i, GetTypeList() ) {
403  if ( type->MayContainType(*i) )
404  return true;
405  }
406  return false;
407 }
408 
409 #if 0 // these are obsolete
414 template class NCBI_XSERIAL_EXPORT CLeafTypeIteratorBase<CTreeIterator>;
415 template class NCBI_XSERIAL_EXPORT CLeafTypeIteratorBase<CTreeConstIterator>;
416 #endif
419 #endif
420 
421 bool CType_Base::Match(const CTypesIterator& it, TTypeInfo typeInfo)
422 {
423  return it.GetMatchType() == typeInfo;
424 }
425 
427 {
428  return it.GetMatchType() == typeInfo;
429 }
430 
432 {
433  it.AddType(typeInfo);
434 }
435 
437 {
438  it.AddType(typeInfo);
439 }
440 
442 {
443  return it.GetFoundPtr();
444 }
445 
447 {
448  return it.GetFoundPtr();
449 }
450 
452  TTypeInfo typeInfo,
453  size_t max_depth,
454  size_t min_depth)
455 {
456  return in.GetParentObjectPtr(typeInfo, max_depth, min_depth);
457 }
458 
460 {
462 public:
463  virtual bool IsParentClassOf(const CClassTypeInfo* classInfo) const override;
464 };
465 
467 {
468  static TTypeInfo typeInfo = new CCObjectClassInfo;
469  return typeInfo;
470 }
471 
473 {
474  return classInfo->IsCObject();
475 }
476 
virtual bool IsParentClassOf(const CClassTypeInfo *classInfo) const override
Definition: iterator.cpp:472
CTypeInfo CParent
Definition: iterator.cpp:461
CConstObjectInfoCV –.
Definition: objectiter.hpp:557
CConstObjectInfoEI –.
Definition: objectiter.hpp:56
CConstObjectInfoMI –.
Definition: objectiter.hpp:397
CConstObjectInfo –.
Definition: objectinfo.hpp:421
TMemberIndex GetIndex(void) const
Definition: iterator.cpp:209
CConstObjectInfo Get(void) const
Definition: iterator.cpp:201
bool Valid(void) const
Definition: iterator.cpp:189
CConstTreeLevelIteratorMany * Clone(void)
Definition: iterator.cpp:213
bool CanGet(void) const
Definition: iterator.cpp:197
const CItemInfo * GetItemInfo(void) const
Definition: iterator.cpp:205
void SetItemInfo(const CItemInfo *)
Definition: iterator.cpp:218
CConstTreeLevelIteratorMany(const CConstObjectInfo &object)
Definition: iterator.cpp:184
CConstTreeLevelIteratorOne * Clone(void)
Definition: iterator.cpp:117
void SetItemInfo(const CItemInfo *info)
Definition: iterator.cpp:122
TMemberIndex GetIndex(void) const
Definition: iterator.cpp:113
const CItemInfo * GetItemInfo(void) const
Definition: iterator.cpp:109
CConstTreeLevelIteratorOne(const CConstObjectInfo &object)
Definition: iterator.cpp:92
CConstObjectInfo m_Object
Definition: iterator.cpp:127
CConstObjectInfo Get(void) const
Definition: iterator.cpp:105
const CItemInfo * m_ItemInfo
Definition: iterator.cpp:128
bool Valid(void) const
Definition: iterator.cpp:97
Class describing stack level of traversal.
Definition: iterator.hpp:118
CObjectIStream –.
Definition: objistr.hpp:93
CObjectInfoCV –.
Definition: objectiter.hpp:588
CObjectInfo –.
Definition: objectinfo.hpp:597
Root class for all serialization exceptions.
Definition: exception.hpp:50
bool CanGet(void) const
Definition: iterator.cpp:148
const CItemInfo * GetItemInfo(void) const
Definition: iterator.cpp:160
TMemberIndex GetIndex(void) const
Definition: iterator.cpp:164
ChildIterator m_Iterator
Definition: iterator.cpp:177
CTreeLevelIteratorMany * Clone(void)
Definition: iterator.cpp:168
void SetItemInfo(const CItemInfo *)
Definition: iterator.cpp:173
bool Valid(void) const
Definition: iterator.cpp:140
CTreeLevelIteratorMany(const CObjectInfo &object)
Definition: iterator.cpp:135
CObjectInfo Get(void) const
Definition: iterator.cpp:152
const CItemInfo * m_ItemInfo
Definition: iterator.cpp:86
TMemberIndex GetIndex(void) const
Definition: iterator.cpp:71
bool Valid(void) const
Definition: iterator.cpp:55
CTreeLevelIteratorOne * Clone(void)
Definition: iterator.cpp:75
CTreeLevelIteratorOne(const CObjectInfo &object)
Definition: iterator.cpp:50
CObjectInfo Get(void) const
Definition: iterator.cpp:63
const CItemInfo * GetItemInfo(void) const
Definition: iterator.cpp:67
CObjectInfo m_Object
Definition: iterator.cpp:85
void SetItemInfo(const CItemInfo *info)
Definition: iterator.cpp:80
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
Template base class for CTypesIterator and CTypesConstIterator Do not use it directly.
Definition: iterator.hpp:565
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#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
TMemberIndex GetIndex(void) const
TTypeInfo GetTypeInfo(void) const
void * TObjectPtr
Definition: serialdef.hpp:55
size_t TMemberIndex
Type used for indexing class members and choice variants.
Definition: serialdef.hpp:230
const TMemberIndex kInvalidMember
Special value returned from FindMember.
Definition: serialdef.hpp:237
const void * TConstObjectPtr
Definition: serialdef.hpp:59
@ eTypeFamilyClass
Definition: serialdef.hpp:140
@ eTypeFamilyContainer
Definition: serialdef.hpp:142
@ eTypeFamilyChoice
Definition: serialdef.hpp:141
@ eTypeFamilyPointer
Definition: serialdef.hpp:143
CParent::TObjectInfo::TObjectPtrType GetFoundPtr(void) const
Definition: iterator.hpp:616
static CTreeLevelIterator * CreateOne(const TObjectInfo &object)
Definition: iterator.cpp:316
virtual void SetItemInfo(const CItemInfo *info)=0
virtual void Erase(void)
Definition: iterator.cpp:347
static void AddTo(CTypesIterator &it, TTypeInfo typeInfo)
Definition: iterator.cpp:431
TTypeInfo GetMatchType(void) const
Definition: iterator.hpp:624
static TObjectPtr GetParentObjectPtr(CObjectIStream &in, TTypeInfo typeInfo, size_t max_depth, size_t min_depth)
Definition: iterator.cpp:451
static TTypeInfo GetTypeInfo(void)
Definition: iterator.cpp:466
static CConstTreeLevelIterator * Create(const TObjectInfo &object)
Definition: iterator.cpp:237
virtual void SetItemInfo(const CItemInfo *info)=0
static CConstTreeLevelIterator * CreateOne(const TObjectInfo &object)
Definition: iterator.cpp:231
virtual ~CTreeLevelIterator(void)
Definition: iterator.cpp:311
static bool Match(const CObjectTypeInfo &type, TTypeInfo typeInfo)
Definition: objecttype.hpp:61
static TObjectPtr GetObjectPtr(const CTypesIterator &it)
Definition: iterator.cpp:441
virtual bool CanEnter(const CConstObjectInfo &object)
Definition: iterator.hpp:652
virtual bool CanSelect(const CConstObjectInfo &object)
Definition: iterator.hpp:638
static bool HaveChildren(const CConstObjectInfo &object)
Definition: iterator.cpp:262
virtual ~CConstTreeLevelIterator(void)
Definition: iterator.cpp:226
void Erase(void)
Delete currently pointed object (throws exception if failed)
Definition: iterator.cpp:354
CTypesIteratorBase< Parent > & AddType(TTypeInfo type)
Definition: iterator.hpp:604
static CTreeLevelIterator * Create(const TObjectInfo &object)
Definition: iterator.cpp:322
CConstObjectInfo GetPointedObject(void) const
Get data and type information of object to which this type refers.
Definition: objectinfo.cpp:96
CObjectInfo GetPointedObject(void) const
Get data and type information of object to which this type refers.
Definition: objectinfo.cpp:102
void Reset(void)
Reset CObjectInfo to empty state.
ETypeFamily GetTypeFamily(void) const
Get data type family.
const CVariantInfo * GetVariantInfo(void) const
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
bool IsCObject(void) const
Check is this TypeInfo object is kind of CClassTypeInfoBase.
bool MayContainType(TTypeInfo type) const
#define NCBI_XSERIAL_EXPORT
Definition: ncbi_export.h:1435
int i
static MDB_envinfo info
Definition: mdb_load.c:37
Useful/utility classes and methods.
std::istream & in(std::istream &in_, double &x_)
static bool s_ContainsType(const CConstObjectInfo &object, TTypeInfo needType)
Definition: iterator.cpp:277
Definition: type.c:6
#define _ASSERT
Modified on Wed Jul 17 13:19:22 2024 by modify_doxy.py rev. 669887