From 3707fd4faab3f2e2521263070b68fd0afaae2900 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 13 Jul 2015 23:10:53 +0200 Subject: avoid virtual in the iterators With a bit of trickery and the Curiously recurring template pattern we can free us from our use of virtual in the iterators were it is unneeded bloat as we never deal with pointers to iterators and similar such. Git-Dch: Ignore --- apt-pkg/cacheiterators.h | 90 +++++++++++++++++++++--------------------------- apt-pkg/cacheset.cc | 3 +- apt-pkg/cacheset.h | 63 ++++++++++++++++----------------- apt-pkg/pkgcache.cc | 8 +++-- 4 files changed, 75 insertions(+), 89 deletions(-) (limited to 'apt-pkg') diff --git a/apt-pkg/cacheiterators.h b/apt-pkg/cacheiterators.h index d7614374e..06deef950 100644 --- a/apt-pkg/cacheiterators.h +++ b/apt-pkg/cacheiterators.h @@ -43,10 +43,6 @@ need to have for doing some walk-over-the-cache magic */ template class pkgCache::Iterator : public std::iterator { - protected: - Str *S; - pkgCache *Owner; - /** \brief Returns the Pointer for this struct in the owner * The implementation of this method should be pretty short * as it will only return the Pointer into the mmap stored @@ -55,12 +51,14 @@ template class pkgCache::Iterator : * basic methods from the actual structure. * \return Pointer to the first structure of this type */ - virtual Str* OwnerPointer() const = 0; + Str* OwnerPointer() const { return static_cast(this)->OwnerPointer(); } + + protected: + Str *S; + pkgCache *Owner; public: // Iteration - virtual void operator ++(int) = 0; - virtual void operator ++() = 0; // Should be {operator ++(0);} inline bool end() const {return Owner == 0 || S == OwnerPointer();} // Comparison @@ -99,20 +97,19 @@ template class pkgCache::Iterator : class pkgCache::GrpIterator: public Iterator { long HashIndex; - protected: + public: inline Group* OwnerPointer() const { return (Owner != 0) ? Owner->GrpP : 0; } - public: // This constructor is the 'begin' constructor, never use it. explicit inline GrpIterator(pkgCache &Owner) : Iterator(Owner), HashIndex(-1) { S = OwnerPointer(); - operator ++(0); + operator++(); } - virtual void operator ++(int); - virtual void operator ++() {operator ++(0);} + GrpIterator& operator++(); + inline GrpIterator operator++(int) { GrpIterator const tmp(*this); operator++(); return tmp; } inline const char *Name() const {return S->Name == 0?0:Owner->StrP + S->Name;} inline PkgIterator PackageList() const; @@ -140,20 +137,19 @@ class pkgCache::GrpIterator: public Iterator { class pkgCache::PkgIterator: public Iterator { long HashIndex; - protected: + public: inline Package* OwnerPointer() const { return (Owner != 0) ? Owner->PkgP : 0; } - public: // This constructor is the 'begin' constructor, never use it. explicit inline PkgIterator(pkgCache &Owner) : Iterator(Owner), HashIndex(-1) { S = OwnerPointer(); - operator ++(0); + operator++(); } - virtual void operator ++(int); - virtual void operator ++() {operator ++(0);} + PkgIterator& operator++(); + inline PkgIterator operator++(int) { PkgIterator const tmp(*this); operator++(); return tmp; } enum OkState {NeedsNothing,NeedsUnpack,NeedsConfigure}; @@ -193,15 +189,14 @@ class pkgCache::PkgIterator: public Iterator { /*}}}*/ // Version Iterator /*{{{*/ class pkgCache::VerIterator : public Iterator { - protected: + public: inline Version* OwnerPointer() const { return (Owner != 0) ? Owner->VerP : 0; } - public: // Iteration - void operator ++(int) {if (S != Owner->VerP) S = Owner->VerP + S->NextVer;} - inline void operator ++() {operator ++(0);} + inline VerIterator& operator++() {if (S != Owner->VerP) S = Owner->VerP + S->NextVer; return *this;} + inline VerIterator operator++(int) { VerIterator const tmp(*this); operator++(); return tmp; } // Comparison int CompareVer(const VerIterator &B) const; @@ -253,15 +248,14 @@ class pkgCache::VerIterator : public Iterator { /*}}}*/ // Description Iterator /*{{{*/ class pkgCache::DescIterator : public Iterator { - protected: + public: inline Description* OwnerPointer() const { return (Owner != 0) ? Owner->DescP : 0; } - public: // Iteration - void operator ++(int) {if (S != Owner->DescP) S = Owner->DescP + S->NextDesc;} - inline void operator ++() {operator ++(0);} + inline DescIterator& operator++() {if (S != Owner->DescP) S = Owner->DescP + S->NextDesc; return *this;} + inline DescIterator operator++(int) { DescIterator const tmp(*this); operator++(); return tmp; } // Comparison int CompareDesc(const DescIterator &B) const; @@ -282,16 +276,15 @@ class pkgCache::DescIterator : public Iterator { class pkgCache::DepIterator : public Iterator { enum {DepVer, DepRev} Type; - protected: + public: inline Dependency* OwnerPointer() const { return (Owner != 0) ? Owner->DepP : 0; } - public: // Iteration - void operator ++(int) {if (S != Owner->DepP) S = Owner->DepP + - (Type == DepVer ? S->NextDepends : S->NextRevDepends);} - inline void operator ++() {operator ++(0);} + inline DepIterator& operator++() {if (S != Owner->DepP) S = Owner->DepP + + (Type == DepVer ? S->NextDepends : S->NextRevDepends); return *this;} + inline DepIterator operator++(int) { DepIterator const tmp(*this); operator++(); return tmp; } // Accessors inline const char *TargetVer() const {return S->Version == 0?0:Owner->StrP + S->Version;} @@ -333,16 +326,15 @@ class pkgCache::DepIterator : public Iterator { class pkgCache::PrvIterator : public Iterator { enum {PrvVer, PrvPkg} Type; - protected: + public: inline Provides* OwnerPointer() const { return (Owner != 0) ? Owner->ProvideP : 0; } - public: // Iteration - void operator ++(int) {if (S != Owner->ProvideP) S = Owner->ProvideP + - (Type == PrvVer?S->NextPkgProv:S->NextProvides);} - inline void operator ++() {operator ++(0);} + inline PrvIterator& operator ++() {if (S != Owner->ProvideP) S = Owner->ProvideP + + (Type == PrvVer?S->NextPkgProv:S->NextProvides); return *this;} + inline PrvIterator operator++(int) { PrvIterator const tmp(*this); operator++(); return tmp; } // Accessors inline const char *Name() const {return ParentPkg().Name();} @@ -368,15 +360,14 @@ class pkgCache::PrvIterator : public Iterator { /*}}}*/ // Release file /*{{{*/ class pkgCache::RlsFileIterator : public Iterator { - protected: + public: inline ReleaseFile* OwnerPointer() const { return (Owner != 0) ? Owner->RlsFileP : 0; } - public: // Iteration - void operator ++(int) {if (S != Owner->RlsFileP) S = Owner->RlsFileP + S->NextFile;} - inline void operator ++() {operator ++(0);} + inline RlsFileIterator& operator++() {if (S != Owner->RlsFileP) S = Owner->RlsFileP + S->NextFile;return *this;} + inline RlsFileIterator operator++(int) { RlsFileIterator const tmp(*this); operator++(); return tmp; } // Accessors inline const char *FileName() const {return S->FileName == 0?0:Owner->StrP + S->FileName;} @@ -399,15 +390,14 @@ class pkgCache::RlsFileIterator : public Iterator /*}}}*/ // Package file /*{{{*/ class pkgCache::PkgFileIterator : public Iterator { - protected: + public: inline PackageFile* OwnerPointer() const { return (Owner != 0) ? Owner->PkgFileP : 0; } - public: // Iteration - void operator ++(int) {if (S != Owner->PkgFileP) S = Owner->PkgFileP + S->NextFile;} - inline void operator ++() {operator ++(0);} + inline PkgFileIterator& operator++() {if (S != Owner->PkgFileP) S = Owner->PkgFileP + S->NextFile; return *this;} + inline PkgFileIterator operator++(int) { PkgFileIterator const tmp(*this); operator++(); return tmp; } // Accessors inline const char *FileName() const {return S->FileName == 0?0:Owner->StrP + S->FileName;} @@ -435,15 +425,14 @@ class pkgCache::PkgFileIterator : public Iterator /*}}}*/ // Version File /*{{{*/ class pkgCache::VerFileIterator : public pkgCache::Iterator { - protected: + public: inline VerFile* OwnerPointer() const { return (Owner != 0) ? Owner->VerFileP : 0; } - public: // Iteration - void operator ++(int) {if (S != Owner->VerFileP) S = Owner->VerFileP + S->NextFile;} - inline void operator ++() {operator ++(0);} + inline VerFileIterator& operator++() {if (S != Owner->VerFileP) S = Owner->VerFileP + S->NextFile; return *this;} + inline VerFileIterator operator++(int) { VerFileIterator const tmp(*this); operator++(); return tmp; } // Accessors inline PkgFileIterator File() const {return PkgFileIterator(*Owner,S->File + Owner->PkgFileP);} @@ -454,15 +443,14 @@ class pkgCache::VerFileIterator : public pkgCache::Iterator { - protected: + public: inline DescFile* OwnerPointer() const { return (Owner != 0) ? Owner->DescFileP : 0; } - public: // Iteration - void operator ++(int) {if (S != Owner->DescFileP) S = Owner->DescFileP + S->NextFile;} - inline void operator ++() {operator ++(0);} + inline DescFileIterator& operator++() {if (S != Owner->DescFileP) S = Owner->DescFileP + S->NextFile; return *this;} + inline DescFileIterator operator++(int) { DescFileIterator const tmp(*this); operator++(); return tmp; } // Accessors inline PkgFileIterator File() const {return PkgFileIterator(*Owner,S->File + Owner->PkgFileP);} diff --git a/apt-pkg/cacheset.cc b/apt-pkg/cacheset.cc index f5cf52159..af607a197 100644 --- a/apt-pkg/cacheset.cc +++ b/apt-pkg/cacheset.cc @@ -935,7 +935,8 @@ PackageContainerInterface& PackageContainerInterface::operator=(PackageContainer } PackageContainerInterface::~PackageContainerInterface() {} -PackageUniverse::PackageUniverse(pkgCache * const Owner) : _cont(Owner), d(NULL) { } +PackageUniverse::PackageUniverse(pkgCache * const Owner) : _cont(Owner), d(NULL) {} +PackageUniverse::PackageUniverse(pkgCacheFile * const Owner) : _cont(Owner->GetPkgCache()), d(NULL) {} PackageUniverse::~PackageUniverse() {} VersionContainerInterface::VersionContainerInterface() : d(NULL) {} diff --git a/apt-pkg/cacheset.h b/apt-pkg/cacheset.h index 5455fe74b..95df55dcc 100644 --- a/apt-pkg/cacheset.h +++ b/apt-pkg/cacheset.h @@ -213,13 +213,13 @@ private: // Iterator templates for our Containers /*{{{*/ template class Container_iterator_base : public std::iterator::iterator_category, container_iterator>, - public Interface::iterator_base + public Interface::template iterator_base { protected: container_iterator _iter; public: explicit Container_iterator_base(container_iterator i) : _iter(i) {} - inline container_value operator*(void) const { return this->getType(); } + inline container_value operator*(void) const { return static_cast(this)->getType(); }; operator container_iterator(void) const { return _iter; } inline iterator_type& operator++() { ++_iter; return static_cast(*this); } inline iterator_type operator++(int) { iterator_type tmp(*this); operator++(); return tmp; } @@ -249,7 +249,7 @@ public: explicit Container_const_iterator(container_iterator i) : Container_iterator_base(i) {} - inline virtual typename Container::value_type getType(void) const APT_OVERRIDE { return *this->_iter; } + inline typename Container::value_type getType(void) const { return *this->_iter; } }; template class Container_iterator : public Container_iterator_base, typename Container::iterator, typename Container::value_type> @@ -264,7 +264,7 @@ public: inline iterator_type& operator=(iterator_type const &i) { this->_iter = i._iter; return static_cast(*this); } inline iterator_type& operator=(container_iterator const &i) { this->_iter = i; return static_cast(*this); } - inline virtual typename Container::value_type getType(void) const APT_OVERRIDE { return *this->_iter; } + inline typename Container::value_type getType(void) const { return *this->_iter; } }; template class Container_const_reverse_iterator : public Container_iterator_base, typename Container::const_reverse_iterator, typename Container::value_type> @@ -275,7 +275,7 @@ public: explicit Container_const_reverse_iterator(container_iterator i) : Container_iterator_base(i) {} - inline virtual typename Container::value_type getType(void) const APT_OVERRIDE { return *this->_iter; } + inline typename Container::value_type getType(void) const { return *this->_iter; } }; template class Container_reverse_iterator : public Container_iterator_base, typename Container::reverse_iterator, typename Container::value_type> @@ -290,7 +290,7 @@ public: inline iterator_type& operator=(iterator_type const &i) { this->_iter = i._iter; return static_cast(*this); } inline iterator_type& operator=(container_iterator const &i) { this->_iter = i; return static_cast(*this); } - inline virtual typename Container::value_type getType(void) const APT_OVERRIDE { return *this->_iter; } + inline typename Container::value_type getType(void) const { return *this->_iter; } }; /*}}}*/ class PackageContainerInterface { /*{{{*/ @@ -304,9 +304,8 @@ class PackageContainerInterface { /*{{{*/ * This class mostly protects use from the need to write all implementation * of the methods working on containers in the template */ public: - class iterator_base { /*{{{*/ - protected: - virtual pkgCache::PkgIterator getType() const = 0; + template class iterator_base { /*{{{*/ + pkgCache::PkgIterator getType() const { return static_cast(this)->getType(); }; public: operator pkgCache::PkgIterator(void) const { return getType(); } @@ -689,41 +688,38 @@ class APT_PUBLIC PackageUniverse : public PackageContainerInterface { public: class const_iterator : public APT::Container_iterator_base { - protected: - inline virtual pkgCache::PkgIterator getType(void) const APT_OVERRIDE - { - return _iter; - } public: explicit const_iterator(pkgCache::PkgIterator i): Container_iterator_base(i) {} + inline pkgCache::PkgIterator getType(void) const { return _iter; } }; typedef const_iterator iterator; - APT_PUBLIC bool empty() const APT_OVERRIDE { return false; } - APT_PUBLIC size_t size() const APT_OVERRIDE { return _cont->Head().PackageCount; } + bool empty() const APT_OVERRIDE { return false; } + size_t size() const APT_OVERRIDE { return _cont->Head().PackageCount; } - APT_PUBLIC const_iterator begin() const { return const_iterator(_cont->PkgBegin()); } - APT_PUBLIC const_iterator end() const { return const_iterator(_cont->PkgEnd()); } - APT_PUBLIC const_iterator cbegin() const { return const_iterator(_cont->PkgBegin()); } - APT_PUBLIC const_iterator cend() const { return const_iterator(_cont->PkgEnd()); } - APT_PUBLIC iterator begin() { return iterator(_cont->PkgBegin()); } - APT_PUBLIC iterator end() { return iterator(_cont->PkgEnd()); } + const_iterator begin() const { return const_iterator(_cont->PkgBegin()); } + const_iterator end() const { return const_iterator(_cont->PkgEnd()); } + const_iterator cbegin() const { return const_iterator(_cont->PkgBegin()); } + const_iterator cend() const { return const_iterator(_cont->PkgEnd()); } + iterator begin() { return iterator(_cont->PkgBegin()); } + iterator end() { return iterator(_cont->PkgEnd()); } - APT_PUBLIC pkgCache * data() const { return _cont; } + pkgCache * data() const { return _cont; } - explicit APT_PUBLIC PackageUniverse(pkgCache * const Owner); - APT_PUBLIC virtual ~PackageUniverse(); + explicit PackageUniverse(pkgCache * const Owner); + explicit PackageUniverse(pkgCacheFile * const Owner); + virtual ~PackageUniverse(); private: - bool insert(pkgCache::PkgIterator const &) APT_OVERRIDE { return true; } - template void insert(PackageContainer const &) { } - void insert(const_iterator, const_iterator) { } + APT_HIDDEN bool insert(pkgCache::PkgIterator const &) APT_OVERRIDE { return true; } + template APT_HIDDEN void insert(PackageContainer const &) { } + APT_HIDDEN void insert(const_iterator, const_iterator) { } - void clear() APT_OVERRIDE { } - iterator erase( const_iterator pos ); - iterator erase( const_iterator first, const_iterator last ); + APT_HIDDEN void clear() APT_OVERRIDE { } + APT_HIDDEN iterator erase( const_iterator pos ); + APT_HIDDEN iterator erase( const_iterator first, const_iterator last ); }; /*}}}*/ typedef PackageContainer > PackageSet; @@ -741,9 +737,8 @@ class VersionContainerInterface { /*{{{*/ Same as APT::PackageContainerInterface, just for Versions */ public: /** \brief smell like a pkgCache::VerIterator */ - class iterator_base { /*{{{*/ - protected: - virtual pkgCache::VerIterator getType() const = 0; + template class iterator_base { /*{{{*/ + pkgCache::VerIterator getType() const { return static_cast(this)->getType(); }; public: operator pkgCache::VerIterator(void) { return getType(); } diff --git a/apt-pkg/pkgcache.cc b/apt-pkg/pkgcache.cc index ae04bc699..aebbffe8b 100644 --- a/apt-pkg/pkgcache.cc +++ b/apt-pkg/pkgcache.cc @@ -411,7 +411,7 @@ pkgCache::PkgIterator pkgCache::GrpIterator::NextPkg(pkgCache::PkgIterator const // GrpIterator::operator ++ - Postfix incr /*{{{*/ // --------------------------------------------------------------------- /* This will advance to the next logical group in the hash table. */ -void pkgCache::GrpIterator::operator ++(int) +pkgCache::GrpIterator& pkgCache::GrpIterator::operator++() { // Follow the current links if (S != Owner->GrpP) @@ -423,12 +423,13 @@ void pkgCache::GrpIterator::operator ++(int) HashIndex++; S = Owner->GrpP + Owner->HeaderP->GrpHashTableP()[HashIndex]; } + return *this; } /*}}}*/ // PkgIterator::operator ++ - Postfix incr /*{{{*/ // --------------------------------------------------------------------- /* This will advance to the next logical package in the hash table. */ -void pkgCache::PkgIterator::operator ++(int) +pkgCache::PkgIterator& pkgCache::PkgIterator::operator ++() { // Follow the current links if (S != Owner->PkgP) @@ -440,6 +441,7 @@ void pkgCache::PkgIterator::operator ++(int) HashIndex++; S = Owner->PkgP + Owner->HeaderP->PkgHashTableP()[HashIndex]; } + return *this; } /*}}}*/ // PkgIterator::State - Check the State of the package /*{{{*/ @@ -685,7 +687,7 @@ void pkgCache::DepIterator::GlobOr(DepIterator &Start,DepIterator &End) for (bool LastOR = true; end() == false && LastOR == true;) { LastOR = (S->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or; - (*this)++; + ++(*this); if (LastOR == true) End = (*this); } -- cgit v1.2.3