NCBI C++ Toolkit Cross Reference

  C++/src/serial/iterator.cpp


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
/* $Id: iterator.cpp 79560 2017-09-20 17:04:38Z gouriano $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Eugene Vasilchenko * * File Description: * !!! PUT YOUR DESCRIPTION HERE !!! */ #include <ncbi_pch.hpp> #include <corelib/ncbistd.hpp> #include <corelib/ncbiutil.hpp> #include <serial/exception.hpp> #include <serial/objectiter.hpp> #include <serial/iterator.hpp> #include <serial/objistr.hpp> BEGIN_NCBI_SCOPE struct SIteratorFunctions { static bool s_ContainsType(const CConstObjectInfo& object, TTypeInfo needType); }; class CTreeLevelIteratorOne : public CTreeLevelIterator { public: CTreeLevelIteratorOne(const CObjectInfo& object) : m_Object(object), m_ItemInfo(0) { } bool Valid(void) const { return m_Object; } void Next(void) { m_Object.Reset(); } CObjectInfo Get(void) const { return m_Object; } const CItemInfo* GetItemInfo(void) const { return m_ItemInfo; } TMemberIndex GetIndex(void) const { return m_ItemInfo ? m_ItemInfo->GetIndex() : kInvalidMember; } protected: void SetItemInfo(const CItemInfo* info) { m_ItemInfo = info; } private: CObjectInfo m_Object; const CItemInfo* m_ItemInfo; }; class CConstTreeLevelIteratorOne : public CConstTreeLevelIterator { public: CConstTreeLevelIteratorOne(const CConstObjectInfo& object) : m_Object(object), m_ItemInfo(0) { } bool Valid(void) const { return m_Object; } void Next(void) { m_Object.Reset(); } CConstObjectInfo Get(void) const { return m_Object; } const CItemInfo* GetItemInfo(void) const { return m_ItemInfo; } TMemberIndex GetIndex(void) const { return m_ItemInfo ? m_ItemInfo->GetIndex() : kInvalidMember; } protected: void SetItemInfo(const CItemInfo* info) { m_ItemInfo = info; } private: CConstObjectInfo m_Object; const CItemInfo* m_ItemInfo; }; template<class ChildIterator> class CTreeLevelIteratorMany : public CTreeLevelIterator { public: CTreeLevelIteratorMany(const CObjectInfo& object) : m_Iterator(object) { } bool Valid(void) const { return m_Iterator; } void Next(void) { m_Iterator.Next(); } bool CanGet(void) const { return m_Iterator.CanGet(); } CObjectInfo Get(void) const { return *m_Iterator; } void Erase(void) { m_Iterator.Erase(); } const CItemInfo* GetItemInfo(void) const { return m_Iterator.GetItemInfo(); } TMemberIndex GetIndex(void) const { return m_Iterator.GetIndex(); } protected: void SetItemInfo(const CItemInfo* /*info*/) { } private: ChildIterator m_Iterator; }; template<class ChildIterator> class CConstTreeLevelIteratorMany : public CConstTreeLevelIterator { public: CConstTreeLevelIteratorMany(const CConstObjectInfo& object) : m_Iterator(object) { } bool Valid(void) const { return m_Iterator; } void Next(void) { m_Iterator.Next(); } bool CanGet(void) const { return m_Iterator.CanGet(); } CConstObjectInfo Get(void) const { return *m_Iterator; } const CItemInfo* GetItemInfo(void) const { return m_Iterator.GetItemInfo(); } TMemberIndex GetIndex(void) const { return m_Iterator.GetIndex(); } protected: void SetItemInfo(const CItemInfo* /*info*/) { } private: ChildIterator m_Iterator; }; CConstTreeLevelIterator::~CConstTreeLevelIterator(void) { } CConstTreeLevelIterator* CConstTreeLevelIterator::CreateOne(const CConstObjectInfo& object) { return new CConstTreeLevelIteratorOne(object); } CConstTreeLevelIterator* CConstTreeLevelIterator::Create(const CConstObjectInfo& obj) { switch ( obj.GetTypeFamily() ) { case eTypeFamilyClass: return new CConstTreeLevelIteratorMany<CConstObjectInfo::CMemberIterator>(obj); case eTypeFamilyContainer: return new CConstTreeLevelIteratorMany<CConstObjectInfo::CElementIterator>(obj); case eTypeFamilyPointer: return CreateOne(obj.GetPointedObject()); case eTypeFamilyChoice: { CConstObjectInfo::CChoiceVariant v(obj); if ( v ) { CConstTreeLevelIterator* it = CreateOne(*v); it->SetItemInfo(v.GetVariantInfo()); return it; } else return 0; } default: return 0; } } bool CConstTreeLevelIterator::HaveChildren(const CConstObjectInfo& object) { if ( !object ) return false; switch ( object.GetTypeFamily() ) { case eTypeFamilyClass: case eTypeFamilyChoice: case eTypeFamilyPointer: case eTypeFamilyContainer: return true; default: return false; } } bool SIteratorFunctions::s_ContainsType(const CConstObjectInfo& object, TTypeInfo needType) { if ( object.GetTypeInfo()->IsType(needType) ) return true; switch ( object.GetTypeFamily() ) { case eTypeFamilyClass: for ( CConstObjectInfo::CMemberIterator m(object); m; ++m ) { if ( m.GetMemberInfo()->GetTypeInfo()->MayContainType(needType) && s_ContainsType(*m, needType) ) return true; } return false; case eTypeFamilyChoice: { CConstObjectInfo::CChoiceVariant v = object.GetCurrentChoiceVariant(); return v && v.GetVariantInfo()->GetTypeInfo()->MayContainType(needType) && s_ContainsType(*v, needType); } case eTypeFamilyPointer: return s_ContainsType(object.GetPointedObject(), needType); case eTypeFamilyContainer: for ( CConstObjectInfo::CElementIterator e(object); e; ++e ) { if ( s_ContainsType(*e, needType) ) return true; } return false; default: return false; } } CTreeLevelIterator::~CTreeLevelIterator(void) { } CTreeLevelIterator* CTreeLevelIterator::CreateOne(const CObjectInfo& object) { return new CTreeLevelIteratorOne(object); } CTreeLevelIterator* CTreeLevelIterator::Create(const CObjectInfo& obj) { switch ( obj.GetTypeFamily() ) { case eTypeFamilyClass: return new CTreeLevelIteratorMany<CObjectInfo::CMemberIterator>(obj); case eTypeFamilyContainer: return new CTreeLevelIteratorMany<CObjectInfo::CElementIterator>(obj); case eTypeFamilyPointer: return CreateOne(obj.GetPointedObject()); case eTypeFamilyChoice: { CObjectInfo::CChoiceVariant v(obj); if ( v ) { CTreeLevelIterator* it = CreateOne(*v); it->SetItemInfo(v.GetVariantInfo()); return it; } else return 0; } default: return 0; } } void CTreeLevelIterator::Erase(void) { NCBI_THROW(CSerialException,eIllegalCall, "cannot erase"); } ///////////////// void CTreeIterator::Erase(void) { _ASSERT(CheckValid()); m_CurrentObject.Reset(); _ASSERT(!m_Stack.empty()); m_Stack.back()->Erase(); Walk(); } template<class Parent> bool CLeafTypeIteratorBase<Parent>::CanSelect(const CConstObjectInfo& object) { return CParent::CanSelect(object) && SIteratorFunctions::s_ContainsType(object, this->GetIteratorType()); } #if 0 // There is an (unconfirmed) opinion that putting these two functions // into header (iterator.hpp) increases the size of an executable. // Still, keeping them here is reported as bug by // Metrowerks Codewarrior 9.0 (Mac OSX) template<class Parent> bool CTypesIteratorBase<Parent>::CanSelect(const CConstObjectInfo& object) { if ( !CParent::CanSelect(object) ) return false; m_MatchType = 0; TTypeInfo type = object.GetTypeInfo(); ITERATE ( TTypeList, i, GetTypeList() ) { if ( type->IsType(*i) ) { m_MatchType = *i; return true; } } return false; } template<class Parent> bool CTypesIteratorBase<Parent>::CanEnter(const CConstObjectInfo& object) { if ( !CParent::CanEnter(object) ) return false; TTypeInfo type = object.GetTypeInfo(); ITERATE ( TTypeList, i, GetTypeList() ) { if ( type->MayContainType(*i) ) return true; } return false; } #if 0 // these are obsolete template class NCBI_XSERIAL_EXPORT CTreeIteratorTmpl<CTreeLevelIterator>; template class NCBI_XSERIAL_EXPORT CTreeIteratorTmpl<CConstTreeLevelIterator>; template class NCBI_XSERIAL_EXPORT CTypeIteratorBase<CTreeIterator>; template class NCBI_XSERIAL_EXPORT CTypeIteratorBase<CTreeConstIterator>; template class NCBI_XSERIAL_EXPORT CLeafTypeIteratorBase<CTreeIterator>; template class NCBI_XSERIAL_EXPORT CLeafTypeIteratorBase<CTreeConstIterator>; #endif template class NCBI_XSERIAL_EXPORT CTypesIteratorBase<CTreeIterator>; template class NCBI_XSERIAL_EXPORT CTypesIteratorBase<CTreeConstIterator>; #endif bool CType_Base::Match(const CTypesIterator& it, TTypeInfo typeInfo) { return it.GetMatchType() == typeInfo; } bool CType_Base::Match(const CTypesConstIterator& it, TTypeInfo typeInfo) { return it.GetMatchType() == typeInfo; } void CType_Base::AddTo(CTypesIterator& it, TTypeInfo typeInfo) { it.AddType(typeInfo); } void CType_Base::AddTo(CTypesConstIterator& it, TTypeInfo typeInfo) { it.AddType(typeInfo); } TObjectPtr CType_Base::GetObjectPtr(const CTypesIterator& it) { return it.GetFoundPtr(); } TConstObjectPtr CType_Base::GetObjectPtr(const CTypesConstIterator& it) { return it.GetFoundPtr(); } TObjectPtr CType_Base::GetParentObjectPtr(CObjectIStream& in, TTypeInfo typeInfo, size_t max_depth, size_t min_depth) { return in.GetParentObjectPtr(typeInfo, max_depth, min_depth); } class CCObjectClassInfo : public CVoidTypeInfo { typedef CTypeInfo CParent; public: virtual bool IsParentClassOf(const CClassTypeInfo* classInfo) const override; }; TTypeInfo CObjectGetTypeInfo::GetTypeInfo(void) { static TTypeInfo typeInfo = new CCObjectClassInfo; return typeInfo; } bool CCObjectClassInfo::IsParentClassOf(const CClassTypeInfo* classInfo) const { return classInfo->IsCObject(); } END_NCBI_SCOPE

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.