NCBI C++ Toolkit Cross Reference

  C++/include/serial/iterator.hpp


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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
#ifndef ITERATOR__HPP #define ITERATOR__HPP /* $Id: iterator.hpp 80729 2018-01-02 16:45:55Z 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: * Iterators through object hierarchy */ #include <corelib/ncbistd.hpp> #include <corelib/ncbiutil.hpp> #include <serial/impl/objecttype.hpp> #include <serial/serialutil.hpp> #include <serial/serialbase.hpp> #include <set> #include <serial/impl/pathhook.hpp> /** @addtogroup ObjHierarchy * * @{ */ BEGIN_NCBI_SCOPE class CTreeIterator; /// Class holding information about root of non-modifiable object hierarchy /// Do not use it directly class CBeginInfo : public pair<TObjectPtr, TTypeInfo> { typedef pair<TObjectPtr, TTypeInfo> CParent; public: typedef CObjectInfo TObjectInfo; CBeginInfo(TObjectPtr objectPtr, TTypeInfo typeInfo, bool detectLoops = false) : CParent(objectPtr, typeInfo), m_DetectLoops(detectLoops) { } CBeginInfo(const CObjectInfo& object, bool detectLoops = false) : CParent(object.GetObjectPtr(), object.GetTypeInfo()), m_DetectLoops(detectLoops) { } CBeginInfo(CSerialObject& object, bool detectLoops = false) : CParent(&object, object.GetThisTypeInfo()), m_DetectLoops(detectLoops) { } bool m_DetectLoops; }; /// Class holding information about root of non-modifiable object hierarchy /// Do not use it directly class CConstBeginInfo : public pair<TConstObjectPtr, TTypeInfo> { typedef pair<TConstObjectPtr, TTypeInfo> CParent; public: typedef CConstObjectInfo TObjectInfo; CConstBeginInfo(TConstObjectPtr objectPtr, TTypeInfo typeInfo, bool detectLoops = false) : CParent(objectPtr, typeInfo), m_DetectLoops(detectLoops) { } CConstBeginInfo(const CConstObjectInfo& object, bool detectLoops = false) : CParent(object.GetObjectPtr(), object.GetTypeInfo()), m_DetectLoops(detectLoops) { } CConstBeginInfo(const CSerialObject& object, bool detectLoops = false) : CParent(&object, object.GetThisTypeInfo()), m_DetectLoops(detectLoops) { } CConstBeginInfo(const CBeginInfo& beginInfo) : CParent(beginInfo.first, beginInfo.second), m_DetectLoops(beginInfo.m_DetectLoops) { } bool m_DetectLoops; }; /// Class describing stack level of traversal class NCBI_XSERIAL_EXPORT CConstTreeLevelIterator { public: typedef CConstBeginInfo TBeginInfo; typedef TBeginInfo::TObjectInfo TObjectInfo; virtual ~CConstTreeLevelIterator(void); virtual bool Valid(void) const = 0; virtual TMemberIndex GetIndex(void) const { return kInvalidMember; } virtual void Next(void) = 0; virtual bool CanGet(void) const { return true; } virtual TObjectInfo Get(void) const = 0; virtual const CItemInfo* GetItemInfo(void) const = 0; virtual CConstTreeLevelIterator* Clone(void) = 0; static CConstTreeLevelIterator* Create(const TObjectInfo& object); static CConstTreeLevelIterator* CreateOne(const TObjectInfo& object); static bool HaveChildren(const CConstObjectInfo& object); protected: virtual void SetItemInfo(const CItemInfo* info) = 0; }; class NCBI_XSERIAL_EXPORT CTreeLevelIterator { public: typedef CBeginInfo TBeginInfo; typedef TBeginInfo::TObjectInfo TObjectInfo; virtual ~CTreeLevelIterator(void); virtual bool Valid(void) const = 0; virtual TMemberIndex GetIndex(void) const { return kInvalidMember; } virtual void Next(void) = 0; virtual bool CanGet(void) const { return true; } virtual TObjectInfo Get(void) const = 0; virtual const CItemInfo* GetItemInfo(void) const = 0; virtual CTreeLevelIterator* Clone(void) = 0; static CTreeLevelIterator* Create(const TObjectInfo& object); static CTreeLevelIterator* CreateOne(const TObjectInfo& object); virtual void Erase(void); protected: virtual void SetItemInfo(const CItemInfo* info) = 0; }; /// Base class for all iterators over non-modifiable object /// Do not use it directly template<class LevelIterator> class CTreeIteratorTmpl { typedef CTreeIteratorTmpl<LevelIterator> TThis; typedef shared_ptr<LevelIterator> TStackLevel; public: typedef typename LevelIterator::TObjectInfo TObjectInfo; typedef typename LevelIterator::TBeginInfo TBeginInfo; typedef set<TConstObjectPtr> TVisitedObjects; typedef list< pair< typename LevelIterator::TObjectInfo, const CItemInfo*> > TIteratorContext; // construct object iterator CTreeIteratorTmpl(void) { } CTreeIteratorTmpl(const TBeginInfo& beginInfo) { Init(beginInfo); } CTreeIteratorTmpl(const TBeginInfo& beginInfo, const string& filter) { Init(beginInfo, filter); } virtual ~CTreeIteratorTmpl(void) { Reset(); } /// Get information about current object TObjectInfo& Get(void) { _ASSERT(CheckValid()); return m_CurrentObject; } /// Get information about current object const TObjectInfo& Get(void) const { _ASSERT(CheckValid()); return m_CurrentObject; } /// Get type information of current object TTypeInfo GetCurrentTypeInfo(void) const { return Get().GetTypeInfo(); } /// Reset iterator to initial state void Reset(void) { m_CurrentObject.Reset(); m_VisitedObjects.reset(); m_Stack.clear(); _ASSERT(!*this); } void Next(void) { _ASSERT(CheckValid()); m_CurrentObject.Reset(); _ASSERT(!m_Stack.empty()); if ( Step(m_Stack.back()->Get()) ) Walk(); } void SkipSubTree(void) { _ASSERT(CheckValid()); m_Stack.push_back(TStackLevel(LevelIterator::CreateOne(TObjectInfo()))); } bool IsValid(void) const { return CheckValid(); } // check whether iterator is not finished DECLARE_OPERATOR_BOOL(IsValid()); /// Go to next object TThis& operator++(void) { Next(); return *this; } /// Initialize iterator to new root of object hierarchy TThis& operator=(const TBeginInfo& beginInfo) { Init(beginInfo); return *this; } /// Get raw context data TIteratorContext GetContextData(void) const { TIteratorContext stk_info; typename vector< TStackLevel >::const_iterator i; for (i = m_Stack.begin(); i != m_Stack.end(); ++i) { stk_info.push_back( make_pair( (*i)->Get(), (*i)->GetItemInfo())); } return stk_info; } /// Get context data as string string GetContext(void) const { string loc; TIteratorContext stk_info = GetContextData(); typename TIteratorContext::const_iterator i; for (i = stk_info.begin(); i != stk_info.end(); ++i) { TTypeInfo tt = i->first.GetTypeInfo(); const CItemInfo* ii = i->second; string name; if (ii) { const CMemberId& mid = ii->GetId(); if (!mid.IsAttlist() && !mid.HasNotag()) { name = mid.GetName(); } } else { if (loc.empty()) { name = tt->GetName(); } } if (!name.empty()) { if (!loc.empty()) { loc += "."; } loc += name; } } return loc; } /// Check context against filter /// /// @param filter /// Context filter string bool MatchesContext(const string& filter) const { if (filter.empty()) { return true; } return CPathHook::Match(filter,GetContext()); } /// Set context filter /// /// @param filter /// Context filter string void SetContextFilter(const string& filter) { m_ContextFilter = filter; if (!MatchesContext(filter)) { Next(); } } /// Return element index in STL container int GetContainerElementIndex(void) const { TMemberIndex ind = kInvalidMember; if (m_Stack.size() > 1) { LevelIterator* l( m_Stack[ m_Stack.size()-2 ].get()); ind = l->GetIndex(); } return int(ind - kInvalidMember - 1); } /// Return member index in sequence, or variant index in choice int GetItemIndex(void) const { TMemberIndex ind = kInvalidMember; if (!m_Stack.empty()) { ind = m_Stack.back().get()->GetIndex(); } return int(ind - kInvalidMember - 1); } protected: bool CheckValid(void) const { return m_CurrentObject; } virtual bool CanSelect(const CConstObjectInfo& obj) { if ( !obj ) return false; TVisitedObjects* visitedObjects = m_VisitedObjects.get(); if ( visitedObjects ) { if ( !visitedObjects->insert(obj.GetObjectPtr()).second ) { // already visited return false; } } return true; } virtual bool CanEnter(const CConstObjectInfo& object) { return CConstTreeLevelIterator::HaveChildren(object); } void Init(const TBeginInfo& beginInfo) { Reset(); if ( !beginInfo.first || !beginInfo.second ) return; if ( beginInfo.m_DetectLoops ) m_VisitedObjects.reset(new TVisitedObjects); m_Stack.push_back(TStackLevel(LevelIterator::CreateOne(beginInfo))); Walk(); } void Init(const TBeginInfo& beginInfo, const string& filter) { m_ContextFilter = filter; Init(beginInfo); } private: bool Step(const TObjectInfo& current) { if ( CanEnter(current) ) { TStackLevel nextLevel(LevelIterator::Create(current)); if ( nextLevel && nextLevel->Valid() ) { m_Stack.push_back(nextLevel); return true; } } // skip all finished iterators _ASSERT(!m_Stack.empty()); do { m_Stack.back()->Next(); if ( m_Stack.back()->Valid() ) { // next child on this level return true; } m_Stack.pop_back(); } while ( !m_Stack.empty() ); return false; } void Walk(void) { _ASSERT(!m_Stack.empty()); TObjectInfo current; do { while (!m_Stack.back()->CanGet()) { for(;;) { m_Stack.back()->Next(); if (m_Stack.back()->Valid()) { break; } m_Stack.pop_back(); if (m_Stack.empty()) { return; } } } current = m_Stack.back()->Get(); if ( CanSelect(current) ) { if (MatchesContext(m_ContextFilter)) { m_CurrentObject = current; return; } } } while ( Step(current) ); } // stack of tree level iterators vector< TStackLevel > m_Stack; // currently selected object TObjectInfo m_CurrentObject; shared_ptr<TVisitedObjects> m_VisitedObjects; string m_ContextFilter; friend class CTreeIterator; protected: void Assign(const CTreeIteratorTmpl& it) { m_Stack.clear(); for (const auto& e : it.m_Stack) { m_Stack.push_back( TStackLevel(e.get()->Clone())); } m_CurrentObject = it.m_CurrentObject; m_VisitedObjects = it.m_VisitedObjects; m_ContextFilter = it.m_ContextFilter; } }; /* NCBI_FAKE_WARNING: MSVC */ typedef CTreeIteratorTmpl<CConstTreeLevelIterator> CTreeConstIterator; /// Base class for all iterators over modifiable object class NCBI_XSERIAL_EXPORT CTreeIterator : public CTreeIteratorTmpl<CTreeLevelIterator> { typedef CTreeIteratorTmpl<CTreeLevelIterator> CParent; public: typedef CParent::TObjectInfo TObjectInfo; typedef CParent::TBeginInfo TBeginInfo; // construct object iterator CTreeIterator(void) { } CTreeIterator(const TBeginInfo& beginInfo) { Init(beginInfo); } CTreeIterator(const TBeginInfo& beginInfo, const string& filter) { Init(beginInfo, filter); } // initialize iterator to new root of object hierarchy CTreeIterator& operator=(const TBeginInfo& beginInfo) { Init(beginInfo); return *this; } /// Delete currently pointed object (throws exception if failed) void Erase(void); }; /// template base class for CTypeIterator<> and CTypeConstIterator<> /// Do not use it directly template<class Parent> class CTypeIteratorBase : public Parent { typedef Parent CParent; protected: typedef typename CParent::TBeginInfo TBeginInfo; CTypeIteratorBase(TTypeInfo needType) : m_NeedType(needType) { } CTypeIteratorBase(TTypeInfo needType, const TBeginInfo& beginInfo) : m_NeedType(needType) { this->Init(beginInfo); } CTypeIteratorBase(TTypeInfo needType, const TBeginInfo& beginInfo, const string& filter) : m_NeedType(needType) { this->Init(beginInfo, filter); } virtual bool CanSelect(const CConstObjectInfo& object) { return CParent::CanSelect(object) && object.GetTypeInfo()->IsType(m_NeedType); } virtual bool CanEnter(const CConstObjectInfo& object) { return CParent::CanEnter(object) && object.GetTypeInfo()->MayContainType(m_NeedType); } TTypeInfo GetIteratorType(void) const { return m_NeedType; } void Assign(const CTypeIteratorBase& it) { CParent::Assign(it); m_NeedType = it.m_NeedType; } private: TTypeInfo m_NeedType; }; /// Template base class for CTypesIterator and CTypesConstIterator /// Do not use it directly template<class Parent> class CTypesIteratorBase : public Parent { typedef Parent CParent; public: typedef typename CParent::TBeginInfo TBeginInfo; typedef list<TTypeInfo> TTypeList; CTypesIteratorBase(void) { } CTypesIteratorBase(TTypeInfo type) { m_TypeList.push_back(type); } CTypesIteratorBase(TTypeInfo type1, TTypeInfo type2) { m_TypeList.push_back(type1); m_TypeList.push_back(type2); } CTypesIteratorBase(const TTypeList& typeList) : m_TypeList(typeList) { } CTypesIteratorBase(const TTypeList& typeList, const TBeginInfo& beginInfo) : m_TypeList(typeList) { Init(beginInfo); } CTypesIteratorBase(const TTypeList& typeList, const TBeginInfo& beginInfo, const string& filter) : m_TypeList(typeList) { Init(beginInfo, filter); } const TTypeList& GetTypeList(void) const { return m_TypeList; } CTypesIteratorBase<Parent>& AddType(TTypeInfo type) { m_TypeList.push_back(type); return *this; } CTypesIteratorBase<Parent>& operator=(const TBeginInfo& beginInfo) { this->Init(beginInfo); return *this; } typename CParent::TObjectInfo::TObjectPtrType GetFoundPtr(void) const { return this->Get().GetObjectPtr(); } TTypeInfo GetFoundType(void) const { return this->Get().GetTypeInfo(); } TTypeInfo GetMatchType(void) const { return m_MatchType; } protected: #if 0 // There is an (unconfirmed) opinion that putting these two functions // into source (iterator.cpp) reduces the size of an executable. // Still, keeping them there is reported as bug by // Metrowerks Codewarrior 9.0 (Mac OSX) virtual bool CanSelect(const CConstObjectInfo& object); virtual bool CanEnter(const CConstObjectInfo& object); #else virtual bool 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; } virtual bool 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; } #endif private: TTypeList m_TypeList; TTypeInfo m_MatchType; }; /// Template class for iteration on objects of class C template<class C, class TypeGetter = C> class CTypeIterator : public CTypeIteratorBase<CTreeIterator>, public iterator< forward_iterator_tag, C, ptrdiff_t, C*, C& > { typedef CTypeIteratorBase<CTreeIterator> CParent; public: typedef typename CParent::TBeginInfo TBeginInfo; CTypeIterator(void) : CParent(TypeGetter::GetTypeInfo()) { } CTypeIterator(const TBeginInfo& beginInfo) : CParent(TypeGetter::GetTypeInfo(), beginInfo) { } CTypeIterator(const TBeginInfo& beginInfo, const string& filter) : CParent(TypeGetter::GetTypeInfo(), beginInfo, filter) { } explicit CTypeIterator(CSerialObject& object) : CParent(TypeGetter::GetTypeInfo(), TBeginInfo(object)) { } CTypeIterator<C, TypeGetter>& operator=(const TBeginInfo& beginInfo) { Init(beginInfo); return *this; } C& operator*(void) { return *CTypeConverter<C>::SafeCast(Get().GetObjectPtr()); } const C& operator*(void) const { return *CTypeConverter<C>::SafeCast(Get().GetObjectPtr()); } C* operator->(void) { return CTypeConverter<C>::SafeCast(Get().GetObjectPtr()); } const C* operator->(void) const { return CTypeConverter<C>::SafeCast(Get().GetObjectPtr()); } CTypeIterator(const CTypeIterator& it) : CParent(TypeGetter::GetTypeInfo()) { CParent::Assign(it); } CTypeIterator& operator=(const CTypeIterator& it) { CParent::Assign(it); return *this; } CTypeIterator& begin(void) { return *this; } CTypeIterator end(void) { return CTypeIterator(); } CTypeIterator& operator++(void) { CParent::operator++(); return *this; } CTypeIterator operator++(int) { CTypeIterator tmp(*this); CParent::operator++(); return tmp; } bool operator==(const CTypeIterator& it) const { if (IsValid() && it.IsValid()) { return Get() == it.Get(); } return IsValid() == it.IsValid(); } bool operator!=(const CTypeIterator& it) const { return !operator==(it); } }; /// Template class for iteration on objects of class C (non-medifiable version) template<class C, class TypeGetter = C> class CTypeConstIterator : public CTypeIteratorBase<CTreeConstIterator>, public iterator< forward_iterator_tag, C, ptrdiff_t, C*, C& > { typedef CTypeIteratorBase<CTreeConstIterator> CParent; public: typedef typename CParent::TBeginInfo TBeginInfo; CTypeConstIterator(void) : CParent(TypeGetter::GetTypeInfo()) { } CTypeConstIterator(const TBeginInfo& beginInfo) : CParent(TypeGetter::GetTypeInfo(), beginInfo) { } CTypeConstIterator(const TBeginInfo& beginInfo, const string& filter) : CParent(TypeGetter::GetTypeInfo(), beginInfo, filter) { } explicit CTypeConstIterator(const CSerialObject& object) : CParent(TypeGetter::GetTypeInfo(), TBeginInfo(object)) { } CTypeConstIterator<C, TypeGetter>& operator=(const TBeginInfo& beginInfo) { Init(beginInfo); return *this; } const C& operator*(void) const { return *CTypeConverter<C>::SafeCast(Get().GetObjectPtr()); } const C* operator->(void) const { return CTypeConverter<C>::SafeCast(Get().GetObjectPtr()); } CTypeConstIterator(const CTypeConstIterator& it) : CParent(TypeGetter::GetTypeInfo()) { CParent::Assign(it); } CTypeConstIterator& operator=(const CTypeConstIterator& it) { CParent::Assign(it); return *this; } CTypeConstIterator& begin(void) { return *this; } CTypeConstIterator end(void) { return CTypeConstIterator(); } CTypeConstIterator& operator++(void) { CParent::operator++(); return *this; } CTypeConstIterator operator++(int) { CTypeConstIterator tmp(*this); CParent::operator++(); return tmp; } bool operator==(const CTypeConstIterator& it) const { if (IsValid() && it.IsValid()) { return Get() == it.Get(); } return IsValid() == it.IsValid(); } bool operator!=(const CTypeConstIterator& it) const { return !operator==(it); } }; #if 0 // these are obsolete (?) Use CTypeIterator and CTypeConstIterator instead /// Do not use it directly template<class Parent> class CLeafTypeIteratorBase : public CTypeIteratorBase<Parent> { typedef CTypeIteratorBase<Parent> CParent; protected: typedef typename CParent::TBeginInfo TBeginInfo; CLeafTypeIteratorBase(TTypeInfo needType) : CParent(needType) { } CLeafTypeIteratorBase(TTypeInfo needType, const TBeginInfo& beginInfo) : CParent(needType) { Init(beginInfo); } CLeafTypeIteratorBase(TTypeInfo needType, const TBeginInfo& beginInfo, const string& filter) : CParent(needType) { Init(beginInfo, filter); } virtual bool CanSelect(const CConstObjectInfo& object); }; /// Template class for iteration on objects of class C template<class C> class CLeafTypeIterator : public CLeafTypeIteratorBase<CTreeIterator> { typedef CLeafTypeIteratorBase<CTreeIterator> CParent; public: typedef typename CParent::TBeginInfo TBeginInfo; CLeafTypeIterator(void) : CParent(C::GetTypeInfo()) { } CLeafTypeIterator(const TBeginInfo& beginInfo) : CParent(C::GetTypeInfo(), beginInfo) { } CLeafTypeIterator(const TBeginInfo& beginInfo, const string& filter) : CParent(C::GetTypeInfo(), beginInfo, filter) { } explicit CLeafTypeIterator(CSerialObject& object) : CParent(C::GetTypeInfo(), TBeginInfo(object)) { } CLeafTypeIterator<C>& operator=(const TBeginInfo& beginInfo) { Init(beginInfo); return *this; } C& operator*(void) { return *CTypeConverter<C>::SafeCast(Get().GetObjectPtr()); } const C& operator*(void) const { return *CTypeConverter<C>::SafeCast(Get().GetObjectPtr()); } C* operator->(void) { return CTypeConverter<C>::SafeCast(Get().GetObjectPtr()); } const C* operator->(void) const { return CTypeConverter<C>::SafeCast(Get().GetObjectPtr()); } }; /// Template class for iteration on objects of class C (non-medifiable version) template<class C> class CLeafTypeConstIterator : public CLeafTypeIteratorBase<CTreeConstIterator> { typedef CLeafTypeIteratorBase<CTreeConstIterator> CParent; public: typedef typename CParent::TBeginInfo TBeginInfo; CLeafTypeConstIterator(void) : CParent(C::GetTypeInfo()) { } CLeafTypeConstIterator(const TBeginInfo& beginInfo) : CParent(C::GetTypeInfo(), beginInfo) { } CLeafTypeConstIterator(const TBeginInfo& beginInfo, const string& filter) : CParent(C::GetTypeInfo(), beginInfo, filter) { } explicit CLeafTypeConstIterator(const CSerialObject& object) : CParent(C::GetTypeInfo(), TBeginInfo(object)) { } CLeafTypeConstIterator<C>& operator=(const TBeginInfo& beginInfo) { Init(beginInfo); return *this; } const C& operator*(void) const { return *CTypeConverter<C>::SafeCast(Get().GetObjectPtr()); } const C* operator->(void) const { return CTypeConverter<C>::SafeCast(Get().GetObjectPtr()); } }; #endif /// Template class for iteration on objects of standard C++ type T template<typename T> using CStdTypeIterator = CTypeIterator<T, CStdTypeInfo<T> >; /// Non-modifiable version template<typename T> using CStdTypeConstIterator = CTypeConstIterator<T, CStdTypeInfo<T> >; // get special typeinfo of CObject class NCBI_XSERIAL_EXPORT CObjectGetTypeInfo { public: static TTypeInfo GetTypeInfo(void); }; // class for iteration on objects derived from class CObject typedef CTypeIterator<CObject, CObjectGetTypeInfo> CObjectIterator; // class for iteration on objects derived from class CObject // (non-modifiable version) typedef CTypeConstIterator<CObject, CObjectGetTypeInfo> CObjectConstIterator; // class for iteration on objects of list of types typedef CTypesIteratorBase<CTreeIterator> CTypesIterator; // class for iteration on objects of list of types (non-modifiable version) typedef CTypesIteratorBase<CTreeConstIterator> CTypesConstIterator; // enum flag for turning on loop detection in object hierarchy enum EDetectLoops { eDetectLoops }; /// Get starting point of object hierarchy template<class C> inline CBeginInfo Begin(C& obj) { return CBeginInfo(&obj, C::GetTypeInfo(), false); } /// Get starting point of non-modifiable object hierarchy template<class C> inline CConstBeginInfo ConstBegin(const C& obj) { return CConstBeginInfo(&obj, C::GetTypeInfo(), false); } template<class C> inline CConstBeginInfo Begin(const C& obj) { return CConstBeginInfo(&obj, C::GetTypeInfo(), false); } /// Get starting point of object hierarchy with loop detection template<class C> inline CBeginInfo Begin(C& obj, EDetectLoops) { return CBeginInfo(&obj, C::GetTypeInfo(), true); } /// Get starting point of non-modifiable object hierarchy with loop detection template<class C> inline CConstBeginInfo ConstBegin(const C& obj, EDetectLoops) { return CConstBeginInfo(&obj, C::GetTypeInfo(), true); } template<class C> inline CConstBeginInfo Begin(const C& obj, EDetectLoops) { return CConstBeginInfo(&obj, C::GetTypeInfo(), true); } /* @} */ END_NCBI_SCOPE #endif /* ITERATOR__HPP */

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.